This repository has been archived by the owner on May 18, 2022. It is now read-only.
forked from deg0nz/MMM-PublicTransportBerlin
-
Notifications
You must be signed in to change notification settings - Fork 7
/
DbFetcher.js
135 lines (106 loc) · 3.44 KB
/
DbFetcher.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
"use strict";
const dbClient = require('db-hafas');
let DbFetcher = function (config) {
this.config = config;
};
let typeData = {
'U': {
type: 'subway',
color: '#0067AD'
},
'Bus': {
type: 'bus',
color: '#a5037b'
},
's': {
type: 'subway',
color: '#006F35'
}
}
DbFetcher.prototype.getStationId = function () {
return this.config.stationId;
};
DbFetcher.prototype.getStationName = function () {
return dbClient.locations(this.config.name).then((response) => {
return response ? response[0].name : '';
});
};
DbFetcher.prototype.fetchDepartures = function () {
// when value for a request is calculated to be 5 minutes before delay time
let when;
if (this.config.delay > 0) {
when = new Date();
when.setTime((Date.now() + this.config.delay * 60000) - (5 * 60000));
} else {
when = Date.now();
}
let direction = null;
if (this.config.direction) {
direction = this.config.direction;
}
let opt = {
when: when,
duration: this.config.departureMinutes,
direction: direction
};
return dbClient.departures(this.config.stationId, opt).then((response) => {
return this.processData(response)
});
};
DbFetcher.prototype.processData = function (data) {
let departuresData = {
stationId: this.config.stationId,
departuresArray: []
};
//console.log(data);
data.forEach((row) => {
if (!this.config.ignoredStations.includes(row.station.id)) {
//console.log('------------------------------------------------------------------------------');
console.log('Parsing: ' + row.product.name + ' nach ' + row.direction + ' um ' + row.when);
console.log(JSON.stringify(row.product));
let delay = row.delay;
if (!delay) {
row.delay = 0
}
let productType = row.product.type;
if (!productType && typeData[row.product.productName]) {
productType = typeData[row.product.productName];
}
if (!productType && row.product.productName) {
productType = { type: row.product.productName, color: "#006F35" }
}
let current = {
when: row.when,
delay: row.delay,
line: row.product.name,
nr: row.product.nr,
type: productType.type,
color: productType.color,
direction: row.direction
};
console.log(current);
departuresData.departuresArray.push(current);
}
});
departuresData.departuresArray.sort(compare);
return departuresData;
};
function compare(a, b) {
// delay must be converted to milliseconds
let timeA = a.when.getTime() + a.delay * 1000;
let timeB = b.when.getTime() + b.delay * 1000;
if (timeA < timeB) {
return -1;
}
if (timeA > timeB) {
return 1
}
return 0
}
// helper function to print departure for debugging
function printDeparture(row) {
let delayMinutes = Math.floor((((delay % 31536000) % 86400) % 3600) / 60);
let time = row.when.toLocaleTimeString([], {hour: '2-digit', minute: '2-digit'});
console.log(time + " " + delayMinutes + " " + row.product.type.unicode + " " + row.direction + " | stationId: " + row.station.id);
}
module.exports = DbFetcher;