This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
80 lines (62 loc) · 1.74 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
'use strict'
let express = require('express');
let cors = require('cors');
let bodyParser = require('body-parser');
let TemperatureSensor = require('./temperature-sensor');
let MovementSensor = require('./movement-sensor');
let Controller = require('./controller');
// Init Controller
let controller = new Controller();
// Init API
let app = express();
app.use(bodyParser.json());
app.use(cors());
app.get('/', (req, res) => {
TemperatureSensor.listSensors().then((sensors) => {
res.send(sensors);
});
});
app.get('/movement', (req, res) => {
let movementSensor = new MovementSensor(23);
movementSensor.detectMovement().then((movementDetected) => {
res.send(movementDetected);
});
});
app.get('/:id/', (req, res) => {
let name = req.params.id;
let tempSensor = new TemperatureSensor(name);
tempSensor.getTemperature().then((temperature) => {
let response = {
date: new Date(),
temp: temperature
};
res.send(response);
});
});
app.get('/status/:active/:type', (req, res) => {
let active = req.params.active;
let type = req.params.type;
if (type === 'movement') {
controller.toggleMovementMeasurement(active);
} else if (type === 'temperature') {
controller.toggleTemperatureMeasurement(active);
}
res.send({});
});
app.get('/confirm/:id', (req, res) => {
let id = req.params.id;
let unsentAlarms = controller.restConnector.sentAlarms.filter((alarm) => {
return !alarm.confirmed;
});
unsentAlarms.forEach((alarm) => {
if (alarm.id === id) {
alarm.confirm();
}
});
console.log('Alarm with ID ' + id + ' confirmed.');
console.log('Other unconfirmed alarms: ', unsentAlarms);
res.send({});
});
app.listen(3000, () => {
console.log('API listening on port 3000!');
});