forked from stephenkeep/node-red-contrib-milight
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMiLight.js
183 lines (173 loc) · 8.06 KB
/
MiLight.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
module.exports = function (RED) {
"use strict";
var Milight = require('node-milight-promise');
var packageFile = require('./package.json');
var Color = require('tinycolor2');
var url = require('url');
function node(config) {
RED.nodes.createNode(this, config);
var node = this;
// backwards compatibility with previous versions
if (config.bridgetype == null || config.bridgetype === '') {
config.bridgetype = 'legacy'
}
var myUrl = url.parse("http://" + config.ip);
var port;
if (myUrl.port != null) {
port = myUrl.port;
}
RED.log.info("Milight:" + myUrl.hostname + ":" + port);
var light = new Milight.MilightController({
ip: myUrl.hostname,
port: port,
delayBetweenCommands: (config.bridgetype !== 'v6') ? 200 : 100,
commandRepeat: 1,
type: config.bridgetype,
broadcastMode: config.broadcast
}),
zone = Number(config.zone),
bulb = config.bulbtype;
if (config.bridgetype === 'v6') {
var commands = Milight.commandsV6[bulb];
}
else if (bulb === 'white') {
var commands = Milight.commands[bulb];
}
else {
var commands = Milight.commands2[bulb];
}
this.on('input', function (msg) {
function argsHelper(vargs) {
var argsArray = [].slice.call(arguments);
if (config.bridgetype === 'v6' && bulb !== 'bridge') {
return [zone].concat(argsArray);
}
return argsArray;
}
function getSelectedObjectValues(sourceObject, keys) {
var values = [];
keys.forEach(function(key) { values.push(sourceObject[key]) });
return values;
}
light.ready().then(function () {
var command = msg.command ? msg.command : msg.topic;
if (commands == null) {
node.error("Selected combination of bridge type and bulb type is not supported");
return;
}
if (bulb !== 'white') {
switch (msg.payload) {
case 'off':
light.sendCommands(commands.off(zone));
break;
case 'on':
light.sendCommands(commands.on(zone));
break;
case 'disco':
light.sendCommands(commands.on(zone));
for (var x = 0; x < 256; x += 5) {
light.sendCommands(
commands.hue.apply(commands, argsHelper(x)));
light.pause(100);
}
break;
case 'mode':
light.sendCommands(commands.on(zone), commands.effectModeNext(zone));
break;
case 'speed_up':
light.sendCommands(commands.on(zone), commands.effectSpeedUp(zone));
break;
case 'speed_down':
light.sendCommands(commands.on(zone), commands.effectSpeedDown(zone));
break;
case 'white':
light.sendCommands(commands.on(zone), commands.whiteMode(zone));
break;
case 'night':
// nightMode command needs to be sent twice with some bulb types
light.sendCommands(commands.nightMode(zone), commands.nightMode(zone));
break;
default:
var value = Number(msg.payload);
if (command === 'rgb') {
var color = new Color(msg.payload);
if (color.isValid()) {
var args = argsHelper.apply(
node,
getSelectedObjectValues(color.toRgb(), ['r', 'g', 'b']));
light.sendCommands(commands.on(zone),
commands.rgb.apply(commands, args));
}
else {
throw(new Error("Invalid color value: " + msg.payload))
}
}
else if (!isNaN(value)) {
if (command === 'brightness')
light.sendCommands(
commands.on(zone),
commands.brightness.apply(commands, argsHelper(value)));
else if (command === 'color')
light.sendCommands(
commands.on(zone),
commands.hue.apply(commands, argsHelper(value, true)));
else if (command === 'saturation' && bulb === 'fullColor')
light.sendCommands(
commands.on(zone),
commands.saturation(zone, value, true));
else if (command === 'temperature' && bulb === 'fullColor')
light.sendCommands(
commands.on(zone),
commands.whiteTemperature(zone, value));
else if (config.bridgetype === 'v6' && command === 'mode')
light.sendCommands(commands.on(zone), commands.effectMode(zone, value));
}
break;
}
} else {
switch (msg.payload) {
case 'off':
light.sendCommands(commands.off(zone));
break;
case 'on':
light.sendCommands(commands.on(zone));
break;
case 'bright_up':
light.sendCommands(commands.brightUp(zone));
break;
case 'bright_down':
light.sendCommands(commands.brightDown(zone));
break;
case 'cooler':
light.sendCommands(commands.cooler(zone));
break;
case 'warmer':
light.sendCommands(commands.warmer(zone));
break;
case 'bright_max':
light.sendCommands(commands.maxBright(zone));
break;
case 'night':
light.sendCommands(commands.nightMode(zone));
break;
}
}
}).catch(function (error) {
node.error('Milight error: ' + error);
});
});
this.on('close', function (done) {
light.close()
.catch(function (error) {
// just log the error as a normal log message
// as it is safe to ignore the error at this point
node.log(error)
})
.finally(function () {
done()
});
});
}
RED.nodes.registerType("MiLight", node);
RED.log.info(packageFile.name + '@' + packageFile.version + ' started');
};