-
Notifications
You must be signed in to change notification settings - Fork 13
/
node_helper.js
executable file
·72 lines (52 loc) · 1.75 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
"use strict";
const NodeHelper = require("node_helper");
const HafasFetcher = require("./core/HafasFetcher");
module.exports = NodeHelper.create({
start: function () {
this.departuresFetchers = [];
},
socketNotificationReceived: function(notification, payload) {
switch (notification) {
case "CREATE_FETCHER":
this.createFetcher(payload);
break;
case "FETCH_DEPARTURES":
this.fetchDepartures(payload);
break;
}
},
createFetcher: function (config) {
let fetcher;
if (typeof this.departuresFetchers[config.identifier] === "undefined") {
fetcher = new HafasFetcher(config);
this.departuresFetchers[config.identifier] = fetcher;
console.log("Transportation fetcher for station with id '" + fetcher.getStationID() + "' created.");
this.sendFetcherLoaded(fetcher);
} else {
fetcher = this.departuresFetchers[config.identifier];
console.log("Using existing transportation fetcher for station id '" + fetcher.getStationID() + "'.");
this.sendFetcherLoaded(fetcher);
}
},
sendFetcherLoaded: function (fetcher) {
this.sendSocketNotification("FETCHER_INITIALIZED", {
identifier: fetcher.getIdentifier()
});
},
fetchDepartures(identifier) {
let fetcher = this.departuresFetchers[identifier];
fetcher.fetchDepartures().then((fetchedDepartures) => {
let payload = {
identifier: fetcher.getIdentifier(),
departures: fetchedDepartures
};
this.sendSocketNotification("DEPARTURES_FETCHED", payload);
}).catch((error) => {
let payload = {
identifier: fetcher.getIdentifier(),
error: error
};
this.sendSocketNotification("FETCH_ERROR", payload);
});
}
});