-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
112 lines (93 loc) · 3.55 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
const { execSync } = require('child_process');
module.exports = function (api) {
api.registerAccessory("Command Accessory", CommandAccessoryPlugin);
}
function durationSeconds(timeExpr) {
if (!isNaN(timeExpr)) {
return parseInt(timeExpr, 10);
}
var units = { 'd': 86400, 'h': 3600, 'm': 60, 's': 1 };
var regex = /(\d+)([dhms])/g;
let seconds = 0;
var match;
while ((match = regex.exec(timeExpr))) {
seconds += parseInt(match[1], 10) * units[match[2]];
}
return seconds;
}
class CommandAccessoryPlugin {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.currentState = false;
// your accessory must have an AccessoryInformation service
this.informationService = new this.api.hap.Service.AccessoryInformation()
.setCharacteristic(this.api.hap.Characteristic.Manufacturer, "adyanth")
.setCharacteristic(this.api.hap.Characteristic.SerialNumber, "#007")
.setCharacteristic(this.api.hap.Characteristic.Model, config["name"]);
// create a new "Switch" service
this.switchService = new this.api.hap.Service.Switch(this.name);
// link methods used when getting or setting the state of the service
this.switchService.getCharacteristic(this.api.hap.Characteristic.On)
.onGet(this.getState.bind(this)) // bind to getStateHandler method below
.onSet(this.setState.bind(this)); // bind to setStateHandler method below
if (this.config.check_status && this.config.poll_check) {
let secs = durationSeconds(this.config.poll_check);
if (isNaN(secs) || secs < 1) {
this.log.error("Too frequent or incorrect poll check time, polling disabled.");
return;
}
this.log.info(`Setting poll interval to ${secs}s`);
this.interval = setInterval(async () => {
this.log.debug("Polling status");
let oldState = this.currentState;
if (await this.getState(secs * 1000) != oldState) {
this.log.debug("Updating state")
this.switchService.getCharacteristic(this.api.hap.Characteristic.On)
.updateValue(this.currentState);
}
this.log.debug(`Polling done`);
}, secs * 1000);
}
this.log.info(`Command Accessory Plugin Loaded`);
}
getServices() {
return [
this.informationService,
this.switchService,
];
}
async getState(timeout = undefined) {
this.log.debug(`Getting switch state`);
if (!this.config.check_status) {
this.log.debug(`No check_status, returning static state: ${this.currentState}`)
return this.currentState;
}
this.log.debug(`Running: ${this.config.check_status}`);
try {
execSync(this.config.check_status, { timeout: timeout });
this.currentState = !this.config.invert_status;
} catch (error) {
this.currentState = this.config.invert_status;
}
this.log.debug(`Returning: ${this.currentState}`);
return this.currentState;
}
async setState(value) {
this.log.debug(`Setting switch state to: `, value);
let cmd = value ? this.config.turn_on : this.config.turn_off;
let exitCode = 1;
this.log.debug(`Running: ${cmd}`);
try {
execSync(cmd);
exitCode = 0;
} catch (error) {
exitCode = 1;
}
// Set state depending on whether the command exited successfully or not.
this.currentState = value ^ (exitCode != 0);
this.log.debug(`Returning: ${this.currentState}}`);
return this.currentState;
}
}