-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiio-cli.js
90 lines (77 loc) · 2.85 KB
/
miio-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
'use strict';
const yargs = require('yargs');
const {hideBin} = require('yargs/helpers');
const miioProtocol = require('./miio-protocol');
/* RegExp patterns */
const testIP = new RegExp(/^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.{0,1}){4}$/gm);
const testToken = new RegExp(/^[A-Fa-f0-9]{32}$/gm);
(async () => {
/* CLI usage description */
const argv = yargs(hideBin(process.argv))
// .usage('Usage: $0 <command> [options]')
// .command('discover <ip>', 'Check device availability')
// .command('send <ip> <token> <cmd>', 'Send command to device')
.string(['ip', 'token', 'cmd'])
.boolean(['debug'])
.nargs('ip', 1)
.nargs('token', 1)
.usage('$0 --ip ip --token token --cmd cmd', 'Send command to device.')
.usage('$0 --ip ip', 'Check device availability')
.usage('$0', 'CLI for simple NodeJS miIO protocol lib')
.describe('ip', 'Device IP address')
.describe('token', 'Device token')
.describe('cmd', 'Command to device')
.describe('debug', 'Enable debug mode for verbose output')
.help('h')
.alias('h', 'help')
.argv;
/* */
const {ip, token, cmd, debug} = argv;
/* if --cmd is not set */
if (cmd == undefined) {
if (ip != undefined && testIP.test(ip) == true) {
const miIO = new miioProtocol(ip, token);
if (debug) console.log('sending HELLO message.');
const [res, msg] = await miIO.discover();
if (res) {
if (debug) console.log(msg[0]);
console.log(`Device (${ip}) is available.`);
} else {
console.error(msg.stack);
}
} else if (ip == undefined || testIP.test(ip) != true) {
console.error('Argument --ip is not set or it is incorrect.');
process.exit(-1);
}
}
/* if --cmd is set */
if (cmd != undefined) {
if (ip == undefined || testIP.test(ip) != true) {
console.error('You set --cmd, but --ip is not set or it is incorrect.');
process.exit(-1);
}
if (token == undefined || testToken.test(token) != true) {
console.error('You set --cmd, but --token is not set or it is incorrect.');
process.exit(-1);
}
const miIO = new miioProtocol(ip, token);
const [res, msg] = await miIO.discover();
if (res) {
if (debug) console.log(msg[0]);
} else {
console.error(msg.stack);
}
try {
const [res1, msg1] = await miIO.cmdSend(JSON.parse(cmd));
if (res1) {
if (debug) console.log(msg1[0]);
console.log(`Device (${ip}) is available and answered:`);
console.log(msg1[1]);
} else {
console.error(msg1.stack);
}
} catch(err) {
console.error(err);
}
}
})();