add xml format option

This commit is contained in:
2023-11-24 14:35:13 -06:00
parent b13e906f44
commit f9ef0682cf
3 changed files with 43 additions and 2 deletions
+27
View File
@@ -2387,6 +2387,27 @@
"integrity": "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==", "integrity": "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==",
"dev": true "dev": true
}, },
"node_modules/fast-xml-parser": {
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-4.3.2.tgz",
"integrity": "sha512-rmrXUXwbJedoXkStenj1kkljNF7ugn5ZjR9FJcwmCfcCbtOMDghPajbc+Tck6vE6F5XsDmx+Pr2le9fw8+pXBg==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/NaturalIntelligence"
},
{
"type": "paypal",
"url": "https://paypal.me/naturalintelligence"
}
],
"dependencies": {
"strnum": "^1.0.5"
},
"bin": {
"fxparser": "src/cli/cli.js"
}
},
"node_modules/fastq": { "node_modules/fastq": {
"version": "1.15.0", "version": "1.15.0",
"resolved": "https://registry.npmjs.org/fastq/-/fastq-1.15.0.tgz", "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.15.0.tgz",
@@ -5297,6 +5318,11 @@
"node": ">=0.10.0" "node": ">=0.10.0"
} }
}, },
"node_modules/strnum": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/strnum/-/strnum-1.0.5.tgz",
"integrity": "sha512-J8bbNyKKXl5qYcR36TIO8W3mVGVHrmmxsd5PAItGkmyzwJvybiw2IVq5nqd0i4LSNSkB/sx9VHllbfFdr9k1JA=="
},
"node_modules/sucrase": { "node_modules/sucrase": {
"version": "3.34.0", "version": "3.34.0",
"resolved": "https://registry.npmjs.org/sucrase/-/sucrase-3.34.0.tgz", "resolved": "https://registry.npmjs.org/sucrase/-/sucrase-3.34.0.tgz",
@@ -5977,6 +6003,7 @@
"dependencies": { "dependencies": {
"aas-lib": "^0.1.0", "aas-lib": "^0.1.0",
"commander": "^11.1.0", "commander": "^11.1.0",
"fast-xml-parser": "^4.3.2",
"serialport": "^12.0.0", "serialport": "^12.0.0",
"ws": "^8.14.2" "ws": "^8.14.2"
}, },
+15 -2
View File
@@ -5,6 +5,7 @@ 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 { WebSocketServer } = require('ws');
const { XMLBuilder } = require('fast-xml-parser');
const packageInfo = require('./package.json'); const packageInfo = require('./package.json');
@@ -13,7 +14,7 @@ program.version(packageInfo.version);
program.description('Simple protocol router /s'); 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', 'xml']).default('json'));
program.option('--websocket <port>', 'enable websocket server for updates', undefined); program.option('--websocket <port>', 'enable websocket server for updates', undefined);
program.parse(process.argv); program.parse(process.argv);
@@ -25,6 +26,9 @@ if (!options.device) {
let ws; let ws;
let updateTimeout; let updateTimeout;
let xmlBuilder;
if (options.websocket) { if (options.websocket) {
ws = new WebSocketServer({ port: options.websocket }); ws = new WebSocketServer({ port: options.websocket });
} }
@@ -72,8 +76,17 @@ port.on('data', (data) => {
} }
if (options.output && aasConsole.sportInitialized) { if (options.output && aasConsole.sportInitialized) {
try { try {
let outputString;
if (options.format === 'json') { if (options.format === 'json') {
writeFileSync(options.output, JSON.stringify(aasConsole, null, 2)); outputString = JSON.stringify(aasConsole, null, 2);
} else if (options.format === 'xml') {
if (!xmlBuilder) {
xmlBuilder = new XMLBuilder();
}
outputString = xmlBuilder.build({ console: aasConsole.toJSON() });
}
if (outputString) {
writeFileSync(options.output, outputString);
} }
} catch (error) { } catch (error) {
console.error(`app: problem writing to output file -> ${options.output}`); console.error(`app: problem writing to output file -> ${options.output}`);
+1
View File
@@ -15,6 +15,7 @@
"dependencies": { "dependencies": {
"aas-lib": "^0.1.0", "aas-lib": "^0.1.0",
"commander": "^11.1.0", "commander": "^11.1.0",
"fast-xml-parser": "^4.3.2",
"serialport": "^12.0.0", "serialport": "^12.0.0",
"ws": "^8.14.2" "ws": "^8.14.2"
}, },