Skip to content

Commit 82bf156

Browse files
committed
Initial commit
0 parents  commit 82bf156

File tree

5 files changed

+671
-0
lines changed

5 files changed

+671
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

index.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const WebSocket = require('ws');
2+
const SerialPort = require('serialport');
3+
const Readline = require('@serialport/parser-readline');
4+
5+
const wss = new WebSocket.Server({ port: 8000, clientTracking: true });
6+
7+
function broadcast(message) {
8+
for (const client of wss.clients) {
9+
if (client.readyState == WebSocket.OPEN) {
10+
client.send(message);
11+
}
12+
}
13+
}
14+
15+
const port = new SerialPort(process.argv[2]),
16+
parser = port.pipe(new Readline());
17+
18+
port.on('open', () => {
19+
broadcast(JSON.stringify({ type: 'status', status: 'open' }));
20+
console.log('open');
21+
});
22+
23+
port.on('close', err => {
24+
broadcast(JSON.stringify({ type: 'status', status: 'closed' }));
25+
broadcast(JSON.stringify({ type: 'error', error: err.toString() }));
26+
console.log(`closed: ${err.toString()}`);
27+
setTimeout(() => port.open(), 1000);
28+
});
29+
30+
port.on('error', err => {
31+
broadcast(JSON.stringify({ type: 'error', error: err.toString() }));
32+
console.log(`serialport error: ${err.toString()}`);
33+
if (!port.isOpen) {
34+
setTimeout(() => port.open(), 1000);
35+
}
36+
});
37+
38+
parser.on('data', line => {
39+
let message;
40+
41+
try {
42+
const data = JSON.parse(line);
43+
message = JSON.stringify({ type: 'json', data });
44+
} catch (e) {
45+
message = JSON.stringify({ type: 'text', data: line });
46+
}
47+
48+
broadcast(message);
49+
});
50+
51+
wss.on('connection', client => {
52+
client.send(JSON.stringify({ type: 'status', status: port.isOpen ? 'open' : 'closed' }));
53+
});

mock-server.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const WebSocket = require('ws');
2+
const readline = require('readline');
3+
4+
let connected = false;
5+
6+
const wss = new WebSocket.Server({ port: 8000, clientTracking: true });
7+
8+
function broadcast(message) {
9+
for (const client of wss.clients) {
10+
if (client.readyState == WebSocket.OPEN) {
11+
client.send(message);
12+
}
13+
}
14+
}
15+
16+
function sendData() {
17+
setTimeout(sendData, 500);
18+
if (!connected) return;
19+
20+
broadcast(JSON.stringify({ type: 'json', data: { altitude: Math.random() * 100 }}));
21+
broadcast(JSON.stringify({
22+
type: 'json',
23+
data: {
24+
position: {
25+
latDeg: Math.floor(Math.random() * 360 - 180),
26+
longDeg: Math.floor(Math.random() * 360 - 180),
27+
latMin: Math.random() * 60,
28+
longMin: Math.random() * 60,
29+
satellites: Math.floor(Math.random() * 3 + 5),
30+
},
31+
},
32+
}));
33+
broadcast(JSON.stringify({ type: 'json', data: { airspeed: Math.random() * 50 }}));
34+
// still need orientation...that may end up in a different format
35+
}
36+
37+
wss.on('connection', client => {
38+
client.send(JSON.stringify({ type: 'status', status: connected ? 'open' : 'closed' }));
39+
});
40+
41+
console.log('Listening on port 8000. Press enter to toggle whether port is connected.');
42+
process.stdout.write('Port is disconnected.');
43+
44+
const rl = readline.createInterface({
45+
input: process.stdin,
46+
output: process.stdout,
47+
terminal: false,
48+
});
49+
50+
rl.on('line', line => {
51+
connected = !connected;
52+
process.stdout.write(`Port is ${connected ? 'connected' : 'disconnected'}.`);
53+
broadcast(JSON.stringify({ type: 'status', status: connected ? 'open' : 'closed' }));
54+
});
55+
56+
sendData();

package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "serial-proxy",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"@serialport/parser-readline": "^8.0.6",
8+
"serialport": "^8.0.7",
9+
"ws": "^7.2.3"
10+
}
11+
}

0 commit comments

Comments
 (0)