-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhue_controller.js
122 lines (104 loc) · 2.76 KB
/
hue_controller.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
var config = require('./config');
var marvin_utils = require('./marvin_utils.js');
var hue = require("node-hue-api"),
HueApi = hue.HueApi,
lightState = hue.lightState;
var host = config.hue_ip,
username = config.hue_user,
api = new HueApi(host, username);
var onState = lightState.create().turnOn();
var offState = lightState.create().turnOff();
var lightsArray = {};
var failedCalls = {}
var database;
var roomToLightMap = {};
Controller.prototype.onState = function() {
return onState;
};
Controller.prototype.offState = function() {
return offState;
};
Controller.prototype.setAway = function(status) {
setAway(status);
};
Controller.prototype.lightsArray = function() {
return lightsArray;
}
Controller.prototype.allOnForHome = function() {
allOnForHome();
}
var allOnForHome = function() {
for (var i in lightsArray) {
if (lightsArray[i].name
.indexOf('Living') > -1 ||
lightsArray[i].name
.indexOf('Kitchen') > -1) {
setLightState(lightsArray[i].id, onState);
}
}
}
Controller.prototype.allOff = function allOff() {
for (var i in lightsArray) {
setLightState(lightsArray[i].id, offState);
}
}
Controller.prototype.allOn = function allOn() {
for (var i in lightsArray) {
setLightState(lightsArray[i].id, onState);
}
}
Controller.prototype.setLightState = function setLight(id, state) {
setLightState(id, state);
}
function setLightState(id, state) {
if (id) {
api.setLightState(id, state, function(err, lights) {
if (err) {
marvin_utils.log(err + " setting light " + id + ", will retry");
failedCalls[id] = state;
} else {
marvin_utils.log("set " + id + " -> " + state._values.on);
}
});
}
}
function fetchBulbs() {
api.lights(function(err, lights) {
if (err) throw err;
for (var i in lights.lights) {
var id = lights.lights[i].id;
if (lights.lights[i].name.toString() != 'LightStrips') {
lightsArray[id] = lights.lights[i];
}
var stmt = database.prepare('INSERT OR REPLACE INTO devices VALUES (?, ?, ?)');
stmt.run(id, lights.lights[i].name, lights.lights[i].type);
stmt.finalize();
marvin_utils.log('saved bulb, ' + lights.lights[i].name);
}
});
}
Controller.prototype.toggleLight = function toggleLight(id) {
marvin_utils.log(id);
api.lightStatus(id, function(err, result) {
if (result) {
if (!result.state.on) {
setLightState(id, onState);
} else {
setLightState(id, offState);
}
} else {
marvin_utils.log(err);
}
});
}
setInterval(function() {
for (var i in failedCalls) {
setLightState(i, failedCalls[i])
}
failedCalls = {};
}, 1000);
function Controller(db) {
database = db;
fetchBulbs();
}
module.exports = Controller;