-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
75 lines (59 loc) · 1.79 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
var Service, Characteristic;
var exec = require("child_process").exec;
module.exports = function(homebridge){
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-cmd", "CMD", CmdAccessory);
}
function CmdAccessory(log, config) {
this.log = log;
// url info
this.on_cmd = config["on_cmd"];
this.off_cmd = config["off_cmd"];
this.name = config["name"];
}
CmdAccessory.prototype = {
cmdRequest: function(cmd, callback) {
exec(cmd,function(error, stdout, stderr) {
callback(error, stdout, stderr)
})
},
setPowerState: function(powerOn, callback) {
var cmd;
if (powerOn) {
cmd = this.on_cmd;
this.log("Setting power state to on");
} else {
cmd = this.off_cmd;
this.log("Setting power state to off");
}
this.cmdRequest(cmd, function(error, stdout, stderr) {
if (error) {
this.log('power function failed: %s', stderr);
callback(error);
} else {
this.log('power function succeeded!');
callback();
this.log(stdout);
}
}.bind(this));
},
identify: function(callback) {
this.log("Identify requested!");
callback(); // success
},
getServices: function() {
// you can OPTIONALLY create an information service if you wish to override
// the default values for things like serial number, model, etc.
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "cmd Manufacturer")
.setCharacteristic(Characteristic.Model, "cmd Model")
.setCharacteristic(Characteristic.SerialNumber, "cmd Serial Number");
var switchService = new Service.Switch(this.name);
switchService
.getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this));
return [switchService];
}
};