-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserialserver.js
executable file
·216 lines (192 loc) · 7.24 KB
/
serialserver.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
The Vent-display project is a ventilator display that consumes PIRDS data and
performs most clinical respiration calculations. This is an important part of
Public Invention's goal of creating an open-source ventilator ecosystem. This
is a stand-alone .html file with about a thousand lines of JavaScript that
implements a clinical display that doctors want to see of an operating
ventilator. It includes live data trace plots of pressure and flow, as well as
calculated values such as tidal volume.
Copyright (C) 2021 Robert Read, Lauria Clarke, Ben Coombs, and Darío Hereñú.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// #!/usr/bin/env node
var argv = require('yargs/yargs')(process.argv.slice(2))
.usage('Usage: $0 -sport [string, seraiport name] -uport [num,reporting port] -uaddress [string, like "127.0.0.1" or "ventmon.coslabs.com"]\nTo do no UDP reporting, leave off uport and uaddress.\nStandard uport is 6111, standard UDP is "ventmon.coslabs.com" or "127.0.0.1"')
.default('sport', "COM4")
.demandOption(['sport'])
.argv;
var express = require('express');
const cors = require('cors');
var app = express();
app.use(cors());
// NOTE: We could enumerate porst with "SerialPort.list()"...
// that would be a little friendlier for people who can't find
// the filename of their serial port!
const SerialPort = require('serialport'); //https://serialport.io/docs/guide-usage
const Readline = require('@serialport/parser-readline');
const sport_name = argv.sport;
const uport = argv.uport;
const uaddress = argv.uaddress;
const NO_UDP = ((uport == null) && (uaddress == null))
console.log("Parameters:");
console.log("argv.sport",argv.sport);
console.log("sport_name (Serial Port name)",sport_name);
console.log("uport (UDP port)",uport);
console.log("uaddress (UDP address)",uaddress);
if (NO_UDP) {
console.log("Becaue uport and uaddress both null, doing no UDP reporting!");
}
// VentOS uses 19200, but the VentMon uses 500000
const VentOSRate = 19200
const VentMon = 500000
const sport = new SerialPort(sport_name, { baudRate: VentMon });
const parser = sport.pipe(new Readline());// Read the port data
// Rob is adding the ability to send UDP datagrams to make this work with our datalake!
// Okay, this code basically works---but I might have to fill a byte buffer.
const dgram = require('dgram');
sport.on("open", () => {
console.log('serial port open');
});
// Open errors will be emitted as an error event
sport.on('error', function(err) {
console.log('Error: ', err.message)
})
// Note: I now believe we need to listen here for
// Acknowledgements and do something special with them to
// thottle the serial buffer.
parser.on('data', data =>{
// Let's see if the data is a PIRDS event...
// Note: The PIRDSlogger accepts JSON, but I'm not sure we ever implemented that
// being interpreted as a message. That possibly should be fixed, but I'm going to just
// construct a buffer here.
if (!NO_UDP) {
// This is an erroneous form that we have seen in crashes, but I don't know where it comes from.
// data = '{ "com" : "C" , "par" : "E" , "int" : "T" "A" , "val" : 150 }';
const message = new Buffer(data);
const client = dgram.createSocket('udp4');
// client.send(message, 0, message.length, 6111,"ventmon.coslabs.com", (err) => {
client.send(message, 0, message.length, uport, uaddress, (err) => {
if (err) {
console.log(err);
}
client.close();
});
}
// console.log(data);
});
// parser.on('data', console.log)
app.get('/', function(req, res) {
res.send('Hello world');
sport.write('hello world\n', (err) => {
if (err) {
return console.log('Error on write: ', err.message);
}
});
})
app.get('/api/set', function(req, res) {
var x = '';
if (req.query.rr){
x += '{"rr":"' + req.query.rr + '"}\n';
}
res.send(x);
sport.write(x, (err) => {
//console.log('wrote to port ' + x);
if (err) {
return console.log('Error on write: ', err.message);
}
});
});
// /api/pircs?com=C&par=P&int=T&mod=0&val=400
app.get('/api/pircs', function(req, res) {
var err = '';
var x = '{ ';
if (req.query.com){
x += '"com" : "' + req.query.com + '" , ';
} else {
err += "com not defined! ";
}
if (req.query.par){
x += '"par" : "' + req.query.par + '" , ';
} else {
err += "par not defined! ";
}
if (req.query.int){
x += '"int" : "' + req.query.int + '" , ';
} else {
err += "int not defined! ";
}
if (req.query.mod){
x += '"mod" : "' + req.query.mod + '" , ';
} else {
err += "mod not defined! ";
}
if (req.query.val){
x += '"val" : ' + req.query.val;
} else {
err += "val not defined! ";
}
x += ' }\n';
// I think what we want to do is await here until
// we have gotten an acknowledgement of the command.
// that requires us to add a specific listener to stream
// we are reading to check it. This should help overiding the buffer.
// A way around this is to just change the html to set one value
// at a time. In any case there is a danger of a buffer overruns;
// this is very clear seen to be happening in the VentOS code.
if (err.length > 0){
err += "\n"
res.setHeader("Content-Type", "text/plain");
res.setHeader('Access-Control-Allow-Origin', '*');
res.status(400).send(err);
} else {
res.setHeader("Content-Type", "application/json");
res.setHeader('Access-Control-Allow-Origin', '*');
res.status(200).send(x);
// The documentaiton supports this:
// function writeAndDrain (data, callback) {
// port.write(data)
// port.drain(callback)
// }
// Howewver, we are fundamentally asynchronous in processing
// requests, so it is unclear how this would work!
// Possibly we could call drain first!
sport.drain( () =>
sport.write(x, (err) => {
if (err) {
return console.log('Error on write: ', err.message);
}
}));
}
});
// /api/pircs2/C/P/T/0/400
app.get('/api/pircs2/:com/:par/:int/:mod/:val', function(req,res) {
res.send(req.params);
sport.write(JSON.stringify(req.params)+'\n', (err) => {
if (err) {
return console.log('Error on write: ', err.message);
}
});
// JSON.stringify returns: {"com":"C","par":"P","int":"T","mod":"0","val":"400"}
})
/*app.get('/rr/:rr', function(req,res) {
res.send(req.params);
port.write(JSON.stringify(req.params)+'\n', (err) => {
if (err) {
return console.log('Error on write: ', err.message);
}
});
})*/
var server = app.listen(5000, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Node.js server running on port %s", port);
})