-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradiator_window.ts
161 lines (127 loc) · 3.36 KB
/
radiator_window.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
namespace Fenstererkennung {
type GroupType = {
"radiator":string;
"contact":string;
"name":string;
};
enum radiatorManufacturer {
hama,
shelly
}
enum contactManufacturer {
sonoff
}
const HamaRadiatorMode = {
heat: "heat",
off: "off"
}
const livingRoom:GroupType = {
"radiator": 'zigbee.0.2c1165fffeb3dc7b.mode', // Hama Radiator
"contact": 'zigbee.0.00124b002fbdc865.opened', //open or close
"name": "Wohnzimmer",
} ;
const sleepingRoom:GroupType = {
"radiator": "2",
"contact": '2',
"name":"Schlafzimmer",
};
const kitchen:GroupType = {
"radiator": 'zigbee.0.cc86ecfffec8882f.mode',
"contact": '3',
"name": "Küche",
};
const childrensRoom:GroupType = {
"radiator": 'zigbee.0.cc86ecfffec3b6cf.mode',
"contact": 'zigbee.0.00124b002fbae5b2.opened'/*Is open*/,
"name": "Kinderzimmer",
};
const toilet:GroupType = {
"radiator": 'zigbee.0.cc86ecfffec3996b.mode',
"contact": '5',
"name": "Toilette",
} ;
const house:GroupType[] = [childrensRoom, kitchen, livingRoom, sleepingRoom, toilet];
const northGroup:GroupType[] = [
childrensRoom,
sleepingRoom,
toilet
];
const southGroup:GroupType[] = [
livingRoom,
kitchen
];
const checkIsWindowOpen = (room : GroupType):boolean => {
if (getState(room.contact).val) {
return true;
} else {
return false;
}
}
const deactivateAllRadiators = (rooms:GroupType[]) => {
for (const room of rooms) {
deactivateRadiator(room);
}
}
const activateAllRadiators = (rooms:GroupType[]) => {
for (const room of rooms) {
activateRadiator(room);
}
}
const deactivateRadiator = (room:GroupType) => {
if (getState(room.radiator).val == HamaRadiatorMode.heat) {
sendDiscordMessage("Thermostat in "+room.name+" abgeschaltet");
setState(room.radiator, HamaRadiatorMode.off);
}
}
const activateRadiator = (room:GroupType) => {
if (getState(room.radiator).val == HamaRadiatorMode.off) {
sendDiscordMessage("Thermostat in "+room.name+" eingeschaltet");
setState(room.radiator, HamaRadiatorMode.heat);
}
}
const checkGroup = (group:GroupType[]):boolean => {
for (const room of group) {
if (checkIsWindowOpen(room)) {
return true;
}
}
return false;
}
const sendDiscordMessage = (message:string) => {
const discordAdapter = 'discord.0';
const discordServerId = '';
const discordChannelId = '';
sendTo(discordAdapter, 'sendMessage', {serverId: discordServerId, channelId: discordChannelId, content: {content: message}});
}
const checkAllWindows = () => {
const northOpen = checkGroup(northGroup);
const southOpen = checkGroup(southGroup);
if (northOpen && southOpen) {
deactivateAllRadiators(house);
return;
}
if (!northOpen && !southOpen) {
activateAllRadiators(house);
return;
}
for (const room of house) {
if (checkIsWindowOpen(room)) {
deactivateRadiator(room);
} else {
activateRadiator(room);
}
}
}
on ({id: livingRoom.contact}, (obj) =>{
checkAllWindows();
});
on ({id: childrensRoom.contact}, (obj) =>{
checkAllWindows();
});
on ({id: kitchen.contact}, (obj) =>{
checkAllWindows();
});
on ({id: sleepingRoom.contact}, (obj) =>{
checkAllWindows();
});
}