-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-Temp2IOT.js
113 lines (93 loc) · 4.08 KB
/
MMM-Temp2IOT.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
Module.register("MMM-Temp2IOT", {
defaults: {
apiUrl: "http://192.168.178.140/api",
updateInterval: 60000, // Aktualisierung alle 60 Sekunden
replaceNames: ["Outside", "Shed"], // Benutzerdefinierte Namen für Sensoren
icons1: [
{ condition: "<0", icon: "twemoji:cold-face" },
{ condition: "<5", icon: "noto:cold-face" },
{ condition: "<15", icon: "fluent-emoji:cold-face" },
{ condition: "<20", icon: "noto:smiling-face-with-sunglasses" },
{ condition: "<25", icon: "noto:grinning-face-with-sweat" },
{ condition: "<30", icon: "noto:hot-face" },
{ condition: "<40", icon: "noto:hot-springs" },
{ condition: "<100", icon: "noto:exploding-head" }
],
icons2: [
{ condition: "<0", icon: "noto:snowflake" },
{ condition: "<10", icon: "noto:cool-button" },
{ condition: "<20", icon: "noto:check-mark-button" },
{ condition: "<30", icon: "noto:fast-up-button" },
{ condition: "<40", icon: "noto:sos-button" },
{ condition: "<100", icon: "noto:exploding-head" }
]
},
getScripts: function () {
return ["https://code.iconify.design/3/3.1.1/iconify.min.js"];
},
getStyles: function () {
return ["MMM-Temp2IOT.css"];
},
start: function () {
this.sensors = [];
this.sendSocketNotification("FETCH_DATA", this.config.apiUrl);
setInterval(() => {
this.sendSocketNotification("FETCH_DATA", this.config.apiUrl);
}, this.config.updateInterval);
},
getDom: function () {
const wrapper = document.createElement("div");
wrapper.className = "MMM-Temp2IOT";
if (this.sensors.length === 0) {
wrapper.innerHTML = "Loading...";
return wrapper;
}
this.sensors.forEach((sensor, index) => {
const sensorRow = document.createElement("div");
sensorRow.className = "sensor-row";
// Passendes Symbol auswählen
const selectedIcon = this.getIconForTemperature(sensor.value, index);
const icon = document.createElement("span");
icon.className = "iconify";
icon.setAttribute("data-icon", selectedIcon);
// Text (Name und Wert)
const textWrapper = document.createElement("div");
const name = document.createElement("span");
name.className = "sensor-name";
const displayName = this.config.replaceNames[index] || `Sensor ${index + 1}`;
name.textContent = `${displayName}:`;
const value = document.createElement("span");
value.className = "sensor-value";
value.textContent = ` ${sensor.value.toFixed(2)} °C`;
textWrapper.appendChild(name);
textWrapper.appendChild(value);
// In Zeile einfügen
sensorRow.appendChild(icon);
sensorRow.appendChild(textWrapper);
wrapper.appendChild(sensorRow);
});
return wrapper;
},
getIconForTemperature: function (temperature, sensorIndex) {
const iconConfig = sensorIndex === 0 ? this.config.icons1 : this.config.icons2;
console.log(`Checking icons for Sensor ${sensorIndex}, Temperature: ${temperature}`);
for (let i = 0; i < iconConfig.length; i++) {
const { condition, icon } = iconConfig[i];
if (condition.startsWith("<")) {
const threshold = parseFloat(condition.slice(1));
if (temperature < threshold) {
console.log(`Selected Icon: ${icon} for Temperature: ${temperature}`);
return icon;
}
}
}
console.log("No matching icon found, using default.");
return "mdi:thermometer"; // Fallback-Symbol
},
socketNotificationReceived: function (notification, payload) {
if (notification === "DATA_FETCHED") {
this.sensors = payload.sensors;
this.updateDom();
}
}
});