-
Notifications
You must be signed in to change notification settings - Fork 20
/
BvgFetcher.js
155 lines (132 loc) · 3.87 KB
/
BvgFetcher.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const shortenStationName = require("vbb-short-station-name");
const Log = require("logger");
const pjson = require("./package.json");
module.exports = class BvgFetcher {
constructor (config) {
this.config = config;
}
async init () {
const {createClient} = await import("hafas-client");
const {profile} = await import("hafas-client/p/bvg/index.js");
this.hafasClient = createClient(
profile,
`MMM-PublicTransportBerlin v${pjson.version}`
);
}
getIdentifier () {
return this.config.identifier;
}
getStationId () {
return this.config.stationId;
}
async getStationName () {
const station = await this.hafasClient.stop(this.config.stationId);
return this.config.shortenStationNames
? shortenStationName(station.name)
: station.name;
}
async getDirectionDescriptor () {
if (typeof this.config.directionStationId === "undefined") {
return "all directions";
}
const station = await this.hafasClient.stop(this.config.directionStationId);
return station.name;
}
async fetchDepartures () {
// when value for a request is calculated to be 5 minutes before travelTimeToStation time
// so we can also show the non-reachable departures in the module
let when;
if (this.config.travelTimeToStation > 0) {
when = new Date(Date.now());
when.setTime(
Date.now() + this.config.travelTimeToStation * 60000 - 5 * 60000
);
} else {
when = Date.now();
}
let opt;
// Handle single direction case
if (
!this.config.directionStationId ||
this.config.directionStationId === ""
) {
opt = {
when,
duration: this.config.departureMinutes
};
} else {
opt = {
direction: this.config.directionStationId,
when,
duration: this.config.departureMinutes
};
}
const departures = await this.hafasClient.departures(
this.config.stationId,
opt
);
const processedDepartures = this.processData(departures);
return processedDepartures;
}
processData (data) {
const departuresData = {
fetcherId: this.config.identifier,
departuresArray: []
};
data.departures.forEach((row) => {
// check for:
// excluded transportation types
// ignored lines
// TODO: Make real stop/station handling here
// Quick fix to work around missing station objects
if (!row.station) {
row.station = row.stop;
}
// If log level is set to debug print infos about departures
if (config.logLevel.includes("DEBUG")) {
BvgFetcher.printDeparture(row);
}
if (
!this.config.excludedTransportationTypes.includes(row.line.product) &&
!this.config.ignoredLines.includes(row.line.name)
) {
const current = {
when: row.when || row.plannedWhen,
delay: row.delay || 0,
cancelled: row.cancelled || false,
name: row.line.name,
nr: row.line.nr,
type: row.line.product,
direction: this.config.shortenStationNames
? shortenStationName(row.direction)
: row.direction
};
departuresData.departuresArray.push(current);
}
});
departuresData.departuresArray.sort(this.compareTimes);
return departuresData;
}
static compareTimes (a, b) {
if (a.when < b.when) {
return -1;
}
if (a.when > b.when) {
return 1;
}
return 0;
}
// helper function to print departure for debugging
static printDeparture (row) {
const delayMinutes = Math.floor(
row.delay % 31536000 % 86400 % 3600 / 60
);
const time = new Date(row.when).toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit"
});
Log.log(
`${time} ${delayMinutes} ${row.line.product} ${row.direction} | stationId: ${row.station.id}`
);
}
};