/
main.js
528 lines (446 loc) · 15.2 KB
/
main.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/**
*
* UniFi ioBroker Adapter
*
* Adapter to communicate with a UniFi-Controller instance
* dealing with UniFi-WiFi-Devices
*
*/
/* jshint -W097 */// jshint strict:false
/*jslint node: true */
"use strict";
// you have to require the utils module and call adapter function
var utils = require('@iobroker/adapter-core'); // Get common adapter utils
// you have to call the adapter function and pass a options object
// name has to be set and has to be equal to adapters folder name and main file name excluding extension
// adapter will be restarted automatically every time as the configuration changed, e.g system.adapter.unifi.0
var adapter = utils.adapter('unifi');
// define a timeout variable so that
// we can check the controller in regular intervals
var queryTimeout;
// is called when adapter shuts down - callback has to be called under any circumstances!
adapter.on('unload', function (callback) {
try {
// clear the timeout
if(queryTimeout)
clearTimeout(queryTimeout);
adapter.log.info('cleaned everything up...');
callback();
} catch (e) {
callback();
}
});
// is called if a subscribed object changes
adapter.on('objectChange', function (id, obj) {
// Warning, obj can be null if it was deleted
adapter.log.info('objectChange ' + id + ' ' + JSON.stringify(obj));
});
// is called if a subscribed state changes
adapter.on('stateChange', function (id, state) {
// you can use the ack flag to detect if it is status (true) or command (false)
// Warning, state can be null if it was deleted
if(state && state.ack === false) {
adapter.log.info('stateChange ' + id + ' ' + JSON.stringify(state));
}
});
// Some message was sent to adapter instance over message box. Used by email, pushover, text2speech, ...
adapter.on('message', function (obj) {
processMessage(obj);
});
// is called when databases are connected and adapter received configuration.
// start here!
adapter.on('ready', function () {
main();
});
function main() {
adapter.getForeignObject('system.adapter.' + adapter.namespace, function (err, obj) {
if (!err && obj && (obj.common.mode !== 'daemon')) {
obj.common.mode = 'daemon';
if (obj.common.schedule) delete(obj.common.schedule);
adapter.setForeignObject(obj._id, obj);
}
});
// subscribe to all state changes
adapter.subscribeStates('*');
updateUniFiData();
}
function processMessage(obj) {
if(!obj)
return;
adapter.log.info('Message received = ' + JSON.stringify(obj.message));
var updateUniFi = false;
if(typeof obj == 'object' && obj.message) {
if(obj.command == 'notify') {
adapter.log.info('got notify');
}
else
updateUniFi = true;
}
if(updateUniFi) {
if(queryTimeout)
clearTimeout(queryTimeout);
updateUniFiData();
}
}
/**
* Function to process a bunch of HTTP requests sequentially
* using 'request' and 'async'
*/
/*
function processRequests(requestList) {
async.eachSeries(requestList, function(item, cb) {
if(item.json) {
request.post(item, function(err, res, body) {
if(item.callback(err, res, body) === false) {
cb("ERROR!!!!");
}
else {
cb(err);
}
});
} else {
request.get(item, function(err, res, body) {
if(item.callback(err, res, body) === false) {
cb("ERROR2!!!!");
}
else {
cb(err);
}
});
}
}, function(err) {
adapter.log.info('DONE: ' + JSON.stringify(err));
});
}
*/
/**
* Helper functions to parse our JSON-based result data in
* a recursive/traversal fashion.
*/
function traverse(x, level, mindepth, maxdepth, cb, depth)
{
if(typeof(depth) === 'undefined')
depth = 0;
depth++;
if(typeof(maxdepth) !== 'undefined' && maxdepth !== 0 && depth > maxdepth)
return;
if(Array.isArray(x))
traverseArray(x, level, mindepth, maxdepth, cb, depth);
else if((typeof(x) === 'object') && (x !== null))
traverseObject(x, level, mindepth, maxdepth, cb, depth);
else if(mindepth <= depth && cb(level, x, depth) === false)
return;
}
function traverseArray(arr, level, mindepth, maxdepth, cb, depth)
{
if(mindepth <= depth && cb(level, arr, depth) === false)
return;
arr.every(function(x, i)
{
if((typeof(x) === 'object'))
traverse(x, level, mindepth, maxdepth, cb, depth);
else
return false;
return true;
});
}
function traverseObject(obj, level, mindepth, maxdepth, cb, depth)
{
if(mindepth <= depth && cb(level, obj, depth) === false)
return;
for(var key in obj)
{
if(obj.hasOwnProperty(key))
traverse(obj[key], level + '.' + key, mindepth, maxdepth, cb, depth);
}
}
/**
* Function to organize setState() calls that we are first checking if
* the value is really changed and only then actually call setState()
* to let others listen for changes
*/
var setStateArray = [];
function processStateChanges(stateArray, callback) {
if(!stateArray || stateArray.length === 0)
{
if(typeof(callback) === 'function')
callback();
// clear the array
setStateArray = [];
}
else
{
var newState = setStateArray.shift();
adapter.getState(newState.name, function(err, oldState) {
if(oldState === null || newState.val != oldState.val)
{
//adapter.log.info('changing state ' + newState.name + ' : ' + newState.val);
adapter.setState(newState.name, {ack: true, val: newState.val}, function() {
setTimeout(processStateChanges, 0, setStateArray, callback);
});
}
else
setTimeout(processStateChanges, 0, setStateArray, callback);
});
}
}
/**
* Function to create a state and set its value
* only if it hasn't been set to this value before
*/
function createState(name, value, desc) {
if(typeof(desc) === 'undefined')
desc = name;
if(Array.isArray(value))
value = value.toString();
adapter.setObjectNotExists(name, {
type: 'state',
common: { name: desc,
type: typeof(value),
read: true,
write: false },
native: { id: name }
});
if(typeof(value) !== 'undefined')
setStateArray.push({name: name, val: value});
}
/**
* Function to create a channel
*/
function createChannel(name, desc) {
if(typeof(desc) === 'undefined')
desc = name;
adapter.setObjectNotExists(name, {
type: 'channel',
common: { name: desc },
native: {}
});
}
/**
* Function that receives the site info as a JSON data array
* and parses through it to create all channels+states
*/
function processSiteInfo(site_data) {
// lets store some site information
for(var i=0; i < site_data.length; i++)
{
// traverse the json with depth 0..2 only
traverse(site_data[i], site_data[i].name, 0, 2, function(name, value, depth)
{
//adapter.log.info('(' + depth + '): ' + name + ' = ' + value + ' type: ' + typeof(value));
if(typeof(value) === 'object')
{
if(depth == 1)
createChannel(name, 'Site ' + value.desc);
else // depth == 2
{
// continue the traversal of the object with depth 2
traverse(value, name, 2, 2, function(name, value, depth)
{
//adapter.log.info('_(' + depth + '): ' + name + ' = ' + value + ' type: ' + typeof(value));
createChannel(name);
// walk through all sub values on a flat level starting with the
// subsystem tree.
traverse(value, name + '.' + value.subsystem, 0, 0, function(name, value, depth)
{
//adapter.log.info('__(' + depth + '): ' + name + ' = ' + value + ' type: ' + typeof(value));
if(typeof(value) === 'object')
createChannel(name, 'Subsystem ' + value.subsystem);
else
createState(name, value);
});
});
}
}
else
createState(name, value);
});
}
}
/**
* Function that receives the client device info as a JSON data array
* and parses through it to create all channels+states
*/
function processClientDeviceInfo(sites, clientDevices) {
// lets store some site information
for(var i=0; i < sites.length; i++)
{
// traverse the json with depth 3..4 only
traverse(clientDevices[i], sites[i] + '.clients', 2, 2, function(name, value, depth)
{
//adapter.log.info('(' + depth + '): ' + name + ' = ' + value + ' type: ' + typeof(value));
if(typeof(value) === 'object')
{
// continue the traversal of the object with depth 2
traverse(value, name + '.' + value.mac, 1, 0, function(name, value, depth)
{
//adapter.log.info('_(' + depth + '): ' + name + ' = ' + value + ' type: ' + typeof(value));
if(depth == 1)
createChannel(name, typeof(value.hostname) !== 'undefined' ? value.hostname : '');
else
createState(name, value);
});
}
else
createState(name, value);
});
}
}
/**
* Function that receives the access device info as a JSON data array
* and parses through it to create all channels+states
*/
function processAccessDeviceInfo(sites, accessDevices) {
// lets store some site information
for(var i=0; i < sites.length; i++)
{
// traverse the json with depth 3..4 only
traverse(accessDevices[i], sites[i] + '.devices', 2, 2, function(name, value, depth)
{
//adapter.log.info('(' + depth + '): ' + name + ' = ' + value + ' type: ' + typeof(value));
if(typeof(value) === 'object' && value !== null)
{
// continue the traversal of the object with depth 2
traverse(value, name + '.' + value.mac, 1, 2, function(name, value, depth)
{
//adapter.log.info('_(' + depth + '): ' + name + ' = ' + value + ' type: ' + typeof(value));
if(depth === 1)
createChannel(name, value.model + ' - ' + value.serial);
else if(typeof(value) === 'object' && value !== null)
{
traverse(value, name, 1, 2, function(name, value, depth)
{
//adapter.log.info('__(' + depth + '): ' + name + ' = ' + value + ' type: ' + typeof(value) + ' is_null: ' + (value === null));
if(depth === 1)
createChannel(name, name);
else if(typeof(value) === 'object' && value !== null)
{
traverse(value, name + '.' + value.name, 1, 0, function(name, value, depth)
{
//adapter.log.info('___(' + depth + '): ' + name + ' = ' + value + ' type: ' + typeof(value));
if(depth === 1)
createChannel(name, name);
else
createState(name, value);
});
}
else
createState(name, value);
});
}
else
createState(name, value);
});
}
else
createState(name, value);
});
}
}
/**
* Function that receives the site sysinfo as a JSON data array
* and parses through it to create all channels+states
*/
function processSiteSysInfo(sites, sysinfo) {
// lets store some site information
for(var i=0; i < sysinfo.length; i++)
{
// traverse the json with depth 0..2 only
traverse(sysinfo[i], sites[i] + '.sysinfo', 2, 4, function(name, value, depth)
{
//adapter.log.info('(' + depth + '): ' + name + ' = ' + value + ' type: ' + typeof(value) + ' array: ' + Array.isArray(value));
if(typeof(value) === 'object')
{
if(depth == 2)
createChannel(name, 'Site Sysinfo');
else if(depth == 3)
createChannel(name);
else
{
if(typeof(value.key) !== 'undefined')
{
// continue the traversal of the object with depth 2
traverse(value, name + '.' + value.key, 1, 2, function(name, value, depth)
{
//adapter.log.info('_(' + depth + '): ' + name + ' = ' + value + ' type2: ' + typeof(value) + ' array: ' + Array.isArray(value));
if(Array.isArray(value) === false && typeof(value) === 'object')
createChannel(name, value.name);
else
createState(name, value);
});
}
else
createState(name, value);
}
}
else
createState(name, value);
});
}
}
function updateUniFiData() {
adapter.log.info('Starting UniFi-Controller query');
var update_interval = parseInt(adapter.config.update_interval, 10) || 60;
var controller_ip = adapter.config.controller_ip || "127.0.0.1";
var controller_port = adapter.config.controller_port || 8443;
var controller_username = adapter.config.controller_username || "admin";
var controller_password = adapter.config.controller_password || "";
adapter.log.info('update_interval = ' + update_interval);
adapter.log.info('controller = ' + controller_ip + ':' + controller_port);
// get unifi class
var unifi = require('node-unifi');
var controller = new unifi.Controller(controller_ip, controller_port);
//////////////////////////////
// LOGIN
controller.login(controller_username, controller_password, function(err) {
if(err)
{
adapter.log.info('ERROR: ' + err);
return;
}
//////////////////////////////
// GET SITE STATS
controller.getSitesStats(function(err, site_data) {
var sites = site_data.map(function(s) { return s.name; });
adapter.log.info('getSitesStats: ' + sites);
//adapter.log.info(JSON.stringify(site_data));
processSiteInfo(site_data);
//////////////////////////////
// GET SITE SYSINFO
controller.getSiteSysinfo(sites, function(err, sysinfo) {
adapter.log.info('getSiteSysinfo: ' + sysinfo.length);
//adapter.log.info(JSON.stringify(sysinfo));
processSiteSysInfo(sites, sysinfo);
//////////////////////////////
// GET CLIENT DEVICES
controller.getClientDevices(sites, function(err, client_data) {
adapter.log.info('getClientDevices: ' + client_data[0].length);
//adapter.log.info(JSON.stringify(client_data));
processClientDeviceInfo(sites, client_data);
//////////////////////////////
// GET ACCESS DEVICES
controller.getAccessDevices(sites, function(err, devices_data) {
adapter.log.info('getAccessDevices: ' + devices_data[0].length);
//adapter.log.info(JSON.stringify(devices_data));
processAccessDeviceInfo(sites, devices_data);
//////////////////////////////
// FINALIZE
// finalize, logout and finish
controller.logout();
// process all schedule state changes
processStateChanges(setStateArray);
// schedule a new execution of updateUniFiData in X seconds
queryTimeout = setTimeout(updateUniFiData, update_interval * 1000);
});
});
});
});
});
//var endpoints = [ unifi_login(controller_username, controller_password),
// unifi_stat_sites(getSites),
// //unifi_stat_sysinfo(sites, getSiteSysinfo),
// //unifi_list_stations(site),
// unifi_logout() ]
//processRequests(endpoints);
//queryTimeout = setTimeout(updateUniFiData, update_interval * 1000);
}