-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·60 lines (54 loc) · 1.82 KB
/
app.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
var config = require('./config/config.json')
var dashButton = require('node-dash-button');
var hue = require("node-hue-api");
var HueApi = hue.HueApi;
var buttons = [];
var hostname = config.philips_hue.ipaddress;
var username = config.philips_hue.username;
var api = new HueApi(hostname, username);
function doLog(message) {
console.log('[' + (new Date().toISOString()) + '] ' + message);
}
var displayResults = function(result) {
console.log(JSON.stringify(result, null, 2));
};
function DasherButton(button, api) {
this.dashButton = dashButton(button.address, null, null, 'all')
doLog("'" + button.name + "' button added.")
this.dashButton.on("detected", function () {
if (button.group) {
doLog("'" + button.name + "' button pressed. Toggling group " + button.group + ".")
toggleGroup(button.group, api);
} else if (button.light) {
doLog("'" + button.name + "' button pressed. Toggling light " + button.light + ".")
toggleLight(button.light, api);
} else {
doLog("'" + button.name + "' button pressed, but there is no light or group to toggle.")
}
})
}
// Reads a group state, then reverses it
function toggleGroup(group, api) {
api.getGroup(group, function (err, result) {
if (err) throw err;
//displayResults(result);
api.setGroupLightState(group, { "on": !result.lastAction.on }, function (err, result) {
if (err) throw err;
});
});
}
// Reads a light state, then reverses it
function toggleLight(light, api) {
api.getLightStatus(light, function (err, result) {
if (err) throw err;
//displayResults(result);
api.setLightState(light, { "on": !result.state.on }, function (err, result) {
if (err) throw err;
});
});
}
// Creates as many buttons as we have in the config file
for (var i = 0; i < config.buttons.length; i++) {
button = config.buttons[i]
buttons.push(new DasherButton(button, api))
}