-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmmm-systemtemperature.js
More file actions
84 lines (73 loc) · 2.63 KB
/
Copy pathmmm-systemtemperature.js
File metadata and controls
84 lines (73 loc) · 2.63 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
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: function () {
return ["mmm-systemtemperature.css", "font-awesome.css"];
},
getScripts: function () {
return [`modules/${this.name}/node_modules/lodash/lodash.js`];
},
start: function() {
this.sendSocketNotification('CONFIG', this.config);
this.config.unit = this.config.unit && this.config.unit.toLowerCase();
this.commandExecutor = this.getCommandExecutor();
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'TEMPERATURE') {
this.temperature = parseFloat(payload);
this.stateConfig = this.getStateConfigByTemperature() || {};
this.updateDom(this.config.animationSpeed);
this.commandExecutor();
}
},
getDom: function() {
var wrapper = document.createElement("div");
if (this.temperature) {
wrapper.innerHTML = this.config.prependString + this.getTemperatureLabel();
wrapper.style.color = this.stateConfig.color || "";
} else {
wrapper.innerHTML = `<i class="fas fa-spinner fa-spin"></i> ${this.translate("LOADING")}`;
}
return wrapper;
},
getTemperatureLabel: function() {
return `<span class="temperatureLabel">
<span class="temperatureValue">${this.getConvertedTemperature()}</span>
<span class="temperatureUnit">${this.config.unit.toUpperCase()}</span>
</span>`;
},
getStateConfigByTemperature: function() {
if (this.config.critical && this.temperature >= this.config.critical.temp) {
return this.config.critical;
} else if (this.config.warning && this.temperature >= this.config.warning.temp) {
return this.config.warning;
}
},
getConvertedTemperature: function() {
if (this.temperature && this.config.unit !== 'c') {
var convertedTemperature;
if (this.config.unit === 'f') {
convertedTemperature = this.temperature * 9 / 5 + 32;
} else if (this.config.unit === 'k') {
convertedTemperature = this.temperature - 273.15;
}
// Round off the temperature to 2 decimal places
return Math.round(convertedTemperature * 100) / 100;
}
return this.temperature;
},
getCommandExecutor: function() {
return _.throttle(() => {
if (this.stateConfig && this.stateConfig.command) {
const command = this.stateConfig.command;
this.sendNotification(command.notification, command.payload);
}
}, this.config.updateInterval * 5, { leading: false, trailing: true });
}
});