forked from nwootton/MMM-UKNationalRail
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode_helper.js
84 lines (68 loc) · 2.19 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
81
82
83
84
/* Magic Mirror
* Module: UK National Rail
*
* Originally by Nick Wootton
* Migrated to OpenLDBWS by Matt Dyson
*
* https://github.com/mattdy/MMM-UKNationalRail
*
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
const Rail = require("national-rail-darwin");
const Log = require("../../js/logger");
module.exports = NodeHelper.create({
start: function () {
Log.info("MMM-UKNationalRail helper started");
this.started = false;
this.config = {};
this.rail = null;
},
getTimetable: function (id) {
var self = this;
if (this.rail === null) {
return;
}
var options = {};
options.rows = this.config[id].fetchRows;
if (this.config[id].filterDestination) {
options.destination = this.config[id].filterDestination;
}
Log.info("Sending request for departure board information");
this.rail.getDepartureBoardWithDetails(
this.config[id].station,
options,
function (error, result) {
Log.info("Return from getDepartureBoard: " + error + " - " + result);
const newResult = { result, id };
if (!error) {
self.sendSocketNotification("UKNR_DATA", newResult);
}
}
);
},
socketNotificationReceived: function (notification, payload) {
Log.info("socketNotificationReceived");
switch (notification) {
case "UKNR_TRAININFO":
this.getTimetable(payload.id);
break;
case "UKNR_CONFIG":
Log.info("MMM-UKNationalRail received configuration");
this.config[payload.id] = payload.config;
var config = this.config[payload.id];
// if the filter destination is not defined ignore
if (config.filterDestination.length === 1) {
// if there is only one filter destination keep it
config.filterDestination = config.filterDestination[0];
} else {
// otherwise remove it and handle the multiple filter destinations on the response
delete config.filterDestination;
}
this.rail = new Rail(this.config[payload.id].token);
this.sendSocketNotification("UKNR_STARTED", true);
this.getTimetable(payload.id);
this.started = true;
}
}
});