Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·164 lines (137 loc) · 4.81 KB
/
Copy pathindex.js
File metadata and controls
executable file
·164 lines (137 loc) · 4.81 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/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, saltName } = config;
const port = new SerialPort({ path: portPath, baudRate })
const pty = require('node-pty');
const os = require('node:os');
// if true, use node-pty to make a pty
const SALT_PTY = config?.saltPty;
// 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` + `Send a '*' to connect to SALT\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() + " - " + HELLOMSG, function (err) {
if (err) {
return console.log('Error on write: ', err.message)
}
console.log('hello 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?)
if (SALT_PTY) {
salt.write(str2);
} else {
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.indexOf('*') !== -1) {
port.write('\r\nConnecting...');
spinInterval = setInterval(() => { port.write('\b' + (`/|\\-`[(++spin) % 4])) }, 500);
console.log('connect', saltCmd, ...saltArgs);
if (SALT_PTY) {
salt = pty.spawn(saltCmd, saltArgs, {
name: saltName || 'ernst',
cols: 80,
rows: 5,
cwd: process.env.HOME,
env: process.env,
});
salt.onData((data) => {
console.log(data);
port.write(data, 'utf-8', (err) => {
clearInterval(spinInterval);
if(err) console.error(err);
});
});
salt.onExit(({exitCode, signal}) => {
port.write(`\n\r\n\rDISCONNECTED: ${exitCode}\n\r\n\r`, () => { });
console.error(`salt close with ${exitCode}: ${signal||''}`);
salt = null;
clearInterval(spinInterval);
sayhello();
});
} else {
salt = spawn(saltCmd, saltArgs);
salt.stdout.on('data', (data) => port.write(data, (err) => {
console.log(data.toString());
clearInterval(spinInterval);
if(err) console.error(err);
}));
salt.stderr.on('data', (data) => {
console.error(data.toString());
port.write(data, (err) => {
clearInterval(spinInterval);
if(err) 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;
clearInterval(spinInterval);
sayhello();
});
salt.on('error', (err) => {
console.error(err);
})
}
} 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)
});