-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_prototype.js
More file actions
208 lines (158 loc) · 4.79 KB
/
Copy pathapi_prototype.js
File metadata and controls
208 lines (158 loc) · 4.79 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
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
'use strict'
/* remember to run export NODE_ENV=production */
const express = require('express')
const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);
client.connect();
const db = client.db('canmsgs');
const API_KEY = ''
function checkAPIKey(req) {
if (req.headers['apikey'] === API_KEY)
return true;
return false;
}
function checkNulls(body, propertyList) {
for (var i = 0; i < propertyList.length; i++) {
var prop = propertyList[i];
if (body[prop] == null)
return false;
}
return true;
}
var app = express()
app.use(express.json({limit: '50mb', inflate: true}));
app.post('/frames', (req, res) => {
if ( ! checkAPIKey(req) ) {
res.status(403);
return res.send('Forbidden\n');
}
if ( ! checkNulls(req.body,
['vehicle_id', 'arbitration_id', 'data_len', 'data_string']) ) {
res.status(400);
return res.send('Missing\n');
}
var { vehicle_id, arbitration_id, data_len, data_string } = req.body;
var frame = {
vehicle_id,
arbitration_id,
data_len,
data_string,
time_received: new Date()
};
// Just use vehicle ID as collection name for now
const collection = db.collection("vehicle_" + String(frame['vehicle_id']));
collection.insertOne(frame);
res.status(200);
return res.send('ack\n');
});
app.post('/frames/bulk', (req, res) => {
if ( ! checkAPIKey(req) ) {
res.status(403);
return res.send('Forbidden\n');
}
if (req.body.length == undefined || req.body.length == 0) {
res.status(400);
return res.send('Invalid Format\n');
}
var frames = req.body;
var time_received = new Date();
var frame_list = [];
frames.forEach(f => {
if ( ! checkNulls(f,
['vehicle_id', 'arbitration_id', 'data_len', 'data_string']) ) {
res.status(400);
return res.send('Missing parameters\n');
}
var { vehicle_id, arbitration_id, data_len, data_string, time_recorded } = f;
var record_date = new Date(0);
record_date.setUTCSeconds(time_recorded);
var frame = {
vehicle_id,
arbitration_id,
data_len,
data_string,
time_recorded: record_date,
time_received: time_received
};
frame_list.push(frame);
});
try {
var vid = frame_list[0]['vehicle_id'];
const collection = db.collection("vehicle_" + String(vid));
collection.insertMany(frame_list);
} catch (e) {
console.log(e);
res.status(500);
return res.send('err\n');
}
res.status(200);
res.send('ack\n');
});
app.post('/compressedframes', (req, res) => {
if ( ! checkAPIKey(req) ) {
res.status(403);
return res.send('Forbidden\n');
}
if ( ! checkNulls(req.body,
['vehicle_id', 'frames']) ) {
res.status(400);
return res.send('Missing\n');
}
var frame_list = [];
var time_received = new Date();
var { vehicle_id, frames } = req.body;
if (frames.length == undefined || frames.length == 0) {
res.status(400);
return res.send('Invalid Format\n');
}
// frames: array of b64 encoded packed frames
/* packing:
u_short u_char u_longlong double
aid len data time
*/
frames.forEach(f => {
var buffer = Buffer.from(f, 'base64');
var arbitration_id = buffer.readUInt16LE(0);
var data_len = buffer.readUInt8(2);
var time_d = buffer.readDoubleLE(11);
var record_date = new Date(0);
record_date.setUTCSeconds(time_d);
var data_bytes = buffer.slice(3, 3 + data_len);
var hex = [];
data_bytes.forEach(byte => {
hex.push( byte.toString(16).toUpperCase().padStart(2, '0') );
});
var data_string = hex.join(' ');
var frame = {
vehicle_id,
arbitration_id,
data_len,
data_string,
time_recorded: record_date,
time_received: time_received
};
frame_list.push(frame);
});
try {
const collection = db.collection("vehicle_" + String(vehicle_id));
collection.insertMany(frame_list);
} catch (e) {
console.log(e);
res.status(500);
return res.send('err\n');
}
res.status(200);
return res.send('ack\n');
});
// Error middleware
app.use((err, req, res, next) => {
// Silence the stack trace - this is an expected behaviour
if (err.type === 'entity.too.large') {
res.status(413);
res.send('');
} else {
next(err);
}
});
app.listen(3000, 'localhost');