forked from luisiam/homebridge-cmdswitch2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
528 lines (460 loc) · 17.4 KB
/
index.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
var exec = require("child_process").exec;
var Accessory, Service, Characteristic, UUIDGen;
var queuedCommands;
module.exports = function (homebridge) {
Accessory = homebridge.platformAccessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
queuedCommands = new Array();
homebridge.registerPlatform("homebridge-cmdswitch2", "cmdSwitch2", cmdSwitchPlatform, true);
}
function cmdSwitchPlatform(log, config, api) {
this.log = log;
this.config = config || {"platform": "cmdSwitch2"};
this.switches = this.config.switches || [];
this.accessories = {};
this.polling = {};
if (api) {
this.api = api;
this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this));
}
}
// Method to restore accessories from cache
cmdSwitchPlatform.prototype.configureAccessory = function (accessory) {
this.setService(accessory);
this.accessories[accessory.context.name] = accessory;
}
// Method to setup accesories from config.json
cmdSwitchPlatform.prototype.didFinishLaunching = function () {
// Add or update accessories defined in config.json
for (var i in this.switches) this.addAccessory(this.switches[i]);
// Remove extra accessories in cache
for (var name in this.accessories) {
var accessory = this.accessories[name];
if (!accessory.reachable) this.removeAccessory(accessory);
}
var self = this;
setInterval(function () {
if (queuedCommands.length) {
var command = queuedCommands.shift();
self.setPowerState(command['thisSwitch'], command['state'], command['callback']);
}
}, 500);
}
// Method to add and update HomeKit accessories
cmdSwitchPlatform.prototype.addAccessory = function (data) {
this.log("Initializing platform accessory '" + data.name + "'...");
// Retrieve accessory from cache
var accessory = this.accessories[data.name];
if (!accessory) {
// Setup accessory as SWITCH (8) category.
var uuid = UUIDGen.generate(data.name);
accessory = new Accessory(data.name, uuid, 8);
// Setup HomeKit switch service
accessory.addService(Service.Switch, data.name);
// New accessory is always reachable
accessory.reachable = true;
// Setup listeners for different switch events
this.setService(accessory);
// Register new accessory in HomeKit
this.api.registerPlatformAccessories("homebridge-cmdswitch2", "cmdSwitch2", [accessory]);
// Store accessory in cache
this.accessories[data.name] = accessory;
}
// Confirm variable type
data.polling = data.polling === true;
data.interval = parseInt(data.interval, 10) || 1;
data.timeout = parseInt(data.timeout, 10) || 1;
if (data.manufacturer) data.manufacturer = data.manufacturer.toString();
if (data.model) data.model = data.model.toString();
if (data.serial) data.serial = data.serial.toString();
// Store and initialize variables into context
var cache = accessory.context;
cache.name = data.name;
cache.on_cmd = data.on_cmd;
cache.off_cmd = data.off_cmd;
cache.state_cmd = data.state_cmd;
cache.polling = data.polling;
cache.interval = data.interval;
cache.timeout = data.timeout;
cache.manufacturer = data.manufacturer;
cache.model = data.model;
cache.serial = data.serial;
if (cache.state === undefined) {
cache.state = false;
if (data.off_cmd && !data.on_cmd) cache.state = true;
}
// Retrieve initial state
this.getInitState(accessory);
// Configure state polling
if (data.polling && data.state_cmd) this.statePolling(data.name);
}
// Method to remove accessories from HomeKit
cmdSwitchPlatform.prototype.removeAccessory = function (accessory) {
if (accessory) {
var name = accessory.context.name;
this.log(name + " is removed from HomeBridge.");
this.api.unregisterPlatformAccessories("homebridge-cmdswitch2", "cmdSwitch2", [accessory]);
delete this.accessories[name];
}
}
// Method to setup listeners for different events
cmdSwitchPlatform.prototype.setService = function (accessory) {
accessory.getService(Service.Switch)
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this, accessory.context))
.on('set', this.queuePowerState.bind(this, accessory.context));
accessory.on('identify', this.identify.bind(this, accessory.context));
}
// Method to retrieve initial state
cmdSwitchPlatform.prototype.getInitState = function (accessory) {
var manufacturer = accessory.context.manufacturer || "Default-Manufacturer";
var model = accessory.context.model || "Default-Model";
var serial = accessory.context.serial || "Default-SerialNumber";
// Update HomeKit accessory information
accessory.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, manufacturer)
.setCharacteristic(Characteristic.Model, model)
.setCharacteristic(Characteristic.SerialNumber, serial);
// Retrieve initial state if polling is disabled
if (!accessory.context.polling) {
accessory.getService(Service.Switch)
.getCharacteristic(Characteristic.On)
.getValue();
}
// Configured accessory is reachable
accessory.updateReachability(true);
}
// Method to determine current state
cmdSwitchPlatform.prototype.getState = function (thisSwitch, callback) {
var self = this;
// Return cached state if no state_cmd provided
if (thisSwitch.state_cmd === undefined) {
callback(null, thisSwitch.state);
return;
}
// Execute command to detect state
exec(thisSwitch.state_cmd, function (error, stdout, stderr) {
var state = error ? false : true;
// Error detection
if (stderr) {
self.log("Failed to determine " + thisSwitch.name + " state.");
self.log(stderr);
}
callback(stderr, state);
});
}
// Method to determine current state
cmdSwitchPlatform.prototype.statePolling = function (name) {
var accessory = this.accessories[name];
var thisSwitch = accessory.context;
// Clear polling
clearTimeout(this.polling[name]);
this.getState(thisSwitch, function (error, state) {
// Update state if there's no error
if (!error && state !== thisSwitch.state) {
thisSwitch.state = state;
accessory.getService(Service.Switch)
.getCharacteristic(Characteristic.On)
.getValue();
}
});
// Setup for next polling
this.polling[name] = setTimeout(this.statePolling.bind(this, name), thisSwitch.interval * 1000);
}
cmdSwitchPlatform.prototype.queuePowerState = function (thisSwitch, state, callback) {
queuedCommands.push({
'thisSwitch': thisSwitch,
'state': state,
'callback': callback,
});
}
// Method to determine current state
cmdSwitchPlatform.prototype.getPowerState = function (thisSwitch, callback) {
var self = this;
if (thisSwitch.polling) {
// Get state directly from cache if polling is enabled
this.log(thisSwitch.name + " is " + (thisSwitch.state ? "on." : "off."));
callback(null, thisSwitch.state);
} else {
// Check state if polling is disabled
this.getState(thisSwitch, function (error, state) {
// Update state if command exists
if (thisSwitch.state_cmd) thisSwitch.state = state;
if (!error) self.log(thisSwitch.name + " is " + (thisSwitch.state ? "on." : "off."));
callback(error, thisSwitch.state);
});
}
}
// Method to set state
cmdSwitchPlatform.prototype.setPowerState = function (thisSwitch, state, callback) {
var self = this;
var cmd = state ? thisSwitch.on_cmd : thisSwitch.off_cmd;
var notCmd = state ? thisSwitch.off_cmd : thisSwitch.on_cmd;
var tout = null;
// Execute command to set state
exec(cmd, function (error, stdout, stderr) {
// Error detection
if (error && (state !== thisSwitch.state)) {
self.log("Failed to turn " + (state ? "on " : "off ") + thisSwitch.name);
self.log(stderr);
} else {
if (cmd) self.log(thisSwitch.name + " is turned " + (state ? "on." : "off."));
thisSwitch.state = state;
error = null;
}
// Restore switch after 1s if only one command exists
if (!notCmd && !thisSwitch.state_cmd) {
setTimeout(function () {
self.accessories[thisSwitch.name].getService(Service.Switch)
.setCharacteristic(Characteristic.On, !state);
}, 1000);
}
if (tout) {
clearTimeout(tout);
callback(error);
}
});
// Allow 1s to set state but otherwise assumes success
//tout = setTimeout(function () {
//tout = null;
self.log("Turning " + (state ? "on " : "off ") + thisSwitch.name + " took too long, assuming success." );
callback();
//}, thisSwitch.timeout * 1000);
}
// Method to handle identify request
cmdSwitchPlatform.prototype.identify = function (thisSwitch, paired, callback) {
this.log(thisSwitch.name + " identify requested!");
callback();
}
// Method to handle plugin configuration in HomeKit app
cmdSwitchPlatform.prototype.configurationRequestHandler = function (context, request, callback) {
if (request && request.type === "Terminate") {
return;
}
// Instruction
if (!context.step) {
var instructionResp = {
"type": "Interface",
"interface": "instruction",
"title": "Before You Start...",
"detail": "Please make sure homebridge is running with elevated privileges.",
"showNextButton": true
}
context.step = 1;
callback(instructionResp);
} else {
switch (context.step) {
case 1:
// Operation choices
var respDict = {
"type": "Interface",
"interface": "list",
"title": "What do you want to do?",
"items": [
"Add New Switch",
"Modify Existing Switch",
"Remove Existing Switch"
]
}
context.step = 2;
callback(respDict);
break;
case 2:
var selection = request.response.selections[0];
if (selection === 0) {
// Info for new accessory
var respDict = {
"type": "Interface",
"interface": "input",
"title": "New Switch",
"items": [{
"id": "name",
"title": "Name (Required)",
"placeholder": "HTPC"
}]
};
context.operation = 0;
context.step = 3;
callback(respDict);
} else {
var names = Object.keys(this.accessories);
if (names.length > 0) {
// Select existing accessory for modification or removal
if (selection === 1) {
var title = "Witch switch do you want to modify?";
context.operation = 1;
context.step = 3;
} else {
var title = "Witch switch do you want to remove?";
context.step = 5;
}
var respDict = {
"type": "Interface",
"interface": "list",
"title": title,
"items": names
};
context.list = names;
} else {
// Error if not switch is configured
var respDict = {
"type": "Interface",
"interface": "instruction",
"title": "Unavailable",
"detail": "No switch is configured.",
"showNextButton": true
};
context.step = 1;
}
callback(respDict);
}
break;
case 3:
if (context.operation === 0) {
var data = request.response.inputs;
} else if (context.operation === 1) {
var selection = context.list[request.response.selections[0]];
var data = this.accessories[selection].context;
}
if (data.name) {
// Add/Modify info of selected accessory
var respDict = {
"type": "Interface",
"interface": "input",
"title": data.name,
"items": [{
"id": "on_cmd",
"title": "CMD to Turn On",
"placeholder": context.operation ? "Leave blank if unchanged" : "wakeonlan XX:XX:XX:XX:XX:XX"
}, {
"id": "off_cmd",
"title": "CMD to Turn Off",
"placeholder": context.operation ? "Leave blank if unchanged" : "net rpc shutdown -I XXX.XXX.XXX.XXX -U user%password"
}, {
"id": "state_cmd",
"title": "CMD to Check ON State",
"placeholder": context.operation ? "Leave blank if unchanged" : "ping -c 2 -W 1 XXX.XXX.XXX.XXX | grep -i '2 received'"
}, {
"id": "polling",
"title": "Enable Polling (true/false)",
"placeholder": context.operation ? "Leave blank if unchanged" : "false"
}, {
"id": "interval",
"title": "Polling Interval (s)",
"placeholder": context.operation ? "Leave blank if unchanged" : "1"
},
{
"id": "timeout",
"title": "On/Off command execution timeout (s)",
"placeholder": context.operation ? "Leave blank if unchanged" : "1"
}, {
"id": "manufacturer",
"title": "Manufacturer",
"placeholder": context.operation ? "Leave blank if unchanged" : "Default-Manufacturer"
}, {
"id": "model",
"title": "Model",
"placeholder": context.operation ? "Leave blank if unchanged" : "Default-Model"
}, {
"id": "serial",
"title": "Serial",
"placeholder": context.operation ? "Leave blank if unchanged" : "Default-SerialNumber"
}]
};
context.name = data.name;
context.step = 4;
} else {
// Error if required info is missing
var respDict = {
"type": "Interface",
"interface": "instruction",
"title": "Error",
"detail": "Name of the switch is missing.",
"showNextButton": true
};
context.step = 1;
}
delete context.list;
delete context.operation;
callback(respDict);
break;
case 4:
var userInputs = request.response.inputs;
var newSwitch = {};
// Clone context if switch exists
if (this.accessories[context.name]) {
newSwitch = JSON.parse(JSON.stringify(this.accessories[context.name].context));
}
// Setup input for addAccessory
newSwitch.name = context.name;
newSwitch.on_cmd = userInputs.on_cmd || newSwitch.on_cmd;
newSwitch.off_cmd = userInputs.off_cmd || newSwitch.off_cmd;
newSwitch.state_cmd = userInputs.state_cmd || newSwitch.state_cmd;
if (userInputs.polling.toUpperCase() === "TRUE") {
newSwitch.polling = true;
} else if (userInputs.polling.toUpperCase() === "FALSE") {
newSwitch.polling = false;
}
newSwitch.interval = userInputs.interval || newSwitch.interval;
newSwitch.timeout = userInputs.timeout || newSwitch.timeout;
newSwitch.manufacturer = userInputs.manufacturer;
newSwitch.model = userInputs.model;
newSwitch.serial = userInputs.serial;
// Register or update accessory in HomeKit
this.addAccessory(newSwitch);
var respDict = {
"type": "Interface",
"interface": "instruction",
"title": "Success",
"detail": "The new switch is now updated.",
"showNextButton": true
};
context.step = 6;
callback(respDict);
break;
case 5:
// Remove selected accessory from HomeKit
var selection = context.list[request.response.selections[0]];
var accessory = this.accessories[selection];
this.removeAccessory(accessory);
var respDict = {
"type": "Interface",
"interface": "instruction",
"title": "Success",
"detail": "The switch is now removed.",
"showNextButton": true
};
delete context.list;
context.step = 6;
callback(respDict);
break;
case 6:
// Update config.json accordingly
var self = this;
delete context.step;
var newConfig = this.config;
// Create config for each switch
var newSwitches = Object.keys(this.accessories).map(function (k) {
var accessory = self.accessories[k];
var data = {
'name': accessory.context.name,
'on_cmd': accessory.context.on_cmd,
'off_cmd': accessory.context.off_cmd,
'state_cmd': accessory.context.state_cmd,
'polling': accessory.context.polling,
'interval': accessory.context.interval,
'timeout': accessory.context.timeout,
'manufacturer': accessory.context.manufacturer,
'model': accessory.context.model,
'serial': accessory.context.serial
};
return data;
});
newConfig.switches = newSwitches;
callback(null, "platform", true, newConfig);
break;
}
}
}