Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions mmm-systemtemperature.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.mmm-systemtemperature .temperatureUnit::before {
content: '°';
display: inline;
}
content: "°";
display: inline;
}
157 changes: 86 additions & 71 deletions mmm-systemtemperature.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,99 @@
Module.register("mmm-systemtemperature",{
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" },
},
},
},

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"];
},

getStyles: function () {
return ["mmm-systemtemperature.css", "font-awesome.css"];
},
getScripts: function () {
return [`modules/${this.name}/node_modules/lodash/lodash.js`];
},

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();
},

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();
}
},

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;
},

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">
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;
},
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;
}
},

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 });
}
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 }
);
},
});
51 changes: 29 additions & 22 deletions node_helper.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
var NodeHelper = require("node_helper");
var exec = require('child_process').exec;
var exec = require("child_process").exec;

module.exports = NodeHelper.create({
start: function() {
console.log("Starting node helper: " + this.name);
},
start: function () {
console.log("Starting node helper: " + this.name);
},

// Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === 'CONFIG') {
this.config = payload;
setInterval(() => {
this.sendTemperature();
}, this.config.updateInterval);
}
},
// Subclass socketNotificationReceived received.
socketNotificationReceived: function (notification, payload) {
if (notification === "CONFIG") {
this.config = payload;
setInterval(() => {
this.sendTemperature();
}, this.config.updateInterval);
}
},

sendTemperature: function() {
exec("/opt/vc/bin/vcgencmd measure_temp", (error, stdout, stderr) => {
if (error) {
console.log(error);
return;
}
this.sendSocketNotification('TEMPERATURE', parseFloat(stdout.replace('temp=','')));
});
}
sendTemperature: function () {
let cmd = "/opt/vc/bin/vcgencmd measure_temp";
if (process.arch === "arm64") {
cmd = "vcgencmd measure_temp";
}
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.log(error);
return;
}
this.sendSocketNotification(
"TEMPERATURE",
parseFloat(stdout.replace("temp=", ""))
);
});
},
});
23 changes: 19 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
],
"license": "MIT",
"dependencies": {
"lodash": ">=4.17.19"
"lodash": ">=4.17.21"
}
}