-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_helper.js
More file actions
43 lines (36 loc) · 1.54 KB
/
node_helper.js
File metadata and controls
43 lines (36 loc) · 1.54 KB
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
const NodeHelper = require("node_helper");
const request = require("request");
const Log = require("../../js/logger.js");
module.exports = NodeHelper.create({
start: function () {
Log.log("Starting node helper for: " + this.name);
this.config = null;
},
socketNotificationReceived: function (notification, payload) {
if (notification === "SET_CONFIG") {
this.config = payload;
}
this.getData();
},
getData: function () {
const self = this;
Log.info(this.name + ": Fetching data for Hong Kong Observatory");
const nineDayWeatherForecastURL = "https://data.weather.gov.hk/weatherAPI/opendata/weather.php?dataType=fnd&lang=en";
request(nineDayWeatherForecastURL, function (error, response, body) {
if (error || response.statusCode !== 200) {
Log.debug(this.name + " :Error getting 9-day Weather Forecast(" + response.statusCode + ")");
self.sendSocketNotification("ERROR", response.statusCode);
return;
}
const generalSituation = JSON.parse(body).generalSituation;
const weatherForecast = JSON.parse(body).weatherForecast;
const updateTime = JSON.parse(body).updateTime;
self.sendSocketNotification("DATA", {
generalSituation: generalSituation,
weatherForecast: weatherForecast,
updateTime: updateTime
});
});
setTimeout(this.getData.bind(this), this.config.updateInterval);
}
});