forked from michael5r/mmm-hue-lights
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
38 lines (27 loc) · 1.15 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
var NodeHelper = require('node_helper');
var request = require('request');
module.exports = NodeHelper.create({
start: function() {
console.log('Starting node_helper for module [' + this.name + ']');
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'MMM_HUE_LIGHTS_GET') {
var bridgeIp = payload.bridgeIp;
var user = payload.user;
var url = 'http://' + bridgeIp + '/api/' + user;
var self = this;
request(url, {method: 'GET'}, function(err, res, body) {
if ((err) || (res.statusCode !== 200)) {
self.sendSocketNotification('MMM_HUE_LIGHTS_DATA_ERROR', 'Hue API Error: ' + err);
} else {
if (body === {}) {
self.sendSocketNotification('MMM_HUE_LIGHTS_DATA_ERROR', 'Hue API Error: No Hue data was received.');
} else {
var data = JSON.parse(body);
self.sendSocketNotification('MMM_HUE_LIGHTS_DATA', data);
}
}
});
}
}
});