Files
fusionpbx/app/system/resources/classes/system_dashboard_service.php
frytimo d5286a12bc Websockets (#7393)
* Initial commit of websockets

* Move app_menu to the active_calls websockets

* Fix hangup function

* Remove connection wait-state on web socket server so events can process

* Add timestamp and debug level to console for service debug output

* Remove debug exit

* Fix typo for ws_client instead of ws_server

* Update app_config.php

* Fix typo and remove empty function

* Remove call to empty function

* Fix the menu to point to the correct location

* Remove Logging Class

* Rename service file

* Rename service file

* Fix the in progress browser request

* Fix browser reload and implement 'active_calls' default values

* Add apply_filter function

* Create new permission_filter object

* In progress active calls now use filter

* Add invalid_uuid_exception class

* add event_key_filter to honor user permissions

* add and_link and or_link for filters

* Fix disconnected subscriber and add filters to honor permissions

* Add $key and $value for filter

* define a service name

* catch throwable instead of exception

* Add $key and $value for filter and allow returning null

* Update permission checks when loading page

* Add apply_filter function to honor subscriber permissions

* Add create_filter_chain_for function to honor subscriber permissions

* Add apply_filter function to honor subscriber permissions

* Add apply_filter function to honor subscriber permissions

* create interface to allow filterable payload

* create interface to define functions required for websocket services

* Pass in service class when creating a service token

* Allow key/name and return null for filter

* Adjust subscriber exceptions to return the ID of the subscriber

* Add event filter to filter chain

* Add command line options for ip and port for websockets and switch

* update service to use is_a syntax

* initial commit of base class for websockets system services

* initial commit of the system cpu status service

* remove extra line feed

* fix path on active_calls

* initial proof of concept for cpu status updated by websockets

* Allow returning null

* Use default settings to set the interval for cpu status broadcast

* Improve the CPU percent function for Linux systems

* Show more debug information

* Allow child processes to re-connect to the web socket service

* Fix websockets as plural instead of singular

* Add class name list-row

* Update active_calls.php

* Update active_calls.php

* Update websocket_client.js

* Update app_config.php

* Update app_menu.php

* Update debian-websockets.service

* Update debian-active_calls.service

---------

Co-authored-by: FusionPBX <markjcrane@gmail.com>
2025-06-24 13:07:57 -06:00

135 lines
3.4 KiB
PHP

<?php
class system_dashboard_service extends base_websocket_system_service {
const PERMISSIONS = [
'system_view_cpu',
'system_view_backup',
'system_view_database',
'system_view_hdd',
'system_view_info',
'system_view_memcache',
'system_view_ram',
'system_view_support',
];
const CPU_STATUS_TOPIC = 'cpu_status';
/**
*
* @var system_information $system_information
*/
protected static $system_information;
/**
* Settings object
* @var settings
*/
private $settings;
/**
* Integer representing the number of seconds to broadcast the CPU usage
* @var int
*/
private $cpu_status_refresh_interval;
protected function reload_settings(): void {
static::set_system_information();
// re-read the config file to get any possible changes
parent::$config->read();
// re-connect to the websocket server if required
$this->connect_to_ws_server();
// Connect to the database
$database = new database(['config' => parent::$config]);
// get the interval
$this->settings = new settings(['database' => $database]);
// get the settings from the global defaults
$this->cpu_status_refresh_interval = $this->settings->get('dashboard', 'cpu_status_refresh_interval', 3);
}
/**
* @override base_websocket_system_service
* @return void
*/
protected function on_timer(): void {
// Send the CPU status
$this->on_cpu_status();
}
/**
* Executes once
* @return void
*/
protected function register_topics(): void {
// get the settings from the global defaults
$this->reload_settings();
// Create a system information object that can tell us the cpu regardless of OS
self::$system_information = system_information::new();
// Register the call back to respond to cpu_status requests
$this->on_topic('cpu_status', [$this, 'on_cpu_status']);
// Set a timer
$this->set_timer($this->cpu_status_refresh_interval);
// Notify the user of the interval
$this->info("Broadcasting CPU Status every {$this->cpu_status_refresh_interval}s");
}
public function on_cpu_status($message = null): void {
// Get the CPU status
$cpu_percent = self::$system_information->get_cpu_percent();
// Send the response
$response = new websocket_message();
$response
->payload([self::CPU_STATUS_TOPIC => $cpu_percent])
->service_name(self::get_service_name())
->topic(self::CPU_STATUS_TOPIC);
// Check if we are responding
if ($message !== null && $message instanceof websocket_message) {
$response->id($message->id());
}
// Log the broadcast
$this->debug("Broadcasting CPU percent '$cpu_percent'");
// Send to subscribers
$this->respond($response);
}
public static function get_service_name(): string {
return "dashboard.system.information";
}
public static function create_filter_chain_for(subscriber $subscriber): ?filter {
// Get the subscriber permissions
$permissions = $subscriber->get_permissions();
// Create a filter
$filter = filter_chain::and_link([new permission_filter()]);
// Match them to create a filter
foreach (self::PERMISSIONS as $permission) {
if (in_array($permission, $permissions)) {
$filter->add_permission($permission);
}
}
// Return the filter with user permissions to ensure they can't receive information they shouldn't
return $filter;
}
public static function set_system_information(): void {
self::$system_information = system_information::new();
}
}