forked from jangxx/node-magichome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·238 lines (190 loc) · 6.52 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
#!/usr/bin/env node
const { Control, Discovery } = 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);
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(turnon);
program.command("turnoff <ip>")
.alias("off")
.description("Turn controller off")
.action(turnoff);
program.command("setcolor <ip> <red> <green> <blue>")
.alias("color")
.description("Set the color")
.action(setrgb);
program.command("setrgbw <ip> <red> <green> <blue> <ww>")
.alias("rgbw")
.description("Set the color and warm white values")
.action(setrgbw);
program.command("setrgbww <ip> <red> <green> <blue> <ww> <cw>")
.alias("rgbww")
.description("Set the color and warm and cold white values")
.action(setrgbww);
program.command("setwarmwhite <ip> <ww>")
.alias("ww")
.description("Set the warm white value")
.action(setww);
program.command("setwhites <ip> <ww> <cw>")
.alias("whites")
.description("Set the warm and cold white value")
.action(setwhites);
program.command("setpattern <ip> <pattern> <speed>")
.alias("pattern")
.description("Activate a built-in pattern")
.action(setpattern);
program.command("setiapattern <ip> <code> <speed>")
.alias("iapattern")
.description("Activate a built-in individually addressable pattern")
.action(setiapattern);
program.command("query <ip>")
.description("Query state of the controller and return result as JSON")
.action((ip, options) => query(ip, options, false));
program.command("query_nice <ip>")
.description("Query state of the controller and return result as formatted JSON")
.action((ip, options) => query(ip, options, true));
program.parse(process.argv);
if (process.argv.length < 3) {
program.help();
}
/*
* 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 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 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 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 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,
};
}