From 8961ebd5de84bafa7af8d1e082c8e530ba208240 Mon Sep 17 00:00:00 2001 From: stevenA Date: Sun, 16 Jan 2022 09:29:53 +0100 Subject: [PATCH] Optimise aminterface, and calls to it Only get activeDevices once in doConfigPageInits Delete redundant functions in Aminterface class Optimise json handling --- .../amInterfaceClasses/Message.class.php | 13 ++++++++----- .../amInterfaceClasses/Response.class.php | 15 ++++++++++----- sccpManClasses/aminterface.class.php | 6 +++--- sccpManTraits/bmoFunctions.php | 14 ++++++++------ 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/sccpManClasses/amInterfaceClasses/Message.class.php b/sccpManClasses/amInterfaceClasses/Message.class.php index d7bb756..a881e58 100644 --- a/sccpManClasses/amInterfaceClasses/Message.class.php +++ b/sccpManClasses/amInterfaceClasses/Message.class.php @@ -103,12 +103,12 @@ abstract class Message { return $this->variables; } - +/* public function getActionID() { return $this->getKey('ActionID'); } - +*/ public function getKeys() { return $this->keys; @@ -126,8 +126,10 @@ abstract class Message $key = strtolower($key); switch ($key) { case 'json': - $this->keys['JSONRAW'] = (string) $value; + //$this->keys['JSONRAW'] = (string) $value; + $this->keys['json'] = (string) $value; break; + case 'response': case 'actionid': case 'desc': $this->keys[$key] = (string) $value; @@ -137,13 +139,14 @@ abstract class Message break; } - - $_string_key = array('actionid', 'descr', 'json'); +/* + $_string_key = array('actionid', 'descr'); if (array_search($key, $_string_key) !== false) { $this->keys[$key] = (string) $this->sanitizeInput($value, 'string'); } else { $this->keys[$key] = $this->sanitizeInput($value); } +*/ } protected function sanitizeInput($value, $prefered_type = '') diff --git a/sccpManClasses/amInterfaceClasses/Response.class.php b/sccpManClasses/amInterfaceClasses/Response.class.php index 101cf4c..da2d076 100644 --- a/sccpManClasses/amInterfaceClasses/Response.class.php +++ b/sccpManClasses/amInterfaceClasses/Response.class.php @@ -70,7 +70,7 @@ abstract class Response extends IncomingMessage { $this->setKey('ActionId', $actionId); } - +/* public function getVariable(string $_rawContent, array $_fields) { dbug($_rawContent); @@ -85,6 +85,8 @@ abstract class Response extends IncomingMessage } } } + */ + } //**************************************************************************** @@ -117,6 +119,7 @@ class Command_Response extends Generic_Response private $_temptable; public function __construct($rawContent) { + //dbug($rawContent); $this->_temptable = array(); parent::__construct($rawContent); $lines = explode(Message::EOL, $rawContent); @@ -155,14 +158,15 @@ class SCCPJSON_Response extends Generic_Response public function __construct($rawContent) { parent::__construct($rawContent); + //dbug($rawContent); //$this->getVariable($rawContent, array("DataType" => "DataType:", "JSONRAW" => "JSON:")); - if (null !== $this->getKey('JSONRAW')) { - $this->setKey('Response', 'Success'); - } + //if (null !== $this->getKey('JSON')) { + //$this->setKey('Response', 'Success'); + //} } public function getResult() { - if (($json = json_decode($this->getKey('JSON'), true)) != false) { + if ($json = json_decode($this->getKey('JSON'), true)) { return $json; } } @@ -248,6 +252,7 @@ class SCCPGeneric_Response extends Response protected function ConvertTableData( $_tablename, array $_fkey, array $_fields) { + //dbug(debug_backtrace(2)); $result = array(); $_rawtable = $this->Table2Array($_tablename); // Check that there is actually data to be converted diff --git a/sccpManClasses/aminterface.class.php b/sccpManClasses/aminterface.class.php index a55d628..e58ccb9 100644 --- a/sccpManClasses/aminterface.class.php +++ b/sccpManClasses/aminterface.class.php @@ -141,7 +141,7 @@ class aminterface $messageToSend = $message->serialize(); $length = strlen($messageToSend); $this->_DumpMessage = ''; - $this->_lastActionId = $message->getActionID(); + $this->_lastActionId = $message->getKey('ActionID'); $this->_lastRequestedResponseHandler = $message->getResponseHandler(); $this->_lastActionClass = $message; $this->_incomingRawMessage[$this->_lastActionId] = ''; @@ -281,7 +281,7 @@ class aminterface } } $response = new $responseClass($message); - $actionId = $response->getActionID(); + $actionId = $response->getKey('ActionID'); if ($actionId === null) { $response->setActionId($this->_lastActionId); } @@ -466,7 +466,7 @@ class aminterface { // Initialise array with default values to eliminate testing later $result = array(); - $cmd_res = array(); + //$cmd_res = array(); $cmd_res = ['sccp' => ['message' => 'legacy value', 'realm' => '', 'status' => 'ERROR']]; if ($this->_connect_state) { $_action = new \FreePBX\modules\Sccp_manager\aminterface\CommandAction('realtime mysql status'); diff --git a/sccpManTraits/bmoFunctions.php b/sccpManTraits/bmoFunctions.php index 839006e..ed6d996 100644 --- a/sccpManTraits/bmoFunctions.php +++ b/sccpManTraits/bmoFunctions.php @@ -15,8 +15,10 @@ trait bmoFunctions { case 'sccpsettings': break; case 'sccp_phone': - $this->extensionData = json_encode($this->getExtensionGrid('extGrid')); - $this->sccpPhoneData = json_encode($this->getPhoneGrid('sccp')); + // Get activeDevices once and pass to functions. + $activeDevices = $this->aminterface->sccp_get_active_device(); + $this->extensionData = json_encode($this->getExtensionGrid('extGrid', $activeDevices)); + $this->sccpPhoneData = json_encode($this->getPhoneGrid('sccp', $activeDevices)); $this->sipPhoneData = json_encode($this->getPhoneGrid('cisco-sip')); break; case 'sccp_adv': @@ -29,7 +31,7 @@ trait bmoFunctions { } } - function getPhoneGrid(string $type){ + function getPhoneGrid(string $type, $activeDevices =array()){ $dbDevices = array(); // Find all devices defined in the database. $dbDevices = $this->dbinterface->getSccpDeviceTableData('phoneGrid', array('type' => $type)); @@ -39,7 +41,7 @@ trait bmoFunctions { return $dbDevices; //this may be empty } // Find all devices currently connected - $activeDevices = $this->aminterface->sccp_get_active_device(); + //$activeDevices = $this->aminterface->sccp_get_active_device(); foreach ($dbDevices as &$dev_id) { if (!empty($activeDevices[$dev_id['name']])) { @@ -87,12 +89,12 @@ trait bmoFunctions { return $dbDevices; } - function getExtensionGrid(string $type) { + function getExtensionGrid(string $type, $activeDevices = array()) { $lineList = $this->dbinterface->getSccpDeviceTableData($type); if (empty($lineList)) { return array(); } - $activeDevices = $this->aminterface->sccp_get_active_device(); + //$activeDevices = $this->aminterface->sccp_get_active_device(); $uniqueLineList = array(); foreach ($lineList as $key => &$lineArr) { if (array_key_exists($lineArr['mac'], $activeDevices)) {