-
Notifications
You must be signed in to change notification settings - Fork 0
/
light_control.js
354 lines (315 loc) · 12.3 KB
/
light_control.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/********************************************************
Copyright (c) 2022 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
*********************************************************
* Author: Gerardo Chaves
* Solutions Engineer
* Cisco Systems
*
* This macro controls a smart light bulbs in the room where the Webex Room Device is located.
* It provides a custom panel with controls for turning on and off the lights and setting the color.
* It communicates with the light bulbs using the HTTP interface provided by the vendor: https://www.shelly.com/documents/developers/ddd_communication.pdf
*
*
*
*
* Released: July 17, 2024
* Updated: September 5, 2024
*
* Version: 1.0.3
*/
import xapi from 'xapi';
const ligthIPs = ['10.0.1.111', '10.0.1.112', '10.0.1.113', '10.0.1.114']; // Set the IP address of the lights
const LIGHT_USERNAME = 'admin'; // Set the username for the smart switch, leave blank if not needed
const LIGHT_PASSWORD = 'password'; // Set the password for the smart switch
// Leave CELEBRATING_URL blank if no video needs to be shown on the main screen during the celebration.
// Otherwise, put the URL of a video such as 'https://www.youtube.com/watch?v=VaOGlkkL0j4' but insure it plays automatically.
let CELEBRATING_URL = ''; // Set the URL of the video to be shown during the celebration
const CELEBRATING_DURATION = 30000; // Set the time in milliseconds for the celebration to last
let IntervId;
let cancelTimer;
let nIntervId;
let red = 255;
let green = 255;
let blue = 255;
let white = 0;
let isCelebrating = false
let get_auth = ""
if (LIGHT_USERNAME != "") get_auth = LIGHT_USERNAME + ':' + LIGHT_PASSWORD + '@'
let custom_panel = `<Extensions>
<Version>1.11</Version>
<Panel>
<Order>3</Order>
<PanelId>panel_lights</PanelId>
<Origin>local</Origin>
<Location>HomeScreen</Location>
<Icon>Lightbulb</Icon>
<Name>Lights</Name>
<ActivityType>Custom</ActivityType>
<Page>
<Name>Lights</Name>
<Row>
<Name>Control</Name>
<Widget>
<WidgetId>widget_20</WidgetId>
<Name>Off</Name>
<Type>Text</Type>
<Options>size=1;fontSize=normal;align=center</Options>
</Widget>
<Widget>
<WidgetId>widget_toggle_on_off</WidgetId>
<Type>ToggleButton</Type>
<Options>size=1</Options>
</Widget>
<Widget>
<WidgetId>widget_21</WidgetId>
<Name>On</Name>
<Type>Text</Type>
<Options>size=1;fontSize=normal;align=center</Options>
</Widget>
</Row>
<Row>
<Name>Color</Name>
<Widget>
<WidgetId>widget_25</WidgetId>
<Name>Red</Name>
<Type>Text</Type>
<Options>size=1;fontSize=normal;align=center</Options>
</Widget>
<Widget>
<WidgetId>widget_red_slider</WidgetId>
<Type>Slider</Type>
<Options>size=2</Options>
</Widget>
<Widget>
<WidgetId>widget_red_value</WidgetId>
<Name>0</Name>
<Type>Text</Type>
<Options>size=1;fontSize=normal;align=center</Options>
</Widget>
<Widget>
<WidgetId>widget_31</WidgetId>
<Name>Green</Name>
<Type>Text</Type>
<Options>size=1;fontSize=normal;align=center</Options>
</Widget>
<Widget>
<WidgetId>widget_green_slider</WidgetId>
<Type>Slider</Type>
<Options>size=2</Options>
</Widget>
<Widget>
<WidgetId>widget_green_value</WidgetId>
<Name>0</Name>
<Type>Text</Type>
<Options>size=1;fontSize=normal;align=center</Options>
</Widget>
<Widget>
<WidgetId>widget_32</WidgetId>
<Name>Blue</Name>
<Type>Text</Type>
<Options>size=1;fontSize=normal;align=center</Options>
</Widget>
<Widget>
<WidgetId>widget_blue_slider</WidgetId>
<Type>Slider</Type>
<Options>size=2</Options>
</Widget>
<Widget>
<WidgetId>widget_blue_value</WidgetId>
<Name>0</Name>
<Type>Text</Type>
<Options>size=1;fontSize=normal;align=center</Options>
</Widget>
</Row>
<Row>
<Name>Celebrate 🎉</Name>
<Widget>
<WidgetId>widget_button_celebrate</WidgetId>
<Name>Go!</Name>
<Type>Button</Type>
<Options>size=3</Options>
</Widget>
<Widget>
<WidgetId>widget_button_cancel</WidgetId>
<Name>Cancel</Name>
<Type>Button</Type>
<Options>size=1</Options>
</Widget>
</Row>
<Options/>
</Page>
</Panel>
</Extensions>
`
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function sendHTTPGet(url) {
try {
await xapi.Command.HttpClient.Get({ AllowInsecureHTTPS: 'True', Url: url })
.then((response) => { if (response.StatusCode === "200") { console.debug("Successfully sent command via get: " + url) } });
}
catch (e) {
console.log("http GET error... continuing")
console.debug(e)
}
}
// randomizeLights function sets each light to a random color and intensity
async function randomizeLights() {
// modified to just always just show green, white and red representing the mexican flag since
// long response times from the lights were causing errors in the macro due to too many http connections
console.debug("setting each bulb to a different color an intensity to celebrate...");
let url = 'http://' + get_auth + ligthIPs[0] + '/color/0?turn=on&red=' + 0 + '&green=' + 255 + '&blue=' + 0 + '&white=0';
await sendHTTPGet(url)
await delay(200);
url = 'http://' + get_auth + ligthIPs[1] + '/color/0?turn=on&red=' + 255 + '&green=' + 255 + '&blue=' + 255 + '&white=0';
await sendHTTPGet(url)
await delay(200);
url = 'http://' + get_auth + ligthIPs[2] + '/color/0?turn=on&red=' + 255 + '&green=' + 255 + '&blue=' + 255 + '&white=0';
await sendHTTPGet(url)
await delay(200);
url = 'http://' + get_auth + ligthIPs[3] + '/color/0?turn=on&red=' + 255 + '&green=' + 0 + '&blue=' + 0 + '&white=0';
await sendHTTPGet(url)
await delay(200);
// original code to randomize all lights
// for (let i = 0; i < ligthIPs.length; i++) {
// let url = 'http://' + get_auth + ligthIPs[i] + '/color/0?turn=on&red=' + Math.floor(Math.random() * 255) + '&green=' + Math.floor(Math.random() * 255) + '&blue=' + Math.floor(Math.random() * 255) + '&white=0';
// await sendHTTPGet(url)
// await delay(200);
// }
}
async function lightSwitch(on_off_setting) {
console.log("Turning all lights " + on_off_setting + "...");
for (let i = 0; i < ligthIPs.length; i++) {
let url = 'http://' + get_auth + ligthIPs[i] + '/color/0?turn=' + on_off_setting;
if (!isCelebrating) await sendHTTPGet(url)
}
}
async function setAllColors() {
console.log("in setAllColors....");
if (!isCelebrating)
await setColor(red, green, blue, white);
}
async function setAllColorMode() {
console.log("Setting all lights to color mode...");
for (let i = 0; i < ligthIPs.length; i++) {
let url = 'http://' + get_auth + ligthIPs[i] + '/color/0?mode=color';
await sendHTTPGet(url)
await delay(200);
}
}
async function setColor(red, green, blue, white) {
console.log("Setting all lights to color " + red + ", " + green + ", " + blue + ", " + white + "...");
for (let i = 0; i < ligthIPs.length; i++) {
let url = 'http://' + get_auth + ligthIPs[i] + '/color/0?red=' + red + '&green=' + green + '&blue=' + blue + '&white=' + white;
if (!isCelebrating) await sendHTTPGet(url)
await delay(200)
}
}
async function handleWidgetActions(event) {
let widgetId = event.WidgetId;
switch (widgetId) {
case 'widget_button_celebrate':
if (event.Type == 'released') {
console.log("Starting celebration!!")
isCelebrating = true;
if (CELEBRATING_URL != "")
await xapi.Command.UserInterface.WebView.Display({ Url: CELEBRATING_URL });
// set initial randomization of lights
await randomizeLights()
// radomize lights every 5000ms since latest version just shows the mexican flag colors always
// set back to somthing smaller like 500ms if you want to randomize the lights
nIntervId = setInterval(randomizeLights, 5000);
// after CELEBRATING_DURATION ms, cancel the celebration!
cancelTimer = setTimeout(function () {
clearInterval(nIntervId);
if (CELEBRATING_URL != "") xapi.Command.UserInterface.WebView.Clear();
isCelebrating = false;
},
CELEBRATING_DURATION);
}
break;
case 'widget_button_cancel':
if (event.Type == 'released') {
console.log("Cancel celebration!!")
if (cancelTimer)
clearTimeout(cancelTimer);
if (isCelebrating) {
clearInterval(nIntervId);
if (CELEBRATING_URL != "") xapi.Command.UserInterface.WebView.Clear();
isCelebrating = false;
}
}
break;
case 'widget_red_slider':
if (event.Type == "changed") {
red = parseInt(event.Value);
console.log("Red set to: " + red);
await xapi.Command.UserInterface.Extensions.Widget.SetValue({ Value: red.toString(), WidgetId: 'widget_red_value' });
}
break;
case 'widget_green_slider':
if (event.Type == "changed") {
green = parseInt(event.Value);
console.log("Green set to: " + green);
await xapi.Command.UserInterface.Extensions.Widget.SetValue({ Value: green.toString(), WidgetId: 'widget_green_value' });
}
break;
case 'widget_blue_slider':
if (event.Type == "changed") {
blue = parseInt(event.Value);
console.log("Blue set to: " + blue);
await xapi.Command.UserInterface.Extensions.Widget.SetValue({ Value: blue.toString(), WidgetId: 'widget_blue_value' });
}
break;
case 'widget_white_slider':
if (event.Type == "changed") {
white = parseInt(event.Value);
console.log("White set to: " + white);
await xapi.Command.UserInterface.Extensions.Widget.SetValue({ Value: white.toString(), WidgetId: 'widget_white_value' });
}
break;
case 'widget_toggle_on_off':
if (event.Type == 'changed') {
if (event.Value == 'on') {
console.log("Turning on lights...");
await lightSwitch('on');
} else {
console.log("Turning off lights...");
await lightSwitch('off');
}
}
break
}
}
async function main() {
// Enable the HTTP Client
xapi.Config.HttpClient.Mode.set('On');
xapi.Config.HttpClient.AllowInsecureHTTPS.set('True');
// create the custom panel
xapi.Command.UserInterface.Extensions.Panel.Save({ PanelId: 'panel_lights' }, custom_panel);
// set initial widget values
await xapi.Command.UserInterface.Extensions.Widget.SetValue({ WidgetId: 'widget_red_slider', Value: red });
await xapi.Command.UserInterface.Extensions.Widget.SetValue({ WidgetId: 'widget_red_value', Value: red.toString() });
await xapi.Command.UserInterface.Extensions.Widget.SetValue({ WidgetId: 'widget_green_slider', Value: green });
await xapi.Command.UserInterface.Extensions.Widget.SetValue({ WidgetId: 'widget_green_value', Value: green.toString() });
await xapi.Command.UserInterface.Extensions.Widget.SetValue({ WidgetId: 'widget_blue_slider', Value: blue });
await xapi.Command.UserInterface.Extensions.Widget.SetValue({ WidgetId: 'widget_blue_value', Value: blue.toString() });
// register the custom panel events
xapi.Event.UserInterface.Extensions.Widget.Action.on(event => handleWidgetActions(event));
await setAllColorMode();
await delay(1000);
// set light color every second
setInterval(setAllColors, 3000);
}
main()