-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
113 lines (92 loc) · 2.33 KB
/
app.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var express = require('express');
var config = require('config');
var request = require('request');
var moment = require('moment');
var app = express();
var devices;
function updateDevices() {
request(config.iTowXmlUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
var now = new moment();
console.log(now.format("HH:mm:ss") + ' : refreshing devices from iTow XML');
parseItowXml(body);
}
})
}
function parseItowXml(xml) {
var parseString = require('xml2js').parseString;
parseString(xml, function (err, result) {
devices = [];
// Temperatures
for (i=1; i<=15; i++) {
if (result.itow['ow_nom_'+i] == undefined) {
break;
}
devices.push(
{
"id" : "itow-temp-"+i,
"name" : config.itowProbes[i].name ? config.itowProbes[i].name : result.itow['ow_nom_'+i][0],
"type" : "DevTemperature",
"room" : config.itowProbes[i].room,
"params" : [
{
"key" : "Value",
"value" : result.itow['ow_temp_'+i][0],
"unit" : "°C",
"graphable" : true
}
]
}
)
}
// Meters
for (i=1; i<=5; i++) {
if (result.itow['adco'+i] == undefined) {
break;
}
devices.push(
{
"id" : "itow-conso-"+i,
"name" : config.itowMeters[i].name,
"type" : "DevElectricity",
"room" : config.itowMeters[i].room,
"params" : [
{
"key" : "Watts",
"value" : result.itow['kwh'+i][0]*1000,
"unit" : "W",
"graphable" : true
}
]
}
)
}
});
}
updateDevices();
setInterval(function() {
updateDevices();
}, config.get('refreshInterval')*1000);
app.get('/', function (req, res) {
res.send('ISS iTow');
})
app.get('/system', function (req, res) {
res.send(config.get('system'));
})
app.get('/rooms', function (req, res) {
var output = {
rooms : config.get('rooms')
}
res.send(output);
})
app.get('/devices', function (req, res) {
var output = {
devices : devices
}
res.send(output)
})
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log('ISS iTow app listening at http://%s:%s', host, port);
})