-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontrol.js
242 lines (204 loc) · 6.07 KB
/
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
/*
Copyright (c) 2019 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.
*/
//Enter IP Address of Endpoint below
const ipAddress = "ssh://172.20.10.2"
const jsxapi = require('jsxapi')
const xapi = jsxapi.connect(ipAddress, {
username: 'integrator',
password: 'integrator'
})
// CE maximum value for Ultrasound's MaxVolume
const MAX = 100; // 90 for a DX, and 70 for a RoomKit
const system = require('system-control')();
xapi.on('error', (err) => {
console.error(`connection failed: ${err}, exiting`);
process.exit(1);
})
const Lookup = require("node-yeelight-wifi").Lookup;
let look = new Lookup();
//UPDATE BRIGHTNESS UI
function updateUI(brightness) {
console.log(`updating UI to brightness configuration: ${brightness}`);
// Update brightness text
xapi.command('UserInterface Extensions Widget SetValue', {
WidgetId: 'brightness_text',
Value: brightness
})
// Update brightness slider
const level = Math.round(parseInt(brightness) * 255 / MAX);
xapi.command('UserInterface Extensions Widget SetValue', {
WidgetId: 'brightness_slider',
Value: level
})
}
//UPDATE VOLUME UI
function updateUIVolume(brightness) {
console.log(`updating UI to volume configuration: ${brightness}`);
// Update volume slider
const level = Math.round(parseInt(brightness) * 255 / 100);
xapi.command('UserInterface Extensions Widget SetValue', {
WidgetId: 'volume_slider',
Value: level
})
}
look.on("detected",(light) =>
{
console.log("connection to Bulb Successful");
//SET STARTING VALUE FOR UI ELEMENTS
xapi.on('ready', () => {
console.log("connection to Device Successful");
//set current bulb power state
if(light.power==false){
console.log("Current State OFF")
xapi.command('UserInterface Extensions Widget SetValue', {
WidgetId: 'power_button',
Value: "off"
})
}
else{
console.log("Current State ON")
xapi.command('UserInterface Extensions Widget SetValue', {
WidgetId: 'power_button',
Value: "on"
})
}
//set current brightness text
xapi.command('UserInterface Extensions Widget SetValue', {
WidgetId: 'brightness_text',
Value: light.bright
})
//set the brightness slider Value
const level = Math.round(parseInt(light.bright) * 255 / MAX)
xapi.command('UserInterface Extensions Widget SetValue', {
WidgetId: 'brightness_slider',
Value: level
})
})
//PRESSING POWER BUTTON
xapi.event.on('UserInterface Extensions Widget Action', (event) => {
if (event.Type !== 'changed') return
if (event.WidgetId !== 'power_button') return
if(event.Value=="on")
{
light.setPower(true).then(() =>
{
console.log("Light Turned ON");
}).catch((error =>
{
console.log("failed",error);
}));
}
else{
light.setPower(false).then(() =>
{
console.log("Light Turned OFF");
}).catch((error =>
{
console.log("failed",error);
}));
}
})
//VARYING BRIGHTNESS SLIDER
xapi.event.on('UserInterface Extensions Widget Action', (event) => {
if (event.WidgetId !== 'brightness_slider') return
if (event.Type !== 'changed') return
const brightness = Math.round(parseInt(event.Value) * 100 / 255)
console.log(`Updating brightness configuration to: ${brightness}`)
//Set Bulb Brightness
light.setBright(brightness).then(() =>
{
console.log("Light Intensity changed");
}).catch((error =>
{
console.log("failed",error);
}));
//update UI
updateUI(brightness)
})
//VARYING VOLUME SLIDER
xapi.event.on('UserInterface Extensions Widget Action', (event) => {
if (event.WidgetId !== 'volume_slider') return
if (event.Type !== 'changed') return
const brightness = Math.round(parseInt(event.Value) * 100 / 255)
console.log(`updating volume configuration to: ${brightness}`)
//Set Bulb Brightness
system.audio.setSystemVolume(brightness).then(function() {
});
//update UI
updateUIVolume(brightness)
})
//PRESSING COLOUR BUTTON
xapi.event.on('UserInterface Extensions Widget Action', (event) => {
if (event.Type !== 'pressed') return
if (event.WidgetId == 'red_button'){
light.setRGB([255,0,0]).then(() =>
{
console.log("Colour changed to RED");
}).catch((error =>
{
console.log("failed",error);
}));
}
else if(event.WidgetId == 'blue_button'){
light.setRGB([0,0,255]).then(() =>
{
console.log("Colour changed to BLUE");
}).catch((error =>
{
console.log("failed",error);
}));
}
else if(event.WidgetId == 'green_button'){
light.setRGB([124,252,0]).then(() =>
{
console.log("Colour changed to GREEN");
}).catch((error =>
{
console.log("failed",error);
}));
}
else if(event.WidgetId == 'yellow_button'){
light.setRGB([255,215,0]).then(() =>
{
console.log("Colour changed to YELLOW");
}).catch((error =>
{
console.log("failed",error);
}));
}
else{
return;
}
})
});
//PRESSING MUTE/UNMUTE BUTTON
xapi.event.on('UserInterface Extensions Widget Action', (event) => {
if (event.Type !== 'pressed') return
if (event.WidgetId == 'mute'){
system.audio.isMuted().then(function(muted) {
if(muted){
console.log(muted);
system.audio.mute(false).then(function() {
});
}
else{
system.audio.mute(true).then(function() {
});
}
});
}
else{
return;
}
})