-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmonitor.mjs
executable file
·65 lines (53 loc) · 1.83 KB
/
monitor.mjs
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
#!/usr/bin/env node
/*
Copyright (c) 2017 Bryan Hughes <[email protected]>
This file is part of Raver Lights.
Raver Lights is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Raver Lights is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Raver Lights. If not, see <http://www.gnu.org/licenses/>.
*/
import { SerialPort } from 'serialport';
import inquirer from 'inquirer';
import { writeFileSync } from 'fs';
const portsInfo = (await SerialPort.list()).filter((portInfo) => !portInfo.path.startsWith('/dev/ttyS'));
let portPath;
if (portsInfo.length === 0) {
console.error('Could not find a serial port to listen on!');
} else if (portsInfo.length === 1) {
portPath = portsInfo[0].path;
} else {
portPath = (await inquirer.prompt({
type: 'list',
name: 'portPath',
message: 'Which port would you like to monitor?',
choices: portsInfo.map((port) => port.path)
})).portPath;
}
const serialport = new SerialPort({
path: portPath,
baudRate: 112500
});
serialport.on('error', (err) => {
console.log('Error: ', err.message);
});
const BUFFER_SIZE = 1000;
const buffer = [];
let timeout;
serialport.on('data', (data) => {
buffer.push(data.toString());
while (buffer.length > BUFFER_SIZE) {
buffer.shift();
}
clearTimeout(timeout);
timeout = setTimeout(() => {
console.log(`Controller stopped responding at ${new Date().toLocaleString()}`);
writeFileSync('log.txt', buffer.join(''))
}, 10000);
});