-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
46 lines (40 loc) · 1.08 KB
/
api.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
'use strict';
const express = require('express');
module.exports = class API {
constructor({HVAC}){
this.HVAC = HVAC;
this.PORT = 3000;
this.app = express();
this.setRoutes();
this.startServer();
}
setRoutes(){
this.app.get('/', function (req, res) {
res.send('Hello World!')
});
this.app.get('/current_HVAC', (req, res) => res.json(this._HVACInfo()));
this.app.post('/current_HVAC/tempTarget?set=:set', (req, res) => {
//Force number for setting
const tempNumber = ~~req.params.set;
if (tempNumber != req.params.set){
return res.send(400, 'Set value must be a number');
}
try {
this.HVAC.set_tempTarget(tempNumber);
res.send(this._HVACInfo());
}
catch(err){
return res.send(500, 'Error setting tempTaret');
}
});
}
_HVACInfo(){
const {currentTempRead, tempTarget} = this.HVAC;
return {currentTempRead, tempTarget};
}
startServer(){
this.app.listen(this.PORT, () => {
console.log(`Perch API server listening on port ${this.PORT}`);
});
}
};