forked from elliots/ninja-hue2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
169 lines (132 loc) · 4.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
var util = require('util');
var stream = require('stream');
var hue = require('node-hue-api');
var hueapi = new hue.HueApi();
var USERNAME = 'ninjablocks';
function HueDriver(opts, app) {
this.opts = opts;
this.app = app;
this.bridges = {};
app.once('client::up', function() {
this.log.debug('Starting up');
this.log.debug('Configuration', this.opts);
for (var id in opts.bridges) {
this.addBridge(id, opts.bridges[id]);
}
setInterval(this.findBridges.bind(this), 5 * 60 * 1000);
this.findBridges();
}.bind(this));
}
util.inherits(HueDriver, stream);
HueDriver.prototype.findBridges = function() {
var log = this.log;
log.debug('Searching for bridges');
hue.locateBridges(function(err, result) {
if (err) {
return log.error('Failed to search for bridges', err);
}
result.forEach(function(bridge) {
this.addBridge(bridge.id, bridge.ipaddress);
}.bind(this));
}.bind(this));
};
HueDriver.prototype.registerBridge = function(id, ip) {
var log = this.log;
hueapi.createUser(ip, USERNAME, 'NinjaBlocks', function(err) {
if (err) {
var retryTime = 10000;
if (err.type == 101) {
retryTime = 1000;
} else {
log.error('Failed to register user with the bridge', id, ip, err);
}
setTimeout(function() {
this.registerBridge(id, ip);
}.bind(this), retryTime);
return;
}
log.info('Registered user with bridge', id, ip);
this.addBridge(id, ip);
}.bind(this));
};
HueDriver.prototype.addBridge = function(id, ip) {
var log = this.log;
if (!this.opts.bridges[id] || this.opts.bridges[id] != ip) {
// Ensure it's saved for next time
this.opts.bridges[id] = ip;
this.save(this.opts);
}
if (this.bridges[id]) {
if (this.bridges[id].host == ip) {
// All good. We know about this bridge, and it hasn't changed ip.
} else {
// The bridge has changed ip. Fix our api client!
log.warn('Bridge', id, 'has changed ip from', this.bridges[id].host, 'to', ip);
this.bridges[id].host = ip;
}
return;
}
log.info('Adding bridge', id, ip);
var api = new hue.HueApi(ip, USERNAME);
this.bridges[id] = api;
api.getFullState(function(err, state) {
if (err) {
log.error('Failed to get bridge state', err);
if (err.code === 'ETIMEDOUT') {
log.error('Timed out... removing bridge from config');
delete(this.bridges[id]);
delete(this.opts.bridges[id]);
this.save(this.opts);
}
if (err.type === 1) {
// Unregistered user
log.info('Unregistered user, registering');
this.emit('announcement', {
'contents': [
{ 'type': 'heading', 'text': 'New Philips Hue Link Detected' },
{ 'type': 'paragraph', 'text': 'To enable your Hue lights on the dashboard please press the link button on your Hue base station.' }
]
});
this.registerBridge(id, ip);
}
return;
}
log.debug('Full state for bridge', id, JSON.stringify(state, 2, 2));
for (var lightId in state.lights) {
this.addLight(api, id, lightId, state.lights[lightId]);
}
}.bind(this));
};
HueDriver.prototype.addLight = function(api, stationId, lightId, light) {
this.log.info('Adding light', lightId, light);
this.emit('register', new Light(api, stationId, lightId, light.name, light.state));
};
function Light(api, stationId, id, name, state) {
this.readable = true;
this.writeable = true;
this.V = 0;
this.D = 1008;
this.name = name;
this.G = 'hue'+stationId + id;
this.id = id;
this.api = api;
process.nextTick(function() {
this.emit('data', state);
}.bind(this));
}
util.inherits(Light, stream);
Light.prototype.write = function(value) {
if (typeof value == 'string') {
value = JSON.parse(value);
}
this.api.setLightState(this.id, value)
.then(function(result) {
this.emit('data', value);
}.bind(this))
.fail(function(e) {
console.error('Hue> Failed to set light', e);
}.bind(this))
.done();
};
module.exports = HueDriver;