-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaccessory.js
380 lines (276 loc) · 10.9 KB
/
accessory.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
const persistentState = require('./helpers/persistentState');
const mqtt = require('mqtt');
const addSaveProxy = (name, target, saveFunc) => {
const handler = {
set (target, key, value) {
target[key] = value;
saveFunc(target);
return true
}
}
return new Proxy(target, handler);
}
class HomebridgeAccessory {
constructor (log, config = {}, serviceManagerType = 'ServiceManager') {
this.serviceManagerType = serviceManagerType;
let { disableLogs, host, name, data, persistState, resendDataAfterReload, resendDataAfterReloadDelay } = config;
this.log = (!disableLogs && log) ? log : () => {};
this.config = config;
this.host = host;
this.name = name;
this.data = data;
this.state = {}
this.checkConfig(config)
this.setupServiceManager()
this.loadState()
this.setDefaults();
this.subscribeToMQTT();
}
setDefaults () { }
restoreStateOrder () { }
correctReloadedState () { }
checkConfig (config) {
const { name, log } = this;
if (typeof config !== 'object') return;
Object.keys(config).forEach((key) => {
const value = config[key];
if (value === 'true' || value === 'false') {
log(`\x1b[31m[CONFIG ERROR]\x1b[0m ${name}Boolean values should look like this: \x1b[32m"${key}": ${value}\x1b[0m not this \x1b[31m"${key}": "${value}"\x1b[0m`);
process.exit(0);
} else if (Array.isArray(value)) {
value.forEach((item) => {
this.checkConfig(item);
})
} else if (typeof value === 'object') {
this.checkConfig(value);
} else if (value === '0' || (typeof value === 'string' && parseInt(value) !== 0 && !isNaN(parseInt(value)))) {
if (typeof value === 'string' && value.split('.').length - 1 > 1) return;
if (typeof value === 'string' && !value.match(/^\d\.{0,1}\d*$/)) return;
log(`\x1b[31m[CONFIG ERROR]\x1b[0m ${name}Numeric values should look like this: \x1b[32m"${key}": ${value}\x1b[0m not this \x1b[31m"${key}": "${value}"\x1b[0m`);
process.exit(0);
}
})
}
identify (callback) {
const { name } = this
this.log(`Identify requested for ${name}`);
callback();
}
performSetValueAction ({ host, data, log, name }) {
throw new Error('The "performSetValueAction" method must be overridden.');
}
async setCharacteristicValue (props, value, callback) {
const { config, host, log, name, debug } = this;
try {
const { delay, resendDataAfterReload, allowResend } = config;
const { service, propertyName, onData, offData, setValuePromise, ignorePreviousValue } = props;
const capitalizedPropertyName = propertyName.charAt(0).toUpperCase() + propertyName.slice(1);
if (delay) {
log(`${name} set${capitalizedPropertyName}: ${value} (delaying by ${delay}s)`);
await delayForDuration(delay);
}
log(`${name} set${capitalizedPropertyName}: ${value}`);
if (this.isReloadingState && !resendDataAfterReload) {
this.state[propertyName] = value;
log(`${name} set${capitalizedPropertyName}: already ${value} (no data sent - A)`);
callback(null, value);
return;
}
if (!ignorePreviousValue && this.state[propertyName] == value && !this.isReloadingState) {
if (!allowResend) {
log(`${name} set${capitalizedPropertyName}: already ${value} (no data sent - B)`);
callback(null, value);
return;
}
}
let previousValue = this.state[propertyName];
if (this.isReloadingState && resendDataAfterReload) {
previousValue = undefined
}
this.state[propertyName] = value;
// Set toggle data if this is a toggle
const data = value ? onData : offData;
if (setValuePromise) {
setValuePromise(data, previousValue);
} else if (data) {
this.performSetValueAction({ host, data, log, name });
}
callback(null, this.state[propertyName]);
} catch (err) {
log('setCharacteristicValue error:', err.message)
if (debug) log(`\x1b[33m[DEBUG]\x1b[0m ${name} setCharacteristicValue error`, err)
callback(err)
}
}
async getCharacteristicValue (props, callback) {
const { propertyName } = props;
const { log, name } = this;
const capitalizedPropertyName = propertyName.charAt(0).toUpperCase() + propertyName.slice(1);
let value = this.state[propertyName];
log(`${name} get${capitalizedPropertyName}: ${value}`);
callback(null, value);
}
loadState () {
const { config, log, name, serviceManager } = this;
let { host, resendDataAfterReload, resendDataAfterReloadDelay, persistState } = config;
// Set defaults
if (persistState === undefined) persistState = true;
if (!resendDataAfterReloadDelay) resendDataAfterReloadDelay = 2
if (!persistState) return;
// Load state from file
const restoreStateOrder = this.restoreStateOrder();
const state = persistentState.load({ host, name }) || {};
// Allow each accessory to correct the state if necessary
this.correctReloadedState(state);
// Proxy so that whenever this.state is changed, it will persist to disk
this.state = addSaveProxy(name, state, (state) => {
persistentState.save({ host, name, state });
});
// Refresh the UI and resend data based on existing state
Object.keys(serviceManager.characteristics).forEach((name) => {
if (this.state[name] === undefined) return;
const characteristcType = serviceManager.characteristics[name];
// Refresh the UI for any state that's been set once the init has completed
// Use timeout as we want to make sure this doesn't happen until after all child contructor code has run
setTimeout(() => {
if (persistState) serviceManager.refreshCharacteristicUI(characteristcType);
}, 200);
// Re-set the value in order to resend
if (resendDataAfterReload) {
// Delay to allow Broadlink to be discovered
setTimeout(() => {
const value = this.state[name];
serviceManager.setCharacteristic(characteristcType, value);
}, (resendDataAfterReloadDelay * 1000));
}
})
if (resendDataAfterReload) {
this.isReloadingState = true;
setTimeout(() => {
this.isReloadingState = false;
log(`${name} Accessory Ready`);
}, (resendDataAfterReloadDelay * 1000) + 300);
} else {
log(`${name} Accessory Ready`);
}
}
getInformationServices () {
const informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer || 'Homebridge Easy Platform')
.setCharacteristic(Characteristic.Model, this.model || 'Unknown')
.setCharacteristic(Characteristic.SerialNumber, this.serialNumber || 'Unknown');
return [ informationService ];
}
getServices () {
const services = this.getInformationServices();
services.push(this.serviceManager.service);
return services;
}
// MQTT Support
subscribeToMQTT () {
const { config, log, name } = this;
let { mqttTopic, mqttURL, mqttUsername, mqttPassword } = config;
if (!mqttTopic || !mqttURL) return;
this.mqttValues = {};
this.mqttValuesTemp = {};
// Perform some validation of the mqttTopic option in the config.
if (typeof mqttTopic !== 'string' && !Array.isArray(mqttTopic)) {
log(`\x1b[31m[CONFIG ERROR]\x1b[0m ${name} \x1b[33mmqttTopic\x1b[0m value is incorrect. Please check out the documentation for more details.`)
return;
}
if (Array.isArray(mqttTopic)) {
const erroneousTopics = mqttTopic.filter((mqttTopicObj) => {
if (typeof mqttTopic !== 'obj') return true;
const { identifier, topic } = mqttTopicObj;
if (!identifier || !topic) return true;
if (typeof identifier !== 'string') return true;
if (typeof topic !== 'string') return true;
});
if (erroneousTopics.length > 0) {
log(`\x1b[31m[CONFIG ERROR]\x1b[0m ${name} \x1b[33mmqttTopic\x1b[0m value is incorrect. Please check out the documentation for more details.`)
return;
}
}
// mqqtTopic may be an array or an array of objects. Add to a new array if string.
if (typeof mqttTopic === 'string') {
const mqttTopicObj = {
identifier: 'unknown',
topic: mqttTopic
}
mqttTopic = [ mqttTopicObj ]
}
// Create an easily referenced instance variable
const mqttTopicIdentifiersByTopic = {};
mqttTopic.forEach(({ identifier, topic }) => {
mqttTopicIdentifiersByTopic[topic] = identifier;
})
// Connect to mqtt
const mqttClientID = 'mqttjs_' + Math.random().toString(16).substr(2, 8);
const options = {
keepalive: 10,
clientId: this.client_Id,
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
reconnectPeriod: 1000,
connectTimeout: 30 * 1000,
serialnumber: mqttClientID,
username: mqttUsername,
password: mqttPassword,
will: {
topic: 'WillMsg',
payload: 'Connection Closed abnormally..!',
qos: 0,
retain: false
},
rejectUnauthorized: false
};
const mqttClient = mqtt.connect(mqttURL, options);
this.mqttClient = mqttClient;
// Subscribe to topics
this.isMQTTConnecting = true;
// Timeout isMQTTConnecting - it's used to prevent error messages about not being connected.
setTimeout(() => {
this.isMQTTConnecting = false;
}, 2000)
mqttClient.on('connect', () => {
this.isMQTTConnecting = false;
log(`\x1b[35m[INFO]\x1b[0m ${name} MQTT client connected.`)
mqttTopic.forEach(({ topic }) => {
mqttClient.subscribe(topic)
})
})
mqttClient.on('error', () => {
this.isMQTTConnecting = false;
})
mqttClient.on('message', (topic, message) => {
const identifier = mqttTopicIdentifiersByTopic[topic];
this.onMQTTMessage(identifier, message);
})
}
onMQTTMessage (identifier, message) {
this.mqttValuesTemp[identifier] = message.toString();
}
mqttValueForIdentifier (identifier) {
const { log, name } = this;
let value = this.mqttValues[identifier];
// No identifier may have been set in the user's config so let's try "unknown" too
if (value === undefined) value = this.mqttValues['unknown'];
if (!this.mqttClient.connected) {
if (!this.isMQTTConnecting) log(`\x1b[31m[ERROR]\x1b[0m ${name} MQTT client is not connected. Value could not be found for topic with identifier "${identifier}".`);
return;
}
if (value === undefined) {
log(`\x1b[31m[ERROR]\x1b[0m ${name} No MQTT value could be found for topic with identifier "${identifier}".`);
return;
}
return value;
}
}
module.exports = HomebridgeAccessory;
const delayForDuration = (duration) => {
return new Promise((resolve) => {
setTimeout(resolve, duration * 1000)
})
}