From 408331edf500f2dd7d2a39a52fdfe3d1bdf1eaad Mon Sep 17 00:00:00 2001 From: Squidly271 Date: Sun, 10 Aug 2025 18:13:10 -0400 Subject: [PATCH] Refactor: Support not sending duplicate messages Keep track of the md5 within a separate function. Avoids duplication of code on the nchan scripts, and allows for the script to still exit if no listeners without adding in more code within each script to handle this. --- .../dynamix.docker.manager/nchan/docker_load | 2 +- .../plugins/dynamix.vm.manager/nchan/vm_usage | 5 +-- emhttp/plugins/dynamix/include/publish.php | 32 ++++++++++++++++++- emhttp/plugins/dynamix/nchan/update_1 | 6 ++-- emhttp/plugins/dynamix/nchan/update_2 | 5 +-- emhttp/plugins/dynamix/nchan/update_3 | 7 ++-- emhttp/plugins/dynamix/nchan/ups_status | 7 ++-- emhttp/plugins/dynamix/nchan/wg_poller | 5 +-- emhttp/plugins/dynamix/nchan/wlan0 | 2 +- 9 files changed, 43 insertions(+), 28 deletions(-) diff --git a/emhttp/plugins/dynamix.docker.manager/nchan/docker_load b/emhttp/plugins/dynamix.docker.manager/nchan/docker_load index 6cc8be993..e9ebe1984 100755 --- a/emhttp/plugins/dynamix.docker.manager/nchan/docker_load +++ b/emhttp/plugins/dynamix.docker.manager/nchan/docker_load @@ -20,7 +20,7 @@ require_once "$docroot/webGui/include/publish.php"; while (true) { $output = shell_exec("docker stats --no-stream --format='{{.ID}};{{.CPUPerc}};{{.MemUsage}}' 2>&1"); - publish("dockerload", $output,true); + publish_md5("dockerload", $output,true); if ( $output === null ) { // slow down publishing if no containers are running sleep(10); diff --git a/emhttp/plugins/dynamix.vm.manager/nchan/vm_usage b/emhttp/plugins/dynamix.vm.manager/nchan/vm_usage index 751a0445a..6af185358 100755 --- a/emhttp/plugins/dynamix.vm.manager/nchan/vm_usage +++ b/emhttp/plugins/dynamix.vm.manager/nchan/vm_usage @@ -88,10 +88,7 @@ while (true) { if ($running < 1) $echo = ""._('No VMs running').""; $echo = json_encode($echo); - $md5_new = md5($echo,true); - if ($md5_new !== $md5_old) { - $md5_old = publish('vm_usage',$echo,true)!==false ? $md5_new : -1; - } + publish_md5('vm_usage',$echo,true); sleep($timer); } diff --git a/emhttp/plugins/dynamix/include/publish.php b/emhttp/plugins/dynamix/include/publish.php index 390b784c4..a8dfd52f1 100644 --- a/emhttp/plugins/dynamix/include/publish.php +++ b/emhttp/plugins/dynamix/include/publish.php @@ -34,7 +34,7 @@ function curl_socket($socket, $url, $message='') { // $opt3: if $opt1 is not numeric, it's a value for $abortTime. function publish($endpoint, $message, $opt1=1, $opt2=false, $opt3=120) { static $abortStart = [], $com = [], $lens = []; - + if ( is_file("/tmp/publishPaused") ) return false; @@ -107,6 +107,36 @@ function publish($endpoint, $message, $opt1=1, $opt2=false, $opt3=120) { return $reply; } +// 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) { + static $md5_old = []; + static $md5_time = []; + + if ( is_numeric($opt1) ) { + $timeout = $opt3; + $abort = $opt2; + } else { + $abort = $opt1; + $timeout = $opt2 ?: $opt3; + } + + // 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 ) { + $md5_old[$endpoint] = null; + } + } + + $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); + } +} + + // Removes the script calling this function from nchan.pid function removeNChanScript() { global $docroot, $argv; diff --git a/emhttp/plugins/dynamix/nchan/update_1 b/emhttp/plugins/dynamix/nchan/update_1 index 143846994..f43f425b5 100755 --- a/emhttp/plugins/dynamix/nchan/update_1 +++ b/emhttp/plugins/dynamix/nchan/update_1 @@ -123,10 +123,8 @@ while (true) { $count = array_count_values($lsof ?? array()); foreach ($share as $name) $echo['stream'][] = $count[$name]??0; $echo = json_encode($echo); - $md5_new = md5($echo,true); - if ($md5_new !== $md5_old) { - $md5_old = publish('update1',$echo,true)!==false ? $md5_new : -1; - } + publish_md5('update1',$echo,true); + sleep(5); } ?> diff --git a/emhttp/plugins/dynamix/nchan/update_2 b/emhttp/plugins/dynamix/nchan/update_2 index bf68b72c2..9706262d1 100755 --- a/emhttp/plugins/dynamix/nchan/update_2 +++ b/emhttp/plugins/dynamix/nchan/update_2 @@ -500,10 +500,7 @@ while (true) { } $echo = json_encode($echo); - $md5_new = md5($echo,true); - if ($md5_new !== $md5_old) { - $md5_old = publish('update2',$echo,true)!==false ? $md5_new : -1; - } + publish_md5('update2',$echo,true); sleep(2); } ?> diff --git a/emhttp/plugins/dynamix/nchan/update_3 b/emhttp/plugins/dynamix/nchan/update_3 index 5d670cf75..d81b9bd34 100755 --- a/emhttp/plugins/dynamix/nchan/update_3 +++ b/emhttp/plugins/dynamix/nchan/update_3 @@ -160,11 +160,8 @@ while (true) { $echo['time'] = [$clock,_($date,0)]; $echo = json_encode($echo); - $md5_new = md5($echo,true); - if ($md5_new !== $md5_old) { - $md5_old = publish('update3',$echo,true)!==false ? $md5_new : -1; - $time0 = $time1; - } + publish_md5('update3',$echo,true); + sleep(1); $time1 = microtime(true); } diff --git a/emhttp/plugins/dynamix/nchan/ups_status b/emhttp/plugins/dynamix/nchan/ups_status index ca25805e0..b201d424c 100755 --- a/emhttp/plugins/dynamix/nchan/ups_status +++ b/emhttp/plugins/dynamix/nchan/ups_status @@ -132,10 +132,9 @@ while (true) { $echo[6] = isset($output) ? ((empty($volt) || ($minv<$output && $output<$maxv) ? "" : "").$echo[6].(isset($freq) ? " ~ $freq Hz" : "")."") : $echo[6]; } $echo = json_encode($echo); - $md5_new = md5($echo,true); - if ($md5_new !== $md5_old) { - $md5_old = publish('apcups',$echo,true)!==false ? $md5_new : -1; - } + + publish_md5('apcups',$echo,true); + sleep(3); } ?> diff --git a/emhttp/plugins/dynamix/nchan/wg_poller b/emhttp/plugins/dynamix/nchan/wg_poller index 875519448..786e8f00a 100755 --- a/emhttp/plugins/dynamix/nchan/wg_poller +++ b/emhttp/plugins/dynamix/nchan/wg_poller @@ -38,10 +38,7 @@ while (true) { 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"]; } $echo = json_encode($echo); - $md5_new = md5($echo,true); - if ($md5_new !== $md5_old) { - $md5_old = publish('wireguard',$echo,true)!==false ? $md5_new : -1; - } + publish_md5('wireguard',$echo,true); sleep(1); } ?> diff --git a/emhttp/plugins/dynamix/nchan/wlan0 b/emhttp/plugins/dynamix/nchan/wlan0 index 34e2b5c6e..dba908aa1 100755 --- a/emhttp/plugins/dynamix/nchan/wlan0 +++ b/emhttp/plugins/dynamix/nchan/wlan0 @@ -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('wlan0',json_encode($echo),1,true); + publish_md5('wlan0',json_encode($echo),true); sleep(3); } ?>