forked from deg0nz/MMM-PublicTransportBerlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
181 lines (159 loc) · 5.18 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const NodeHelper = require("node_helper");
const lineColors = require("vbb-line-colors");
const Log = require("logger");
const BvgFetcher = require("./BvgFetcher");
module.exports = NodeHelper.create({
start() {
this.departuresFetchers = [];
},
async createFetcher(config) {
const fetcherId = config.identifier;
let fetcher;
if (typeof this.departuresFetchers[fetcherId] === "undefined") {
fetcher = new BvgFetcher(config);
await fetcher.init();
this.departuresFetchers[fetcherId] = fetcher;
this.sendInit(fetcher);
try {
const stationName = await fetcher.getStationName();
const directionDescriptor = await fetcher.getDirectionDescriptor();
Log.log(
`Created transportation fetcher for station ${stationName} (toward ${directionDescriptor}). (Station ID: ${fetcher.getStationId()}, Direction ID: ${
config.directionStationId
})`
);
} catch (error) {
Log.error(error);
}
} else {
fetcher = this.departuresFetchers[fetcherId];
this.sendInit(fetcher);
try {
const stationName = await fetcher.getStationName();
const directionDescriptor = await fetcher.getDirectionDescriptor();
Log.log(
`Using existing transportation fetcher for station ${stationName} (toward ${directionDescriptor}) created. (Station ID: ${fetcher.getStationId()}, Direction ID: ${
config.directionStationId
})`
);
} catch (error) {
Log.error(error);
}
}
await this.getDepartures(fetcherId);
},
async sendInit(fetcher) {
try {
let stationName = await fetcher.getStationName();
const directionDescriptor = await fetcher.getDirectionDescriptor();
if (
directionDescriptor !== "all directions" &&
fetcher.config.showDirection
) {
stationName += `<br />(toward ${directionDescriptor})`;
}
this.sendSocketNotification("FETCHER_INITIALIZED", {
stationId: fetcher.getStationId(),
stationName,
fetcherId: fetcher.getIdentifier()
});
} catch (error) {
Log.error(`Error initializing fetcher: ${error}`);
}
},
async getDepartures(fetcherId) {
try {
const departuresData =
await this.departuresFetchers[fetcherId].fetchDepartures();
this.pimpDeparturesArray(departuresData.departuresArray);
this.sendSocketNotification("DEPARTURES_FETCHED", departuresData);
} catch (error) {
Log.log(
`Error while fetching departures (for module instance ${fetcherId}): ${error}`
);
// Add stationId to error for identification in the main instance
error.fetcherId = fetcherId;
error.message = error;
this.sendSocketNotification("FETCH_ERROR", error);
}
},
pimpDeparturesArray(departuresArray) {
let currentProperties = {};
departuresArray.forEach((current) => {
currentProperties = this.getLineProperties(current);
// if (!this.config.marqueeLongDirections) {
// current.direction = this.trimDirectionString(current.direction);
// }
current.bgColor = currentProperties.bgColor;
current.fgColor = currentProperties.fgColor;
current.cssClass = currentProperties.cssClass;
});
return departuresArray;
},
getLineProperties(product) {
const properties = {
bgColor: "#333",
fgColor: "#FFF",
cssClass: ""
};
const type = product.type;
const lineType = product.line;
const name = product.name;
let colors = {};
switch (type) {
case "suburban":
colors = lineColors.suburban[name];
properties.cssClass = "ptb-sbahnsign";
break;
case "subway":
colors = lineColors.subway[name];
properties.cssClass = "ptb-ubahnsign";
break;
case "bus":
colors.bg = "#B60079";
colors.fg = "#FFF";
properties.cssClass = "ptb-bussign";
break;
case "tram":
colors = lineColors.tram[name];
properties.cssClass = "ptb-tramsign";
break;
case "regional":
colors = lineColors.regional[name];
properties.cssClass = "ptb-dbsign";
break;
case "express":
if (lineType === "LOCOMORE") {
colors.bg = "#E5690B";
colors.fg = "#3E1717";
properties.cssClass = "ptb-locsign";
} else {
properties.cssClass = "ptb-expresssign";
}
break;
}
// In case new lines get added but are not listed in the vbb-line-colors module
if (typeof colors !== "undefined") {
// Change default values if we changed them
if ("bg" in colors) {
properties.bgColor = colors.bg;
}
if ("fg" in colors) {
properties.fgColor = colors.fg;
}
} else {
// If no color has been found for the line, default to grey
properties.bgColor = "#535353";
properties.fgColor = "#fff";
}
return properties;
},
socketNotificationReceived(notification, payload) {
if (notification === "FETCH_DEPARTURES") {
this.getDepartures(payload);
}
if (notification === "CREATE_FETCHER") {
this.createFetcher(payload);
}
}
});