forked from MichMich/mmm-systemtemperature
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMM-SystemTemperature.js
More file actions
133 lines (111 loc) · 3.4 KB
/
Copy pathMMM-SystemTemperature.js
File metadata and controls
133 lines (111 loc) · 3.4 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
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
/* global Module */
Module.register("MMM-SystemTemperature", {
defaults: {
prependString: "System temperature: ",
updateInterval: 5000,
animationSpeed: 0,
unit: "c",
warning: {
temp: 60,
color: "orange",
command: undefined
},
critical: {
temp: 75,
color: "red",
command: {
notification: "REMOTE_ACTION",
payload: { action: "SHUTDOWN" }
}
}
},
getStyles() {
return ["MMM-SystemTemperature.css", "font-awesome.css"];
},
start() {
this.sendSocketNotification("CONFIG", this.config);
this.config.unit = this.config.unit && this.config.unit.toLowerCase();
this.commandExecutor = this.getCommandExecutor();
},
socketNotificationReceived(notification, payload) {
if (notification === "TEMPERATURE") {
const temperature = Number.parseFloat(payload);
this.temperature =
Number.isFinite(temperature) && temperature !== 0 ? temperature : null;
this.stateConfig = this.getStateConfigByTemperature();
this.updateDom(this.config.animationSpeed);
this.commandExecutor();
}
},
getDom() {
const wrapper = document.createElement("div");
if (Number.isFinite(this.temperature)) {
wrapper.append(this.config.prependString, this.getTemperatureLabel());
wrapper.style.color = this.stateConfig?.color || "";
} else {
const spinner = document.createElement("i");
spinner.className = "fas fa-spinner fa-spin";
wrapper.append(spinner, ` ${this.translate("LOADING")}`);
}
return wrapper;
},
getTemperatureLabel() {
const label = document.createElement("span");
const value = document.createElement("span");
const unit = document.createElement("span");
label.className = "temperatureLabel";
value.className = "temperatureValue";
unit.className = "temperatureUnit";
value.textContent = this.getConvertedTemperature();
unit.textContent = this.config.unit.toUpperCase();
label.append(value, unit);
return label;
},
getStateConfigByTemperature() {
if (!this.temperature) return null;
const { critical, warning } = this.config;
if (critical && this.temperature >= critical.temp) {
return critical;
} else if (warning && this.temperature >= warning.temp) {
return warning;
}
return null;
},
getConvertedTemperature() {
if (!this.temperature) return this.temperature;
const unit = this.config.unit;
if (unit === "c") return this.temperature;
let convertedTemp;
switch (unit) {
case "f":
convertedTemp = (this.temperature * 9) / 5 + 32;
break;
case "k":
convertedTemp = this.temperature + 273.15;
break;
default:
return this.temperature;
}
// Round to 2 decimal places
return Math.round(convertedTemp * 100) / 100;
},
getCommandExecutor() {
const wait = this.config.updateInterval * 5;
let timeoutId = null;
const context = this;
return function throttledExecutor() {
if (!context.stateConfig?.command) return;
if (timeoutId) return; // Already scheduled
timeoutId = setTimeout(() => {
timeoutId = null;
const { stateConfig } = context;
if (stateConfig?.command) {
context.sendNotification(
stateConfig.command.notification,
stateConfig.command.payload
);
}
}, wait);
};
}
});