-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
142 lines (112 loc) · 3.63 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
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
139
140
141
142
'use strict';
var mqtt = require('mqtt');
var request = require('request');
var extend = require('deep-extend');
var equal = require('equals');
var config = require('./config.json');
config = extend({
bridges: [],
broker: {
host: 'localhost',
username: '',
password: ''
},
}, config);
if (undefined === config.bridges || !config.bridges.length) {
console.error('No Philips Hue bridges are configured. Please configure a bridge and try again.');
process.exit(1);
}
function slugify(value) {
return value.toString().toLowerCase().replace(/[ \.\-\/\\]/g, '_').replace(/[^a-z0-9_]/g, '');
}
function startPolling() {
config.bridges.forEach(function(bridge, index) {
if (!bridge || undefined === bridge.host || !bridge.host) {
console.error('Cannot poll Hue bridge: missing required argument "host"');
process.exit(1);
}
if (undefined === bridge.username || !bridge.username) {
console.error('Cannot poll Hue bridge %s: missing required argument "username"', bridge.host);
process.exit(1);
}
bridge.id = index;
bridge.interval = bridge.interval || 1000;
bridge.polling = false;
bridge.prefix = bridge.prefix || 'hue';
bridge.sensors = {};
bridge.skipped = false;
console.log('Polling Hue bridge %s every %dms', bridge.host, bridge.interval);
bridge.timer = setInterval(pollSensors, bridge.interval, bridge);
pollSensors(bridge);
});
}
function pollSensors(_bridge) {
var bridge = _bridge;
if (bridge.polling) {
if (!bridge.skipped) {
bridge.skipped = true;
console.log('Polling skipped on Hue bridge %s. Consider raising your polling interval.', bridge.host);
}
return false;
}
bridge.polling = true;
var opts = {
method: 'GET',
uri: 'http://' + bridge.host + '/api/' + bridge.username + '/sensors',
json: true
};
request(opts, function(err, res, body) {
if (err) {
bridge.polling = false;
return console.error('Error polling sensors on Hue bridge %s: %s', bridge.host, err.toString());
}
var sensors = body;
Object.keys(sensors).forEach(function(id) {
var sensorA = sensors[id];
var sensorB = bridge.sensors[id];
if (undefined !== sensorA.error) {
bridge.polling = false;
return console.error('Error polling sensors on Hue bridge %s: %s', bridge.host, sensorA.error.description);
}
if (undefined === sensorB) {
bridge.sensors[id] = sensorA;
return;
}
if (getTime(sensorA) >= getTime(sensorB) && !equal(sensorA.state, sensorB.state)) {
var nameSlug = slugify(sensorA.name);
Object.keys(sensorA.state).forEach(function(key) {
var keySlug = slugify(key);
var topic = bridge.prefix + '/' + nameSlug + '/' + keySlug;
var payload = sensorA.state[key];
// console.log('%s %s', topic, payload.toString());
client.publish(topic, payload.toString());
});
}
bridge.sensors[id] = sensorA;
});
bridge.polling = bridge.skipped = false;
});
}
function getTime(sensor) {
return new Date(sensor.state.lastupdated);
}
// Exit handling to disconnect client
function exitHandler() {
client.end();
process.exit();
}
// Disconnect client when script exits
process.on('exit', exitHandler);
process.on('SIGINT', exitHandler);
var client = mqtt.connect(config.broker);
client.on('connect', function() {
startPolling();
});
client.on('error', function(err) {
if (err)
return console.error('MQTT Error: %s', err.toString());
});
client.on('close', function(err) {
if (err)
return console.error('MQTT Error: %s', err.toString());
});