-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalServer.js
122 lines (101 loc) · 2.74 KB
/
localServer.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
import express from 'express';
import { createServer } from 'https';
import { readFileSync } from 'fs';
import { WebSocketServer } from 'ws';
//import conf from 'lemons.json'
import { readFile } from 'fs/promises';
const conf = JSON.parse(
await readFile(
new URL('./lemons.json', import.meta.url)
)
)['local'];
//console.log('conf',conf)
/**
* MiniDexedLemons localServer.js
* @TODO Net Midi && ble? jzz?
* @TODO move ssl gen to node and automate? https://github.com/digitalbazaar/forge
*/
/**
* UART
*/
import { SerialPort, ReadlineParser } from 'serialport';
/**
* Called on UART RX
* @param {*} dat
*/
function RX_UART(dat) {
if (conf.dev) console.log('--> RX_UART', dat)
// Send it to Websocket
//ws.send(dat)
}
if (conf.uart_midi) {
const serial = new SerialPort({ path: conf.uart_port, baudRate: conf.uart_rate })
//const { SerialPort, ReadlineParser } = require('serialport') //DelimiterParser,
//const { DelimiterParser } = require('@serialport/parser-delimiter')
//const parser = serial.pipe(new DelimiterParser({ delimiter: 0xF7 }))
const parser = new ReadlineParser()
serial.pipe(parser)
//parser.on('--> RX_UART', console.log)
parser.on('--> RX_UART', RX_UART)
}
/**
* Write to UART
* @param {*} dat
*/
function TX_UART(dat) {
if (conf.dev) console.log('<-- TX_UART', dat)
if (conf.uart_midi) serial.write(dat)
}
/**
* Static web server and web socket
*/
const app = express()
app.use(express.static('www'))
app.use('/www/assets', express.static('assets'))
/*
app.get('*.js', function(req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
res.set('Content-Type', 'text/javascript');
next();
});
app.get('*.css', function(req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
res.set('Content-Type', 'text/css');
next();
});
app.get('*.html', function(req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
res.set('Content-Type', 'text/html');
next();
});
*/
const server = createServer({
cert: readFileSync('minidexed.crt'),
key: readFileSync('minidexed.key'),
//port: conf.port
}, app);
/**
* Called on web socket data
* @param {*} dat
*/
function RX_WS(dat) {
if (conf.dev) console.log('----> RX_WS', dat)
// Send it to UART
if(conf.tx_uart) TX_UART(dat)
}
const wss = new WebSocketServer({ server, path: '/wss' });
wss.on('connection', function connection(ws) {
ws.on('error', console.error);
ws.on('message', function message(data) {
// //console.log('received: %s', data);
RX_WS(data)
});
//ws.send('something');
});
server.listen(conf.www_port);
console.log(`--- MinidexedLemon ---`)
console.log(`https://localhost:${conf.www_port}`)
console.log(`Ctrl + c to exit..`)