-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
80 lines (68 loc) · 2.87 KB
/
node_helper.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
const NodeHelper = require("node_helper");
const request = require("request");
const Log = require("logger");
const apiAlplakesBaseUrl = "https://alplakes-api.eawag.ch/simulations";
const averageTemperaturePath = "/layer/average_temperature/delft3d-flow";
const pointPath = "/point/delft3d-flow";
module.exports = NodeHelper.create({
start: function() {
Log.log("Starting node helper for: " + this.name);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "GET_TEMPERATURE_REQUEST") {
this.config = payload;
this.getLiveTemperature();
}
},
getLiveTemperature: function() {
let now = new Date();
let threeHoursAgo = new Date(now.getTime() - (1000 * 60 * 60 * 3));
let isAverageTemperature = this.config.latitude === undefined || this.config.longitude === undefined;
let url;
if (isAverageTemperature) {
url = apiAlplakesBaseUrl + averageTemperaturePath + "/" + this.config.lake + "/" + this.getDateTime(threeHoursAgo) + "/" + this.getDateTime(now) + "/" + this.config.depth;
} else {
url = apiAlplakesBaseUrl + pointPath + "/" + this.config.lake + "/" + this.getDateTime(threeHoursAgo) + "/" + this.getDateTime(now) + "/" + this.config.depth + "/" + this.config.latitude + "/" + this.config.longitude;
}
let self = this;
request(url, { json: true }, (error, response, body) => {
if (error) {
let errorMessage = `${self.name}: Alplakes API request failed: ${error.message}`;
Log.error(errorMessage);
this.sendSocketNotification("GET_TEMPERATURE_ERROR", error.message);
} else if (response.statusCode === 200) {
let temperature = self.getTemperatureValueFromJson(body, isAverageTemperature);
this.sendSocketNotification("GET_TEMPERATURE_RESULT", temperature);
} else if (response.statusCode === 400) {
let errorMessage = `${self.name}: Alplakes API returned a validation error: ${response.statusCode}`;
Log.error(errorMessage + " - " + body.detail);
this.sendSocketNotification("GET_TEMPERATURE_ERROR", errorMessage);
} else {
let errorMessage = `${self.name}: Alplakes API returned an error: ${response.statusCode}`;
Log.error(errorMessage + " - " + JSON.stringify(body));
this.sendSocketNotification("GET_TEMPERATURE_ERROR", errorMessage);
}
});
},
getTemperatureValueFromJson: function(jsonBody, isAverageTemperature) {
if (isAverageTemperature) {
return Math.round(jsonBody.variable.data[0]);
} else {
return Math.round(jsonBody.variables.temperature.data[0]);
}
},
getDateTime: function(date) {
let year = "" + date.getFullYear();
let month = "" + (date.getMonth() + 1);
let day = "" + date.getDate();
let hours = "" + date.getHours();
let minutes = "" + date.getMinutes();
return year + this.pad(month) + this.pad(day) + this.pad(hours) + this.pad(minutes);
},
pad: function(value) {
if (value.length < 2) {
return "0" + value;
}
return value;
}
});