From 4f877bbe9e1bd20b46d077d3239cd5904c0b9f69 Mon Sep 17 00:00:00 2001
From: eremem <56274896+eremem@users.noreply.github.com>
Date: Fri, 17 May 2024 15:25:36 +0200
Subject: [PATCH] Temperature unit selection (#38)
Add temperature unit selection
---
pve-mod-gui-sensors.sh | 193 +++++++++++++++++++++++++++++++++--------
readme.md | 2 +-
2 files changed, 156 insertions(+), 39 deletions(-)
diff --git a/pve-mod-gui-sensors.sh b/pve-mod-gui-sensors.sh
index 9ab2b9c..153e15a 100644
--- a/pve-mod-gui-sensors.sh
+++ b/pve-mod-gui-sensors.sh
@@ -61,12 +61,12 @@ function install_packages {
# If the 'sensors' command is not available, prompt the user to install lm-sensors
read -p "lm-sensors is not installed. Would you like to install it? (y/n) " choice
case "$choice" in
- y | Y)
+ [yY])
# If the user chooses to install lm-sensors, update the package list and install the package
apt-get update
apt-get install lm-sensors
;;
- n | N)
+ [nN])
# If the user chooses not to install lm-sensors, exit the script with a zero status code
msg "Decided to not install lm-sensors. The mod cannot run without it. Exiting..."
exit 0
@@ -80,6 +80,7 @@ function install_packages {
}
function configure {
+ sensorsDetected=false
local sensorsOutput=$(sensors -j)
if [ $? -ne 0 ]; then
err "Sensor output error.\n\nCommand output:\n${sensorsOutput}\n\nExiting...\n"
@@ -111,10 +112,8 @@ function configure {
if [ -n "$CPU_ADDRESS_PREFIX" ]; then
msg "Detected sensors:\n$(echo "$sensorsOutput" | grep -o "\"${CPU_ADDRESS_PREFIX}[^\"]*\"" | sed 's/"//g')"
- fi
-
- # If cpu is not known, ask the user for input
- if [ -z "$CPU_ADDRESS_PREFIX" ]; then
+ else
+ # If cpu is not known, ask the user for input
warn "Could not automatically detect the CPU temperature sensor. Please configure it manually."
# Ask user for CPU information
# Inform the user and prompt them to press any key to continue
@@ -131,6 +130,8 @@ function configure {
if [[ -z "$CPU_ADDRESS_PREFIX" || -z "$CPU_ITEM_PREFIX" ]]; then
warn "The CPU configuration is not complete. Temperatures will not be available."
+ else
+ sensorsDetected=true
fi
# Check if HDD/SSD data is installed
@@ -140,11 +141,13 @@ function configure {
if (echo "$sensorsOutput" | grep -q "drivetemp-scsi-"); then
msg "Detected sensors:\n$(echo "$sensorsOutput" | grep -o '"drivetemp-scsi[^"]*"' | sed 's/"//g')"
enableHddTemp=true
+ sensorsDetected=true
else
warn "Kernel module \"drivetemp\" is not installed. HDD/SDD temperatures will not be available."
enableHddTemp=false
fi
else
+ warn "No HDD/SSD temperature sensors found."
enableHddTemp=false
fi
@@ -153,6 +156,7 @@ function configure {
if (echo "$sensorsOutput" | grep -q "nvme-"); then
msg "Detected sensors:\n$(echo "$sensorsOutput" | grep -o '"nvme[^"]*"' | sed 's/"//g')"
enableNvmeTemp=true
+ sensorsDetected=true
else
warn "No NVMe temperature sensors found."
enableNvmeTemp=false
@@ -163,11 +167,29 @@ function configure {
if (echo "$sensorsOutput" | grep -q "fan[0-9]*_input"); then
msg "Detected fan speed sensors:\n$(echo "$sensorsOutput" | grep -o 'fan[0-9]*_input[^"]*')"
enableFanSpeed=true
+ sensorsDetected=true
else
warn "No fan speed sensors found."
enableFanSpeed=false
fi
+ if [ $sensorsDetected = true ]; then
+ msg "\nSelect a unit for temperature readings..."
+ read -p "Type C for Celsius or F for Fahrenheit and press Enter: " TEMP_UNIT
+
+ case "$TEMP_UNIT" in
+ [cC])
+ TEMP_UNIT="C"
+ ;;
+ [fF])
+ TEMP_UNIT="F"
+ ;;
+ *)
+ warn "Invalid selection. Temperatures will be presented in degrees Celsius."
+ TEMP_UNIT="C"
+ ;;
+ esac
+ fi
echo # add a new line
}
@@ -189,7 +211,10 @@ function install_mod {
cp "$nodespm" "$BACKUP_DIR/Nodes.pm.$timestamp"
msg "Backup of \"$nodespm\" saved to \"$BACKUP_DIR/Nodes.pm.$timestamp\"."
- sed -i '/my $dinfo = df('\''\/'\'', 1);/i\'$'\t''$res->{sensorsOutput} = `sensors -j`;\n\t# sanitize JSON output\n\t$res->{sensorsOutput} =~ s/ERROR:.+\\s(\\w+):\\s(.+)/\\"$1\\": 0.000,/g;\n\t$res->{sensorsOutput} =~ s/ERROR:.+\\s(\\w+)!/\\"$1\\": 0.000,/g;\n\t$res->{sensorsOutput} =~ s/,(.*[.\\n]*.+})/$1/g;\n' "$nodespm"
+ # WTF: sensors -f used for Fahrenheit breaks the fan speeds :|
+ #local sensorsCmd=$([[ "$TEMP_UNIT" = "F" ]] && echo "sensors -j -f" || echo "sensors -j")
+ local sensorsCmd="sensors -j"
+ sed -i '/my \$dinfo = df('\''\/'\'', 1);/i\'$'\t''$res->{sensorsOutput} = `'"$sensorsCmd"'`;\n\t# sanitize JSON output\n\t$res->{sensorsOutput} =~ s/ERROR:.+\\s(\\w+):\\s(.+)/\\"$1\\": 0.000,/g;\n\t$res->{sensorsOutput} =~ s/ERROR:.+\\s(\\w+)!/\\"$1\\": 0.000,/g;\n\t$res->{sensorsOutput} =~ s/,(.*[.\\n]*.+})/$1/g;\n' "$nodespm"
msg "Sensors' output added to \"$nodespm\"."
else
warn "Sensors' output already integrated in in \"$nodespm\"."
@@ -201,6 +226,7 @@ function install_mod {
cp "$pvemanagerlibjs" "$BACKUP_DIR/pvemanagerlib.js.$timestamp"
msg "Backup of \"$pvemanagerlibjs\" saved to \"$BACKUP_DIR/pvemanagerlib.js.$timestamp\"."
+ local tempHelperCtorParams=$([[ "$TEMP_UNIT" = "F" ]] && echo '{srcUnit: PVE.mod.TempHelper.CELSIUS, dstUnit: PVE.mod.TempHelper.FAHRENHEIT}' || echo '{srcUnit: PVE.mod.TempHelper.CELSIUS, dstUnit: PVE.mod.TempHelper.CELSIUS}')
# Expand space in StatusView
sed -i "/Ext.define('PVE\.node\.StatusView'/,/\},/ {
s/\(bodyPadding:\) '[^']*'/\1 '20 15 20 15'/
@@ -209,6 +235,94 @@ function install_mod {
}" "$pvemanagerlibjs"
msg "Expanded space in \"$pvemanagerlibjs\"."
+ sed -i "/^Ext.define('PVE.node.StatusView'/i\
+Ext.define('PVE.mod.TempHelper', {\n\
+ //singleton: true,\n\
+\n\
+ requires: ['Ext.util.Format'],\n\
+\n\
+ statics: {\n\
+ CELSIUS: 0,\n\
+ FAHRENHEIT: 1\n\
+ },\n\
+\n\
+ srcUnit: null,\n\
+ dstUnit: null,\n\
+\n\
+ isValidUnit: function (unit) {\n\
+ return (\n\
+ Ext.isNumber(unit) && (unit === this.self.CELSIUS || unit === this.self.FAHRENHEIT)\n\
+ );\n\
+ },\n\
+\n\
+ constructor: function (config) {\n\
+ this.srcUnit = config && this.isValidUnit(config.srcUnit) ? config.srcUnit : this.self.CELSIUS;\n\
+ this.dstUnit = config && this.isValidUnit(config.dstUnit) ? config.dstUnit : this.self.CELSIUS;\n\
+ },\n\
+\n\
+ toFahrenheit: function (tempCelsius) {\n\
+ return Ext.isNumber(tempCelsius)\n\
+ ? tempCelsius * 9 / 5 + 32\n\
+ : NaN;\n\
+ },\n\
+\n\
+ toCelsius: function (tempFahrenheit) {\n\
+ return Ext.isNumber(tempFahrenheit)\n\
+ ? (tempFahrenheit - 32) * 5 / 9\n\
+ : NaN;\n\
+ },\n\
+\n\
+ getTemp: function (value) {\n\
+ if (this.srcUnit !== this.dstUnit) {\n\
+ switch (this.srcUnit) {\n\
+ case this.self.CELSIUS:\n\
+ switch (this.dstUnit) {\n\
+ case this.self.FAHRENHEIT:\n\
+ return this.toFahrenheit(value);\n\
+\n\
+ default:\n\
+ Ext.raise({\n\
+ msg:\n\
+ 'Unsupported destination temperature unit: ' + this.dstUnit,\n\
+ });\n\
+ }\n\
+ case this.self.FAHRENHEIT:\n\
+ switch (this.dstUnit) {\n\
+ case this.self.CELSIUS:\n\
+ return this.toCelsius(value);\n\
+\n\
+ default:\n\
+ Ext.raise({\n\
+ msg:\n\
+ 'Unsupported destination temperature unit: ' + this.dstUnit,\n\
+ });\n\
+ }\n\
+ default:\n\
+ Ext.raise({\n\
+ msg: 'Unsupported source temperature unit: ' + this.srcUnit,\n\
+ });\n\
+ }\n\
+ } else {\n\
+ return value;\n\
+ }\n\
+ },\n\
+\n\
+ getUnit: function(plainText) {\n\
+ switch (this.dstUnit) {\n\
+ case this.self.CELSIUS:\n\
+ return plainText !== true ? '\°C' : '\\\'C';\n\
+\n\
+ case this.self.FAHRENHEIT:\n\\n\
+ return plainText !== true ? '\°F' : '\\\'F';\n\
+\n\
+ default:\n\
+ Ext.raise({\n\
+ msg: 'Unsupported destination temperature unit: ' + this.srcUnit,\n\
+ });\n\
+ }\n\
+ },\n\
+});\n" "$pvemanagerlibjs"
+
sed -i "/^Ext.define('PVE.node.StatusView',/ {
:a;
/items:/!{N;ba;}
@@ -228,6 +342,7 @@ function install_mod {
const addressPrefix = \"$CPU_ADDRESS_PREFIX\";\n\
const cpuItemPrefix = \"$CPU_ITEM_PREFIX\";\n\
const cpuTempCaption = \"$CPU_TEMP_CAPTION\";\n\
+ const cpuTempHelper = Ext.create('PVE.mod.TempHelper', $tempHelperCtorParams);\n\
// display configuration\n\
const itemsPerRow = $CPU_ITEMS_PER_ROW;\n\
// ---\n\
@@ -249,11 +364,11 @@ function install_mod {
let tempVal = NaN, tempMax = NaN, tempCrit = NaN;\n\
Object.keys(items[coreKey]).forEach((secondLevelKey) => {\n\
if (secondLevelKey.endsWith('_input')) {\n\
- tempVal = parseFloat(items[coreKey][secondLevelKey]);\n\
+ tempVal = cpuTempHelper.getTemp(parseFloat(items[coreKey][secondLevelKey]));\n\
} else if (secondLevelKey.endsWith('_max')) {\n\
- tempMax = parseFloat(items[coreKey][secondLevelKey]);\n\
+ tempMax = cpuTempHelper.getTemp(parseFloat(items[coreKey][secondLevelKey]));\n\
} else if (secondLevelKey.endsWith('_crit')) {\n\
- tempCrit = parseFloat(items[coreKey][secondLevelKey]);\n\
+ tempCrit = cpuTempHelper.getTemp(parseFloat(items[coreKey][secondLevelKey]));\n\
}\n\
});\n\
if (!isNaN(tempVal)) {\n\
@@ -268,24 +383,24 @@ function install_mod {
let tempIndex = coreKey.match(/\\\S+\\\s*(\\\d+)/);\n\
if (tempIndex !== null && tempIndex.length > 1) {\n\
tempIndex = tempIndex[1];\n\
- tempStr = \`\${cpuTempCaption} \${tempIndex}: \${tempVal}°C\`;\n\
+ tempStr = \`\${cpuTempCaption} \${tempIndex}: \${Ext.util.Format.number(tempVal, '0.#')}\${cpuTempHelper.getUnit()}\`;\n\
} else {\n\
- tempStr = \`\${cpuTempCaption}: \${tempVal}°C\`;\n\
+ tempStr = \`\${cpuTempCaption}: \${Ext.util.Format.number(tempVal, '0.#')}\${cpuTempHelper.getUnit()}\`;\n\
}\n\
cpuTemps.push(tempStr);\n\
}\n\
} catch (e) { /*_*/ }\n\
});\n\
if(cpuTemps.length > 0) {\n\
- temps.push(cpuTemps);\n\
- }\n\
- });\n\
+ temps.push(cpuTemps);\n\
+ }\n\
+ });\n\
let result = '';\n\
temps.forEach((cpuTemps, cpuIndex) => {\n\
- const strCoreTemps = cpuTemps.map((strTemp, index, arr) => { return strTemp + (index + 1 < arr.length ? (itemsPerRow > 0 && (index + 1) % itemsPerRow === 0 ? '
' : ' | ') : ''); })\n\
- if(strCoreTemps.length > 0) {\n\
- result += (cpuCount > 1 ? \`CPU \${cpuIndex+1}: \` : '') + strCoreTemps.join('') + (cpuIndex < cpuCount ? '
' : '');\n\
- }\n\
+ const strCoreTemps = cpuTemps.map((strTemp, index, arr) => { return strTemp + (index + 1 < arr.length ? (itemsPerRow > 0 && (index + 1) % itemsPerRow === 0 ? '
' : ' | ') : ''); })\n\
+ if(strCoreTemps.length > 0) {\n\
+ result += (cpuCount > 1 ? \`CPU \${cpuIndex+1}: \` : '') + strCoreTemps.join('') + (cpuIndex < cpuCount ? '
' : '');\n\
+ }\n\
});\n\
return '