forked from zettajs/zetta-hue-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (56 loc) · 1.68 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
var util = require('util')
, Scout = require('zetta-scout')
, hue = require("node-hue-api")
, HueApi = require("node-hue-api").HueApi
, HueHubDriver = require('./hub')
, HueBulbDriver = require('./bulb');
var HueScout = module.exports = function() {
this.interval = 15000;
Scout.call(this);
};
util.inherits(HueScout, Scout);
HueScout.prototype.init = function(next) {
// start search logic
this.search();
setInterval(this.search.bind(this),this.interval);
next();
};
HueScout.prototype.search = function() {
var self = this;
hue.nupnpSearch(5000).then(function(hubs) {
hubs.forEach(self.foundHub.bind(self));
hue.upnpSearch(5000).then(function(hubs2) {
hubs.forEach(self.foundHub.bind(self));
}).fail(function(){}).done();
}).fail(function(){}).done();
};
HueScout.prototype.foundHub = function(data) {
var self = this;
var hubQuery = this.server.where({ type: 'huehub', hubId: data.id });
this.server.find(hubQuery, function(err, results){
var huehub = null;
if (results.length > 0) {
data.auth = results[0].auth;
huehub = self.provision(results[0], HueHubDriver, data);
} else {
huehub = self.discover(HueHubDriver, data);
}
if (huehub) {
huehub.onDiscoveredLight = self.foundLight.bind(self);
}
});
};
HueScout.prototype.foundLight = function(data, hue) {
var self = this;
var hubQuery = this.server.where({ type: 'huebulb', bulbId: data.id });
this.server.find(hubQuery, function(err, results){
if (err) {
return;
}
if (results.length > 0) {
self.provision(results[0], HueBulbDriver, data, hue);
} else {
self.discover(HueBulbDriver, data, hue);
}
});
};