From e28d4a32fbd1ce8393828b9303fec583cd790fb3 Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 27 May 2024 19:19:04 +0100 Subject: [PATCH] Add in the ability to set a manual UPS capacity override in `dynamix.apcupsd`, so that Nominal Power can still be estimated based on the load percentage for UPS's that don't report their `NOMPOWER` --- emhttp/languages/en_US/helptext.txt | 4 ++++ emhttp/plugins/dynamix.apcupsd/UPSsettings.page | 5 +++++ emhttp/plugins/dynamix.apcupsd/default.cfg | 1 + emhttp/plugins/dynamix.apcupsd/include/UPSstatus.php | 12 +++++++++++- emhttp/plugins/dynamix/nchan/ups_status | 9 ++++++++- 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/emhttp/languages/en_US/helptext.txt b/emhttp/languages/en_US/helptext.txt index b878ec9af..79c381420 100644 --- a/emhttp/languages/en_US/helptext.txt +++ b/emhttp/languages/en_US/helptext.txt @@ -1954,6 +1954,10 @@ Enter the *device* which corresponds to your situation, only applicable when *UP + **modbus** - /dev/tty** :end +:apc_ups_override_ups_capacity_help: +If your device doesn't natively report Nominal Power (`NOMPOWER`) from `apcupsd`, but does report the Load Percentage (`LOADPCT`), you can manually define the UPS capacity rating in Watts (W) (this is the 'real power' value in Watts (W), not the 'apparent power' in Volt Amps (VA), and should be detailed on your UPS manual or product listing) and the plugin will dynamically calculate a virtual Nominal Power estimate (`≈`) by comparing the Override UPS Capacity (W) and the current Load Percentage. It is only an estimate, as it doesn't factor in things like the UPS' efficiency. +:end + :apc_battery_level_help: If during a power failure, the remaining battery percentage (as reported by the UPS) is below or equal to *Battery level*, apcupsd will initiate a system shutdown. :end diff --git a/emhttp/plugins/dynamix.apcupsd/UPSsettings.page b/emhttp/plugins/dynamix.apcupsd/UPSsettings.page index cdde2cf31..025e0cdf1 100644 --- a/emhttp/plugins/dynamix.apcupsd/UPSsettings.page +++ b/emhttp/plugins/dynamix.apcupsd/UPSsettings.page @@ -91,6 +91,11 @@ _(Device)_: :apc_ups_device_help: +_(Override UPS Capacity (Watts))_: +: + +:apc_ups_override_ups_capacity_help: + _(Battery level to initiate shutdown)_ (%): : diff --git a/emhttp/plugins/dynamix.apcupsd/default.cfg b/emhttp/plugins/dynamix.apcupsd/default.cfg index 8ad7bb8c1..5ea9819a9 100644 --- a/emhttp/plugins/dynamix.apcupsd/default.cfg +++ b/emhttp/plugins/dynamix.apcupsd/default.cfg @@ -3,6 +3,7 @@ UPSCABLE="usb" CUSTOMUPSCABLE="" UPSTYPE="usb" DEVICE="" +OVERRIDE_UPS_CAPACITY="" BATTERYLEVEL="10" MINUTES="10" TIMEOUT="0" diff --git a/emhttp/plugins/dynamix.apcupsd/include/UPSstatus.php b/emhttp/plugins/dynamix.apcupsd/include/UPSstatus.php index 381f70969..507131898 100644 --- a/emhttp/plugins/dynamix.apcupsd/include/UPSstatus.php +++ b/emhttp/plugins/dynamix.apcupsd/include/UPSstatus.php @@ -18,6 +18,10 @@ $docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp'); $_SERVER['REQUEST_URI'] = 'settings'; require_once "$docroot/webGui/include/Translations.php"; +require_once "$docroot/webGui/include/Helpers.php"; +$cfg = parse_plugin_cfg('dynamix.apcupsd'); +$overrideUpsCapacity = (int) htmlspecialchars($cfg['OVERRIDE_UPS_CAPACITY'] ?: 0); + $state = [ 'ONLINE' => _('Online'), 'SLAVE' => '('._('slave').')', @@ -34,7 +38,8 @@ $state = [ $red = "class='red-text'"; $green = "class='green-text'"; $orange = "class='orange-text'"; -$status = array_fill(0,7,"-"); +$defaultCell = "-"; +$status = array_fill(0,7,$defaultCell); $result = []; $level = $_POST['level'] ?: 10; $runtime = $_POST['runtime'] ?: 5; @@ -89,6 +94,11 @@ if (file_exists("/var/run/apcupsd.pid")) { if ($power && isset($load)) $status[5] = ($load<90 ? "" : "").round($power*$load/100)." W (".$status[5].")"; elseif (isset($load)) $status[5] = ($load<90 ? "" : "").$status[5].""; $status[6] = isset($output) ? ((!$volt || ($minv<$output && $output<$maxv) ? "" : "").$status[6].(isset($freq) ? " ~ $freq Hz" : "")."") : $status[6]; + + if ($status[4] == $defaultCell && $overrideUpsCapacity > 0 && isset($load) && $load > 0) { + $nominalPower = round($load * 0.01 * $overrideUpsCapacity); + $status[4] = ($nominalPower > 0 ? "" : "") . "≈ $nominalPower W"; + } } if (empty($rows)) $result[] = ""._('No information available').""; diff --git a/emhttp/plugins/dynamix/nchan/ups_status b/emhttp/plugins/dynamix/nchan/ups_status index 37f414b43..9090e91a2 100755 --- a/emhttp/plugins/dynamix/nchan/ups_status +++ b/emhttp/plugins/dynamix/nchan/ups_status @@ -69,12 +69,14 @@ while (true) { update_translation($locale_init); } unset($echo,$rows,$power,$load,$freq,$output,$volt); - $echo = array_fill(0,7,"-"); + $defaultCell = "-"; + $echo = array_fill(0,7,$defaultCell); if (file_exists("/var/run/apcupsd.pid")) { // get battery-level and runtime settings $cfg = parse_plugin_cfg('dynamix.apcupsd'); $level = $cfg['BATTERYLEVEL'] ?: 10; $runtime = $cfg['MINUTES'] ?: 5; + $overrideUpsCapacity = (int) htmlspecialchars($cfg['OVERRIDE_UPS_CAPACITY'] ?: 0); exec("/sbin/apcaccess -u 2>/dev/null", $rows); foreach ($rows as $row) { [$key, $val] = array_map('trim', explode(':', $row, 2)); @@ -120,6 +122,11 @@ while (true) { if (isset($power) && isset($load)) $echo[5] = ($load<90 ? "" : "").round($power*$load/100)." W (".$echo[5].")"; elseif (isset($load)) $echo[5] = ($load<90 ? "" : "").$echo[5].""; $echo[6] = isset($output) ? ((empty($volt) || ($minv<$output && $output<$maxv) ? "" : "").$echo[6].(isset($freq) ? " ~ $freq Hz" : "")."") : $echo[6]; + + if ($echo[4] == $defaultCell && $overrideUpsCapacity > 0 && isset($load) && $load > 0) { + $nominalPower = round($load * 0.01 * $overrideUpsCapacity); + $echo[4] = ($nominalPower > 0 ? "" : "") . "≈ $nominalPower W"; + } } $echo = json_encode($echo); $md5_new = md5($echo,true);