-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.ts
More file actions
executable file
·134 lines (128 loc) · 3.76 KB
/
Copy pathcli.ts
File metadata and controls
executable file
·134 lines (128 loc) · 3.76 KB
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
#!/usr/bin/env node
import debugLib from 'debug';
import { program } from 'commander';
import { createSerialCommunicator } from './lib';
import { createTransport } from './lib/createTransport';
const delayMS = (ms: number) =>
new Promise((res) => setTimeout(res, ms)) as Promise<void>;
program
.command('run <command>')
.description(
'run the given <command> through the serial line and returns result'
)
.option(
'-p, --port-name [PORT_NAME]',
'PORT_NAME is the serial port to communicate with',
'/dev/ttyUSB0'
)
.option(
'-b, --baudrate [BAUD_RATE]',
'BAUD_RATE is the serial baud rate',
'115200'
)
.option(
'-d, --debug-enabled',
'when set, outputs debug message to the console'
)
.option('--prompt [PROMPT]', 'PROMPT is the serial console prompt', '/ #')
.option(
'-t, --timeout [TIMEOUT]',
'TIMEOUT in ms to be used for lon grun commands such as "du -h ."',
'3000'
)
.option('--dont-wait-answer', 'execute command and do not wait answer')
.option(
'-l, --line-separator [LINE_SEPARATOR]',
'LINE_SEPARATOR is the serial console line separator',
'\n'
)
.action((command, options) => {
const debug = Object.assign(debugLib('cli'), {
enabled: options.debugEnabled,
});
const communicator = createSerialCommunicator({
debugEnabled: options.debugEnabled,
baudrate: Number(options.baudrate),
prompt: options.prompt,
lineSeparator: options.lineSeparator,
});
communicator
.connect(options.portName)
.then(() =>
communicator.executeCmd(command, {
timeout: Number(options.timeout),
waitAnswer: !options.dontWaitAnswer,
})
)
.then((result) => {
const { output, errorCode } = result;
console.log(output.join(options.lineSeparator));
debug(`ran command: '${command}'`);
debug(`error code: ${errorCode}`);
process.exit(errorCode);
})
.catch((e) => {
console.error(e);
process.exit(1);
});
});
program
.command('write-bytes <bytes>')
.description('write the given bytes to serial line')
.option(
'-p, --port-name [PORT_NAME]',
'PORT_NAME is the serial port to communicate with',
'/dev/ttyUSB0'
)
.option(
'-b, --baudrate [BAUD_RATE]',
'BAUD_RATE is the serial baud rate',
'115200'
)
.option(
'--delay-between-bytes-ms [DELAY_BETWEEN_BYTES_MS]',
'DELAY_BETWEEN_BYTES_MS is the delay between bytes in milliseconds',
'0'
)
.option(
'-d, --debug-enabled',
'when set, outputs debug message to the console'
)
.option('--append-line-feed', 'when set, append line feed to sent bytes')
.action(async (bytes: string, options) => {
const {
debugEnabled,
portName,
baudrate,
delayBetweenBytesMs,
appendLineFeed,
} = options;
const interBytesDelayMs = Number(delayBetweenBytesMs);
const debug = debugLib('cli');
debug.enabled = debugEnabled;
const serial = createTransport({
debugEnabled,
baudRate: Number(baudrate),
});
const received: Buffer[] = [];
serial.data$.subscribe((d) => received.push(d));
debug(`connecting to ${portName} at ${baudrate}`);
await serial.connect(portName);
if (interBytesDelayMs) {
debug(`writing ${bytes.length} bytes, one every ${interBytesDelayMs} ms`);
for (const b of bytes.split('')) {
await serial.write(b);
await delayMS(interBytesDelayMs);
}
} else {
debug(`writing ${bytes.length} bytes in a raw`);
await serial.write(bytes);
}
if (appendLineFeed) {
await serial.write('\n');
}
await delayMS(1000);
console.log('received:', Buffer.concat(received).toString());
process.exit(0);
});
program.parse(process.argv);