-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
cli.js
executable file
·316 lines (255 loc) · 8.78 KB
/
cli.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env node
const { Control, Discovery, ControlAddressable } = require('./index');
const program = require('commander');
program
.version(require('./package.json').version)
.option('--bytes', "Output all received bytes", false)
.option('-Q, --quiet', "Suppress output", false)
.option('-T, --timeout [timeout]', "Connection timeout in milliseconds", null)
.option('-C, --cmd-timeout [timeout]', "Command timeout in milliseconds", 1000)
.option('--masks', "Use byte masks when setting colors", false)
.option('--cw_support', "Enable support for setting the cold white values", false)
.option('-A, --ack <mask>', "Wait for replies by setting a bitmask. Bits: 1=power 2=color 3=pattern 4=custom_pattern. Set to 15 to wait for all.", Control.ackMask, 0)
.option('-a, --addressable', "Use the Addressable protocol instead of the normal one", false);
program.command("discover")
.description("Discover devices on the network")
.action(() => discover(false));
program.command("discover_json")
.description("Discover devices on the network and output result as JSON")
.action(() => discover(true));
program.command("list_patterns")
.description("Returns a list of all built-in patterns")
.action(list_patterns);
program.command("turnon <ip>")
.alias("on")
.description("Turn controller on")
.action(mode_proxy(turnon, turnon_a));
program.command("turnoff <ip>")
.alias("off")
.description("Turn controller off")
.action(mode_proxy(turnoff, turnoff_a));
program.command("setcolor <ip> <red> <green> <blue>")
.alias("color")
.description("Set the color")
.action(mode_proxy(setrgb, setrgb_a));
program.command("setrgbw <ip> <red> <green> <blue> <ww>")
.alias("rgbw")
.description("Set the color and warm white values")
.action(mode_proxy(setrgbw, not_found));
program.command("setrgbww <ip> <red> <green> <blue> <ww> <cw>")
.alias("rgbww")
.description("Set the color and warm and cold white values")
.action(mode_proxy(setrgbww, not_found));
program.command("setwarmwhite <ip> <ww>")
.alias("ww")
.description("Set the warm white value")
.action(mode_proxy(setww, not_found));
program.command("setwhites <ip> <ww> <cw>")
.alias("whites")
.description("Set the warm and cold white value")
.action(mode_proxy(setwhites, not_found));
program.command("setpattern <ip> <pattern> <speed>")
.alias("pattern")
.description("Activate a built-in pattern")
.action(mode_proxy(setpattern, not_found));
program.command("setiapattern <ip> <code> <speed>")
.alias("iapattern")
.description("Activate a built-in individually addressable pattern")
.action(mode_proxy(setiapattern, not_found));
program.command("query <ip>")
.description("Query state of the controller and return result as JSON")
.action(mode_proxy(
function(ip, options) { query(ip, options, false) },
function(ip, options) { query_a(ip, options, false) },
));
program.command("query_nice <ip>")
.description("Query state of the controller and return result as formatted JSON")
.action(mode_proxy(
function(ip, options) { query(ip, options, false) },
function(ip, options) { query_a(ip, options, false) },
));
program.parse(process.argv);
if (process.argv.length < 3) {
program.help();
}
function mode_proxy(command_fn, addressable_command_fn) {
return function() {
const options = arguments[arguments.length - 1];
if (options.parent.addressable) {
return addressable_command_fn.apply(null, arguments);
} else {
return command_fn.apply(null, arguments);
}
}
}
function not_found() {
console.log("This command is not available for addressable controllers");
process.exit(1);
}
/*
* Command functions
*/
function list_patterns() {
console.log("Available patterns:");
console.log();
for(let pattern of Control.patternNames) {
console.log(pattern);
}
}
function discover(json_output) {
Discovery.scan(1000).then(devices => {
if (json_output) {
console.log(JSON.stringify(devices));
} else {
console.log("Discovered the following devices:");
console.log();
console.log("Address \t| ID \t| Model");
console.log("---------------------------------------");
for(let device of devices) {
console.log(`${device.address}\t| ${device.id}\t| ${device.model}`);
}
}
}).catch(err => {
return console.log("Error:", err.message);
});
}
function query(ip, options, formatted) {
const c = new Control(ip, getOptions(options.parent));
c.queryState().then(state => {
if(formatted) {
console.log(JSON.stringify(state, null, 4));
} else {
console.log(JSON.stringify(state));
}
}).catch(err => {
return console.log("Error:", err.message);
});
}
function query_a(ip, options, formatted) {
const c = new ControlAddressable(ip, getOptions_a(options.parent));
c.queryState().then(state => {
if(formatted) {
console.log(JSON.stringify(state, null, 4));
} else {
console.log(JSON.stringify(state));
}
}).catch(err => {
return console.log("Error:", err.message);
});
}
function setpattern(ip, pattern, speed, options) {
const c = new Control(ip, getOptions(options.parent));
console.log(speed);
c.setPattern(pattern, speed).then(success => {
if (!options.quiet) console.log((success) ? "success" : "failed");
}).catch(err => {
return console.log("Error:", err.message);
});
}
function setiapattern(ip, code, speed, options) {
const c = new Control(ip, getOptions(options.parent));
c.setIAPattern(Number(code), speed).then(success => {
if (!options.quiet) console.log((success) ? "success" : "failed");
}).catch(err => {
return console.log("Error:", err.message);
});
}
function setrgbww(ip, r, g, b, ww, cw, options) {
const c = new Control(ip, getOptions(options.parent));
c.setColorAndWhites(r, g, b, ww, cw).then(success => {
if (!options.quiet) console.log((success) ? "success" : "failed");
}).catch(err => {
return console.log("Error:", err.message);
});
}
function setrgbw(ip, r, g, b, ww, options) {
const c = new Control(ip, getOptions(options.parent));
c.setColorAndWarmWhite(r, g, b, ww).then(success => {
if (!options.quiet) console.log((success) ? "success" : "failed");
}).catch(err => {
return console.log("Error:", err.message);
});
}
function setrgb(ip, r, g, b, options) {
const c = new Control(ip, getOptions(options.parent));
c.setColor(r, g, b).then(success => {
if (!options.quiet) console.log((success) ? "success" : "failed");
}).catch(err => {
return console.log("Error:", err.message);
});
}
function setrgb_a(ip, r, g, b, options) {
const c = new ControlAddressable(ip, getOptions_a(options.parent));
c.setColor(r, g, b).then(success => {
if (!options.quiet) console.log((success) ? "success" : "failed");
}).catch(err => {
return console.log("Error:", err.message);
});
}
function setww(ip, ww, options) {
const c = new Control(ip, getOptions(options.parent));
c.setWarmWhite(ww).then(success => {
if (!options.quiet) console.log((success) ? "success" : "failed");
}).catch(err => {
return console.log("Error:", err.message);
});
}
function setwhites(ip, ww, cw, options) {
const c = new Control(ip, getOptions(options.parent));
c.setWhites(ww, cw).then(success => {
if (!options.quiet) console.log((success) ? "success" : "failed");
}).catch(err => {
return console.log("Error:", err.message);
});
}
function turnon(ip, options) {
const c = new Control(ip, getOptions(options.parent));
c.turnOn().then(success => {
if (!options.quiet) console.log((success) ? "success" : "failed");
}).catch(err => {
return console.log("Error:", err.message);
});
}
function turnon_a(ip, options) {
const c = new ControlAddressable(ip, getOptions_a(options.parent));
c.turnOn().then(success => {
if (!options.quiet) console.log((success) ? "success" : "failed");
}).catch(err => {
return console.log("Error:", err.message);
});
}
function turnoff(ip, options) {
const c = new Control(ip, getOptions(options.parent));
c.turnOff().then(success => {
if (!options.quiet) console.log((success) ? "success" : "failed");
}).catch(err => {
return console.log("Error:", err.message);
});
}
function turnoff_a(ip, options) {
const c = new ControlAddressable(ip, getOptions_a(options.parent));
c.turnOff().then(success => {
if (!options.quiet) console.log((success) ? "success" : "failed");
}).catch(err => {
return console.log("Error:", err.message);
});
}
function getOptions(options) {
const cmd_timeout = (options.cmdTimeout === "null") ? null : options.cmdTimeout;
return {
log_all_received: options.bytes === true,
apply_masks: options.masks === true,
ack: options.ack,
connect_timeout: options.timeout,
command_timeout: cmd_timeout,
cold_white_support: options.cw_support === true,
};
}
function getOptions_a(options) {
const cmd_timeout = (options.cmdTimeout === "null") ? null : options.cmdTimeout;
return {
log_all_received: options.bytes === true,
connect_timeout: options.timeout,
command_timeout: cmd_timeout,
};
}