This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
forked from frli4797/homebridge-sector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
290 lines (251 loc) · 9.02 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
var Service, Characteristic;
var pollingtoevent = require("polling-to-event");
const waitUntil = require("wait-until");
const sectoralarm = require("sectoralarm");
module.exports = function(homebridge){
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-sector-securitysystem", "Sector-SecuritySystem", SectorSecuritySystemAccessory);
};
var currentState;
/**
* The main class acting as the Security System Accessory
*
* @param log The logger to use
* @param config The config received from HomeBridge
* @constructor
*/
function SectorSecuritySystemAccessory(log, config) {
var self = this;
self.log = log;
self.name = config.name;
self.email = config.email;
self.password = config.password;
self.siteId = config.siteId;
self.code = config.code;
// the service
self.securityService = null;
// polling settings
self.polling = true;
self.pollInterval = config.pollInterval || 3000;
// cached values
self.previousCurrentState = null;
self.previousTargetState = null;
self.log("About to initialize.");
self.init();
}
function translateFromState(log, aState) {
log.debug("translateFromState() State is " + aState);
var translatedSate = "UNKNOWN";
switch (aState) {
case Characteristic.SecuritySystemTargetState.STAY_ARM:
translatedSate = "partial";
break;
case Characteristic.SecuritySystemTargetState.NIGHT_ARM:
translatedSate = "partial";
break;
case Characteristic.SecuritySystemTargetState.AWAY_ARM:
translatedSate = "armed";
break;
case Characteristic.SecuritySystemTargetState.DISARM:
translatedSate = "disarmed";
break;
case 4:
translatedSate = "ALARM";
break;
}
return translatedSate;
}
function translateToState(log, aState) {
log.debug("translateToState() State is " + aState);
// 0 - Characteristic.SecuritySystemTargetState.STAY_ARM:
// 1 - Characteristic.SecuritySystemTargetState.AWAY_ARM:
// 2- Characteristic.SecuritySystemTargetState.NIGHT_ARM:
// 3 - Characteristic.SecuritySystemTargetState.DISARM:
var translatedSate = "UNKNOWN";
switch (String(aState)) {
case "partialArmed":
translatedSate = Characteristic.SecuritySystemTargetState.NIGHT_ARM;
break;
case "annex":
translatedSate = Characteristic.SecuritySystemTargetState.STAY_ARM;
break;
case "armed":
translatedSate = Characteristic.SecuritySystemTargetState.AWAY_ARM;
break;
case "disarmed":
translatedSate = Characteristic.SecuritySystemTargetState.DISARM;
break;
case 4:
translatedSate = "ALARM";
break;
}
log.debug("translateToState() Translated state is " + translatedSate);
return translatedSate;
}
/**
* Initializer method, fired after the config has been applied
*/
SectorSecuritySystemAccessory.prototype.init = function() {
var self = this;
self.log("Initilizing...");
// set up polling if requested
if (self.polling) {
self.log("Starting polling with an interval of %s ms", self.pollInterval);
var emitter = pollingtoevent(function (done) {
self.getState(function (err, result) {
done(err, result);
});
}, {
longpolling: true,
interval: self.pollInterval
});
emitter.on("longpoll", function (state) {
self.log.debug("In poll function");
if (state) {
// Get OnceMore time Current State:
self.log.debug("New state detected: (" + state + ") -> " + translateFromState(self.log, state) + ". Notify!");
self.securityService.setCharacteristic(Characteristic.SecuritySystemCurrentState, state);
currentState = state;
}
});
emitter.on("err", function (err) {
self.log("Polling failed, error was %s", err);
});
}
self.log.debug("Exiting init...");
};
/**
* Gets the state of the security system from a given URL
*
* @param {string} url The URL to poke for the result
* @param {string} body The body of the request
* @param {Function} callback The method to call with the results
*/
SectorSecuritySystemAccessory.prototype.getState = function(callback) {
var self = this;
self.log.debug("getState() Getting state");
sectoralarm.connect(self.email, self.password, self.siteId)
.then(site => {
return site.status();
})
.then(status => {
status = JSON.parse(status);
var alarmstate;
alarmstate = status.armedStatus;
self.log.debug("getState() Armed status: " + alarmstate);
callback(null, translateToState(self.log, alarmstate));
})
.catch(error => {
self.log(error.message);
self.log(error.code);
callback(error);
});
};
SectorSecuritySystemAccessory.prototype.getCurrentState = function(callback) {
var self = this;
self.log("getCurrentState() Getting current state");
if (self.polling) {
self.log("getCurrentState() Returning current state " + currentState);
callback(null, currentState);
} else {
self.log("getCurrentState() Getting current state - delayed...");
waitUntil()
.interval(500)
.times(15)
.condition(function () {
self.log("getCurrentState() In condition " + currentState);
return (currentState ? true : false);
})
.done(function (result) {
// do stuff
self.log("getCurrentState() Update current state to:", currentState);
callback(null, currentState);
});
}
};
/**
* Gets the state of the security system from a given URL
*
* @param {string} url The URL to poke for the result
* @param {string} body The body of the request
* @param {Function} callback The method to call with the results
*/
SectorSecuritySystemAccessory.prototype.setTargetState = function(state, callback) {
var self = this;
self.log.debug("Setting target state to " + state + ". Current state is " + currentState);
sectoralarm.connect(self.email, self.password, self.siteId)
.then((site) => {
switch (state) {
case Characteristic.SecuritySystemTargetState.STAY_ARM:
site.partialArm(self.code);
break;
case Characteristic.SecuritySystemTargetState.NIGHT_ARM:
site.partialArm(self.code);
break;
case Characteristic.SecuritySystemTargetState.AWAY_ARM:
site.arm(self.code);
break;
case Characteristic.SecuritySystemTargetState.DISARM:
site.disarm(self.code);
break;
}
})
.then(output => {
self.log.debug("setTargetState() Raw output: " + output);
currentState = state;
self.securityService.setCharacteristic(Characteristic.SecuritySystemCurrentState, currentState);
callback(null, state);
})
.catch(error => {
self.log(error.message);
self.log(error.code);
callback(error);
});
};
SectorSecuritySystemAccessory.prototype.getTargetState = function(callback) {
var self = this;
self.log.info("getTargetState() Getting target state.");
if (self.polling) {
self.log("getTargetState() Getting target state using polling. Current state is " + currentState);
if(currentState) {
callback(null, currentState);
} else {
self.getState(callback);
}
} else {
self.log("getTargetState() Getting target state using getState() ...");
self.getState(callback);
}
};
/**
* Identifies the security device (?)
*
* @param {Function} callback The method to call with the results
*/
SectorSecuritySystemAccessory.prototype.identify = function(callback) {
var self = this;
self.log("Identify requested!");
callback();
};
/**
* Returns the services offered by this security device
*
* @returns {Array} The services offered
*/
SectorSecuritySystemAccessory.prototype.getServices = function() {
this.securityService = new Service.SecuritySystem(this.name);
this.securityService
.getCharacteristic(Characteristic.SecuritySystemCurrentState)
.on("get", this.getCurrentState.bind(this));
this.securityService
.getCharacteristic(Characteristic.SecuritySystemTargetState)
.on("get", this.getTargetState.bind(this))
.on("set", this.setTargetState.bind(this));
this.infoService = new Service.AccessoryInformation();
this.infoService
.setCharacteristic(Characteristic.Manufacturer, "Fredrik JL")
.setCharacteristic(Characteristic.Model, this.name)
.setCharacteristic(Characteristic.SerialNumber, "1234");
return [ this.securityService ];
};