-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
170 lines (155 loc) · 4.18 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
#!/usr/bin/env node
const assert = require('assert');
const commander = require('commander');
const fs = require('fs').promises;
const path = require('path');
const readline = require('readline');
const { SerialPort } = require('serialport');
const { JsonRpcClient } = require('./lib');
const pkg = require('./package.json');
const program = new commander.Command();
const jsonRpcMethods = [
'restart',
'echo',
'getinfo',
'getconfig',
'setconfig',
'getlogs',
'deletelogs',
'spiffs_reformat',
];
const slowRpcMethods = [
'spiffs_reformat',
'getlogs',
'deletelogs',
];
const promptPrefix = '> ';
let rl;
const logsFilePath = path.join(process.cwd(), 'bleskomat.log');
program
.version(pkg.version)
.description(pkg.description)
.command('connect')
.option(
'--devicePath <value>',
'File path of USB device',
value => value,
'/dev/ttyUSB0',
)
.option(
'--baudRate <value>',
'The baud rate used for serial communication with USB device',
value => value,
115200,
)
.action(function(options) {
const port = new SerialPort({
path: options.devicePath,
baudRate: options.baudRate,
autoOpen: false,
});
port.on('open', () => {
console.log('Serial port open!');
initializeJsonRpc().then(() => {
rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: promptPrefix,
completer: line => {
if (line.split(' ').length > 1) {
return [[], line];
}
const hits = jsonRpcMethods.filter(method => method.substr(0, line.length) === line);
return [ hits.length ? hits : jsonRpcMethods, line ];
},
terminal: true,
});
rl.on('line', line => {
let [ method, params ] = line.split(' ');
if (!method) {
process.stdout.write(promptPrefix);
// Ignore.
return;
}
try { params = JSON.parse(params || '[]'); } catch {
console.error('ERROR: Invalid params: JSON expected');
}
let timeout = 500;
if (slowRpcMethods.includes(method)) {
timeout = 20000;
}
rl.pause();
client.cmd(method, params, { timeout })
.then(result => {
if (method === 'getlogs') {
return fs.writeFile(logsFilePath, result).then(() => {
console.log('Logs written to bleskomat.log file in current working directory');
});
} else {
console.log(result);
}
})
.catch(console.error)
.finally(() => {
rl.resume();
process.stdout.write(promptPrefix);
});
});
process.stdout.write(promptPrefix);
}).catch(error => console.error);
});
port.on('error', error => {
if (/no such file or directory/i.test(error.message)) {
console.error('ERROR: USB device not found');
} else if (/permission denied/i.test(error.message)) {
console.error(`ERROR: User lacks necessary permissions to access USB device. Try \`sudo chown $USER ${options.devicePath}\` to set owner.`);
} else {
console.error(error);
}
});
let client = new JsonRpcClient(port);
client.parser.on('data', data => {
if (rl) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
}
console.log('BLESKOMAT: $', data.toString());
if (rl) {
process.stdout.write(promptPrefix);
rl.line && process.stdout.write(rl.line);
}
});
const initializeJsonRpc = () => {
return Promise.resolve().then(() => {
return new Promise((resolve, reject) => {
console.log('Initializing JSON-RPC...');
const done = error => {
if (error) return reject(error);
resolve();
};
let attempt = 0;
const tryEcho = () => {
attempt++;
client.cmd('echo', ['Ahoj!']).then(() => {
console.log('JSON-RPC interface is ready!');
done();
}).catch(error => {
if (/timed-out/i.test(error.message)) {
// Ignore timed-out error. Wait and then try again.
if (attempt === 2) {
console.log('Press RST/EN button on device to reboot it.');
}
setTimeout(tryEcho, 500);
} else {
return done(error);
}
});
}
tryEcho();
});
});
};
console.log(`Connecting to device at ${options.devicePath}...`);
port.open();
});
program.parse(process.argv);