forked from snowdd1/homebridge-knx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
361 lines (320 loc) · 12.3 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
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
/*
* Platform shim for use with nfarina's homebridge plugin system
* This is the version for plugin support
* ********************************************************************************************
*
ALL NEW VERSION WITH OWN PERSISTENCE LAYER (file based, anyhow)
ECMA-Script 2015 (6.0) Language required
*/
'use strict';
var AccConstructor = require('./lib/knxdevice.js');
var userOpts = require('./lib/user').User;
var Service, Characteristic; // passed default objects from hap-nodejs
var globs = {}; // the storage for cross module data pooling;
//var iterate = require('./lib/iterate');
var knxmonitor = require('./lib/knxmonitor');
var KNXAccess = require("./lib/knxaccess");
var http = require('http');
/**
* KNXPlatform
*
* @constructor
* @param {function} log - logging function for console etc. out
* @param {object} config - configuration object from global config.json
*/
function KNXPlatform(log, config, newAPI) {
var that = this;
this.log = log;
//this.Old_config = config;
// new API for creating accessory and such.
globs.newAPI = newAPI;
/**
* Talkative Info spitting thingy.
*
* @param {string} comment
*
*/
globs.info = function(comment) {
that.log.info(comment);
};
globs.debug = function(comment) {
that.log.debug(comment);
};
globs.errorlog = function(comment) {
that.log.error(comment);
};
/* our own config file */
globs.debug("Trying to load user settings");
userOpts.setStoragePath(newAPI.user.storagePath()); // get path from homebridge!
globs.debug(userOpts.configPath());
this.config = userOpts.loadConfig();
globs.config = this.config;
globs.restoredAccessories = []; //plugin-2
/* we should have now:
* - knxd_ip
* - knxd_port
* - GroupAddresses object
* - Devices Object
*/
globs.knxd_ip = this.config.knxd_ip;
globs.knxd_port = this.config.knxd_port || 6720;
globs.log = log;
globs.knxmonitor = knxmonitor;
/**
* To store all unique read requests
*
* @type {string[]}
*/
globs.readRequests = {};
KNXAccess.setGlobs(globs); // init link for module;
knxmonitor.startMonitor({
host : globs.knxd_ip,
port : globs.knxd_port
});
// plugin-2 system: wait for the homebridge to finish restoring the accessories from its own persistence layer.
if (newAPI) {
newAPI.on('didFinishLaunching', function() {
globs.info('homebridge event didFinishLaunching');
this.configure();
}.bind(this));
}
}
/**
* Registers the plugin with homebridge. Will be called by homebridge if found in directory structure and package.json
* is right This function needs to be exported.
*
* @param {homebridge/lib/api.js/API} homebridgeAPI - The API Object made available by homebridge. Contains the HAP type library e.g.
*
*/
function registry(homebridgeAPI) {
console.log("homebridge API version: " + homebridgeAPI.version);
Service = homebridgeAPI.hap.Service;
Characteristic = homebridgeAPI.hap.Characteristic;
globs.Service = Service;
globs.Characteristic = Characteristic;
globs.API = homebridgeAPI;
/* load our custom types
*
*/
require('./lib/customtypes/knxthermostat.js')(homebridgeAPI);
//debug
//iterate(globs.API.hap.Characteristic.KNXThermAtHome);
//iterate(globs.API.hap.Characteristic.On);
// third parameter dynamic = true
homebridgeAPI.registerPlatform("homebridge-knx", "KNX", KNXPlatform, true); //update signature for plugin-2
//homebridgeAPI.registerPlatform("homebridge-knx", "KNX", KNXPlatform, false); //update signature
}
module.exports = registry;
//Function invoked when homebridge tries to restore cached accessory
//Developer can configure accessory at here (like setup event handler)
//Update current value
/**
* configureAccessory() is invoked for each accessory homebridge restores from its persistence layer. The restored
* accessory has all the homekit properties, but none of the implementation at this point of time. This happens before
* the didFinishLaunching event.
*
* @param {platformAccessory} accessory
*/
KNXPlatform.prototype.configureAccessory = function(accessory) {
console.log("Plugin - Configure Accessory: " + accessory.displayName + " --> Added to restoredAccessories[]");
// set the accessory to reachable if plugin can currently process the accessory
// otherwise set to false and update the reachability later by invoking
// accessory.updateReachability()
accessory.updateReachability(false);
// collect the accessories
globs.restoredAccessories.push(accessory);
};
/**
* With plugin-2 system, accessories are re-created by the homebridge itself, but without all the event functions etc.
*
* We need to re-connect all our accessories to the right functions
*
* This is my event handler for the "didFinishLaunching" event of the newAPI
*/
KNXPlatform.prototype.configure = function() {
globs.info('Configuration starts');
userOpts.LogHomebridgeKNXSTarts();
// homebridge has now finished restoring the accessories from its persistence layer.
// Now we need to get their implementation back to them
globs.debug('We think homebridge has restored ' + globs.restoredAccessories.length + ' accessories.');
/* *************** read the config the first time
*
*/
if (!this.config.GroupAddresses) {
this.config.GroupAddresses = [];
}
// iterate through all devices the platform my offer
// for each device, create an accessory
// read accessories from file !!!!!
var foundAccessories = this.config.Devices || [];
//create array of accessories
/** @type {lib/knxdevice.js~knxDevice[]} */
globs.devices = [];
for (var int = 0; int < foundAccessories.length; int++) {
var currAcc = foundAccessories[int];
globs.info("Reading from config: Device/Accessory " + (int + 1) + " of " + foundAccessories.length);
globs.debug("Match device [" + currAcc.DeviceName + "]");
//match them to the restored accessories:
/** @type {homebridge/lib/platformAccessory.js/PlatformAccessory} */
var matchAcc = getAccessoryByUUID(globs.restoredAccessories, currAcc.UUID);
if (matchAcc) {
// we found one
globs.debug('Matched an accessory: ' + currAcc.DeviceName + ' === ' + matchAcc.displayName);
// Instantiate and pass the existing platformAccessory
matchAcc.active = true;
globs.devices.push(new AccConstructor(globs, foundAccessories[int], matchAcc));
} else {
// this one is new
globs.debug('New accessory found: ' + currAcc.DeviceName);
globs.devices.push(new AccConstructor(globs, foundAccessories[int]));
}
// do not construct here: var acc = new accConstructor(globs,foundAccessories[int]);
globs.info("Done with [" + currAcc.DeviceName + "] accessory");
}
// now the globs.devices contains an array of working accessories, that are not yet passed to homebridge
globs.info('We have read ' + globs.devices.length + ' devices from file.');
//now we need to store our updated config file to disk, or else all that is in vain next startup!
globs.info('Saving config file!');
userOpts.storeConfig();
// start the tiny web server for deleting orphaned devices
globs.debug('BEFORE http.createServer');
var that=this;
this.startUpDateAndTime = new Date();
this.startUpDateAndTimeString = this.startUpDateAndTime.toString();
this.requestServer = http.createServer(function(request, response) {
globs.debug('http.createServer CALLBACK FUNCTION URL=' + request.url);
var reqparsed = request.url.substr(1).split('?');
var params = {};
var paramstemp = [];
if (reqparsed[1]) {
paramstemp = reqparsed[1].split('&');
for (var i = 0; i < paramstemp.length; i++) {
var b = paramstemp[i].split('=');
params[decodeURIComponent(b[0])] = decodeURIComponent(b[1] || '');
}
}
/*
* Now we have: path in reqparsed[0] like "list" or "delete"
* param
*/
if (request.url === "/list") {
//response.writeHead(200);
response.write('<HEAD><meta http-equiv="content-type" content="text/html; charset=utf-8"><TITLE>Homebridge-KNX</TITLE></HEAD>');
response.write('<BODY>');
response.write('homebridge-knx started at ' + that.startUpDateAndTimeString);
response.write('<hr>');
response.write('Restored devices from homebridge cache:<BR><BR>');
var idev = 0, tdev = {};
for (idev = 0; idev < globs.restoredAccessories.length; idev++) {
tdev = globs.restoredAccessories[idev];
// debug spit-out:
//response.write('<BR><HR><BR>' + JSON.stringify(tdev) + '<BR><BR>');
globs.debug(tdev.UUID);
if (tdev.UUID !== 'ERASED') {
response.write('Device ' + tdev.displayName);
response.write(' <a href="/delete?UUID=' + tdev.UUID + '">[Delete from cache!]</a> ');
if (!tdev.active) {
response.write(' (orphaned) ');
}
response.write(' <BR>');
}
}
response.write('<HR><BR>Devices from homebridge-knx config:<BR><BR>');
for (idev = 0; idev < globs.devices.length; idev++) {
tdev = globs.devices[idev].getPlatformAccessory();
if (tdev.UUID !== 'ERASED') {
response.write('Device ' + tdev.displayName);
response.write(' <a href="/delete?UUID=' + tdev.UUID + '">[Delete from cache!]</a> ' + ' <BR>');
// debug spit-out:
//response.write(JSON.stringify(tdev) + '<BR><BR>');
}
}
if (that.config.AllowKillHomebridge===true) {
response.write(' <br><hr><br><a href="/kill">Kill homebridge</a> by throwing an Error. Use this to restart HomeBridge if you have it configured as a self-starting service ' + ' <BR>');
}
response.end('</BODY>');
} else if (reqparsed[0] === 'delete') {
// now delete the accessory from homebridge
globs.debug("delete accessory with UUID ");
if (params.UUID) {
try {
globs.debug(params.UUID);
var delAcc = getAccessoryByUUID(globs.restoredAccessories, params.UUID);
if (delAcc) {
globs.newAPI.unregisterPlatformAccessories(undefined, undefined, [ delAcc ]);
delAcc.UUID = "ERASED";
} else {
delAcc = getAccessoryByUUID(globs.devices, params.UUID);
if (delAcc) {
globs.newAPI.unregisterPlatformAccessories(undefined, undefined, [ delAcc ]);
delAcc.UUID = "ERASED";
}
}
globs.debug(params.UUID + ' deleted');
} catch (err) {
globs.errorlog('ERR Could not delete accessory with UUID ' + params.UUID);
} finally {
response.end('<HEAD><meta http-equiv="refresh" content="0; url=http:/list" /></HEAD><BODY> done. Go back in browser and refresh</BODY>');
}
}
} else if (reqparsed[0] === 'kill') {
// commit suicide
if (that.config.AllowKillHomebridge===true) {
response.end('<HEAD><meta http-equiv="refresh" content="20; url=http:/list" /></HEAD><BODY> Committed suicide. Reloading in 20 seconds.</BODY>');
var timerX = setTimeout(function() {
throw "Commited_Suicide";
}, 500);
}
} else {
// any other URL
response.write('<HEAD><TITLE>Homebridge-KNX</TITLE></HEAD>');
response.write('<BODY>');
response.write('URL<BR><BR>' + request.url + '<BR>');
response.write(JSON.stringify(params) + '<BR>');
response.end('</BODY>');
}
}.bind(this));
globs.debug('BEFORE requestServer.listen');
if (this.config.AllowWebserver) {
this.requestServer.listen(18081, function() {
console.log("Server Listening...localhost:18081/list");
});
}
// we're done, now issue the startup read requests to the bus
require('./lib/knxaccess.js').knxreadhash(globs.readRequests);
};
/**
* returns an accessory from an array of accessories if the context property is matched, or undefined.
*
* @param {homebridge/lib/platformAccessory.js~PlatformAccessory[]} accessories The array of accessories.
* @param {String} uuid The context object (presumably a string) to be matched.
* @return {homebridge/lib/platformAccessory.js~PlatformAccessory} or undefined
*
*/
function getAccessoryByUUID(accessories, uuid) {
globs.debug('--compare----------------');
for (var ina = 0; ina < accessories.length; ina++) {
var thisAcc = accessories[ina];
globs.debug('Comparing ' + thisAcc.UUID + ' === ' + uuid + ' ==>' + (thisAcc.UUID === uuid));
//console.log(thisAcc); // spit it out
if (thisAcc.UUID === uuid) {
globs.debug('---------------done---');
return thisAcc;
}
}
// nothing found:
globs.debug('-----none----------return-undefined--');
return undefined;
}
/**
* Search the globs object's devices[] array for an knxDevice with name 'name'
*/
globs.getDeviceByName = function(name) {
for (var idevice = 0; idevice < globs.devices.length; idevice++) {
var oDevice = globs.devices[idevice];
if (oDevice.name === name) {
return oDevice;
}
}
return undefined;
};