|
@@ -1,64 +1,265 @@
|
1
|
|
-/*
|
2
|
|
- * The idea is to use retained messages.
|
3
|
|
- * This way we can keep the state of the lights.
|
4
|
|
- * Make sure all senders use retained messages!
|
5
|
|
- * (in here and shell scripts on PC)
|
6
|
|
- */
|
7
|
|
-
|
8
|
|
-const options = {
|
9
|
|
- clean: true,
|
10
|
|
- connectTimeout: 4000,
|
11
|
|
- clientId: 'lights-web',
|
12
|
|
- username: mqttUsername,
|
13
|
|
- password: mqttPassword,
|
14
|
|
-}
|
15
|
|
-const callbacks = []
|
16
|
|
-const client = mqtt.connect(mqttUrl, options)
|
|
1
|
+// --------------------------
|
|
2
|
+// bathroom
|
|
3
|
+// --------------------------
|
17
|
4
|
|
18
|
|
-client.on('connect', function () {
|
19
|
|
- console.log('MQTT Connected')
|
20
|
|
-})
|
|
5
|
+const btnsBath = document.querySelectorAll("#bathroomlightauto, #bathroomlightbig, #bathroomlightsmall, #bathroomlightoff")
|
21
|
6
|
|
22
|
|
-client.on('message', function (topic, message) {
|
23
|
|
- console.log("Rx \"" + topic.toString() + "\": \"" + message.toString() + "\"")
|
|
7
|
+// handle changes to bathroom lights
|
|
8
|
+subscribeTopic("bathroom/force_light", function (msg) {
|
|
9
|
+ // clear all buttons
|
|
10
|
+ for (const btn of btnsBath) {
|
|
11
|
+ btn.checked = false
|
|
12
|
+ }
|
24
|
13
|
|
25
|
|
- for (const cb of callbacks) {
|
26
|
|
- if (cb.topic == topic) {
|
27
|
|
- console.log("Routing to Callback")
|
28
|
|
- cb.callback(message)
|
|
14
|
+ // activate proper button
|
|
15
|
+ if (msg == "none") {
|
|
16
|
+ const btn = document.querySelector("#bathroomlightauto")
|
|
17
|
+ btn.checked = true
|
|
18
|
+ } else if (msg == "big") {
|
|
19
|
+ const btn = document.querySelector("#bathroomlightbig")
|
|
20
|
+ btn.checked = true
|
|
21
|
+ } else if (msg == "small") {
|
|
22
|
+ const btn = document.querySelector("#bathroomlightsmall")
|
|
23
|
+ btn.checked = true
|
|
24
|
+ } else if (msg == "off") {
|
|
25
|
+ const btn = document.querySelector("#bathroomlightoff")
|
|
26
|
+ btn.checked = true
|
|
27
|
+ } else {
|
|
28
|
+ console.log("unknown bathroom/force_light msg " + msg)
|
|
29
|
+ }
|
|
30
|
+})
|
|
31
|
+
|
|
32
|
+// set new bathroom light state
|
|
33
|
+for (const btn of btnsBath) {
|
|
34
|
+ btn.addEventListener('change', function (e) {
|
|
35
|
+ if (this.checked) {
|
|
36
|
+ if (this == document.querySelector("#bathroomlightauto")) {
|
|
37
|
+ setTopic("bathroom/force_light", "none")
|
|
38
|
+ } else if (this == document.querySelector("#bathroomlightbig")) {
|
|
39
|
+ setTopic("bathroom/force_light", "big")
|
|
40
|
+ } else if (this == document.querySelector("#bathroomlightsmall")) {
|
|
41
|
+ setTopic("bathroom/force_light", "small")
|
|
42
|
+ } else if (this == document.querySelector("#bathroomlightoff")) {
|
|
43
|
+ setTopic("bathroom/force_light", "off")
|
|
44
|
+ } else {
|
|
45
|
+ console.log("unknown bathroom/force_light btn value " + this.value)
|
|
46
|
+ }
|
29
|
47
|
}
|
|
48
|
+ })
|
|
49
|
+}
|
|
50
|
+
|
|
51
|
+// handle bathroom sensors
|
|
52
|
+subscribeSensor("bathroom/temperature", "°C", "#bathtemp")
|
|
53
|
+subscribeSensor("bathroom/humidity", "%", "#bathhumid")
|
|
54
|
+subscribeSensor("bathroom/pressure", "mbar", "#bathpress", 100.0)
|
|
55
|
+subscribeSensor("bathroom/tvoc", "ppb", "#bathtvoc")
|
|
56
|
+subscribeSensor("bathroom/eco2", "ppm", "#batheco2")
|
|
57
|
+
|
|
58
|
+// --------------------------
|
|
59
|
+// livingroom
|
|
60
|
+// --------------------------
|
|
61
|
+
|
|
62
|
+const btnsKitchen = document.querySelectorAll("#kitchenon, #kitchenoff")
|
|
63
|
+const btnsWorkspace = document.querySelectorAll("#workspaceon, #workspaceoff, #workspacepc, #workspacebench")
|
|
64
|
+const btnsTv = document.querySelectorAll("#tvon, #tvamp, #tvbox, #tvoff")
|
|
65
|
+
|
|
66
|
+// handle changes to kitchen lights
|
|
67
|
+subscribeTopic("livingroom/light_kitchen", function (msg) {
|
|
68
|
+ // clear all buttons
|
|
69
|
+ for (const btn of btnsKitchen) {
|
|
70
|
+ btn.checked = false
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ // activate proper button
|
|
74
|
+ if (msg == "on") {
|
|
75
|
+ const btn = document.querySelector("#kitchenon")
|
|
76
|
+ btn.checked = true
|
|
77
|
+ } else if (msg == "off") {
|
|
78
|
+ const btn = document.querySelector("#kitchenoff")
|
|
79
|
+ btn.checked = true
|
|
80
|
+ } else {
|
|
81
|
+ console.log("unknown livingroom/light_kitchen msg " + msg)
|
30
|
82
|
}
|
31
|
83
|
})
|
32
|
84
|
|
33
|
|
-/*
|
34
|
|
-function clearSubscriptions() {
|
35
|
|
- for (const cb of callbacks) {
|
36
|
|
- client.unsubscribe(cb.topic)
|
|
85
|
+// set new kitchen light state
|
|
86
|
+for (const btn of btnsKitchen) {
|
|
87
|
+ btn.addEventListener('change', function (e) {
|
|
88
|
+ if (this.checked) {
|
|
89
|
+ if (this == document.querySelector("#kitchenon")) {
|
|
90
|
+ setTopic("livingroom/light_kitchen", "on")
|
|
91
|
+ } else if (this == document.querySelector("#kitchenoff")) {
|
|
92
|
+ setTopic("livingroom/light_kitchen", "off")
|
|
93
|
+ } else {
|
|
94
|
+ console.log("unknown livingroom/light_kitchen btn value " + this.value)
|
|
95
|
+ }
|
|
96
|
+ }
|
|
97
|
+ })
|
|
98
|
+}
|
|
99
|
+
|
|
100
|
+const state_light_pc = 0
|
|
101
|
+const state_light_bench = 0
|
|
102
|
+
|
|
103
|
+function setWorkspaceLightsButtons() {
|
|
104
|
+ // clear all buttons
|
|
105
|
+ for (const btn of btnsWorkspace) {
|
|
106
|
+ btn.checked = false
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ // activate proper button
|
|
110
|
+ if ((state_light_pc == 1) && (state_light_bench == 1)) {
|
|
111
|
+ const btn = document.querySelector("#workspaceon")
|
|
112
|
+ btn.checked = true
|
|
113
|
+ } else if ((state_light_pc == 1) && (state_light_bench == 0)) {
|
|
114
|
+ const btn = document.querySelector("#workspacepc")
|
|
115
|
+ btn.checked = true
|
|
116
|
+ } else if ((state_light_pc == 0) && (state_light_bench == 1)) {
|
|
117
|
+ const btn = document.querySelector("#workspacebench")
|
|
118
|
+ btn.checked = true
|
|
119
|
+ } else {
|
|
120
|
+ const btn = document.querySelector("#workspaceoff")
|
|
121
|
+ btn.checked = true
|
37
|
122
|
}
|
38
|
|
- callbacks = []
|
39
|
123
|
}
|
40
|
|
-*/
|
41
|
124
|
|
42
|
|
-function subscribeTopic(topic, callback) {
|
43
|
|
- console.log("Sub to \"" + topic.toString() + "\"")
|
|
125
|
+// handle changes to workspace lights
|
|
126
|
+subscribeTopic("livingroom/light_pc", function (msg) {
|
|
127
|
+ if (msg == "on") {
|
|
128
|
+ state_light_pc = 1
|
|
129
|
+ } else if (msg == "off") {
|
|
130
|
+ state_light_pc = 0
|
|
131
|
+ } else {
|
|
132
|
+ console.log("unknown livingroom/light_pc msg " + msg)
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ setWorkspaceLightsButtons()
|
|
136
|
+})
|
|
137
|
+subscribeTopic("livingroom/light_bench", function (msg) {
|
|
138
|
+ if (msg == "on") {
|
|
139
|
+ state_light_bench = 1
|
|
140
|
+ } else if (msg == "off") {
|
|
141
|
+ state_light_bench = 0
|
|
142
|
+ } else {
|
|
143
|
+ console.log("unknown livingroom/light_bench msg " + msg)
|
|
144
|
+ }
|
|
145
|
+
|
|
146
|
+ setWorkspaceLightsButtons()
|
|
147
|
+})
|
|
148
|
+
|
|
149
|
+// set new workspace light state
|
|
150
|
+for (const btn of btnsWorkspace) {
|
|
151
|
+ btn.addEventListener('change', function (e) {
|
|
152
|
+ if (this.checked) {
|
|
153
|
+ if (this == document.querySelector("#workspaceon")) {
|
|
154
|
+ state_light_pc = 1
|
|
155
|
+ state_light_bench = 1
|
|
156
|
+ setTopic("livingroom/light_pc", "on")
|
|
157
|
+ setTopic("livingroom/light_bench", "on")
|
|
158
|
+ } else if (this == document.querySelector("#workspaceoff")) {
|
|
159
|
+ state_light_pc = 0
|
|
160
|
+ state_light_bench = 0
|
|
161
|
+ setTopic("livingroom/light_pc", "off")
|
|
162
|
+ setTopic("livingroom/light_bench", "off")
|
|
163
|
+ } else if (this == document.querySelector("#workspacepc")) {
|
|
164
|
+ state_light_pc = 1
|
|
165
|
+ state_light_bench = 0
|
|
166
|
+ setTopic("livingroom/light_pc", "on")
|
|
167
|
+ setTopic("livingroom/light_bench", "off")
|
|
168
|
+ } else if (this == document.querySelector("#workspacebench")) {
|
|
169
|
+ state_light_bench = 1
|
|
170
|
+ state_light_pc = 0
|
|
171
|
+ setTopic("livingroom/light_bench", "on")
|
|
172
|
+ setTopic("livingroom/light_pc", "off")
|
|
173
|
+ } else {
|
|
174
|
+ console.log("unknown btn value " + this.value)
|
|
175
|
+ }
|
|
176
|
+ }
|
|
177
|
+ })
|
|
178
|
+}
|
|
179
|
+
|
|
180
|
+const state_light_amp = 0
|
|
181
|
+const state_light_box = 0
|
44
|
182
|
|
45
|
|
- subOptions = {
|
46
|
|
- rh: true,
|
|
183
|
+function setTvLightsButtons() {
|
|
184
|
+ // clear all buttons
|
|
185
|
+ for (const btn of btnsTv) {
|
|
186
|
+ btn.checked = false
|
47
|
187
|
}
|
48
|
|
- client.subscribe(topic)
|
49
|
188
|
|
50
|
|
- callbackObj = {
|
51
|
|
- topic: topic,
|
52
|
|
- callback: callback,
|
|
189
|
+ // activate proper button
|
|
190
|
+ if ((state_light_amp == 1) && (state_light_box == 1)) {
|
|
191
|
+ const btn = document.querySelector("#tvon")
|
|
192
|
+ btn.checked = true
|
|
193
|
+ } else if ((state_light_amp == 1) && (state_light_box == 0)) {
|
|
194
|
+ const btn = document.querySelector("#tvamp")
|
|
195
|
+ btn.checked = true
|
|
196
|
+ } else if ((state_light_amp == 0) && (state_light_box == 1)) {
|
|
197
|
+ const btn = document.querySelector("#tvbox")
|
|
198
|
+ btn.checked = true
|
|
199
|
+ } else {
|
|
200
|
+ const btn = document.querySelector("#tvoff")
|
|
201
|
+ btn.checked = true
|
53
|
202
|
}
|
54
|
|
- callbacks.push(callbackObj)
|
55
|
203
|
}
|
56
|
204
|
|
57
|
|
-function setTopic(topic, message) {
|
58
|
|
- console.log("Tx \"" + topic.toString() + "\": \"" + message.toString() + "\"")
|
|
205
|
+// handle changes to tv lights
|
|
206
|
+subscribeTopic("livingroom/light_amp", function (msg) {
|
|
207
|
+ if (msg == "on") {
|
|
208
|
+ state_light_amp = 1
|
|
209
|
+ } else if (msg == "off") {
|
|
210
|
+ state_light_amp = 0
|
|
211
|
+ } else {
|
|
212
|
+ console.log("unknown livingroom/light_amp msg " + msg)
|
|
213
|
+ }
|
59
|
214
|
|
60
|
|
- pubOptions = {
|
61
|
|
- retain: true,
|
|
215
|
+ setWorkspaceLightsButtons()
|
|
216
|
+})
|
|
217
|
+subscribeTopic("livingroom/light_box", function (msg) {
|
|
218
|
+ if (msg == "on") {
|
|
219
|
+ state_light_box = 1
|
|
220
|
+ } else if (msg == "off") {
|
|
221
|
+ state_light_box = 0
|
|
222
|
+ } else {
|
|
223
|
+ console.log("unknown livingroom/light_box msg " + msg)
|
62
|
224
|
}
|
63
|
|
- client.publish(topic, message, pubOptions)
|
|
225
|
+
|
|
226
|
+ setWorkspaceLightsButtons()
|
|
227
|
+})
|
|
228
|
+
|
|
229
|
+// set new tv light state
|
|
230
|
+for (const btn of btnsTv) {
|
|
231
|
+ btn.addEventListener('change', function (e) {
|
|
232
|
+ if (this.checked) {
|
|
233
|
+ if (this == document.querySelector("#tvon")) {
|
|
234
|
+ state_light_amp = 1
|
|
235
|
+ state_light_box = 1
|
|
236
|
+ setTopic("livingroom/light_amp", "on")
|
|
237
|
+ setTopic("livingroom/light_box", "on")
|
|
238
|
+ } else if (this == document.querySelector("#tvoff")) {
|
|
239
|
+ state_light_amp = 0
|
|
240
|
+ state_light_box = 0
|
|
241
|
+ setTopic("livingroom/light_amp", "off")
|
|
242
|
+ setTopic("livingroom/light_box", "off")
|
|
243
|
+ } else if (this == document.querySelector("#tvamp")) {
|
|
244
|
+ state_light_amp = 1
|
|
245
|
+ state_light_box = 0
|
|
246
|
+ setTopic("livingroom/light_amp", "on")
|
|
247
|
+ setTopic("livingroom/light_box", "off")
|
|
248
|
+ } else if (this == document.querySelector("#tvbox")) {
|
|
249
|
+ state_light_box = 1
|
|
250
|
+ state_light_amp = 0
|
|
251
|
+ setTopic("livingroom/light_box", "on")
|
|
252
|
+ setTopic("livingroom/light_amp", "off")
|
|
253
|
+ } else {
|
|
254
|
+ console.log("unknown btn value " + this.value)
|
|
255
|
+ }
|
|
256
|
+ }
|
|
257
|
+ })
|
64
|
258
|
}
|
|
259
|
+
|
|
260
|
+// handle livingroom sensors
|
|
261
|
+subscribeSensor("livingroom/temperature", "°C", "#livingtemp")
|
|
262
|
+subscribeSensor("livingroom/humidity", "%", "#livinghumid")
|
|
263
|
+subscribeSensor("livingroom/pressure", "mbar", "#livingpress", 100.0)
|
|
264
|
+subscribeSensor("livingroom/tvoc", "ppb", "#livingtvoc")
|
|
265
|
+subscribeSensor("livingroom/eco2", "ppm", "#livingeco2")
|