-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathindex.js
More file actions
156 lines (133 loc) · 5.04 KB
/
index.js
File metadata and controls
156 lines (133 loc) · 5.04 KB
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
let Service;
let Characteristic;
const exec = require("child_process").exec;
const fileExists = require("file-exists");
const chokidar = require("chokidar");
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory(
"homebridge-script2",
"Script2",
script2Accessory
);
};
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;
this.uniqueSerial = config["unique_serial"] || "script2 Serial Number";
this.onValue = this.onValue.trim().toLowerCase();
try {
this.currentState = this.fileState
? fileExists.sync(this.fileState)
: false;
} catch (err) {
this.log.error(`Error checking initial file state: ${err.message}`);
this.currentState = false;
}
this.setStateHandler = function (powerOn, callback) {
function setStateHandlerExecCallback(error, stdout, stderr) {
if (error || stderr) {
const errMessage = stderr
? `${stderr} (${error?.message ?? 'unknown error'})`
: `${error?.message ?? 'unknown error'}`;
this.log.error(`Set State returned an error: ${errMessage}`);
callback(new Error(errMessage), null);
return;
}
const commandOutput = stdout.trim().toLowerCase();
this.log.debug(`Set State Command returned ${commandOutput}`);
this.currentState = powerOn;
this.log.info(`Set ${this.name} to ${powerOn ? "ON" : "OFF"}`);
callback(null, powerOn);
}
const command = powerOn ? this.onCommand : this.offCommand;
this.log.debug(`Executing command: ${command}`);
exec(command, setStateHandlerExecCallback.bind(this));
};
this.getStateHandler = function (callback) {
function getStateHandlerExecCallback(error, stdout, stderr) {
if (error || stderr) {
const errMessage = stderr
? `${stderr} (${error.message})`
: error.message;
this.log.error(`Get State returned an error: ${errMessage}`);
callback(new Error(errMessage), null);
return;
}
const cleanCommandOutput = stdout.trim().toLowerCase();
this.log.debug(`Get State Command returned ${cleanCommandOutput}`);
const poweredOn = cleanCommandOutput == this.onValue;
this.log.info(`State of ${this.name} is: ${poweredOn ? "ON" : "OFF"}`);
callback(null, poweredOn);
}
const command = this.stateCommand;
this.log.debug(`Executing command: ${command}`);
exec(command, getStateHandlerExecCallback.bind(this));
};
this.getFileStateHandler = function (callback) {
try {
const poweredOn = fileExists.sync(this.fileState);
this.log.info(`State of ${this.name} is: ${poweredOn ? "ON" : "OFF"}`);
callback(null, poweredOn);
} catch (err) {
this.log.error(`Error checking file state: ${err.message}`);
callback(err, null);
}
};
}
script2Accessory.prototype.setState = function (powerOn, callback) {
this.log.info(`Setting ${this.name} to ${powerOn ? "ON" : "OFF"}...`);
this.setStateHandler(powerOn, callback);
};
script2Accessory.prototype.getState = function (callback) {
this.log.info(`Getting ${this.name} state...`);
if (this.fileState) {
this.getFileStateHandler(callback);
} else if (this.stateCommand) {
this.getStateHandler(callback);
} else {
this.log.error("Must set config value for fileState or state.");
}
};
script2Accessory.prototype.getServices = function () {
const informationService = new Service.AccessoryInformation();
const switchService = new Service.Switch(this.name);
const theSerial = this.uniqueSerial.toString();
informationService
.setCharacteristic(Characteristic.Manufacturer, "script2 Manufacturer")
.setCharacteristic(Characteristic.Model, "script2 Model")
.setCharacteristic(Characteristic.SerialNumber, theSerial);
const 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) {
const fileCreatedHandler = function (path, stats) {
if (!this.currentState) {
this.log.info(`File "${path}" was created`);
this.currentState = true;
switchService.setCharacteristic(Characteristic.On, true);
}
}.bind(this);
const fileRemovedHandler = function (path, stats) {
if (this.currentState) {
this.log.info(`File "${path}" was deleted`);
this.currentState = false;
switchService.setCharacteristic(Characteristic.On, false);
}
}.bind(this);
const watcher = chokidar.watch(this.fileState, { alwaysStat: true });
watcher.on("add", fileCreatedHandler);
watcher.on("unlink", fileRemovedHandler);
}
return [informationService, switchService];
};