mirror of
https://github.com/jwetzell/aas-js.git
synced 2026-09-01 18:29:04 +00:00
add websocket flag to cli
This commit is contained in:
@@ -4,6 +4,7 @@ const { SerialPort } = require('serialport');
|
|||||||
const { Console, Packets } = require('aas-lib');
|
const { Console, Packets } = require('aas-lib');
|
||||||
const { writeFileSync } = require('fs');
|
const { writeFileSync } = require('fs');
|
||||||
const { program, Option } = require('commander');
|
const { program, Option } = require('commander');
|
||||||
|
const { WebSocketServer } = require('ws');
|
||||||
|
|
||||||
const packageInfo = require('./package.json');
|
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('-d, --device <serial port path>', 'serialport path');
|
||||||
program.option('-o, --output <output file>', 'file to write console info to');
|
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.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);
|
program.parse(process.argv);
|
||||||
|
|
||||||
const options = program.opts();
|
const options = program.opts();
|
||||||
@@ -21,12 +23,25 @@ if (!options.device) {
|
|||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let ws;
|
||||||
|
if (options.websocket) {
|
||||||
|
ws = new WebSocketServer({ port: options.websocket });
|
||||||
|
}
|
||||||
|
|
||||||
const aasConsole = new Console();
|
const aasConsole = new Console();
|
||||||
const port = new SerialPort({
|
const port = new SerialPort({
|
||||||
path: options.device,
|
path: options.device,
|
||||||
baudRate: 76800,
|
baudRate: 76800,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function sendToWSClients(payload) {
|
||||||
|
if (ws && ws.clients) {
|
||||||
|
ws.clients.forEach((socket) => {
|
||||||
|
socket.send(JSON.stringify(payload));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
port.on('error', (error) => {
|
port.on('error', (error) => {
|
||||||
console.error('app: problem with serial port');
|
console.error('app: problem with serial port');
|
||||||
console.error(error.message);
|
console.error(error.message);
|
||||||
@@ -38,6 +53,12 @@ port.on('data', (data) => {
|
|||||||
try {
|
try {
|
||||||
aasConsole.update(data);
|
aasConsole.update(data);
|
||||||
console.log('app: console data updated');
|
console.log('app: console data updated');
|
||||||
|
if (ws) {
|
||||||
|
sendToWSClients({
|
||||||
|
eventName: 'update',
|
||||||
|
data: aasConsole.toJSON(),
|
||||||
|
});
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('app: error updating console state');
|
console.error('app: error updating console state');
|
||||||
}
|
}
|
||||||
@@ -59,12 +80,23 @@ const initializeInterval = setInterval(function initialize() {
|
|||||||
} else if (port) {
|
} else if (port) {
|
||||||
console.log('app: requesting console data to start streaming');
|
console.log('app: requesting console data to start streaming');
|
||||||
port.write(Packets.START_LIVE);
|
port.write(Packets.START_LIVE);
|
||||||
|
if (ws) {
|
||||||
|
sendToWSClients({ eventName: 'initializing' });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
|
||||||
process.on('SIGINT', () => {
|
process.on('SIGINT', () => {
|
||||||
console.log('app: shutting down');
|
console.log('app: shutting down');
|
||||||
clearInterval(initializeInterval);
|
clearInterval(initializeInterval);
|
||||||
|
if (ws) {
|
||||||
|
if (ws.clients) {
|
||||||
|
ws.clients.forEach((socket) => {
|
||||||
|
socket.close();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
ws.close();
|
||||||
|
}
|
||||||
port.write(Packets.STOP_LIVE);
|
port.write(Packets.STOP_LIVE);
|
||||||
port.close();
|
port.close();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -15,7 +15,8 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"aas-lib": "^0.1.0",
|
"aas-lib": "^0.1.0",
|
||||||
"commander": "^11.1.0",
|
"commander": "^11.1.0",
|
||||||
"serialport": "^12.0.0"
|
"serialport": "^12.0.0",
|
||||||
|
"ws": "^8.14.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"pkg": "^5.8.1",
|
"pkg": "^5.8.1",
|
||||||
|
|||||||
Reference in New Issue
Block a user