-
Notifications
You must be signed in to change notification settings - Fork 0
/
heatmiser.js
81 lines (71 loc) · 2.3 KB
/
heatmiser.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
81
var exec = require('child_process').exec;
var express = require('express');
var app = express();
var data = {
targetHeatingCoolingState: 1,
targetTemperature: 20,
targetRelativeHumidity: 30,
currentHeatingCoolingState: 1,
currentTemperature: 19,
currentRelativeHumidity: 30
};
plCmd = '~/heatmiser-wifi/bin/heatmiser_json.pl';
//ROUTING
app
.get('/status', function (req, res, next) {
child = exec(plCmd,
function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}
hmOutput = JSON.parse(stdout);
data.currentTemperature = hmOutput.heater.temperature.internal;
data.targetTemperature = hmOutput.heater.heating.target;
data.currentHeatingCoolingState = hmOutput.heater.heating.on;
if (hmOutput.heater.runmode == "heating") {
data.targetHeatingCoolingState = 1;
} else {
data.targetHeatingCoolingState = 0;
}
res.send(data);
console.log(data);
});
})
.get('/targetTemperature/:temperature', function (req, res, next) { //Set Temperature
var heatOn = {"heating":{"target":19}};
data.targetTemperature = parseFloat(req.params.temperature);
heatOn.heating.target = data.targetTemperature;
sendMessage(heatOn);
res.sendStatus(200);
console.log('set temp to ', req.params.temperature);
})
.get('/targetHeatingCoolingState/:state', function (req, res, next) { //Set target state
var runMode = { "runmode":"heating" };
if (req.params.state == 0) {
runMode.runmode = "frost";
}
sendMessage(runMode);
res.sendStatus(200);
})
// NOT IMPLEMENTED
.get('/targetRelativeHumidity/:humidity', function (req, res, next) { //Set target state
data.currentRelativeHumidity = data.targetRelativeHumidity;
data.targetRelativeHumidity = parseFloat(req.params.humidity);
res.sendStatus(200);
});
function sendMessage(message) {
command = plCmd + " '" + JSON.stringify(message) + "'";
child = exec(command,
function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}
console.log('Message Sent');
console.log(message);
});
}
var server = app.listen(4321, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Heatmiser API listening at', host, port);
});