-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
62 lines (47 loc) · 1.85 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
"use strict";
const NodeHelper = require('node_helper');
const LvbFetcher = require('./LvbFetcher');
module.exports = NodeHelper.create({
start: function () {
this.departuresFetchers = [];
},
createFetcher: function (config) {
let fetcher;
if (typeof this.departuresFetchers[config.stationId] === "undefined") {
fetcher = new LvbFetcher(config);
this.departuresFetchers[config.stationId] = fetcher;
this.sendInit(fetcher);
console.log("Transportation fetcher for station '" + fetcher.getStationName() + "' created. (Station ID: " + fetcher.getStationId() + ")");
} else {
fetcher = this.departuresFetchers[config.stationId];
this.sendInit(fetcher);
console.log("Using existing transportation fetcher for station '" + fetcher.getStationName() + "' (Station ID: " + fetcher.getStationId() + ")");
}
this.getDepartures(fetcher.getStationId());
},
sendInit: function (fetcher) {
this.sendSocketNotification('FETCHER_INIT', {
stationId: fetcher.getStationId(),
stationName: fetcher.getStationName()
});
},
getDepartures: function (stationId) {
this.departuresFetchers[stationId].fetchDepartures().then((departuresData) => {
this.sendSocketNotification('DEPARTURES', departuresData);
}).catch((e) => {
let error = {};
console.log("Error while fetching departures (for Station ID " + stationId + "): " + e);
// Add stationId to error for identification in the main instance
error.stationId = stationId;
error.message = e;
this.sendSocketNotification('FETCH_ERROR', error);
});
},
socketNotificationReceived: function (notification, payload) {
if (notification === 'GET_DEPARTURES') {
this.getDepartures(payload);
} else if (notification === 'CREATE_FETCHER') {
this.createFetcher(payload);
}
}
});