-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtankerkoenig.js
328 lines (280 loc) · 8.81 KB
/
tankerkoenig.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* @file apis/tankerkoenig.js
*
* @author fewieden
* @license MIT
*
* @see https://github.com/fewieden/MMM-Fuel
*/
/**
* @external lodash
* @see https://www.npmjs.com/package/lodash
*/
const _ = require('lodash');
/**
* @external node-fetch
* @see https://www.npmjs.com/package/node-fetch
*/
const fetch = require('node-fetch');
/**
* @external logger
* @see https://github.com/MichMich/MagicMirror/blob/master/js/logger.js
*/
const Log = require('logger');
const { sortByPrice } = require('./utils');
const BASE_URL = 'https://creativecommons.tankerkoenig.de/json';
let config;
let stationInfos;
/**
* @function generateRadiusUrl
* @description Helper function to generate API request url.
*
* @returns {string} url
*/
function generateRadiusUrl() {
return `${BASE_URL}/list.php?lat=${config.lat}&lng=${config.lng}&rad=${config.radius}&type=all&apikey=${
config.api_key}&sort=dist`;
}
/**
* @function generateStationPricesUrl
* @description Helper function to generate API request url.
*
* @param {string[]} ids - Gas Station IDs
*
* @returns {string} url
*/
function generateStationPricesUrl(ids) {
return `${BASE_URL}/prices.php?ids=${ids.join(',')}&apikey=${config.api_key}`;
}
/**
* @function generateStationInfoUrl
* @description Helper function to generate API request url.
*
* @param {string} id - Gas Station ID
*
* @returns {string} url
*/
function generateStationInfoUrl(id) {
return `${BASE_URL}/detail.php?id=${id}&apikey=${config.api_key}`;
}
/**
* @function filterStations
* @description Helper function to filter gas stations.
*
* @param {Object} station - Gas Station
*
* @returns {boolean} To keep or filter the station.
*/
function filterStations(station) {
const hideClosedStation = config.showOpenOnly && !station.isOpen;
const excludeStation = config.excludeStationIds.includes(station.id);
const noPriceAvailable = config.types.some(type => station[type] <= 0);
if (hideClosedStation || excludeStation || noPriceAvailable) {
return false;
}
return true;
}
/**
* @function normalizeStations
* @description Helper function to normalize the structure of gas stations for the UI.
*
* @param {Object} value - Gas Station
* @param {int} index - Array index
* @param {Object[]} stations - Original Array.
*
* @returns {void}
*
* @see apis/README.md
*/
function normalizeStations(value, index, stations) {
/* eslint-disable no-param-reassign */
stations[index].prices = {
diesel: value.diesel,
e5: value.e5,
e10: value.e10
};
stations[index].distance = value.dist;
stations[index].street = `${value.street} ${value.houseNumber}`;
stations[index].address = `${`0${value.postCode}`.slice(-5)} ${
value.place} - ${stations[index].street}`;
/* eslint-enable no-param-reassign */
}
/**
* @function getPricesByRadius
* @description Fetches the prices by radius.
* @async
*
* @returns {Object[]} List of stations in raw format.
*/
async function getPricesByRadius() {
const response = await fetch(generateRadiusUrl());
const parsedResponse = await response.json();
if (!parsedResponse.ok) {
throw new Error('Error no fuel radius prices');
}
return parsedResponse.stations;
}
/**
* @function degreesToRadians
* @description Converst degrees to radians
* @see https://stackoverflow.com/questions/365826/calculate-distance-between-2-gps-coordinates/365853#365853
*
* @param {number} degrees - Degrees
*
* @returns {number} Radians
*/
function degreesToRadians(degrees) {
return degrees * Math.PI / 180;
}
/**
* @function distanceInMBetweenCoordinates
* @description Calculates the distance of two coordinates in meters.
* @see https://stackoverflow.com/questions/365826/calculate-distance-between-2-gps-coordinates/365853#365853
*
* @param {number} lat1 - Latitude of point 1.
* @param {number} lon1 - Longitude of point 1.
* @param {number} lat2 - Latitude of point 2.
* @param {number} lon2 - Longitude of point 2.
*
* @returns {number} Distance in meters rounded to the closest 100m.
*/
function distanceInMBetweenCoordinates(lat1, lon1, lat2, lon2) {
const earthRadiusM = 6371000;
const dLat = degreesToRadians(lat2 - lat1);
const dLon = degreesToRadians(lon2 - lon1);
lat1 = degreesToRadians(lat1);
lat2 = degreesToRadians(lat2);
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return Math.round(earthRadiusM * c / 100) * 100;
}
/**
* @function setStationInfos
* @description Initializes the gas station information.
* @async
*
* @param {Object[]} stationsByRadius - Gas Stations by radius. Used to filter out possible duplicate stations.
*
* @returns {void}
*/
async function setStationInfos(stationsByRadius) {
for (const station of stationsByRadius) {
config.stationIds = config.stationIds.filter(id => id !== station.id);
}
if (config.stationIds.length > 10) {
Log.warn(`MMM-Fuel: You can only ask for a maximum of 10 station prices`);
config.stations = config.stationIds.slice(0, 10);
}
stationInfos = {};
for (const stationId of config.stationIds) {
const response = await fetch(generateStationInfoUrl(stationId));
const parsedResponse = await response.json();
if (!parsedResponse.ok) {
Log.warn(`MMM-Fuel: No fuel station detail. StationId: ${stationId} Error: ${parsedResponse.message}`);
continue;
}
const station = parsedResponse.station;
const distanceMeters = distanceInMBetweenCoordinates(config.lat, config.lng, station.lat, station.lng);
stationInfos[station.id] = { ...station, dist: distanceMeters / 1000 };
}
}
/**
* @function getPricing
* @description Helper function to calculate prices for getPricesByStationList.
* @async
*
* @returns {Object} Fuel prices for all types.
*/
function getPricing({ status, ...prices }) {
const pricing = { diesel: -1, e5: -1, e10: -1 };
if (status !== 'open') {
return pricing;
}
for (const type in prices) {
if (prices[type]) {
pricing[type] = prices[type];
}
}
return pricing;
}
/**
* @function getPricesByStationList
* @description Fetches the prices by station ID list.
* @async
*
* @param {Object[]} stationsByRadius - Gas Stations by radius. Used to filter out possible duplicate stations.
*
* @returns {Object[]} List of stations in raw format.
*/
async function getPricesByStationList(stationsByRadius) {
if (!stationInfos) {
await setStationInfos(stationsByRadius);
}
const stationIds = Object.keys(stationInfos);
const stations = [];
if (!stationIds.length) {
Log.warn('MMM-Fuel: Filtered stationIds list is empty');
return stations;
}
const response = await fetch(generateStationPricesUrl(stationIds));
const parsedResponse = await response.json();
if (!parsedResponse.ok) {
throw new Error('Error no fuel station prices');
}
for (const [stationId, info] of Object.entries(parsedResponse.prices)) {
stations.push({
...stationInfos[stationId],
...getPricing(info),
isOpen: info.status !== 'closed'
});
}
return stations;
}
/**
* @function getData
* @description Performs the data query and processing.
* @async
*
* @returns {Object} Returns object described in the provider documentation.
*
* @see apis
*/
async function getData() {
let stations = [];
if (config.radius > 0) {
stations = stations.concat(await getPricesByRadius());
}
stations = stations.concat(await getPricesByStationList(stations));
const stationsFiltered = stations.filter(filterStations);
stationsFiltered.forEach(normalizeStations);
return {
types: ['diesel', 'e5', 'e10'],
unit: 'kilometer',
currency: 'EUR',
byPrice: _.sortBy(stationsFiltered, sortByPrice.bind(null, config)),
byDistance: _.sortBy(stationsFiltered, 'dist')
};
}
/**
* @module apis/tankerkoenig
* @description Queries data from tankerkoenig.de
*
* @requires external:node-fetch
* @requires external:logger
*
* @param {Object} options - Configuration.
* @param {number} options.lat - Latitude of Coordinate.
* @param {number} options.lng - Longitude of Coordinate.
* @param {int} options.radius - Lookup area for gas stations.
* @param {string} options.sortBy - Type to sort by price.
* @param {string[]} options.types - Requested fuel types.
* @param {boolean} options.showOpenOnly - Flag to show only open gas stations.
*
* @returns {Object} Object with function getData.
*
* @see https://creativecommons.tankerkoenig.de/
*/
module.exports = options => {
config = options;
return { getData };
};