-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (75 loc) · 2 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
const SerialPort = require('serialport')
const Readline = SerialPort.parsers.Readline
const port = new SerialPort('/dev/ttyACM0')
const parser = new Readline()
const readline = require('readline');
const express = require('express')
const app = express()
var path = require('path');
const sleep = require('util').promisify(setTimeout)
var lastData = "";
var lastSensorData = [];
var avgValue = 0;
port.pipe(parser)
parser.on('data', function(data){
data = data.substring(0, data.length - 1);
lastData = data;
})
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.get('/updateSensor', (req, res) => {
if (req.query['sensor'] && req.query['status']) {
port.write("1" + req.query['sensor']+ req.query['status'] + "\n");
// res.send({"msg":"sensor updated"});
} else {
// res.send({"msg":"Invalid Parameters"});
}
res.redirect('/');
})
app.get('/requestData', (req, res) => {
port.write("2\n");
setTimeout(function() {
lastSensorData = JSON.parse(lastData);
res.send(lastSensorData);
}, 1000);
})
app.get('/sendData', (req, res) => {
sendData();
res.send("data has been sended");
})
function sendData() {
var values = 0;
var count = 0;
for (var index in lastSensorData) {
if (lastSensorData[index]["sensor"].value){
values += lastSensorData[index]["sensor"].value;
count += 1;
}
}
if (isNaN(values))
values = 0;
if (isNaN(count))
count = 0;
avgValue = (values/count).toFixed(2);
port.write("3" + avgValue + "\n");
}
app.get('/', function (req, res) {
port.write("2\n");
setTimeout(function() {
lastSensorData = JSON.parse(lastData);
lastSensorData.sort(function(a, b) {
if (a.sensor['value']) {
return false;
}
return true;
});
sendData();
res.render('index',{data: lastSensorData, avg: avgValue});
}, 1000);
});
app.listen(3000, () => console.log('Agrowth app listening on port 3000!'))
var minutes = 1, the_interval = minutes * 60 * 1000;
setInterval(function() {
sendData();
}, the_interval);