-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
161 lines (125 loc) · 4.21 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
var util = require('util')
, stream = require('stream')
, os = require('os')
, helpers = require('./lib/helpers')
, Hue = require('hue.js')
, Light = require('./lib/Light')
, configHandlers = require('./lib/config');
util.inherits(hue,stream);
module.exports = hue;
function hue(opts,app) {
var self = this;
this._app = app;
this._opts = opts;
this._opts.stations = opts.stations || [];
this.appName = 'NinjaBlocks-' + app.id;
app.on('client::up', function() {
if (self._opts.stations.length>0) {
self.loadStations.call(self);
} else {
self.findStations.call(self);
}
});
};
var HUE_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." }
]
};
/**
* Called when a user prompts a configuration
* @param {Object} payload The return payload from the UI
* @param {String} rpc Used to match up requests.
* @param {Function} cb Callback with return data
*/
hue.prototype.config = function(rpc,cb) {
var self = this;
if (!rpc) {
return configHandlers.probe.call(this,cb);
}
switch (rpc.method) {
case 'manual_remove_hue': return configHandlers.manual_remove_hue.call(this,rpc.params,cb); break;
case 'manual_show_remove': return configHandlers.manual_show_remove.call(this,rpc.params,cb); break;
case 'manual_get_ip': return configHandlers.manual_get_ip.call(this,rpc.params,cb); break;
case 'manual_set_ip': return configHandlers.manual_set_ip.call(this,rpc.params,cb); break;
case 'search': return configHandlers.search.call(this,rpc.params,cb); break;
default: return cb(true); break;
}
};
hue.prototype.findStations = function() {
this._app.log.info('Hue: No configuration')
var self = this;
// If we do not want to auto register
if (!this._opts.autoRegister) return;
Hue.discover(function(stations) {
stations.forEach(function(station) {
self._app.log.info('Hue: Station', station);
// If we have already registered this
if (self._opts.stations.indexOf(station)>-1) return;
// If we have already announced this
// if (self._opts.sentAnnouncements.indexOf(station) > -1) return;
self.emit('announcement',HUE_ANNOUNCEMENT);
self.save();
self.registerStation(station);
});
});
};
hue.prototype.registerStation = function(station,cb) {
var stationIndex = this._opts.stations.indexOf(station);
if (stationIndex>-1) {
// We already have this station registered.
return this.fetchLights(station,stationIndex);
}
var self = this;
var client = Hue.createClient({
stationIp:station,
appName:this.appName
});
var registerOpts = {
interval:2000,
attempts:0
};
self._app.log.info('Hue: Please press link button on station %s',station);
client.register(registerOpts,function(err) {
if (err) {
// Do nothing?
self._app.log.info('Timed out waiting for station');
cb(err);
return;
}
if (self._opts.stations.indexOf(station)>-1) {
// We already have this station registered.
return;
}
self._app.log.info('Hue: station %s registered, saving',station);
self._opts.stations.push(station);
self.save();
self.loadStations();
if (typeof cb==="function") cb(null);
});
};
hue.prototype.loadStations = function() {
this._opts.stations.forEach(this.fetchLights.bind(this));
};
hue.prototype.fetchLights = function(stationIp,stationIndex) {
var self = this;
var client = Hue.createClient({
stationIp:stationIp,
appName:this.appName
});
client.station(function(err,station) {
if (err) {
// TODO check we are registered
if (err.type===1) {
self._app.log.info('Hue: Unauthorised user, aborting light registration');
self._opts.stations.splice(stationIndex,1);
self.save();
} else self._app.log.error(err);
return;
}
Object.keys(station.lights).forEach(function(lightIndex) {
self.emit('register',new Light(client, station, lightIndex));
});
});
};