-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode_helper.js
138 lines (114 loc) · 3.75 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"use strict";
/* Magic Mirror
* Module: MMM-max
*
* By mirko30000
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const Player = require('node-aplay');
const fs = require('fs');
const path = require('path');
const miio = require('miio');
var deviceList = {};
module.exports = NodeHelper.create({
start: function () {
console.log("Starting xiaomi helper");
this.deviceList = {};
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === 'XIAOMI_CONNECT') {
console.log(new Date() + ": Triggering Xiaomi Gateway connect");
var self = this;
if (payload.token == null || payload.token === "") {
var device = miio.device({ address: payload.ip})
.then(
function(gateway) {
self.getDevices(gateway)
})
.catch(console.error);
}
else {
var device = miio.device({ address: payload.ip, token: payload.token })
.then(
function(gateway) {
self.getDevices(gateway)
})
.catch(console.error);
}
}
if (notification === 'PLAY_SOUND') {
this.playFile(payload, 500);
}
},
getDevices: function(gateway) {
var sensors = [];
var index = 0;
var self = this;
gateway.on('unavailable', reg => {
if(! reg.device) return;
console.log(new Date() + ": Gateway unavailable");
});
gateway.on('error', reg => {
if(! reg.device) return;
console.log(new Date() + ": Gateway error");
});
for (var i = 0; i < gateway.devices.length; i++) {
var currentDevice = gateway.devices[i]
console.log("Found device with ID " + currentDevice.id + " of type " + currentDevice.type)
// Register property change listener (only if not already known)
//if (!this.deviceList[currentDevice.id]) {
currentDevice.on('propertyChanged', e => self.propertyChanged(e));
//this.deviceList[currentDevice.id] = currentDevice;
//}
// Handle different devices types
if (currentDevice.type === 'sensor') {
var newDev = {};
newDev.temperature = currentDevice.temperature;
newDev.humidity = currentDevice.humidity;
newDev.id = currentDevice.id;
newDev.type = currentDevice.type;
sensors[index++] = newDev
}
if (currentDevice.type === 'magnet') {
var newDev = {};
newDev.open = currentDevice.property('open');
newDev.id = currentDevice.id;
newDev.type = currentDevice.type;
sensors[index++] = newDev
}
if (currentDevice.type === 'light') {
var newDev = {};
newDev.power = currentDevice.property('power');
newDev.id = currentDevice.id;
newDev.type = currentDevice.type;
sensors[index++] = newDev
}
}
this.sendSocketNotification('XIAOMI_INITDATA', sensors);
},
propertyChanged: function(event) {
// console.log(new Date() + ": " + event.id + " updated property '" + event.property + "' (" + event.oldValue + " --> " + event.value + ")");
this.sendSocketNotification('XIAOMI_CHANGEDATA', event);
},
/**
* @param {String} filename
* @param {Number} [delay] in ms
*/
playFile: function (filename, delay) {
let soundfile = __dirname + '/sounds/' + filename;
// Make sure file exists before playing
try {
fs.accessSync(soundfile, fs.F_OK);
} catch (e) {
// Custom sequence doesn't exist
console.log('Sound does not exist: ' + soundfile);
return;
}
console.log('Playing ' + filename + ' with ' + delay + 'ms delay', true);
setTimeout(() => {
new Player(path.normalize(soundfile)).play();
}, delay);
},
});