Repository navigation
Expand file tree
/
Copy pathMMM-SystemTemperature.js
More file actions
168 lines (141 loc) · 4.51 KB
/
Copy pathMMM-SystemTemperature.js
File metadata and controls
168 lines (141 loc) · 4.51 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
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
/* global Module */
const MIN_UPDATE_INTERVAL = 1000;
const MAX_UPDATE_INTERVAL = 86400000;
const VALID_UNITS = ["c", "f", "k"];
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() {
const configuredInterval = Number.isFinite(this.config.updateInterval)
? this.config.updateInterval
: this.defaults.updateInterval;
this.config.updateInterval = Math.max(
MIN_UPDATE_INTERVAL,
Math.min(configuredInterval, MAX_UPDATE_INTERVAL)
);
const unit = String(this.config.unit ?? "")
.trim()
.toLowerCase();
this.config.unit = VALID_UNITS.includes(unit) ? unit : this.defaults.unit;
this.sendSocketNotification("CONFIG", this.config);
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.sensorUnavailable = false;
this.stateConfig = this.getStateConfigByTemperature();
this.updateDom(this.config.animationSpeed);
this.commandExecutor();
} else if (notification === "TEMPERATURE_UNAVAILABLE") {
this.sensorUnavailable = true;
this.updateDom(this.config.animationSpeed);
}
},
getDom() {
const wrapper = document.createElement("div");
if (this.sensorUnavailable) {
wrapper.append("Temperature unavailable");
} else 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;
let lastStateConfig = null;
const alertedStates = new Set();
const context = this;
return function stateChangeExecutor() {
const stateConfig = context.stateConfig ?? null;
if (stateConfig === lastStateConfig) return;
lastStateConfig = stateConfig;
if (timeoutId !== null) {
clearTimeout(timeoutId);
timeoutId = null;
}
if (!stateConfig) {
alertedStates.clear();
return;
}
if (!stateConfig.command || alertedStates.has(stateConfig)) return;
timeoutId = setTimeout(() => {
timeoutId = null;
if (context.stateConfig !== stateConfig) return;
alertedStates.add(stateConfig);
context.sendNotification(
stateConfig.command.notification,
stateConfig.command.payload
);
}, wait);
};
}
});