Skip to content

Commit 03ffa9e

Browse files
authored
check on null
1 parent 4c479dd commit 03ffa9e

File tree

1 file changed

+59
-25
lines changed

1 file changed

+59
-25
lines changed

radiator_window.ts

+59-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Fenstererkennung {
2-
type GroupType = {
3-
"radiator":string;
4-
"contact":string;
2+
type Room = {
3+
"radiator":string|null;
4+
"contact":string|null;
55
"name":string;
66
};
77

@@ -18,85 +18,94 @@ const HamaRadiatorMode = {
1818
off: "off"
1919
}
2020

21-
const livingRoom:GroupType = {
21+
const ShellyRadiatorMode = {
22+
23+
}
24+
25+
const livingRoom:Room = {
2226
"radiator": 'zigbee.0.2c1165fffeb3dc7b.mode', // Hama Radiator
2327
"contact": 'zigbee.0.00124b002fbdc865.opened', //open or close
2428
"name": "Wohnzimmer",
2529
} ;
2630

27-
const sleepingRoom:GroupType = {
28-
"radiator": "2",
29-
"contact": '2',
31+
const sleepingRoom:Room = {
32+
"radiator": null,
33+
"contact": null,
3034
"name":"Schlafzimmer",
3135
};
3236

33-
const kitchen:GroupType = {
37+
const kitchen:Room = {
3438
"radiator": 'zigbee.0.cc86ecfffec8882f.mode',
35-
"contact": '3',
39+
"contact": 'zigbee.0.00124b002fbdabcb.opened',/*Is open*/
3640
"name": "Küche",
3741
};
3842

39-
const childrensRoom:GroupType = {
43+
const childrensRoom:Room = {
4044
"radiator": 'zigbee.0.cc86ecfffec3b6cf.mode',
4145
"contact": 'zigbee.0.00124b002fbae5b2.opened'/*Is open*/,
4246
"name": "Kinderzimmer",
4347
};
4448

45-
const toilet:GroupType = {
49+
const bath:Room = {
4650
"radiator": 'zigbee.0.cc86ecfffec3996b.mode',
47-
"contact": '5',
48-
"name": "Toilette",
51+
"contact": null,
52+
"name": "Bdezimmer",
4953
} ;
5054

51-
const house:GroupType[] = [childrensRoom, kitchen, livingRoom, sleepingRoom, toilet];
55+
const house:Room[] = [childrensRoom, kitchen, livingRoom, sleepingRoom, bath];
5256

53-
const northGroup:GroupType[] = [
57+
const northGroup:Room[] = [
5458
childrensRoom,
5559
sleepingRoom,
56-
toilet
60+
bath
5761
];
58-
const southGroup:GroupType[] = [
62+
const southGroup:Room[] = [
5963
livingRoom,
6064
kitchen
6165
];
6266

63-
const checkIsWindowOpen = (room : GroupType):boolean => {
67+
const checkIsWindowOpen = (room : Room):boolean => {
68+
if (room.contact == null) return false;
6469
if (getState(room.contact).val) {
6570
return true;
6671
} else {
6772
return false;
6873
}
6974
}
7075

71-
const deactivateAllRadiators = (rooms:GroupType[]) => {
76+
const deactivateAllRadiators = (rooms:Room[]) => {
7277
for (const room of rooms) {
78+
if (room.radiator == null) continue;
7379
deactivateRadiator(room);
7480
}
7581
}
7682

77-
const activateAllRadiators = (rooms:GroupType[]) => {
83+
const activateAllRadiators = (rooms:Room[]) => {
7884
for (const room of rooms) {
85+
if (room.radiator == null) continue;
7986
activateRadiator(room);
8087
}
8188
}
8289

83-
const deactivateRadiator = (room:GroupType) => {
90+
const deactivateRadiator = (room:Room) => {
8491

8592
if (getState(room.radiator).val == HamaRadiatorMode.heat) {
8693
sendDiscordMessage("Thermostat in "+room.name+" abgeschaltet");
94+
sendNtfyMessage("Thermostat in "+room.name+" abgeschaltet");
8795
setState(room.radiator, HamaRadiatorMode.off);
8896
}
8997
}
9098

91-
const activateRadiator = (room:GroupType) => {
99+
const activateRadiator = (room:Room) => {
92100

93101
if (getState(room.radiator).val == HamaRadiatorMode.off) {
94102
sendDiscordMessage("Thermostat in "+room.name+" eingeschaltet");
103+
sendNtfyMessage("Thermostat in "+room.name+" eingeschaltet");
95104
setState(room.radiator, HamaRadiatorMode.heat);
96105
}
97106
}
98107

99-
const checkGroup = (group:GroupType[]):boolean => {
108+
const checkGroup = (group:Room[]):boolean => {
100109
for (const room of group) {
101110
if (checkIsWindowOpen(room)) {
102111
return true;
@@ -114,6 +123,31 @@ const sendDiscordMessage = (message:string) => {
114123
sendTo(discordAdapter, 'sendMessage', {serverId: discordServerId, channelId: discordChannelId, content: {content: message}});
115124

116125
}
126+
const sendNtfyMessage = (message:string) => {
127+
const ntfyAdapter = 'ntfy.0';
128+
const ntfySubscription = 'iobroker';
129+
const ntfyTitle = 'Heizung';
130+
131+
const data = {
132+
"instance": ntfyAdapter,
133+
"topic": ntfySubscription,
134+
"title": ntfyTitle,
135+
"message": message,
136+
"priority": 5,
137+
"tags": null,
138+
"attachment": null,
139+
"clickURL": null,
140+
"actionButton": null,
141+
"delay": null
142+
}
143+
try {
144+
sendTo(ntfyAdapter, "send", data, (value) => {
145+
console.log(value);
146+
});
147+
} catch (error) {
148+
console.error("Error: "+error);
149+
}
150+
}
117151

118152

119153
const checkAllWindows = () => {
@@ -154,8 +188,8 @@ on ({id: childrensRoom.contact}, (obj) =>{
154188
on ({id: kitchen.contact}, (obj) =>{
155189
checkAllWindows();
156190
});
157-
191+
/*
158192
on ({id: sleepingRoom.contact}, (obj) =>{
159193
checkAllWindows();
160-
});
194+
});*/
161195
}

0 commit comments

Comments
 (0)