Initial Commit (#7541)

This commit is contained in:
frytimo
2025-10-03 12:17:11 -03:00
committed by GitHub
parent 2494fe7f5e
commit 871e5c0f9e
3 changed files with 57 additions and 44 deletions

View File

@@ -190,5 +190,19 @@
$apps[$x]['default_settings'][$y]['default_setting_value'] = "";
$apps[$x]['default_settings'][$y]['default_setting_enabled'] = "false";
$apps[$x]['default_settings'][$y]['default_setting_description'] = "Network interface used to show traffic on dashboard";
?>
$y++;
$apps[$x]['default_settings'][$y]['default_setting_uuid'] = "2e9e5b8e-aef3-4a57-b0f6-041b14b6db44";
$apps[$x]['default_settings'][$y]['default_setting_category'] = "system";
$apps[$x]['default_settings'][$y]['default_setting_subcategory'] = "network_status_refresh_interval";
$apps[$x]['default_settings'][$y]['default_setting_name'] = "text";
$apps[$x]['default_settings'][$y]['default_setting_value'] = "";
$apps[$x]['default_settings'][$y]['default_setting_enabled'] = "false";
$apps[$x]['default_settings'][$y]['default_setting_description'] = "Network interface used to show traffic on dashboard";
$y++;
$apps[$x]['default_settings'][$y]['default_setting_uuid'] = "5998a3b8-e7cc-4a93-8657-e1a65b1ddaf8";
$apps[$x]['default_settings'][$y]['default_setting_category'] = "system";
$apps[$x]['default_settings'][$y]['default_setting_subcategory'] = "cpu_status_refresh_interval";
$apps[$x]['default_settings'][$y]['default_setting_name'] = "text";
$apps[$x]['default_settings'][$y]['default_setting_value'] = "";
$apps[$x]['default_settings'][$y]['default_setting_enabled'] = "false";
$apps[$x]['default_settings'][$y]['default_setting_description'] = "Network interface used to show traffic on dashboard";

View File

@@ -54,28 +54,13 @@ class system_dashboard_service extends base_websocket_system_service {
$this->settings = new settings(['database' => $database]);
// get the cpu interval
$this->cpu_status_refresh_interval = $this->settings->get('dashboard', 'cpu_status_refresh_interval', 3);
$this->cpu_status_refresh_interval = intval($this->settings->get('system', 'cpu_status_refresh_interval', 3));
// get the network interval
$this->network_status_refresh_interval = $this->settings->get('dashboard', 'network_status_refresh_interval', 3);
$this->network_status_refresh_interval = intval($this->settings->get('system', 'network_status_refresh_interval', 3));
// get the network card to watch
$this->network_interface = $this->settings->get('system', 'network_interface', 'eno1');
}
/**
* @override base_websocket_system_service
* @return void
*/
protected function on_timer(): void {
// Send the CPU status
$this->on_cpu_status();
// Send the network average
$this->on_network_status();
// Reset the timer
$this->set_timer($this->cpu_status_refresh_interval);
$this->network_interface = $this->settings->get('system', 'network_interface', 'eth0');
}
/**
@@ -94,17 +79,18 @@ class system_dashboard_service extends base_websocket_system_service {
$this->on_topic(self::CPU_STATUS_TOPIC, [$this, 'on_cpu_status']);
// Register the call back to respond to network_status requests
$this->on_topic(self::NETWORK_STATUS_TOPIC, [$this, 'on_cpu_status']);
$this->on_topic(self::NETWORK_STATUS_TOPIC, [$this, 'on_network_status']);
// Set a timer
$this->set_timer($this->cpu_status_refresh_interval);
// Set timer callbacks
$this->set_timer($this->cpu_status_refresh_interval, [$this, 'on_cpu_status']);
$this->set_timer($this->network_status_refresh_interval, [$this, 'on_network_status']);
// Notify the user of the interval
$this->info("Broadcasting CPU Status every {$this->cpu_status_refresh_interval}s");
$this->info("Broadcasting Network Status every {$this->network_status_refresh_interval}s");
}
public function on_network_status($message = null): void {
public function on_network_status($message = null): int {
// Get RX (receive) and TX (transmit) bps
$network_rates = self::$system_information->get_network_speed($this->network_interface);
@@ -143,6 +129,9 @@ class system_dashboard_service extends base_websocket_system_service {
}
$this->warn("Websocket server connected");
}
// return a timer value so another timer will be set
return $this->network_status_refresh_interval;
}
public function on_network_interface_select($message = null): void {
@@ -154,7 +143,7 @@ class system_dashboard_service extends base_websocket_system_service {
}
}
public function on_cpu_status($message = null): void {
public function on_cpu_status($message = null): int {
// Get total and per-core CPU usage
$cpu_percent_total = self::$system_information->get_cpu_percent();
$cpu_percent_per_core = self::$system_information->get_cpu_percent_per_core();
@@ -198,10 +187,11 @@ class system_dashboard_service extends base_websocket_system_service {
}
$this->warn("Websocket server connected");
}
// return a timer value so another timer will be set
return $this->cpu_status_refresh_interval;
}
public static function get_service_name(): string {
return "dashboard.system.information";
}

View File

@@ -22,6 +22,8 @@ abstract class base_websocket_system_service extends service implements websocke
*/
protected $ws_client;
private $timers;
//abstract protected function reload_settings(): void;
protected static function display_version(): void {
@@ -29,25 +31,14 @@ abstract class base_websocket_system_service extends service implements websocke
}
/**
* Set a timer to trigger the on_timer function every $seconds. To stop the timer, set the value to null
* Set a timer to trigger the defined function every $seconds. To stop the timer, set the value to null
* @param int $seconds
* @return void
* @see on_timer
*/
protected function set_timer(?int $seconds): void {
if ($seconds !== null) $this->timer_expire_time = time() + $seconds;
else $this->timer_expire_time = null;
}
/**
* When the set_timer is used to set a timer, this function will run. Override
* the function in the child class.
* @return void
* @see set_timer
*/
protected function on_timer(): void {
return;
}
protected function set_timer(int $seconds, callable $callable): void {
$this->timers[] = ['expire_time' => time() + $seconds, 'callable' => $callable];
}
protected static function set_command_options() {
parent::append_command_option(
@@ -79,6 +70,9 @@ abstract class base_websocket_system_service extends service implements websocke
}
public function run(): int {
// set the timers property as an array
$this->timers = [];
// re-read the config file to get any possible changes
parent::$config->read();
@@ -140,8 +134,23 @@ abstract class base_websocket_system_service extends service implements websocke
}
// Timers can be set by child classes
if ($this->timer_expire_time !== null && time() >= $this->timer_expire_time) {
$this->on_timer();
if (!empty($this->timers)) {
// Check all timers
foreach($this->timers as $key => $array) {
// Check if the timer should be run
if (time() >= $array['expire_time']) {
// Get the callback function
$callable = $array['callable'];
// Call the callback and see if it returns a value for the next timer
$next_timer = call_user_func($callable);
if ($next_timer !== null && is_numeric($next_timer)) {
// Set the timer again when requested by called function returning a value
$this->set_timer($next_timer, $callable);
}
// Remove the expired timer from tracking list
unset($this->timers[$key]);
}
}
}
}
return 0;