-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifx_control.js
288 lines (252 loc) · 7.07 KB
/
lifx_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
var LifxClient = require('node-lifx').Client;
var exec = require('child_process').exec;
var awsIot = require('aws-iot-device-sdk');
//
var device = awsIot.device({
"host": "a1hpdzughvb7b0.iot.us-west-2.amazonaws.com",
"port": 8883,
"clientId": "my-room-activator",
"thingName": "My_Room",
"caCert": "aws_certs/root-CA.crt",
"clientCert": "aws_certs/0a5ea7d8f3-certificate.pem.crt",
"privateKey": "aws_certs/0a5ea7d8f3-private.pem.key"
});
// Keep track of the room's state as last seen by the pi. This will help decide which buttons
// need to be pressed to achieve the desired state that was published by Alexa.
var roomState = {
soundbar: {
on: false,
volume: 8
},
tv: {
on: false
},
ac: {
on: false,
mode: 'energy_saver',
timer: 0
}
}
var ac_modes = ['energy_saver', 'cool', 'fan', 'dry'];
var ac_interval;
// Subscribe to the AWS IoT MQTT topic corresponding to the room
//
// Device is an instance returned by mqtt.Client(), see mqtt.js for full
// documentation.
//
device.on('connect', function() {
console.log('Connecting to AWS IoT...');
device.subscribe('$aws/things/My_Room/shadow/update/accepted', 0, function(err, result) {
if(err) {
console.log(err)
} else {
console.log('Subscribed to /update/accepted: ' + JSON.stringify(result));
}
});
device.subscribe('$aws/things/My_Room/shadow/update/rejected', 0, function(err, result) {
if(err) {
console.log(err)
} else {
console.log('Subscribed to /update/rejected: ' + JSON.stringify(result));
}
});
});
device.on('message', function(topic, payload) {
console.log( 'Received message on topic ' + topic + ': ' + JSON.stringify(JSON.parse(payload).state.desired) );
payload = JSON.parse( payload ).state.desired;
// Deal with changes to the lights
if ( payload.lights ) {
var hue = payload.lights.hue ? parseInt( payload.lights.hue ) : 0;
var sat = payload.lights.sat ? parseInt( payload.lights.sat ) : 0;
var bri = payload.lights.bri ? parseInt( payload.lights.bri ) : 100;
// Change the lights based on what was just received on the topic
for ( var i = 0; i < lights.length; i++ ) {
if ( payload.lights.on === false ) {
lights[i].off();
} else {
lights[i].on();
lights[i].color( hue, sat, bri );
}
}
console.log( 'Successfully sent color change command to lights.' );
}
// Deal with changes to the soundbar
if ( payload.soundbar ) {
// If the power of the soundbar isn't what we asked for, hit the soundbar's remote button
if ( payload.soundbar.on !== roomState.soundbar.on ) {
sendIRCommand( 'VIZIO_SOUNDBAR', 'KEY_POWER' );
roomState.soundbar.on = payload.soundbar.on;
}
if ( payload.soundbar.volume >= 0 ) {
changeSoundbarVolume( payload.soundbar.volume );
}
}
// Deal with changes to the TV
if ( payload.tv ) {
// If the power of the TV isn't what we asked for, hit the TV's remote button
if ( payload.tv.on !== roomState.tv.on ) {
sendIRCommand( 'TV', 'KEY_POWER' );
roomState.tv.on = payload.tv.on;
}
}
// Deal with changes to the AC
if ( payload.ac ) {
// If the power of the AC isn't what we asked for, hit the AC's remote button
if ( payload.ac.on !== roomState.ac.on ) {
sendIRCommand( 'AIR_CONDITIONER', 'KEY_POWER' );
roomState.ac.on = payload.ac.on;
}
if ( payload.ac.mode ) {
changeACmode( payload.ac.mode );
}
if ( payload.ac.timer ) {
setACtimer( payload.ac.timer );
} else {
roomState.ac.timer = 0;
}
}
console.log('New room state: ' + JSON.stringify( roomState, null, 2 ));
})
// Create a new client we can use in the future to talk to the lights
var client = new LifxClient();
var lightCounter = 0;
var lights = [];
var color = '';
client.on('light-new', function(light) {
//console.log(light);
lights.push( light );
if ( lights.length === 3 ) {
allLightsWhite();
}
});
client.init();
function allLightsWhite() {
for ( var i = 0; i < lights.length; i++ ) {
lights[i].color( 0, 0, 100 );
}
}
var outstandingIRCommands = 0;
function sendIRCommand( remote, key ) {
// If we have one or more outstanding IR commands, wait until the other have finished before sending a new one
if ( outstandingIRCommands > 0 ) {
setTimeout(function() {
sendIRCommand( remote, key );
}, 10);
} else {
outstandingIRCommands ++ ;
var cmd = exec('irsend SEND_ONCE ' + remote + ' ' + key, function(error, stdout, stderr) {
if ( error ) {
console.log( 'Error ' + error + ' sending command line command: ' + stderr );
return;
}
console.log( 'Successfully sent IR key press: ' + remote + ', ' + key );
outstandingIRCommands -- ;
});
}
}
function changeACmode( mode ) {
var numModeChanges = 0;
if ( mode !== roomState.ac.mode ) {
switch( roomState.ac.mode ) {
case 'energy_saver':
switch( mode ) {
case 'cool':
numModeChanges = 1;
break;
case 'fan':
numModeChanges = 2;
break;
case 'dry':
numModeChanges = 3;
break;
}
break;
case 'cool':
switch( mode ) {
case 'fan':
numModeChanges = 1;
break;
case 'dry':
numModeChanges = 2;
break;
case 'energy_saver':
numModeChanges = 3;
break;
}
break;
case 'fan':
switch( mode ) {
case 'dry':
numModeChanges = 1;
break;
case 'energy_saver':
numModeChanges = 2;
break;
case 'cool':
numModeChanges = 3;
break;
}
break;
case 'dry':
switch( mode ) {
case 'energy_saver':
numModeChanges = 1;
break;
case 'cool':
numModeChanges = 2;
break;
case 'fan':
numModeChanges = 3;
break;
}
break;
}
for ( var i = 0; i < numModeChanges; i++ ) {
sendIRCommand( 'AIR_CONDITIONER', 'KEY_MODE');
}
roomState.ac.mode = mode;
}
};
function setACtimer( hours ) {
// Make sure the AC is off
if ( roomState.ac.on ) {
sendIRCommand( 'AIR_CONDITIONER', 'KEY_POWER' );
}
// If there's a timer set for a large number of hours, reset the AC
if ( roomState.ac.timer > hours ) {
sendIRCommand( 'AIR_CONDITIONER', 'KEY_POWER' );
sendIRCommand( 'AIR_CONDITIONER', 'KEY_POWER' );
} else if ( roomState.ac.timer > 0 ) {
// If there's already a number of hours set, just increase the number of hours
hours = hours - roomState.ac.timer;
}
for ( var i = 0; i < hours; i++ ) {
sendIRCommand( 'AIR_CONDITIONER', 'KEY_TIMER' );
}
roomState.ac.timer = hours;
// Create an interval to count down the hours as the AC counts down the hours
acInterval = setInterval(function() {
if ( roomState.ac.timer > 0 ) {
roomState.ac.timer--;
} else {
clearInterval( acInterval );
roomState.ac.on = true;
}
}, 3600000);
};
function changeSoundbarVolume( volume ) {
// Soundbar's max volume is 35
if ( volume > 35 ) {
volume = 35;
}
if ( volume > roomState.soundbar.volume ) {
for ( var i = 0; i < (volume - roomState.soundbar.volume); i++ ) {
sendIRCommand( 'VIZIO_SOUNDBAR', 'KEY_VOLUMEUP');
}
} else if ( volume < roomState.soundbar.volume ) {
for ( var i = 0; i < (roomState.soundbar.volume - volume); i++ ) {
sendIRCommand( 'VIZIO_SOUNDBAR', 'KEY_VOLUMEDOWN');
}
}
roomState.soundbar.volume = volume;
}