add selector if no device specified

This commit is contained in:
2023-11-24 16:01:39 -06:00
parent c9b0c0a663
commit 60de1632eb
3 changed files with 276 additions and 95 deletions
+104 -81
View File
@@ -7,6 +7,7 @@ const { program, Option } = require('commander');
const { WebSocketServer } = require('ws');
const { XMLBuilder } = require('fast-xml-parser');
const { stringify: yamlify } = require('yaml');
const Select = require('@inquirer/select');
const packageInfo = require('./package.json');
program.name(packageInfo.name);
@@ -19,26 +20,11 @@ program.option('--websocket <port>', 'enable websocket server for updates', unde
program.parse(process.argv);
const options = program.opts();
if (!options.device) {
console.error('app: no device specified');
process.exit(1);
}
let ws;
let updateTimeout;
let xmlBuilder;
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) => {
@@ -47,80 +33,117 @@ function sendToWSClients(payload) {
}
}
port.on('error', (error) => {
console.error('app: problem with serial port');
console.error(error.message);
});
function setup(devicePath) {
if (!devicePath) {
console.log('app: device must not be undefined');
return;
}
if (options.websocket) {
ws = new WebSocketServer({ port: options.websocket });
}
// Switches the port into "flowing mode"
port.on('data', (data) => {
if (aasConsole) {
try {
aasConsole.update(data);
console.log('app: console state update received');
if (updateTimeout) {
clearTimeout(updateTimeout);
}
updateTimeout = setTimeout(() => {
console.log('app: console update timed out re-initializing');
aasConsole.reset();
}, 1000);
if (ws) {
sendToWSClients({
eventName: 'update',
data: aasConsole.toJSON(),
});
}
} catch (error) {
console.error('app: error updating console state');
}
if (options.output && aasConsole.sportInitialized) {
const aasConsole = new Console();
const port = new SerialPort({
path: devicePath,
baudRate: 76800,
});
port.on('error', (error) => {
console.error('app: problem with serial port');
console.error(error.message);
});
// Switches the port into "flowing mode"
port.on('data', (data) => {
if (aasConsole) {
try {
let outputString;
if (options.format === 'json') {
outputString = JSON.stringify(aasConsole, null, 2);
} else if (options.format === 'xml') {
if (!xmlBuilder) {
xmlBuilder = new XMLBuilder();
}
outputString = xmlBuilder.build({ console: aasConsole.toJSON() });
} else if (options.format === 'yaml') {
outputString = yamlify(aasConsole.toJSON());
aasConsole.update(data);
console.log('app: console state update received');
if (updateTimeout) {
clearTimeout(updateTimeout);
}
if (outputString) {
writeFileSync(options.output, outputString);
updateTimeout = setTimeout(() => {
console.log('app: console update timed out re-initializing');
aasConsole.reset();
}, 1000);
if (ws) {
sendToWSClients({
eventName: 'update',
data: aasConsole.toJSON(),
});
}
} catch (error) {
console.error(`app: problem writing to output file -> ${options.output}`);
console.error('app: error updating console state');
}
if (options.output && aasConsole.sportInitialized) {
try {
let outputString;
if (options.format === 'json') {
outputString = JSON.stringify(aasConsole, null, 2);
} else if (options.format === 'xml') {
if (!xmlBuilder) {
xmlBuilder = new XMLBuilder();
}
outputString = xmlBuilder.build({ console: aasConsole.toJSON() });
} else if (options.format === 'yaml') {
outputString = yamlify(aasConsole.toJSON());
}
if (outputString) {
writeFileSync(options.output, outputString);
}
} catch (error) {
console.error(`app: problem writing to output file -> ${options.output}`);
}
}
}
}
});
});
const initializeInterval = setInterval(() => {
if (aasConsole.sportInitialized) {
console.log('app: console is already initialized');
} else if (port) {
console.log('app: requesting console data to start streaming');
port.write(Packets.START_LIVE);
const initializeInterval = setInterval(() => {
if (aasConsole.sportInitialized) {
console.log('app: console is already initialized');
} 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');
clearTimeout(updateTimeout);
clearInterval(initializeInterval);
if (ws) {
sendToWSClients({ eventName: 'initializing' });
if (ws.clients) {
ws.clients.forEach((socket) => {
socket.close();
});
}
ws.close();
}
}
}, 1000);
port.write(Packets.STOP_LIVE);
port.close();
});
}
process.on('SIGINT', () => {
console.log('app: shutting down');
clearTimeout(updateTimeout);
clearInterval(initializeInterval);
if (ws) {
if (ws.clients) {
ws.clients.forEach((socket) => {
socket.close();
});
}
ws.close();
}
port.write(Packets.STOP_LIVE);
port.close();
});
if (!options.device) {
console.error('app: no device specified');
SerialPort.list().then((ports) => {
console.log('app: available devices');
const choices = ports.map((port) => ({
name: port.path,
value: port.path,
}));
Select.default({
message: 'Select a device',
choices,
}).then((value) => {
if (value) {
setup(value);
}
});
});
} else {
setup(options.device);
}
+1
View File
@@ -13,6 +13,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@inquirer/select": "^1.3.1",
"aas-lib": "^0.1.0",
"commander": "^11.1.0",
"fast-xml-parser": "^4.3.2",