Chore: Rename func and set properly set default values

This commit is contained in:
Squidly271
2025-08-12 12:14:37 -04:00
parent b15416219f
commit 3c2c2c2635
12 changed files with 33 additions and 56 deletions
@@ -14,13 +14,12 @@
<?
$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/webGui/include/Helpers.php";
require_once "$docroot/webGui/include/publish.php";
while (true) {
$output = shell_exec("docker stats --no-stream --format='{{.ID}};{{.CPUPerc}};{{.MemUsage}}' 2>&1");
publish_md5("dockerload", $output,true);
publish_noDupe("dockerload", $output);
if ( $output === null ) {
// slow down publishing if no containers are running
sleep(10);
@@ -1,7 +1,7 @@
#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2024, Lime Technology
* Copyright 2024-2024, Simon Fairweather.
/* Copyright 2005-2025, Lime Technology
* Copyright 2024-2025, Simon Fairweather.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2,
@@ -87,7 +87,7 @@ while (true) {
}
if ($running < 1) $echo = "<tr><td colspan='7' style='text-align:center;padding-top:12px'>"._('No VMs running')."</td></tr>";
publish_md5('vm_usage',$echo,true);
publish_noDupe('vm_usage',$echo);
sleep($timer);
}
+12 -34
View File
@@ -11,7 +11,7 @@
*/
?>
<?
$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?? '/usr/local/emhttp');
$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/webGui/include/Wrappers.php";
@@ -25,15 +25,12 @@ function curl_socket($socket, $url, $message='') {
return $reply;
}
// $message: if an array, it will be converted to a JSON string
// $abort: if true, the script will exit if the endpoint is without subscribers on the next publish attempt after $abortTime seconds
// If a script publishes to multiple endpoints, timing out on one endpoint will terminate the entire script even if other enpoints succeed.
// If this is a problem, don't use $abort and instead handle this in the script or utilize a single sript per endpoint.
//
// $opt1: if numeric it's the length of the buffer. If its true|false it signifies whether to utilise the abort if no listeners.
// $opt2: if $opt1 is numeric, it's a boolean for $abort. If $opt1 is boolean, its the timeout defaulting to $opt3
// $opt3: if $opt1 is numeric, it's a value for $abortTime. If $opt1 is boolean, this parameter shouldn't be used
function publish($endpoint, $message, $opt1=1, $opt2=false, $opt3=120) {
// $endpoint: the name of the endpoint to publish to
// $message: the message to publish (if an array, it will be converted to a JSON string)
// $len: the length of the buffer (default 1)
// $abort: if true, the script will exit if the endpoint is without subscribers on the next publish attempt after $abortTime seconds (default true)
// $abortTime: the time in seconds to wait before exiting the script if the endpoint is without subscribers (default 120)
function publish($endpoint, $message, $len=1, $abort=true, $abortTime=30) {
static $abortStart = [], $com = [], $lens = [];
if ( is_file("/tmp/publishPaused") )
@@ -43,17 +40,6 @@ function publish($endpoint, $message, $opt1=1, $opt2=false, $opt3=120) {
$message = json_encode($message);
}
// handle the $opt1/$opt2/$opt3 parameters while remaining backwards compatible
if ( is_numeric($opt1) ) {
$len = $opt1;
$abort = $opt2;
$abortTime = $opt3;
} else {
$len = 1;
$abort = $opt1;
$abortTime = $opt2 ?: $opt3;
}
// Check for the unlikely case of a buffer length change
if ( (($lens[$endpoint] ?? 1) !== $len) && isset($com[$endpoint]) ) {
curl_close($com[$endpoint]);
@@ -113,17 +99,10 @@ function publish($endpoint, $message, $opt1=1, $opt2=false, $opt3=120) {
}
// Function to not continually republish the same message if it hasn't changed since the last publish
function publish_md5($endpoint, $message, $opt1=1, $opt2=false, $opt3=120) {
function publish_noDupe($endpoint, $message, $len=1, $abort=true, $abortTime=30) {
static $md5_old = [];
static $md5_time = [];
if ( is_numeric($opt1) ) {
$timeout = $opt3;
$abort = $opt2;
} else {
$abort = $opt1;
$timeout = $opt2 ?: $opt3;
}
if ( is_array($message) ) {
$message = json_encode($message);
@@ -131,18 +110,17 @@ function publish_md5($endpoint, $message, $opt1=1, $opt2=false, $opt3=120) {
// if abort is set, republish the message even if it hasn't changed after $timeout seconds to check for subscribers and exit accordingly
if ( $abort ) {
if ( (time() - ($md5_time[$endpoint]??0)) > $timeout ) {
if ( (time() - ($md5_time[$endpoint]??0)) > $abortTime ) {
$md5_old[$endpoint] = null;
}
}
// Always hash the payload to avoid collapsing distinct "falsey" values
$md5_new = md5((string)$message, true);
$md5_new = $message ? md5($message,true) : -1 ;
if ($md5_new !== ($md5_old[$endpoint]??null)) {
$md5_old[$endpoint] = $md5_new;
$md5_time[$endpoint] = time();
return publish($endpoint, $message, $opt1, $opt2, $opt3);
return publish($endpoint, $message, $len, $abort, $abortTime);
}
}
+2 -2
View File
@@ -252,10 +252,10 @@ while (true) {
}
if (time() - $timer) {
// update every second
publish('filemonitor', file_exists($active) ? 1 : 0);
publish('filemonitor', file_exists($active) ? 1 : 0,1,false);
$timer = time();
}
publish('filemanager', json_encode($reply));
publish('filemanager', json_encode($reply),1,false);
usleep(250000);
}
?>
+1 -1
View File
@@ -1,6 +1,6 @@
#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2023, Lime Technology
/* Copyright 2005-2025, Lime Technology
* Copyright 2012-2023, Bergware International.
*
* This program is free software; you can redistribute it and/or
+2 -2
View File
@@ -1,6 +1,6 @@
#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2023, Lime Technology
/* Copyright 2005-2025, Lime Technology
* Copyright 2012-2023, Bergware International.
*
* This program is free software; you can redistribute it and/or
@@ -123,7 +123,7 @@ while (true) {
$count = array_count_values($lsof ?? array());
foreach ($share as $name) $echo['stream'][] = $count[$name]??0;
publish_md5('update1',$echo,true);
publish_noDupe('update1',$echo);
sleep(5);
}
+1 -1
View File
@@ -499,7 +499,7 @@ while (true) {
$echo[$a] = [implode($echo[$a]), $extra];
}
publish_md5('update2',$echo,true);
publish_noDupe('update2',$echo);
sleep(2);
}
?>
+1 -1
View File
@@ -159,7 +159,7 @@ while (true) {
$date = my_date($xdate ? 'D j M Y, T' : $display['date'].', T',$now);
$echo['time'] = [$clock,_($date,0)];
publish_md5('update3',$echo,true);
publish_noDupe('update3',$echo);
sleep(1);
$time1 = microtime(true);
+2 -2
View File
@@ -1,6 +1,6 @@
#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2023, Lime Technology
/* Copyright 2005-2025, Lime Technology
* Copyright 2012-2023, Bergware International.
* Copyright 2015, Dan Landon.
*
@@ -132,7 +132,7 @@ while (true) {
$echo[6] = isset($output) ? ((empty($volt) || ($minv<$output && $output<$maxv) ? "<span $green>" : "<span $red>").$echo[6].(isset($freq) ? " ~ $freq Hz" : "")."</span>") : $echo[6];
}
publish_md5('apcups',$echo,true);
publish_noDupe('apcups',$echo);
sleep(3);
}
+3 -3
View File
@@ -1,7 +1,7 @@
#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2024, Lime Technology
* Copyright 2024-2024, Simon Fairweather.
/* Copyright 2005-2025, Lime Technology
* Copyright 2024-2025, Simon Fairweather.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2,
@@ -109,7 +109,7 @@ while (true) {
}
}
publish_md5('vm_dashusage',$echo,true);
publish_noDupe('vm_dashusage',$echo);
sleep($timer);
}
+4 -4
View File
@@ -1,6 +1,6 @@
#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2023, Lime Technology
/* Copyright 2005-2025, Lime Technology
* Copyright 2012-2023, Bergware International.
*
* This program is free software; you can redistribute it and/or
@@ -15,7 +15,7 @@
$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/webGui/include/publish.php";
function my_scale($value, &$unit) {
function scale($value, &$unit) {
$units = ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi','Yi'];
$size = count($units);
$base = $value ? floor(log($value, 1024)) : 0;
@@ -35,9 +35,9 @@ while (true) {
exec('wg show all dump',$dump);
foreach ($dump as $row) {
$row = preg_split('/\s+/',$row);
if (count($row)>5) $echo[] = [$row[0], $row[5]?$now-$row[5]:0, my_scale($row[6],$unit)." $unit", my_scale($row[7],$unit)." $unit"];
if (count($row)>5) $echo[] = [$row[0], $row[5]?$now-$row[5]:0, scale($row[6],$unit)." $unit", scale($row[7],$unit)." $unit"];
}
publish_md5('wireguard',$echo,true);
publish_noDupe('wireguard',$echo);
sleep(1);
}
?>
+1 -1
View File
@@ -66,7 +66,7 @@ while (true) {
}
}
// Short of closing all web pages, the timeout should only happen if wifi is enabled and then disabled
publish_md5('wlan0',$echo,true);
publish_noDupe('wlan0',$echo);
sleep(3);
}
?>