-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (90 loc) · 3.41 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
const express = require('express');
const admin = require('firebase-admin');
const bodyParser = require('body-parser');
const crypto = require('crypto');
const serviceAccount = require("./serviceAccountKey.json");
const firebaseApp = admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "{{DATABASE_URL}}",
});
const key = Buffer.from('{{JS_PASS}}', 'hex');
const app = express();
app.use(bodyParser.raw());
app.use(express.static('public'));
const port = process.env.PORT || 3000;
app.post('/push', (req, res) => {
const data = req.body;
const ciphertextFromArduino = data.slice(0, 16);
const signatureFromArduino = data.slice(16, 32);
const ivFromArduino = data.slice(32, 44); // Randoms sent on last response
const decipher = crypto.createDecipheriv('aes-128-gcm', key, ivFromArduino);
decipher.setAuthTag(signatureFromArduino);
decipher.setAutoPadding(false);
let partialPlaintext = decipher.update(ciphertextFromArduino);
let plaintext = Buffer.concat([partialPlaintext, decipher.final()]);
const db = admin.database();
const timestamp = new Date().toJSON();
db.ref().push().set({
date: timestamp,
moisture1: plaintext.readUInt16BE(0),
moisture2: plaintext.readUInt16BE(2),
moisture3: plaintext.readUInt16BE(4),
moisture4: plaintext.readUInt16BE(6),
temperature1: plaintext.readUInt16BE(8),
temperature2: plaintext.readUInt16BE(10),
light1: plaintext.readUInt16BE(12),
light2: plaintext.readUInt16BE(14),
});
const randomsForArduino = Buffer.alloc(12);
crypto.randomFillSync(randomsForArduino);
const ivForResponse = Buffer.alloc(12);
crypto.randomFillSync(ivForResponse);
const cipher = crypto.createCipheriv('aes-128-gcm', key, ivForResponse);
cipher.setAAD(randomsForArduino);
cipher.final();
const signatureOfResponse = cipher.getAuthTag();
res.send(Buffer.concat([
randomsForArduino, // 12 bytes
signatureOfResponse, // 16 bytes
ivForResponse, // 12 bytes
]));
});
app.get('/data', (req, res) => {
const currentTimestamp = Date.now();
const startTimestamp = currentTimestamp - 14 * 24 * 60 * 60 * 1000;
const startDate = new Date(startTimestamp).toJSON();
admin
.database()
.ref()
.orderByChild('date')
.startAt(startDate)
.once('value')
.then((snapshot) => {
data = snapshot.val();
const moisture1 = [];
const moisture2 = [];
const moisture3 = [];
const moisture4 = [];
const temperature1 = [];
const temperature2 = [];
const light1 = [];
const light2 = [];
Object.values(data).forEach(data => {
moisture1.push({ value: data.moisture1, date: data.date });
moisture2.push({ value: data.moisture2, date: data.date });
moisture3.push({ value: data.moisture3, date: data.date });
moisture4.push({ value: data.moisture4, date: data.date });
temperature1.push({ value: data.temperature1, date: data.date });
temperature2.push({ value: data.temperature2, date: data.date });
light1.push({ value: data.light1, date: data.date });
light2.push({ value: data.light2, date: data.date });
})
return res.json({
moisture1, moisture2, moisture3, moisture4,
temperature1, temperature2, light1, light2
});
}).catch((error) => {
console.error(error);
});
});
app.listen(port, () => console.log(`Plant-monitor listening on port ${port}!`));