This repository has been archived by the owner on Apr 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
175 lines (149 loc) · 5.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
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
const express = require('express')
const app = express()
const { createServer } = require('http')
const server = createServer(app)
const { Server } = require('socket.io')
const io = new Server(server)
const bodyParser = require('body-parser')
const { SerialPort } = require('serialport')
const { autoDetect } = require('@serialport/bindings-cpp')
require('dotenv').config()
const PORT_PATH = process.env.npm_config_port
const LIST = process.env.npm_config_list
const operationTypes = {
L: {
type: 'binary',
values: ['0', '1', 'x'],
keys: [
'leftHeadlight',
'leftFrontSignal',
'leftBacklight',
'leftRearSignal',
'leftRearBrake',
'rightHeadlight',
'rightFrontSignal',
'rightBacklight',
'rightRearSignal',
'rightRearBrake',
],
},
S: {
type: 'binary',
values: ['0', '1', 'x'],
keys: [
'running',
'leftDoor',
'leftSafetyBelt',
'leftDoorLock',
'rightDoor',
'rightSafetyBelt',
'rightDoorLock',
'trunk',
'wiper',
],
},
T: {
type: 'number',
keys: [
'speed',
'leftSpeed',
'rightSpeed',
'temperature',
'battery',
'compass',
],
},
}
const parseData = data => {
data = data.trim()
const opType = data[0]
let output = {}
if (!operationTypes[opType]) throw new Error(`Undefined operation type: ${opType}`)
else if (!data.endsWith(';')) throw new Error(`Data could not be fully retrieved`)
else {
const operation = operationTypes[opType]
data = data.slice(1, data.length - 1).trim()
if (operation.type === 'binary') {
data = data.split('')
} else if (operation.type === 'number') {
data = data.split(',')
} else throw new Error(`Undefined operation data type: ${operation.type}`)
if (data.length !== operation.keys.length)
throw new Error(`Pattern and input not matches. Searching for ${operation.keys.length}, given ${data.length}`)
operation.keys.map((v, k) => {
if (operation.type === 'binary') output[v] = data[k]
else if (operation.type === 'number') output[v] = data[k].trim() === 'x' ? 'x' : parseFloat(data[k])
})
}
return { key: opType, output }
}
let uiViewer = null
let dataSender = null
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static('public'))
if (LIST || PORT_PATH) {
(async () => {
if (LIST) {
const Binding = autoDetect()
console.log(await Binding.list())
} else {
const Binding = autoDetect()
const foundPort = (await Binding.list()).find((p) => p.path === PORT_PATH.trim())
if (!foundPort) throw new Error(`Port not found: ${PORT_PATH}`)
else {
const port = new SerialPort({
path: foundPort.path,
baudRate: 9600,
autoOpen: false,
})
port.open((e) => {
if (e) throw new Error(`An error occurred while opening port ${foundPort.path}: ${e.message}`)
})
port.on('open', () => {
console.log(`Port opened: ${foundPort.path}`)
})
port.on('data', (data) => {
data = String(data)
if (data && data.length) {
const { key: opType, output: parsedData } = parseData(data)
if (uiViewer) io.emit('update', opType, parsedData)
}
})
}
}
})()
}
app.post('/update', (req, res) => {
try {
const { key: opType, output: parsedData } = parseData(req.body.data)
if (uiViewer) io.emit('update', opType, parsedData)
res.send({ status: 1 })
} catch (e) {
console.error(`An error occurred while parsing data: ${e.message}`)
res.send({ status: 0, error: e.message })
}
})
app.get('/custom-ui', (req, res) => {
res.sendFile(`${__dirname}/views/custom-ui.html`)
})
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/views/index.html`)
})
io.on('connection', (socket) => {
console.log(`Socket connected: ${socket.id}`)
socket.emit('set operation types', operationTypes)
socket.on('assign', (type) => {
if (type === 'ui') uiViewer = socket.id
else if (type === 'sender') dataSender = socket.id
})
socket.on('update stats', (type, values) => {
if (!uiViewer) return
socket.to(uiViewer).emit('update', type, values)
})
socket.on('disconnect', () => {
console.log(`Socket disconnected: ${socket.id}`)
if (socket.id === uiViewer) uiViewer = null
else if (socket.id === dataSender) dataSender = null
})
})
server.listen(process.env.PORT, () => console.log(`Listening on port ${process.env.PORT}`))