-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
57 lines (47 loc) · 1.56 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
var express = require('express');
var app = express();
var tellstick = require('./server/tellstick.js');
var temperature = require('./server/temperature.js');
var tempRepo = require('./server/temperatureRepository.js');
// var thermostat = require('./server/thermostat');
var thermostat = require('./server/thermostat.js');
var interval = 1000*30;
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/client'));
app.get('/', function(request, response) {
response.sendFile(__dirname + '/client/index.html');
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
app.post('/start', function(request, response){
tellstick.startFridge(request, response);
response.sendStatus(200);
});
app.post('/stop', function(request, response){
tellstick.stopFridge(request, response);
response.sendStatus(200);
});
app.get('/data', function (request, response) {
tempRepo.getTemp(response);
});
app.get('/currentTemp', function (request, response) {
var responseObj = {temp: currentTemp, isFridgeOn: tellstick.getIsFridgeOn(), isHeaterOn: tellstick.getIsHeaterOn};
response.send(responseObj);
})
setInterval(fridgeController, interval);
fridgeController();
function fridgeController () {
temperature.getTemperature(onGetTemperature);
}
function onGetTemperature(temp){
console.log("Temperatur er: " +temp);
if(temp != -127){
currentTemp = temp.toFixed(2);
tempRepo.insertTemp(currentTemp);
thermostat.controlTemperature(temp);
}
else{
fridgeController();
}
}