forked from KristjanESPERANTO/MMM-PublicTransportHafas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
executable file
·134 lines (118 loc) · 4.54 KB
/
node_helper.js
File metadata and controls
executable file
·134 lines (118 loc) · 4.54 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const Log = require("../../js/logger");
const NodeHelper = require("../../js/node_helper");
const DepartureFetcher = require("./core/DepartureFetcher.mjs").default;
module.exports = NodeHelper.create({
start () {
this.departuresFetchers = [];
this.fetcherConfigs = {};
},
socketNotificationReceived (notification, payload) {
switch (notification) {
case "CREATE_FETCHER":
this.createFetcher(payload);
break;
case "FETCH_DEPARTURES":
this.fetchDepartures(payload);
break;
}
},
/**
* Creates a new departure fetcher for a station or returns an existing one.
* Sends FETCHER_INITIALIZED immediately to start the fetch loop, then initializes
* the HAFAS client in the background.
*
* @param {object} config - Configuration object containing station ID, profile, and other settings
*/
async createFetcher (config) {
this.fetcherConfigs[config.identifier] = config;
let fetcher;
if (typeof this.departuresFetchers[config.identifier] === "undefined") {
fetcher = new DepartureFetcher(config);
this.departuresFetchers[config.identifier] = fetcher;
// Send initialization complete immediately so the fetch loop can start
// The actual HAFAS client init will be done on first fetch if needed
this.sendFetcherLoaded(fetcher);
// Init in background
try {
await fetcher.init();
Log.info(`[MMM-PublicTransportHafas] Transportation fetcher for station with id '${fetcher.getStationID()}' created.`);
} catch (error) {
Log.error(`[MMM-PublicTransportHafas] Failed to initialize fetcher for station id '${config.stationID}'. Will retry on next fetch cycle.`, error);
}
} 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") {
await this.handleMissingFetcher(identifier);
return;
}
await this.fetchWithInitializedFetcher(fetcher);
},
/**
* Handles the case when a fetcher is requested but doesn't exist yet.
* Attempts to create the fetcher from saved config and retries the fetch.
*
* @param {string} identifier - The unique identifier of the fetcher/module instance
*/
async handleMissingFetcher (identifier) {
// Fetcher not initialized yet - try to create it now
const config = this.fetcherConfigs[identifier];
if (config) {
Log.log("[MMM-PublicTransportHafas] Fetcher undefined, attempting to create it now.");
await this.createFetcher(config);
// Try again after creation attempt
await this.fetchDepartures(identifier);
return;
}
Log.log("[MMM-PublicTransportHafas] fetcher is undefined. If this occurs only sporadically, it is not a problem.");
},
/**
* Fetches departures using an existing fetcher instance.
* Checks if the HAFAS client is initialized and retries initialization if needed.
* Sends either DEPARTURES_FETCHED or FETCH_ERROR notification based on the result.
*
* @param {object} fetcher - The DepartureFetcher instance to use
*/
async fetchWithInitializedFetcher (fetcher) {
// Check if fetcher needs initialization (in case it failed before)
if (!fetcher.hafasClient) {
Log.log("[MMM-PublicTransportHafas] Fetcher not fully initialized, attempting init now.");
try {
await fetcher.init();
Log.info("[MMM-PublicTransportHafas] Fetcher initialization successful on retry.");
} catch (error) {
Log.error("[MMM-PublicTransportHafas] Fetcher initialization failed again.", error);
const payload = {
error,
identifier: fetcher.getIdentifier()
};
this.sendSocketNotification("FETCH_ERROR", payload);
return;
}
}
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);
}
}
});