mirror of
https://github.com/jwetzell/aas-js.git
synced 2026-08-19 03:54:03 +00:00
move apps to apps folder
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
*.json
|
||||
*.xml
|
||||
Executable
+148
@@ -0,0 +1,148 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const { SerialPort } = require('serialport');
|
||||
const { Console, Packets } = require('aas-lib');
|
||||
const { writeFileSync } = require('fs');
|
||||
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);
|
||||
program.version(packageInfo.version);
|
||||
program.description('AAS Serial Decoder');
|
||||
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', 'xml', 'yaml']).default('json'));
|
||||
program.option('--websocket <port>', 'enable websocket server for updates', undefined);
|
||||
program.parse(process.argv);
|
||||
|
||||
const options = program.opts();
|
||||
|
||||
let ws;
|
||||
let updateTimeout;
|
||||
let xmlBuilder;
|
||||
|
||||
function sendToWSClients(payload) {
|
||||
if (ws && ws.clients) {
|
||||
ws.clients.forEach((socket) => {
|
||||
socket.send(JSON.stringify(payload));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function setup(devicePath) {
|
||||
if (!devicePath) {
|
||||
console.log('app: device must not be undefined');
|
||||
return;
|
||||
}
|
||||
if (options.websocket) {
|
||||
ws = new WebSocketServer({ port: options.websocket });
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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) {
|
||||
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);
|
||||
if (ws) {
|
||||
sendToWSClients({ eventName: 'initializing' });
|
||||
}
|
||||
}
|
||||
}, 1000);
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
html/assets/css/tailwind.css
|
||||
@@ -0,0 +1,56 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link href="assets/css/tailwind.css" rel="stylesheet" />
|
||||
<title>AAS Console</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="absolute top-2 left-2 rounded-full hidden bg-green-300 w-3 h-3" id="status"></div>
|
||||
<div class="hidden w-full justify-around" id="scoreboard">
|
||||
<div class="flex flex-col items-center">
|
||||
<div>Home</div>
|
||||
<div id="home_score"></div>
|
||||
</div>
|
||||
<div id="clock"></div>
|
||||
<div class="flex flex-col items-center">
|
||||
<div>Guest</div>
|
||||
<div id="guest_score"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="initializing" class="w-screen h-screen flex justify-center items-center">
|
||||
<div>initializing</div>
|
||||
</div>
|
||||
<script>
|
||||
const socket = new WebSocket(`ws://localhost:8080`);
|
||||
let statusTimeout;
|
||||
|
||||
socket.onmessage = (message) => {
|
||||
const msgObj = JSON.parse(message.data);
|
||||
console.log(msgObj);
|
||||
if (msgObj.eventName === 'update') {
|
||||
document.getElementById('status').classList.remove('hidden');
|
||||
|
||||
if (statusTimeout) {
|
||||
console.log('cancelling existing timeout');
|
||||
clearTimeout(statusTimeout);
|
||||
}
|
||||
statusTimeout = setTimeout(() => {
|
||||
document.getElementById('status').classList.add('hidden');
|
||||
}, 100);
|
||||
document.getElementById('scoreboard').classList.remove('hidden');
|
||||
document.getElementById('scoreboard').classList.add('flex');
|
||||
document.getElementById('initializing').classList.add('hidden');
|
||||
|
||||
document.getElementById('clock').innerHTML = msgObj.data.time.display;
|
||||
document.getElementById('home_score').innerHTML = msgObj.data.home.score;
|
||||
document.getElementById('guest_score').innerHTML = msgObj.data.guest.score;
|
||||
} else if (msgObj.eventName === 'initializing') {
|
||||
document.getElementById('scoreboard').classList.add('hidden');
|
||||
document.getElementById('initializing').classList.remove('hidden');
|
||||
}
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,8 @@
|
||||
const express = require('express');
|
||||
const path = require('path');
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(express.static(path.join(__dirname, './html')));
|
||||
|
||||
app.listen(3000);
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "aas-webui",
|
||||
"version": "0.1.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"prestart": "tailwindcss -i tailwind.css -o ./html/assets/css/tailwind.css",
|
||||
"start": "node index.js",
|
||||
"dev:css": "tailwindcss -i tailwind.css -o ./html/assets/css/tailwind.css --watch"
|
||||
},
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"express": "^4.18.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tailwindcss": "^3.3.5"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/** @type {import('tailwindcss').Config} */
|
||||
module.exports = {
|
||||
content: ['./html/**/*.{html,js}'],
|
||||
theme: {
|
||||
extend: {},
|
||||
},
|
||||
plugins: [],
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
Reference in New Issue
Block a user