-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·119 lines (97 loc) · 3.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env node
/*
Copyright (c) 2023-2024 Code Hive Tx, LLC
SPDX-License-Identifier: Apache-2.0
*/
const { spawn } = require('node:child_process');
const { SerialPort } = require('serialport')
const { name, version } = require('./package.json');
const { readFileSync } = require('node:fs');
const config = JSON.parse(readFileSync('./config.json', 'utf-8'));
const { baudRate, portPath, saltCmd, saltArgs } = config;
const port = new SerialPort({ path: portPath, baudRate })
// print a welcome message to the console
console.log(name, version, portPath, baudRate);
// TODO: localize!
function now() {
return new Date().toLocaleString("en");
}
// Greeting message.
// TODO: parameterize
const HELLOMSG = `${name} ${version}\n\r\n\r` + `Send a '*' to connect to SALT\n\r\n\r`;
// the spawned command
let salt = null;
// timeout till we say hello again
let timeout = null;
// Countdown until we say hello again if no text comes in
let cc = 100;
// initial hello of 5 seconds
timeout = setTimeout(() => sayHello(), 5000);
// greet the user
function sayhello() {
if (timeout) {
clearTimeout(timeout);
cc = 100;
}
port.write(now() + "\r\n" + HELLOMSG, function (err) {
if (err) {
return console.log('Error on write: ', err.message)
}
console.log('message written');
if (!salt) {
// greet the user again 15s later.
timeout = setTimeout(() => sayhello(), 15000);
}
});
}
// greet the user once. Then, on a timeout
sayhello();
// spinner while waiting for the sublaunched program to write something
let spin = 0;
let spinInterval = null;
// data handler
port.on('data', function (data) {
if (salt) {
clearTimeout(timeout);
timeout = null;
cc = 100;
// forward data
const str = data.toString('utf-8');
const str2 = str.replaceAll('\r', ''); // strip CR (TODO: configurable?)
salt.stdin.write(str2);
} else {
const str = data.toString('utf-8');
console.log('Data:', data.toString('utf-8'));
// If they type a '*' then connect.
if (str === '*') {
port.write('\r\nConnecting...');
spinInterval = setInterval(() => { port.write('\b' + (`/|\\-`[(++spin) % 4])) }, 500);
console.log('connect');
salt = spawn(saltCmd, saltArgs);
salt.stdout.on('data', (data) => port.write(data, (err) => {
clearInterval(spinInterval);
console.error(err);
}));
salt.stderr.on('data', (data) => port.write(data, (err) => {
clearInterval(spinInterval);
console.error(err);
}));
salt.on('close', (code) => {
port.write('\n\r\n\rDISCONNECTED\n\r\n\r', () => { });
console.error(`salt close with ${code}`);
salt = null;
sayhello();
});
} else {
// if we get enough garbage input, say hello again
cc--;
if (cc < 10) {
timeout = setTimeout(() => sayhello(), 15000);
}
}
}
});
// log errs to console
port.on('error', function (err) {
console.log('Error: ', err.message)
});