This repository has been archived by the owner on Jul 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
498 lines (458 loc) · 16.9 KB
/
main.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
'use strict';
/*
* Created with @iobroker/create-adapter v1.24.2
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
const {default: axios} = require('axios');
const object = require('./lib/object_definition');
const logos = require('./lib/logoName');
// Load your modules here, e.g.:
const object_openingHours = object['object_openingHours_definitions'];
const object_prices = object['object_prices_definitions'];
const object_jsonTable = object['object_jsonTable_definitions'];
const object_main = object['object_main_definitions'];
const object_logo = object['object_logo_definitions'];
const logosName = logos['logosName'];
let timer = null;
let requestTimeout = null;
const apiUrl = [];
const cityName = [];
const fuel = [];
// const apiResult = [];
const weekDay = [`Montag`, `Dienstag`, `Mittwoch`, `Donnerstag`, `Feiertag`, `Samstag`, `Sonntag`, `Freitag`];
const format = [`svg`, `png`];
class EControlAtFuel extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'e-control-at-fuel',
});
this.on('ready', this.onReady.bind(this));
this.on('unload', this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
// Reset the connection indicator during startup
this.setState('info.connection', false, true);
// timer control min limit.
timer = this.config.interval;
if (timer < 10) {
timer = 10;
this.log.warn(`Attention the polling interval is below the minimum limit, please set it to at least 10 min`);
}
for (const i in this.config.address) {
// @ts-ignore
cityName[i] = await this.replaceFunction(this.config.address[i].city);
}
await this.api();
await this.request();
}
async api() {
try {
for (const i in this.config.address) {
// @ts-ignore
const latitude = this.config.address[i].latitude;
// @ts-ignore
const longitude = this.config.address[i].longitude;
// @ts-ignore
const fuelType = this.config.address[i].fuelType;
switch (fuelType) {
case 'GAS':
fuel[i] = 'CNG';
break;
case 'DIE':
fuel[i] = 'diesel';
break;
case 'SUP':
fuel[i] = 'super_95';
break;
default:
}
apiUrl[i] = `https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=${latitude}&longitude=${longitude}&fuelType=${fuelType}&includeClosed=true`;
}
} catch (error) {
this.log.error(`[api construction]: ${error.message}, stack: ${error.stack}`);
}
}
async request() {
for (const i in this.config.address) {
try {
// Try to reach API and receive data
const apiResult = await axios.get(apiUrl[i]);
await this.create_States(apiResult, parseInt(i));
await this.writeState(apiResult, parseInt(i));
} catch (error) {
this.log.error(`[request] Unable to contact: ${error} | ${error}`);
}
}
this.setState('info.connection', true, true);
requestTimeout = setTimeout(async () => {
await this.request();
}, timer * 60000);
}
/**
* here the values are written into the data points
* @param {object} apiResult // query the object from the api
* @param {number} index // number of the device
* @return {Promise<void>}
*/
async writeState(apiResult, index) {
try {
const openingHours = [];
const stationAddress = [];
const stationCity = [];
const stationPostalCode = [];
const stationOpen = [];
const stationDistance = [];
const stationName = [];
const prices = [];
const stationFuelType = [];
const open = [];
let number_of_station = -1;
for (const c in apiResult.data) {
if (apiResult.data[c].prices[0]) {
number_of_station = number_of_station + 1;
}
}
const jsonTable = [];
for (const c in apiResult.data) {
if (apiResult.data[c].prices[0]) {
const result = apiResult.data[c];
stationName[c] = result.name.replace(/,/gi, ' ');
const stationLogoName = stationName[c].split(' ');
stationAddress[c] = result.location.address;
stationCity[c] = result.location.city;
stationPostalCode[c] = result.location.postalCode;
stationOpen[c] = result.open;
stationDistance[c] = Math.round(result.distance * 100) / 100;
for (const u in result.openingHours) {
openingHours[u] = result.openingHours[u].from + ' to ' + result.openingHours[u].to;
}
const State_id = [`station_${[c]}.name`, `station_${[c]}.city`, `station_${[c]}.open`, `station_${[c]}.distance`, `station_${[c]}.address`, `station_${[c]}.lastRequest`, `station_${[c]}.prices.price`,
`station_${[c]}.postalCode`, `station_${[c]}.prices.fuelType`, `station_${[c]}.prices.price3rd`, `station_${[c]}.prices.priceshort`, `station_${[c]}.prices.combined`, `jsonTable`];
const states = [];
if (result.prices[0]) {
let States_value;
const stationPrices = result.prices[0].amount;
stationFuelType[c] = result.prices[0].label;
prices[c] = await this.cutPrice(stationPrices);
this.log.debug(`stationPrices: ${stationPrices}`);
this.log.debug(`stationFuelType: ${stationFuelType}`);
this.log.debug(`prices: ${JSON.stringify(prices[c])}`);
if (stationOpen[c]) {
open[c] = 'open';
}
else {
open[c] = 'closed';
}
// Json table is created here
jsonTable[c] = {
'Station Name': stationName[c],
'Stadt': stationCity[c],
'Address': stationAddress[c],
'Preis': (prices[c]).priceshort + ' €',
'Fuel_Typ': stationFuelType[c],
'Distance': stationDistance[c] + ' km',
'Open': open[c]
};
// All values are written into an array
if (stationOpen[c]) {
States_value = [`${stationName[c]}`, `${stationCity[c]}`, `${stationOpen[c]}`, `${stationDistance[c]}`, `${stationAddress[c]}`, parseInt(`${Date.now()}`), `${(prices[c]).price}`,
`${stationPostalCode[c]}`, `${stationFuelType[c]}`, `${(prices[c]).price3rd}`, `${(prices[c]).priceshort}`, `<span class="station_open"> ${(prices[c]).priceshort}<sup style="font-size: 50%"> ${(prices[c]).price3rd} </sup> <span class="station_combined_euro">€</span></span>`,
`${JSON.stringify(jsonTable)}`];
}
else {
States_value = [`${stationName[c]}`, `${stationCity[c]}`, `${stationOpen[c]}`, `${stationDistance[c]}`, `${stationAddress[c]}`, parseInt(`${Date.now()}`), `${(prices[c]).price}`,
`${stationPostalCode[c]}`, `${stationFuelType[c]}`, `${(prices[c]).price3rd}`, `${(prices[c]).priceshort}`, `<span class="station_closed"> ${(prices[c]).priceshort}<sup style="font-size: 50%"> ${(prices[c]).price3rd} </sup> <span class="station_combined_euro">€</span></span>`,
`${JSON.stringify(jsonTable)}`];
}
// push all dp and values into an object ( id: e-control-at-fuel.0.salzburg_super_95.station_0.name , value: AVIA )
for (const Key in State_id) {
states.push(
{
'State': `${cityName[index]}_${fuel[index]}.${State_id[Key]}`,
'State_value': States_value[Key]
}
);
}
// here the cheapest gas station is written into the data points
if (c === '0') {
const State_id_cheapest = ['cheapest.name', 'cheapest.city', 'cheapest.open', 'cheapest.distance', 'cheapest.address', 'cheapest.lastRequest', 'cheapest.prices.price',
'cheapest.postalCode', 'cheapest.prices.fuelType', 'cheapest.prices.price3rd', 'cheapest.prices.priceshort', 'cheapest.prices.combined'];
// push all dp and values for cheapest gas station into an object ( id: e-control-at-fuel.0.salzburg_super_95.cheapest.name , value: AVIA )
for (const Key in State_id_cheapest) {
states.push(
{
'State': `${cityName[index]}_${fuel[index]}.${State_id_cheapest[Key]}`,
'State_value': States_value[Key]
}
);
}
}
if (result.prices[0]) {
// here are the gas stations logos written in the dp
for (const logo in logosName) {
this.log.debug(`logosName : ${JSON.stringify(logosName)}`);
if (stationLogoName[0].toLowerCase().replace(/-/gi, '_') === logosName[logo]) {
// push all Dp and values for the logos into one object
for (const f in format) {
if (c === '0') {
states.push(
{
'State': `${cityName[index]}_${fuel[index]}.station_${[c]}.logo_${format[f]}`,
'State_value': `/e-control-at-fuel.admin/logo/${format[f]}/${logo}.${format[f]}`
},
{
'State': `${cityName[index]}_${fuel[index]}.cheapest.logo_${format[f]}`,
'State_value': `/e-control-at-fuel.admin/logo/${format[f]}/${logo}.${format[f]}`
},
);
}
else {
states.push(
{
'State': `${cityName[index]}_${fuel[index]}.station_${[c]}.logo_${format[f]}`,
'State_value': `/e-control-at-fuel.admin/logo/${format[f]}/${logo}.${format[f]}`
},
);
}
}
// here is written the cheapest gas station logo no.
if (c === '0') {
states.push(
{
'State': `${cityName[index]}_${fuel[index]}.station_${[c]}.logo_Nr`,
'State_value': `${logo}`
},
{
'State': `${cityName[index]}_${fuel[index]}.cheapest.logo_Nr`,
'State_value': `${logo}`
}
);
}
else {
states.push(
{
'State': `${cityName[index]}_${fuel[index]}.station_${[c]}.logo_Nr`,
'State_value': `${logo}`
}
);
}
}
}
}
else {
// pushes all Dp for the logos where the values are to be deleted into an object
const delete_Logo = [];
for (const f in format) {
delete_Logo.push(
{
'State': `${cityName[index]}_${fuel[index]}.station_${[c]}.logo_${format[f]}`
},
{
'State': `${cityName[index]}_${fuel[index]}.station_${[c]}.logo_Nr`
},
);
}
// Deletes all values in the Dp that stick in the object
for (const delete_LogoKey in delete_Logo) {
await this.setStateAsync(delete_Logo[delete_LogoKey].State, '', true);
this.log.debug(`${delete_Logo[delete_LogoKey].State}: 'station not found'`);
}
}
// push all Dp and values for openingHours into one object
for (const d in openingHours) {
if (result.prices[0]) {
if (c === '0') {
states.push(
{
'State': `${cityName[index]}_${fuel[index]}.station_${[c]}.openingHours.${weekDay[d]}`,
'State_value': `${openingHours[d]}`
},
{
'State': `${cityName[index]}_${fuel[index]}.cheapest.openingHours.${weekDay[d]}`,
'State_value': `${openingHours[d]}`
}
);
}
else {
states.push(
{
'State': `${cityName[index]}_${fuel[index]}.station_${[c]}.openingHours.${weekDay[d]}`,
'State_value': `${openingHours[d]}`
},
);
}
}
else {
this.setState(`${cityName[index]}_${fuel[index]}.station_${[c]}.openingHours.${weekDay[d]}`, ``, true);
}
}
}
else { //Empty data points if there is no station
const State_id = [`station_${[c]}.name`, `station_${[c]}.city`, `station_${[c]}.open`, `station_${[c]}.distance`, `station_${[c]}.address`, `station_${[c]}.lastRequest`, `station_${[c]}.prices.price`,
`station_${[c]}.postalCode`, `station_${[c]}.prices.fuelType`, `station_${[c]}.prices.price3rd`, `station_${[c]}.prices.priceshort`, `station_${[c]}.prices.combined`];
// push all Dp where the value should be deleted into an object
const delete_State_id = [];
delete_State_id.push(
{
'State': `${cityName[index]}_${fuel[index]}.${State_id}`
}
);
// Deletes all values in the Dp that stick in the object
for (const delete_Key in delete_State_id) {
await this.setStateAsync(delete_State_id[delete_Key].State, '', true);
this.log.debug(`${cityName[index]}_${fuel[index]}.station_${[c]} station not found`);
}
}
// Writes all values from the Object into the Dp
for (const stateKey in states) {
await this.setStateAsync(states[stateKey].State, states[stateKey].State_value, true);
this.log.debug(`${states[stateKey].State}: ${states[stateKey].State_value}`);
}
}
else {
this.log.debug(`${cityName[index]}_${fuel[index]}.station_${[c]} station has no price`);
}
}
} catch (error) {
this.log.warn(`[writeState]: ${error.message}, stack: ${error.stack}`);
}
}
/**
* here the data points are created (must be executed first)
* @param {object} apiResult // Query the object from the API
* @param {number} index // number of the device
* @return {Promise<void>}
*/
async create_States(apiResult, index) {
try {
for (const c in apiResult.data) {
if (apiResult.data[c].prices[0]) {
const folder = [`cheapest`, `station_${[c]}`];
await this.setObjectNotExistsAsync(`${cityName[index]}_${fuel[index]}`, {
type: 'channel',
common: {
name: `${cityName[index]} for ${fuel[index]}`
},
native: {}
});
for (const v in folder) {
await this.setObjectNotExistsAsync(`${cityName[index]}_${fuel[index]}.${folder[v]}.openingHours`, {
type: 'channel',
common: {
name: `openingHours`
},
native: {}
});
await this.setObjectNotExistsAsync(`${cityName[index]}_${fuel[index]}.${folder[v]}.prices`, {
type: 'channel',
common: {
name: `prices`
},
native: {}
});
await this.setObjectNotExistsAsync(`${cityName[index]}_${fuel[index]}.${folder[v]}`, {
type: 'channel',
common: {
name: `${folder[v]}`
},
native: {}
});
for (const obj in object_openingHours) {
await this.setObjectNotExistsAsync(`${cityName[index]}_${fuel[index]}.${folder[v]}.openingHours.${obj}`, object_openingHours[obj]);
}
for (const obj in object_prices) {
await this.setObjectNotExistsAsync(`${cityName[index]}_${fuel[index]}.${folder[v]}.prices.${obj}`, object_prices[obj]);
}
for (const obj in object_jsonTable) {
await this.setObjectNotExistsAsync(`${cityName[index]}_${fuel[index]}.${obj}`, object_jsonTable[obj]);
}
for (const obj in object_main) {
await this.setObjectNotExistsAsync(`${cityName[index]}_${fuel[index]}.${folder[v]}.${obj}`, object_main[obj]);
}
for (const obj in object_logo) {
await this.setObjectNotExistsAsync(`${cityName[index]}_${fuel[index]}.${folder[v]}.${obj}`, object_logo[obj]);
}
}
}
}
} catch (error) {
this.log.error(`[create_States]: ${error.message}, stack: ${error.stack}`);
}
}
// Decimal point calculation and separation function
/**
*
* @param {number|string} fuel_price
* @return {Promise<{price3rd: number, price: number, priceshort: string}>}
*/
async cutPrice(fuel_price) {
// @ts-ignore
fuel_price = parseFloat(fuel_price);
let temp = fuel_price * 100; // the price now with one decimal place
const temp2 = fuel_price * 1000; // Price without decimal place
temp = Math.floor(temp); // Decimal place (.x) is cut off
temp = temp / 100; // there are two decimal places
const price_short = temp.toFixed(2); // Output price with 2 decimal places (cut off)
const price_3rd_digit = Math.ceil(temp2 - (temp * 1000)); // Determine the third decimal place individually
return {
priceshort: price_short, // as a string Zeros e.g. 1.10 instead of 1.1
// @ts-ignore
price3rd: parseInt(price_3rd_digit, 10),
price: fuel_price
};
}
/**
* replace function for strings
* @param {string} str // a string
* @return {Promise<string|undefined>}
*/
async replaceFunction(str) {
if (str) {
str = str.replace(/ü/g, 'ue');
str = str.replace(/Ü/g, 'Ue');
str = str.replace(/ö/g, 'oe');
str = str.replace(/Ö/g, 'Oe');
str = str.replace(/Ä/g, 'Ae');
str = str.replace(/ä/g, 'ae');
str = str.replace(/\.*\./gi, '_');
str = str.replace(/ /gi, '_');
str = str.toLowerCase();
return str;
}
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
if (requestTimeout) clearTimeout(requestTimeout);
this.setState('info.connection', false, true);
callback();
} catch (e) {
callback();
}
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new EControlAtFuel(options);
}
else {
// otherwise start the instance directly
new EControlAtFuel();
}