-
Notifications
You must be signed in to change notification settings - Fork 2
/
holfuyReader.js
49 lines (43 loc) · 1.06 KB
/
holfuyReader.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
//
// # holfuyReader
//
// A module to read weather-data (wind direction and speed).
//
var http = require("http");
function parseObject(o) {
var result = {};
if(o.error) {
console.log("Got error from holfuy station: ");
console.log(o);
return null;
}
result.dir = o.wind.direction;
result.avgSpeed = o.wind.speed;
result.maxSpeed = o.wind.gust;
result.name = o.stationName;
result.temp = o.temperature;
return result;
}
exports.pollServer = function(cfg, cb) {
var url = cfg.url + cfg.args.PlatsId;
// console.log(url);
http.get(url, function(res) {
if (res.statusCode == 200) {
try {
var body = "";
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
// console.log(body);
var wd = parseObject(JSON.parse(body));
cb(wd);
});
} catch(ex) {
console.log("Got error while parsing weather station '" + cfg.url + "': " + ex);
};
}
}).on('error', function(e) {
console.log("Got error while reading weather station '" + cfg.url + "': " + e.message);
});;
}