add websocket flag to cli

This commit is contained in:
2023-11-24 13:19:56 -06:00
parent 1b9bffa140
commit 36404cb0d5
2 changed files with 34 additions and 1 deletions
+32
View File
@@ -4,6 +4,7 @@ const { SerialPort } = require('serialport');
const { Console, Packets } = require('aas-lib');
const { writeFileSync } = require('fs');
const { program, Option } = require('commander');
const { WebSocketServer } = require('ws');
const packageInfo = require('./package.json');
@@ -13,6 +14,7 @@ program.description('Simple protocol router /s');
program.option('-d, --device <serial port path>', 'serialport path');
program.option('-o, --output <output file>', 'file to write console info to');
program.addOption(new Option('-f, --format <output format>').choices(['json']).default('json'));
program.option('--websocket <port>', 'enable websocket server for updates', undefined);
program.parse(process.argv);
const options = program.opts();
@@ -21,12 +23,25 @@ if (!options.device) {
process.exit(1);
}
let ws;
if (options.websocket) {
ws = new WebSocketServer({ port: options.websocket });
}
const aasConsole = new Console();
const port = new SerialPort({
path: options.device,
baudRate: 76800,
});
function sendToWSClients(payload) {
if (ws && ws.clients) {
ws.clients.forEach((socket) => {
socket.send(JSON.stringify(payload));
});
}
}
port.on('error', (error) => {
console.error('app: problem with serial port');
console.error(error.message);
@@ -38,6 +53,12 @@ port.on('data', (data) => {
try {
aasConsole.update(data);
console.log('app: console data updated');
if (ws) {
sendToWSClients({
eventName: 'update',
data: aasConsole.toJSON(),
});
}
} catch (error) {
console.error('app: error updating console state');
}
@@ -59,12 +80,23 @@ const initializeInterval = setInterval(function initialize() {
} else if (port) {
console.log('app: requesting console data to start streaming');
port.write(Packets.START_LIVE);
if (ws) {
sendToWSClients({ eventName: 'initializing' });
}
}
}, 1000);
process.on('SIGINT', () => {
console.log('app: shutting down');
clearInterval(initializeInterval);
if (ws) {
if (ws.clients) {
ws.clients.forEach((socket) => {
socket.close();
});
}
ws.close();
}
port.write(Packets.STOP_LIVE);
port.close();
});
+2 -1
View File
@@ -15,7 +15,8 @@
"dependencies": {
"aas-lib": "^0.1.0",
"commander": "^11.1.0",
"serialport": "^12.0.0"
"serialport": "^12.0.0",
"ws": "^8.14.2"
},
"devDependencies": {
"pkg": "^5.8.1",