-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
117 lines (100 loc) · 3.49 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
var Service;
var Characteristic;
var exec = require('child_process').exec;
var assign = require('object-assign');
var fileExists = require('file-exists');
var chokidar = require('chokidar');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-script2', 'Script2', script2Accessory);
}
function puts(error, stdout, stderr) {
console.log(stdout)
}
function script2Accessory(log, config) {
this.log = log;
this.service = 'Switch';
this.name = config['name'];
this.onCommand = config['on'];
this.offCommand = config['off'];
this.stateCommand = config['state'] || false;
this.onValue = config['on_value'] || "true";
this.fileState = config['fileState'] || false;
if (!this.fileState) {
this.onValue = this.onValue.trim().toLowerCase();
}
this.uniqueSerial = config['unique_serial'] || "script2 Serial Number";
//this.exactMatch = config['exact_match'] || true;
}
/*
script2Accessory.prototype.matchesString = function(match) {
if(this.exactMatch) {
return (match === this.onValue);
}
else {
return (match.indexOf(this.onValue) > -1);
}
}
*/
script2Accessory.prototype.setState = function(powerOn, callback) {
var accessory = this;
var state = powerOn ? 'on' : 'off';
var prop = state + 'Command';
var command = accessory[prop];
exec(command, puts);
accessory.log('Set ' + accessory.name + ' to ' + state);
accessory.currentState = powerOn;
callback(null);
}
script2Accessory.prototype.getState = function(callback) {
var accessory = this;
if (this.fileState) {
var flagFile = fileExists.sync(this.fileState);
accessory.log('State of ' + accessory.name + ' is: ' + flagFile);
callback(null, flagFile);
}
else if (this.stateCommand) {
exec(this.stateCommand, function (error, stdout, stderr) {
if (stderr) { return; }
var cleanOut=stdout.trim().toLowerCase();
accessory.log('State of ' + accessory.name + ' is: ' + cleanOut);
callback(null, cleanOut == accessory.onValue);
});
}
else {
accessory.log('Must set config value for fileState or state.');
}
}
script2Accessory.prototype.getServices = function() {
var informationService = new Service.AccessoryInformation();
var switchService = new Service.Switch(this.name);
var theSerial = this.uniqueSerial.toString();
informationService
.setCharacteristic(Characteristic.Manufacturer, 'script2 Manufacturer')
.setCharacteristic(Characteristic.Model, 'script2 Model')
.setCharacteristic(Characteristic.SerialNumber, theSerial);
var characteristic = switchService.getCharacteristic(Characteristic.On)
.on('set', this.setState.bind(this));
if (this.stateCommand || this.fileState) {
characteristic.on('get', this.getState.bind(this))
};
if (this.fileState) {
var fileCreatedHandler = function(path, stats){
if (!this.currentState) {
this.log('File ' + path + ' was created');
switchService.setCharacteristic(Characteristic.On, true);
}
}.bind(this);
var fileRemovedHandler = function(path, stats){
if (this.currentState) {
this.log('File ' + path + ' was deleted');
switchService.setCharacteristic(Characteristic.On, false);
}
}.bind(this);
var watcher = chokidar.watch(this.fileState, {alwaysStat: true});
watcher.on('add', fileCreatedHandler);
watcher.on('unlink', fileRemovedHandler);
}
return [informationService, switchService];
}