-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-Bensinpriser.js
173 lines (141 loc) · 5.68 KB
/
MMM-Bensinpriser.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
Module.register("MMM-Bensinpriser", {
defaults: {
apiURL: "https://api.drivstoffappen.no/api/stations?stationType=0&countryCode=no",
apiKey: "YOUR_API_KEY",
coordinates: {
latitude: 59.910981, // Aker Brygge latitude, Oslo
longitude: 10.727169, // Aker Brygge longitude, Oslo
},
numberOfStations: 3, // Number of gas stations to show
updateInterval: 180, // Update interval in minutes (default 3 hours)
fuelTypes: ["95", "D"], // The fuel types you want to display (95, 98, 100, D, FD or EN590)
decimalSeparator: "." // You can set it to "." or ","
},
start: function () {
this.wrapper = document.createElement("div");
this.wrapper.className = "fuel-price-wrapper";
this.content = document.createElement("div");
this.content.className = "fuel-price-content";
this.wrapper.appendChild(this.content);
this.loadLanguageJSON(config.language);
this.loadData();
this.scheduleUpdate();
},
scheduleUpdate: function () {
const self = this;
setInterval(function () {
self.loadData();
}, this.config.updateInterval * 60000);
},
// ... other functions ...
loadData: function () {
const url = `${this.config.apiURL}`;
const headers = {
"X-API-KEY": this.config.apiKey,
};
const self = this;
fetch(url, { headers })
.then(response => response.json())
.then(data => {
if (!data || data.statusCode === 401) {
self.content.innerHTML = self.translate("unauthorized");
return;
}
const sortedStations = self.sortStationsByDistance(data);
const nearestStations = sortedStations.slice(0, self.config.numberOfStations);
const table = self.createTable(nearestStations);
self.content.innerHTML = "";
self.content.appendChild(table);
})
.catch(error => {
console.error("Error fetching data:", error);
self.content.innerHTML = self.translate("errorFetching");
});
},
sortStationsByDistance: function (stations) {
const { latitude, longitude } = this.config.coordinates;
stations.forEach(station => {
const stationLatitude = parseFloat(station.latitude);
const stationLongitude = parseFloat(station.longitude);
const distance = this.calculateDistance(latitude, longitude, stationLatitude, stationLongitude);
station.distance = distance;
});
stations.sort((a, b) => a.distance - b.distance);
return stations;
},
deg2rad: function (deg) {
return deg * (Math.PI / 180);
},
// Calculating distance between two points on a globe
calculateDistance: function (lat1, lon1, lat2, lon2) {
const R = 6371;
const dLat = this.deg2rad(lat2 - lat1);
const dLon = this.deg2rad(lon2 - lon1);
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(this.deg2rad(lat1)) * Math.cos(this.deg2rad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const distance = R * c;
return distance;
},
createTable: function (stations) {
const self = this;
const table = document.createElement("table");
table.className = "fuel-price-table";
const headerRow = document.createElement("tr");
headerRow.innerHTML = `<th></th><th>${self.translate("stationHeader")}</th>${this.config.fuelTypes.map(type => `<th>${type}</th>`).join("")}`;
table.appendChild(headerRow);
updates = [];
for (const station of stations) {
const stationRow = document.createElement("tr");
const stationName = station.name + (station.extras?.facilities?.airPump ? " 🧰" : ""); // Add marker if there are airpumps at the given station
stationRow.innerHTML = `<td><img class="station-icon" src="${station.pictureUrl}"></td><td>${stationName}</td>${this.config.fuelTypes.map(type => this.getPriceCell(station, type)).join("")}`;
table.appendChild(stationRow);
updates.push(new Date(station.lastUpdated));
}
const wrapper = document.createElement("div");
wrapper.appendChild(table);
const lastUpdate = document.createElement("p");
lastUpdate.className = "fuel-last-updated";
const seconds = Math.floor((new Date() - new Date(Math.min.apply(null, updates))));
// If time is more than 30 minutes, show hours
if (seconds > 1800000) {
lastUpdate.innerHTML = `Sist oppdatert for ${Math.round(seconds / 3600000)} ${Math.round(seconds / 3600000) > 1 ? "timer" : "time"} siden`;
} else {
lastUpdate.innerHTML = `Oppdatert nå nettopp`;
}
wrapper.appendChild(lastUpdate);
return wrapper;
},
getPriceCell: function (station, type) {
const fuelTypeDetails = station.stationDetails.find(detail => detail.type === type);
const decimalSeparator = this.config.decimalSeparator; // Get the configured decimal separator
const priceFormatted = fuelTypeDetails ? `${fuelTypeDetails.price.toFixed(2).replace(".", decimalSeparator)} kr` : "";
return `<td>${priceFormatted}</td>`;
},
loadLanguageJSON: function (language) {
const self = this;
const file = this.file(`translations/${language}.json`);
const xhr = new XMLHttpRequest();
xhr.overrideMimeType("application/json");
xhr.open("GET", file, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
self.translateJSON = JSON.parse(xhr.responseText);
}
};
xhr.send(null);
},
translate: function (key) {
if (this.translateJSON && this.translateJSON[key]) {
return this.translateJSON[key];
}
return key;
},
getStyles: function () {
return ["MMM-Bensinpriser.css"]; // Add your CSS file
},
getDom: function () {
return this.wrapper;
},
});