forked from raywo/MMM-PublicTransportHafas
-
Notifications
You must be signed in to change notification settings - Fork 6
/
node_helper.js
executable file
·77 lines (65 loc) · 2 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
const NodeHelper = require("node_helper");
const Log = require("logger");
const HafasFetcher = require("./core/HafasFetcher");
module.exports = NodeHelper.create({
start() {
this.departuresFetchers = [];
},
socketNotificationReceived(notification, payload) {
switch (notification) {
case "CREATE_FETCHER":
this.createFetcher(payload);
break;
case "FETCH_DEPARTURES":
this.fetchDepartures(payload);
break;
}
},
createFetcher(config) {
let fetcher;
if (typeof this.departuresFetchers[config.identifier] === "undefined") {
fetcher = new HafasFetcher(config);
this.departuresFetchers[config.identifier] = fetcher;
Log.info(
`Transportation fetcher for station with id '${fetcher.getStationID()}' created.`
);
this.sendFetcherLoaded(fetcher);
} else {
fetcher = this.departuresFetchers[config.identifier];
Log.info(
`Using existing transportation fetcher for station id '${fetcher.getStationID()}'.`
);
this.sendFetcherLoaded(fetcher);
}
},
sendFetcherLoaded(fetcher) {
this.sendSocketNotification("FETCHER_INITIALIZED", {
identifier: fetcher.getIdentifier()
});
},
fetchDepartures(identifier) {
const fetcher = this.departuresFetchers[identifier];
if (typeof fetcher !== "undefined") {
fetcher
.fetchDepartures()
.then((fetchedDepartures) => {
const payload = {
identifier: fetcher.getIdentifier(),
departures: fetchedDepartures
};
this.sendSocketNotification("DEPARTURES_FETCHED", payload);
})
.catch((error) => {
const payload = {
identifier: fetcher.getIdentifier(),
error
};
this.sendSocketNotification("FETCH_ERROR", payload);
});
} else {
Log.log(
"MMM-PublicTransportHafas: fetcher is undefined. If this occurs only sporadically, it is not a problem."
);
}
}
});