-
Notifications
You must be signed in to change notification settings - Fork 13
/
node_helper.js
executable file
·65 lines (58 loc) · 1.83 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
/* global Module */
/* Magic Mirror
* Module: MMM-vvsDeparture
*
* By niklaskappler
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
var request = require("request");
const BASE_URL = "https://www3.vvs.de/mngvvs/XML_DM_REQUEST?";
module.exports = NodeHelper.create({
/* socketNotificationReceived(notification, payload)
* This method is called when a socket notification arrives.
*
* argument notification string - The identifier of the noitication.
* argument payload mixed - The payload of the notification.
*/
socketNotificationReceived: function (notification, payload) {
var self = this;
if (notification === "GET_DEPARTURES") {
self.retrieveStationData(
payload.config.station_id,
payload.config.offset,
payload.identifier);
setInterval(function () {
self.retrieveStationData(
payload.config.station_id,
payload.config.offset,
payload.identifier);
}, payload.config.reloadInterval);
}
},
retrieveStationData: function (stationId, offset, moduleIdentifier) {
var self = this;
var url = BASE_URL +
`limit=40&`+
`mode=direct&`+
`name_dm=${stationId}&`+
`outputFormat=rapidJSON&`+ //`outputFormat=JSON&`
`type_dm=any&`+
`useRealtime=1`;
if (offset != undefined) {
var d = new Date();
d.setMinutes(d.getMinutes() + offset);
url += `&itdDateYear=` + d.getFullYear().toString();
url += `&itdDateMonth=` + (d.getMonth() + 1).toString();
url += `&itdDateDay=` + d.getDate().toString();
url += `&itdTimeHour=` + d.getHours().toString();
url += `&itdTimeMinute=` + d.getMinutes().toString();
}
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
self.sendSocketNotification(moduleIdentifier+"_NEW_DEPARTURES", JSON.parse(body));
}
});
}
});