-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
368 lines (337 loc) · 11.5 KB
/
Copy pathindex.js
File metadata and controls
368 lines (337 loc) · 11.5 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"use strict";
require('dotenv').config()
const Hapi = require("@hapi/hapi");
const https = require('https');
const { Pool } = require('pg')
const {registerDevice, sendDeviceDoesNotExist, sendDeviceRegistrationSuccess } = require('./registerDevice');
const io = require('./io')
const server = Hapi.server({
port: process.env.PORT || 3001,
host: "0.0.0.0"
});
io.initialize(server);
// Credentials come from env. variable
const pool = new Pool({
ssl: {
rejectUnauthorized: true
}
});
const init = async () => {
let data = [];
server.route({
method: "GET",
path: "/",
handler: (request, h) => {
return "api";
}
});
server.route({
method: "GET",
path: "/addresses",
handler: (request, reply) => {
return ({ addresses: data.map(({ address }) => address) });
}
});
server.route({
method: "POST",
path: "/api/telemetry",
handler: async (request, h) => {
const payload = typeof request.payload === 'object' ? request.payload : JSON.parse(request.payload);
const { days, hours, minutes, seconds } = payload.interval;
// start should be in the ISO 8601 format. for eg. 2011-10-10T14:48:00
// TODO : Validation for start and maybe other
try {
const sqlRes = await pool.query("SELECT count(*) FROM TELEMETRY WHERE time > NOW() - interval '$1 days $2 hours $3 minutes $4 seconds';", [days, hours, minutes, seconds]); // TODO figure out parameters
return h.response(sqlRes).code(200);
} catch (ex) {
console.log(ex);
return h.response().code(400);
}
}
});
server.route({
method: "POST",
path: "/webhook/telemetry",
handler: async (request, h) => {
console.log("POST: /webhook/telemetry");
const base64enc = request.payload.data.body;
const utf8enc = (new Buffer.from(base64enc, 'base64')).toString('utf8');
const hookData = JSON.parse(utf8enc);
const {time, address, temperature, humidity, pressure, txpower, rssi, voltage} = hookData;
const data = {};
data[address] = {
telemetry: hookData,
level: request.payload.data.properties.level,
};
io.sendDeviceData(address, data[address]);
try {
await pool.query('\
INSERT INTO telemetry(time, iot_device_id, temperature, humidity, pressure, txpower, rssi, voltage) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)', [time, address, temperature, humidity, pressure, txpower, rssi, voltage]);
return h.response().code(200);
} catch (ex) {
console.log("ERROR inserting telemetry data:", ex);
return h.response().code(400);
}
}
});
server.route({
method: "POST",
path: "/webhook/device-registration",
handler: async (request, h) => {
console.log("POST: /webhook/device-registration");
const reqPayload = (process.env.EXEC_ENV === 'azure') ? request.payload : JSON.parse(request.payload);
const base64enc = reqPayload.data.body;
const utf8enc = (new Buffer.from(base64enc, 'base64')).toString('utf8');
const data = JSON.parse(utf8enc);
const addressAvailableSql = await pool.query('\
SELECT 1 FROM iot_devices i WHERE i.id=$1 AND i.edge_device_id IS NULL AND EXISTS(SELECT e.id FROM edge_devices e WHERE e.id=$2);',
[data.address, data.edgeDeviceId]);
if(addressAvailableSql.rows.length === 1) {
registerDevice(data.address, data.edgeDeviceId).then(async value => {
console.log('After registering the device with the callback, the returned values is ', value);
console.log('updating the iot_device in the db');
if(value.wasSuccessful) {
await pool.query('UPDATE iot_devices SET edge_device_id = $1 WHERE id = $2', [value.edgeDeviceId, value.registrationId]);
}
});
} else {
const idsMatch = await pool.query('SELECT 1 FROM iot_devices i WHERE i.id=$1 and i.edge_device_id=$2', [data.address, data.edgeDeviceId]);
if(idsMatch.rows.length === 1) {
sendDeviceRegistrationSuccess(data.address, data.edgeDeviceId);
} else {
sendDeviceDoesNotExist(data.address, data.edgeDeviceId);
}
}
return h.response().code(200);
}
});
server.route({
method: "GET",
path: "/api/devices",
config: {
cors: {
"origin": ['*']
}
},
handler: async (request, h) => {
let data;
await pool.query('Select id, address, edge_device_id from iot_devices;')
.then(res => {
//response = h.response(res.rows);
data = res.rows;
})
.catch(err => {
//response.data()
});
const response = h.response(data);
response.type("application/json");
return response;
}
});
server.route({
method: "POST",
path: "/api/devices",
config: {
cors: {
"origin": ['*']
}
},
handler: async (request, h) => {
const {data} = request.payload;
const response = h.response();
try {
await pool.query("INSERT INTO iot_devices(id) VALUES($1)", [data]);
}
catch(ex) {
console.log(ex);
if(ex.detail && ex.detail.includes("already exists")) {
response.code(409);
} else {
response.code(400);
}
return response;
}
response.code(200);
return response;
}
});
server.route({
method: "PUT",
path: "/api/devices",
config: {
cors: {
"origin": ['*']
}
},
handler: async (request, h) => {
const {id} = request.payload;
const response = h.response();
try {
await pool.query("UPDATE iot_devices SET edge_device_id = null WHERE id=$1", [id]);
}
catch(ex) {
response.code(400);
return response;
}
response.code(200);
return response;
}
});
server.route({
method: "DELETE",
path: "/api/devices",
config: {
cors: {
"origin": ['*']
}
},
handler: async (request, h) => {
const {id} = request.payload;
const response = h.response();
try {
await pool.query("DELETE FROM iot_devices WHERE ID = $1", [id]);
}
catch(ex) {
console.log(ex);
response.code(400);
return response;
}
response.code(200);
return response;
}
});
server.route({
method: "POST",
path: "/webhook/device-update",
config: {
cors: {
"origin": ['*']
}
},
handler: async (request, h) => {
console.log("POST: /webhook/device-update");
const reqPayload = (process.env.EXEC_ENV === 'azure') ? request.payload : JSON.parse(request.payload);
const base64enc = reqPayload.data.body;
const utf8enc = (new Buffer.from(base64enc, 'base64')).toString('utf8');
const data = JSON.parse(utf8enc);
const {registrationId, edgeDeviceId} = data;
const response = h.response();
try {
await pool.query("UPDATE iot_devices SET edge_device_id = $1 WHERE id = $2", [edgeDeviceId, registrationId]);
}
catch(ex) {
console.log(ex);
response.code(400);
return response;
}
response.code(200);
return response;
}
});
server.route({
method: "GET",
path: "/api/edges",
config: {
cors: {
"origin": ['*']
}
},
handler: async (request, h) => {
const sqlres = await pool.query("Select id from edge_devices;");
const response = h.response(sqlres.rows);
response.code(200)
response.type("application/json");
return response;
}
});
server.route({
method: "POST",
path: "/api/edges",
config: {
cors: {
"origin": ['*']
}
},
handler: async (request, h) => {
const {data} = request.payload;
const response = h.response();
try {
await pool.query("INSERT INTO edge_devices(id) VALUES($1)", [data]);
}
catch(ex) {
console.log(ex);
if(ex.detail && ex.detail.includes("already exists")) {
response.code(409);
} else {
response.code(400);
}
return response;
}
response.code(200);
return response;
}
});
server.route({
method: "DELETE",
path: "/api/edges",
config: {
cors: {
"origin": ['*']
}
},
handler: async (request, h) => {
const {id} = request.payload;
const response = h.response();
try {
await pool.query("DELETE FROM edge_devices WHERE ID = $1", [id]);
}
catch(ex) {
console.log(ex);
response.code(400);
return response;
}
response.code(200);
return response;
}
});
// AUTOMATIC VALIDATION for all Azure IoT Hub Event Subscription endpoints
server.route({
method: "OPTIONS",
path: "/webhook/{path*}",
config: {
cors: {
"origin": ['*']
}
},
handler: (request, h) => {
if(request.headers["webhook-request-callback"]){
console.log("Trying to automatically validate endpoint...");
console.log(request.headers);
callValidationUrl(request.headers["webhook-request-callback"])
}
const response = h.response();
response.code(204);
return response;
}
});
await server.start();
console.log("Server running on %s", server.info.uri);
};
const callValidationUrl = (callBackUrl) => {
setTimeout(() => {
https.get(callBackUrl, (resp) => {
resp.on('data', () => {
console.log("Got response from validation url:", callBackUrl);
});
}).on("error", (err) => {
console.log("Error automatically validating the endpoint:", callBackUrl);
console.log(err.message);
});
}, 5000);
}
process.on("unhandledRejection", err => {
console.log(err);
process.exit(1);
});
init();