-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
76 lines (69 loc) · 2.63 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
/* Magic Mirror
* Node Helper: MMM-Yeelight
*
* By Slamet PS/slametps@gmail.com
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
var Lookup = require('node-yeelight-wifi').Lookup;
var arrDevices;
var arrDevicesItem;
var look;
module.exports = NodeHelper.create({
// Subclass start method.
start: function() {
console.log("Starting node_helper.js for MMM-Yeelight.");
},
search: function (config, params) {
try {
console.log('Searching Yeelight...');
look = new Lookup();
arrDevices = [];
look.on("detected",(device) =>
{
//console.log("new yeelight detected: id="+device.id + " name="+device.name + " mac="+device.mac + " power="+device.power + " ip="+device.host + " port="+device.port);
let stateDevice = (device.power ? 1 : 0);
//console.log(config.devicesInfo);
let deviceInfo = config.devicesInfo.find(deviceInfo => deviceInfo.mac.toLowerCase() === device.mac.toLowerCase());
//console.log(deviceInfo);
let deviceAlias;
if (!deviceInfo) deviceAlias = "XXX";
else deviceAlias = deviceInfo.alias;
arrDevicesItem = {alias:deviceAlias, type:"bulb", ip:device.host, port:device.port, on_off:stateDevice};
arrDevices.push(arrDevicesItem);
});
} catch (err) {
console.log(err);
}
},
socketNotificationReceived: function(notification, payload) {
console.log(this.name + " node helper received a socket notification: " + notification + " - Payload: " + payload);
if (notification == "YEELIGHT_NETWORK_SEARCH") {
//console.log("Yeelight SEARCH BEGIN");
this.search(payload.config, {});
var that = this;
function sendInfo() {
// nothing to do
//console.log("in sendInfo-" + arrDevices.length);
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);
//console.log("2-PRINT OUTPUT LENGTH = " + arrDevices.length);
that.sendSocketNotification('YEELIGHT_NETWORK_SEARCH_RESULT', {devices: arrDevices});
//console.log("before process.exit()");
clearInterval(look.interval);
//console.log("after process.exit()");
}
}
setTimeout(sendInfo, payload.config.timeout);
//console.log(payload.config.timeout + " ms -> CHECK arrDevices-" + arrDevices.length);
//console.log("Yeelight SEARCH END");
}
},
});