forked from raywo/MMM-PublicTransportHafas
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnode_helper.js
executable file
·68 lines (58 loc) · 2.02 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
const Log = require("logger");
const NodeHelper = require("node_helper");
const DepartureFetcher = require("./core/DepartureFetcher");
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;
}
},
async createFetcher (config) {
let fetcher;
if (typeof this.departuresFetchers[config.identifier] === "undefined") {
fetcher = new DepartureFetcher(config);
await fetcher.init();
this.departuresFetchers[config.identifier] = fetcher;
Log.info(`[MMM-PublicTransportHafas] Transportation fetcher for station with id '${fetcher.getStationID()}' created.`);
this.sendFetcherLoaded(fetcher);
} else {
fetcher = this.departuresFetchers[config.identifier];
Log.info(`[MMM-PublicTransportHafas] Using existing transportation fetcher for station id '${fetcher.getStationID()}'.`);
this.sendFetcherLoaded(fetcher);
}
},
sendFetcherLoaded (fetcher) {
this.sendSocketNotification("FETCHER_INITIALIZED", {
identifier: fetcher.getIdentifier()
});
},
async fetchDepartures (identifier) {
const fetcher = this.departuresFetchers[identifier];
if (typeof fetcher === "undefined") {
Log.log("[MMM-PublicTransportHafas] fetcher is undefined. If this occurs only sporadically, it is not a problem.");
} else {
try {
const fetchedDepartures = await fetcher.fetchDepartures();
const payload = {
departures: fetchedDepartures,
identifier: fetcher.getIdentifier()
};
this.sendSocketNotification("DEPARTURES_FETCHED", payload);
} catch (error) {
const payload = {
error,
identifier: fetcher.getIdentifier()
};
this.sendSocketNotification("FETCH_ERROR", payload);
}
}
}
});