-
Notifications
You must be signed in to change notification settings - Fork 4
/
node_helper.js
118 lines (108 loc) · 3.83 KB
/
node_helper.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
/* Magic Mirror
* Node Helper: MMM-TPLink
*
* By Slamet PS/slametps@gmail.com
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
var { Client, ResponseError, Util } = require('tplink-smarthome-api');
let logLevel;
let client;
let arrDevices;
let arrDevicesItem;
module.exports = NodeHelper.create({
// Subclass start method.
start: function() {
console.log("Starting node_helper.js for MMM-TPLink.");
},
dump: function(v, s) {
s = s || 1;
var t = '';
switch (typeof v) {
case "object":
t += "\n";
for (var i in v) {
t += Array(s).join(" ")+i+": ";
t += this.dump(v[i], s+3);
}
break;
default: //number, string, boolean, null, undefined
t += v+" ("+typeof v+")\n";
break;
}
return t;
},
outputError: function (err) {
if (err instanceof ResponseError) {
console.log(err.response);
} else {
console.error(err);
}
},
setupClient: function (config) {
let defaultSendOptions = {};
if (config.udp) defaultSendOptions.transport = 'udp';
if (config.timeout) defaultSendOptions.timeout = config.timeout;
let client = new Client({ logLevel, defaultSendOptions });
return client;
},
// params in JSON object
search: function (config, params) {
try {
console.log('Searching...');
client = this.setupClient(config);
arrDevices = [];
let commandParams = Object.assign({}, {discoveryInterval: config.discoveryInterval || 2000, discoveryTimeout: config.timeout || 5000 }, params); // {discoveryInterval: 2000, discoveryTimeout: timeout, ...params};
//console.log(`startDiscovery(${Util.inspect(commandParams)})`);
//console.log('startDiscovery()');
client.startDiscovery(commandParams)
.on('device-new', (device) => {
//console.log(`${device.model} ${device.deviceType} ${device.type} ${device.host} ${device.port} ${device.deviceId} ${device.alias}`);
let stateDevice = 0;
switch (device.deviceType)
{
case "bulb":
stateDevice = device.lighting.lightState.on_off;
break;
case "plug":
stateDevice = device.sysInfo.relay_state;
break;
default: stateDevice = 0;
}
arrDevicesItem = {alias:device.alias, type:device.deviceType, ip:device.host, port:device.port, on_off:stateDevice};
arrDevices.push(arrDevicesItem);
//console.log(">> stateDevice = " + stateDevice);
//console.log(">> arrDevices = " + arrDevices.length);
});
} catch (err) {
this.outputError(err);
}
},
socketNotificationReceived: function(notification, payload) {
console.log(this.name + " node helper received a socket notification: " + notification + " - Payload: " + payload);
if (notification == "TPLINK_NETWORK_SEARCH") {
//console.log("TPLink SEARCH BEGIN");
this.search(payload.config, {});
var that = this;
function sendInfo() {
// nothing to do
//console.log("in sendInfo");
if (arrDevices.length >= 1) {
//console.log("1-PRINT OUTPUT LENGTH = " + arrDevices.length);
arrDevices.sort(function(a, b) {
var x = a.alias.toLowerCase();
var y = b.alias.toLowerCase();
if (x < y) {return -1;}
if (x > y) {return 1;}
});
//console.log("arrDevices = [" + this.dump(arrDevices) + "]");
//console.log("2-PRINT OUTPUT LENGTH = " + arrDevices.length);
that.sendSocketNotification('TPLINK_NETWORK_SEARCH_RESULT', {devices: arrDevices});
}
}
setTimeout(sendInfo, payload.config.timeout + 100);
//console.log(payload.config.timeout + " ms -> CHECK arrDevices-" + arrDevices.length);
//console.log("TPLink SEARCH END");
}
},
});