-
Notifications
You must be signed in to change notification settings - Fork 6
/
lights.js
103 lines (92 loc) · 3.23 KB
/
lights.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
"use strict";
// TODO: capabilities
// lights/hue/{unique_id}/get/on
// lights/hue/{unique_id}/get/hue
// lights/hue/{unique_id}/get/brightness
// lights/hue/{unique_id}/get/saturation
// lights/hue/{unique_id}/get/name
//
// lights/hue/{unique_id}/set/state
// {
// brightness: 0,
// hue: 0,
// transitionTime: 0,
// on: true,
// saturation: 0
// }
// lights/hue/{unique_id}/set/on
// lights/hue/{unique_id}/set/brightness
let lights = {
timeout: 1000,
regex: "lights/hue/(.*)/set/(.*)",
lights: {}
};
module.exports = lights;
lights.init = function(options) {
this.hue = options.hue;
this.mqtt = options.mqtt;
this.callback();
this.mqtt.subscribe('lights/hue/+/set/+');
this.mqtt.on('message', this.onMessage.bind(this));
};
lights.onMessage = function(topic, message) {
let parts = topic.match(this.regex);
if(parts == null) return;
let identifier = parts[1];
let property = parts[2];
let currentLight = this.lights[identifier];
if(currentLight === undefined) return;
this.hue.lights.getById(currentLight.id)
.then(light => {
switch(property) {
case "on":
light.on = message.toString() == 'true';
break;
case "brightness":
light.brightness = message.toString();
break;
case "state":
try {
let json = JSON.parse(message.toString());
if(json["brightness"]!=undefined) light.brightness = json["brightness"];
if(json["transitionTime"]!=undefined) light.transitionTime = json["transitionTime"];
if(json["hue"]!=undefined) light.hue = json["hue"];
if(json["saturation"]!=undefined) light.saturation = json["saturation"];
if(json["on"]!=undefined) light.on = json["on"];
} catch (ex) {
console.log(ex);
}
break;
}
return this.hue.lights.save(light);
})
.then(light => {
this.publishLight(null, light);
});
};
lights.callback = function() {
this.hue.lights.getAll()
.then(lights => {
for (let light of lights) {
let previous = this.lights[light.uniqueId];
this.lights[light.uniqueId] = light;
this.publishLight(previous, light);
}
setTimeout(this.callback.bind(this), this.timeout);
})
.catch(error => {
console.log(`An error occurred: ${error.message}`);
});
};
lights.publishLight = function(previous, current) {
if(!previous || previous.name != current.name)
this.mqtt.publish(`lights/hue/${current.uniqueId}/get/name`, `${current.name}`, { retain: true });
if(!previous || previous.on != current.on)
this.mqtt.publish(`lights/hue/${current.uniqueId}/get/on`, `${current.on}`, { retain: true });
if(!previous || previous.hue != current.hue)
this.mqtt.publish(`lights/hue/${current.uniqueId}/get/hue`, `${current.hue}`, { retain: true });
if(!previous || previous.saturation != current.saturation)
this.mqtt.publish(`lights/hue/${current.uniqueId}/get/saturation`, `${current.saturation}`, { retain: true });
if(!previous || previous.brightness != current.brightness)
this.mqtt.publish(`lights/hue/${current.uniqueId}/get/brightness`, `${current.brightness}`, { retain: true });
};