forked from fhinder/MMM-vvsDeparture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
executable file
·78 lines (68 loc) · 2.01 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
/* global Module */
/* Magic Mirror
* Module: MMM-vvsDeparture
*
* By Fabian Hinder
* forked from nilaskappler
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
var Axios = require('axios');
const Log = require('logger');
const BASE_URL = "https://www3.vvs.de";
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 path = '/mngvvs/XML_DM_REQUEST?' +
`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);
path += `&itdDateYear=` + d.getFullYear().toString();
path += `&itdDateMonth=` + (d.getMonth() + 1).toString();
path += `&itdDateDay=` + d.getDate().toString();
path += `&itdTimeHour=` + d.getHours().toString();
path += `&itdTimeMinute=` + d.getMinutes().toString();
}
var url = BASE_URL + path;
var config = {
method: 'get',
url: encodeURI(url),
headers: {}
};
Axios(config)
.then(function (response) {
self.sendSocketNotification(moduleIdentifier+"_NEW_DEPARTURES", (response.data));
})
.catch(function (error) {
self.sendSocketNotification(moduleIdentifier + "_ERROR", error);
//Log.error(error);
});
}
});