forked from yourdawi/MMM-Blitzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMM-Blitzer.js
More file actions
106 lines (90 loc) · 3.6 KB
/
Copy pathMMM-Blitzer.js
File metadata and controls
106 lines (90 loc) · 3.6 KB
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
/* MagicMirror Module: MMM-Blitzer
* By Dawi
* MIT Licensed.
*/
Module.register("MMM-Blitzer", {
defaults: {
latitude: 52.5200,
longitude: 13.4050,
distance: 10,
updateInterval: 10 * 60 * 1000,
},
start: function() {
this.loaded = false;
this.getData();
this.scheduleUpdate();
},
getData: function() {
const targetDistance_km = this.config.distance; // +/- 1 km vom Zentrum aus
// Konstanten für die Umrechnung (für Breitengrad 49.24°, basierend auf this.config.latitude)
const kmPerDegreeLatitude = 111.32;
const latitudeInRadians = this.config.latitude * (Math.PI / 180);
const kmPerDegreeLongitude = kmPerDegreeLatitude * Math.cos(latitudeInRadians);
// Berechnung der Delta-Werte in Grad (Delta-Werte für 1 km)
const deltaLatitude = targetDistance_km / kmPerDegreeLatitude; // ≈ 0.00898 Grad
const deltaLongitude = targetDistance_km / kmPerDegreeLongitude; // ≈ 0.01377 Grad
// --- Konstruktion der URL mit dynamischen Box-Grenzen ---
const url = `https://cdn2.atudo.net/api/4.0/pois.php?type=ts,0,1,2,3,4,5,6&box=${this.config.latitude - deltaLatitude},${this.config.longitude - deltaLongitude},${this.config.latitude + deltaLatitude},${this.config.longitude + deltaLongitude}&z=18`;
console.log(`https://www.google.com/maps/dir/${this.config.latitude - deltaLatitude},${this.config.longitude - deltaLongitude}/${this.config.latitude + deltaLatitude},${this.config.longitude + deltaLongitude}`);
console.log(url);
fetch(url)
.then(response => response.json())
.then(data => {
this.processData(data.pois);
})
.catch(error => {
console.error("Error fetching data: ", error);
});
},
processData: function(data) {
this.blitzer = data.map(blitzer => (blitzer));
this.loaded = true;
this.updateDom();
},
scheduleUpdate: function() {
setInterval(() => {
this.getData();
}, this.config.updateInterval);
},
getDom: function() {
const wrapper = document.createElement("div");
if (!this.loaded) {
wrapper.innerHTML = "Loading...";
return wrapper;
}
//console.error('blitzer',this.blitzer);
const header = document.createElement("div");
header.style.display = "flex";
header.style.alignItems = "center";
header.style.justifyContent = "flex-end";
const alertText = document.createElement("div");
alertText.innerHTML = "<h2>Blitzer:</h2>";
header.appendChild(alertText);
const list = document.createElement("ul");
list.style.textAlign = "right";
list.style.listStyleType = "none";
if(this.blitzer.length !== 0)
{
this.blitzer.forEach(blitzer => {
const listItem = document.createElement("li");
let stars = '';
for (let i = 0; i < blitzer.counter; i++) {
stars += '<img src="https://map.blitzer.de/v5/images/star_full.svg" width="12">';
}
for (let i = 0; i < (3 - blitzer.counter); i++) {
stars += '<img src="https://map.blitzer.de/v5/images/star_contour.svg" width="12">';
}
listItem.innerHTML = `${blitzer.address.city}, ${blitzer.address.street}, ${blitzer.vmax} km/h ` + stars;
list.appendChild(listItem);
});
}
else
{
const listItem = document.createElement("li");
listItem.innerHTML = `Keine`;
list.appendChild(listItem);
}
wrapper.appendChild(list);
return wrapper;
}
});