-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
71 lines (57 loc) · 2.14 KB
/
main.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
const express = require('express');
const bodyParser = require('body-parser');
const TemperatureSensor = require('./things/TemperatureSensor').TemperatureSensor;
const HumiditySensor = require('./things/HumiditySensor').HumiditySensor;
let http_port = process.env.PORT || 8888;
let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
let randomDelay = () => {
let getRandomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
return getRandomInt(1500, 4000);
}
// temporary: always authorized
let authorized = (req, res, next) => {
return next();
/*
if (req.headers['things-signature']==process.env.PASS_WORD) {
return next();
} else {
return res.sendStatus(401);
}
*/
}
// Generate n temperature sensors
let temperatureSensors = [...Array(100).keys()].map(item => {
let t = new TemperatureSensor({id:`t${item}`, minTemperature:-10, maxTemperature:10, delay:randomDelay()});
t.start("generateData");
return t;
});
// Generate n humidity sensors
let humiditySensors = [...Array(100).keys()].map(item => {
let h = new HumiditySensor({id:`h${item}`, delay:randomDelay()});
h.start("generateData");
return h;
});
app.get('/sensors', authorized, (req, res) => {
res.send({
temperatureSensors: temperatureSensors.map(sensor => sensor.getData()),
humiditySensors: humiditySensors.map(sensor => sensor.getData())
});
});
app.get('/sensors/temperature', authorized, (req, res) => {
res.send(temperatureSensors.map(sensor => sensor.getData()));
});
app.get('/sensors/temperature/:id', authorized, (req, res) => {
let sensor = temperatureSensors.find(sensor => sensor.id == req.params.id);
if(sensor) { res.send(sensor.getData()) } else { res.sendStatus(404) };
});
app.get('/sensors/humidity', authorized, (req, res) => {
res.send(humiditySensors.map(sensor => sensor.getData()));
});
app.get('/sensors/humidity/:id', authorized, (req, res) => {
let sensor = humiditySensors.find(sensor => sensor.id == req.params.id);
if(sensor) { res.send(sensor.getData()) } else { res.sendStatus(404) };
});
app.listen(http_port)
console.log(`Listening on ${http_port}`);