-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodeforwarder.js
172 lines (129 loc) · 4.15 KB
/
nodeforwarder.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
/*
NodeForwader: an serial to http proxy driven by ghetto get calls
requirements
-- serialport -> npm install serialport
-- express -> npm install express
-- sleep -> npm install sleep
-- socket.io -> npm install socket.io
-- cors -> npm install cors
to start: node nodeforwader.js [HTTP PORT] [SERIAL PORT] [BAUD] [BUFFER LENGTH]
to read: http://[yourip]:[spec'd port]/read/ -> returns the last [BUFFER LENGTH] bytes from the serial port as a string
to write: http://[yourip]:[spec'd port]/write/[YOUR STRING HERE]
what will probably create farts/list of things to deal with later if I need to:
- returning characters that html has issues with
- spaces in the url
*/
parts = process.argv
if (parts.length < 6) {
console.log("usage: node nodeforwader.js [HTTP PORT] [SERIAL PORT] [BAUD] [BUFFER LENGTH] [LOG=YES optional]")
process.exit(1);
}
else {
console.log(parts);
hp = parts[2]
sp = parts[3]
baud = parseInt(parts[4])
blen = parseInt(parts[5])
}
var logfile = false;
if (parts.length == 7) logfile = true;
var bodyParser = require('body-parser');
var app = require('express')();
var fs = require('fs');
var http = require('http').Server(app);
var io = require('socket.io')(http);
var cors = require('cors')
const createCsvWriter = require('csv-writer').createArrayCsvWriter;
const csvWriter = createCsvWriter({
header: ['Time (seconds since epoch)', 'Pump A duty (0-255)', 'Pump B Duty (0-255)','Pump A rpm', 'Pump B rpm'],
path: 'C:\\Users\\monroegroup\\Nextcloud\\docs\\projects\\PhD\\data\\pumps\\speeds\\' + (new Date().getTime()/1000).toString() + '.csv'
});
http.listen(hp);
var SerialPort = require("serialport"); //per ak47 fix
var serialPort = new SerialPort(sp,
{
baudRate: baud
});
serialPort.on("open", function () {
console.log('open');
});
serialPort.on("close", function () {
console.log('closed, reopening');
var serialPort = new SerialPort(sp,
{
baudrate: baud
});
});
//sleep for 3 seconds for arduino serialport purposes
console.log('Sleeping three seconds for microcontroller...')
var waitTill = new Date(new Date().getTime() + 3000);
while(waitTill > new Date()){}
//On Data fill a circular buf of the specified length
buf = ""
//last heard
var lh = 0;
serialPort.on('data', function (data) {
data = data.toString('binary')
lh = new Date().getTime()
data = lh/1000 + ',' + data
records = data.replace('\r\n','').split(',')
records = [records] //needed for csvwriter
if (logfile) {
csvWriter.writeRecords(records).then(() => console.log(data));
}
buf += data.toString('binary')
if (buf.length > blen) buf = buf.substr(buf.length - blen, buf.length)
io.emit('data', data.toString('utf8'));
});
//Enable Cross Site Scripting
app.use(cors())
//Allows us to rip out data?
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//Write to serial port
app.get('/write/*', function (req, res) {
toSend = req.originalUrl.replace("/write/", "")
toSend = decodeURIComponent(toSend);
console.log(toSend)
serialPort.write(Buffer.from(toSend))
res.send(toSend)
});
app.get('/writecf/*', function (req, res) {
toSend = req.originalUrl.replace("/writecf/", "")
toSend = decodeURIComponent(toSend);
console.log(toSend)
serialPort.write(Buffer.from(toSend + "\r\n"))
res.send(toSend)
});
app.post('/write', function (req, res) {
x = req.body
toSend = x
console.log(toSend)
serialPort.write(Buffer.from(toSend['payload']))
res.send(toSend)
});
//Show Last Updated
app.get('/lastread/', function (req, res) {
lhs = lh.toString();
console.log(lhs)
res.send(lhs)
});
//read buffer
app.get('/read/', function (req, res) {
res.send(buf)
});
//weak interface
app.get('/', function (req, res) {
res.sendFile(__dirname + '/readout.html');
});
app.get('/readout/', function (req, res) {
res.sendFile(__dirname + '/readout.html');
});
//sockets
io.on('connection', function (socket) {
io.emit('data', buf)
socket.on('input', function (msg) {
//console.log('message: ' + msg);
serialPort.write(msg + "\r\n")
});
});