-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
45 lines (35 loc) · 1.09 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
var eWeLink = require('node-ewelink-cloud');
var mqtt = require('mqtt')
var config = require('./config.js')
var topic = config.topic;
var connection = new eWeLink({
"platform" : config.platform,
"name" : config.name,
"email" : config.email,
"password" : config.password,
"imei" : config.imei
})
var client = mqtt.connect(config.mqtt)
client.on('close', () => {
console.log('MQTT DEAD');
process.exit();
})
client.on('error', (error) => {
console.log(error);
})
client.on('connect', () => {
client.subscribe(topic + '/set/+');
})
client.on('message', (topic, message) => {
// console.log("MQTT MESSAGE:", topic, message.toString())
let pieces = topic.split('/');
connection.setData(pieces[2],JSON.parse(message.toString()));
})
// connection.on(/.*/,(event) => console.log("EMITTED EVENT:", event.type,event.data));
connection.on(/state:.*/, (event) => {
if(event.data != 0) {
let pieces = event.type.split(':');
let deviceId = pieces[1];
client.publish(topic + '/' + deviceId, JSON.stringify(event.data), {retain: true});
}
});