This repository has been archived by the owner on Jun 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
68 lines (56 loc) · 2.17 KB
/
server.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
'use strict'
const express = require('express');
const cors = require('cors');
const mysql = require('mysql');
const moment = require('moment');
const settings = require('./settings');
const port = process.env.PORT || 8080;
const app = express().use(cors());
app.get('/api', (req, res) => {
const connection = mysql.createConnection(settings.connectionSettings);
connection.connect((err) => {
if (err) {
res.status(500).json({status: err})
} else {
connection.query(settings.gatherAllQuery, (error, results) => {
if (error) res.status(500).json({ status: error });
else {
let temperatures = [];
let particles = [];
const response = {};
results.forEach(element => {
if (element.TypeSet === 'Particle') particles.push(element);
else if (element.TypeSet === 'Temperature') temperatures.push(element);
})
particles.forEach(element => {
// Particles has combined DateTime stamps, therefore we do not need to parse it with MomentJS
const date = element.Recorded.split(' ')[0];
const time = element.Recorded.split(' ')[1];
if (!response[date]) response[date] = { particles: [] }
response[date].particles.push({
time: time,
pm25: +element.Measure1,
pm10: +element.Measure2
});
});
temperatures.forEach(element => {
const date = moment(element.Recorded, 'YYYY-MM-DD').format('DD.MM.YYYY');
if (!response[date]) response[date] = { sensors: [] }
if (!response[date].sensors) response[date].sensors = [];
response[date].sensors.push({
time: element.Tijd,
temperatureLow: +element.Measure1,
temperatureHigh: +element.Measure2,
pressure: +element.press,
lightLevel: +element.light,
humidity: +element.humi
});
});
res.status(200).json({ response });
}
});
}
});
})
app.use(express.static('dist/climate'))
app.listen(port, () => console.log(`Started on :${port}`))