Tidy up exception handling

Simplify handling of AMI lists and trap exceptions
This commit is contained in:
steve-lad 2021-02-27 10:00:26 +01:00 committed by Diederik de Groot
parent a89866e81e
commit 5beee7f9a2
No known key found for this signature in database
GPG key ID: AFA728250A1BECD6
3 changed files with 142 additions and 157 deletions

View file

@ -184,19 +184,17 @@ class SCCPGeneric_Response extends Response
public function addEvent($event)
{
// not eventlist (start/complete)
// print_r('<br>---- addEvent --<br>');
// print_r($event);
// print_r('<br>---- Event List--<br>');
// print_r($event->getEventList());
// Nothing to do with this - we need a table start
if (stristr($event->getEventList(), 'start')) { return; }
if ($event->getEventList() === 'start') {
// Have started a list of events; this may include tables
// Nothing to do with this event, only need to handle
// the events that follow
return;
}
if ( empty($thisSetEventEntryType)) {
// This is empty as soon as we have received a TableStart.
// The next message is the first of the data sets
// We use this variable in the switch to add set entries
if ( empty($thisSetEventEntryType)) {
if (strpos($event->getName(), 'Entry')) {
$thisSetEventEntryType = $event->getName();
} else {
@ -204,7 +202,10 @@ class SCCPGeneric_Response extends Response
}
}
$unknownevent = "FreePBX\\modules\\Sccp_manager\\aminterface\\UnknownEvent";
if (!($event instanceof $unknownevent)) {
if ($event instanceof $unknownevent) {
$this->_events[] = $event;
return;
}
switch ( $event->getName()) {
case $thisSetEventEntryType :
$this->_temptable['Entries'][] = $event;
@ -222,8 +223,8 @@ class SCCPGeneric_Response extends Response
$this->_tables = array();
}
$this->_tables[$event->getTableName()] = $this->_temptable;
unset($this->_temptable);
$thisSetEventEntryType = '';
$this->_temptable = array();
$thisSetEventEntryType = 'undefinedAsThisIsNotASet';
// Finished the table. Now check to see if everything was received
// If counts do not match return false and table will not be
@ -233,15 +234,12 @@ class SCCPGeneric_Response extends Response
}
break;
default:
// add regular event
// add regular list event
$this->_events[] = $event;
}
} else {
// add unknown event
$this->_events[] = $event;
}
// Received a complete eventList outside of a table.
if (stristr($event->getEventList(), 'complete') || stristr($event->getName(), 'complete')) {
if ($event->getEventList() === 'Complete') {
// Received a complete eventList.
return $this->_completed = true;
}
}
@ -252,7 +250,6 @@ class SCCPGeneric_Response extends Response
$_rawtable = $this->Table2Array($_tablename);
// Check that there is actually data to be converted
if (empty($_rawtable)) { return $result;}
foreach ($_rawtable as $_row) {
$all_key_ok = true;
// No need to test if $_fkey is arrray as array required
@ -264,11 +261,12 @@ class SCCPGeneric_Response extends Response
}
}
$Data = &$result;
if ($all_key_ok) {
foreach ($set_name as $value_id) {
$Data = &$Data[$value_id];
}
// Label converter in case labels and keys are different - not actually required.
// Label converter in case labels and keys are different
foreach ($_fields as $value_key => $value_id) {
$Data[$value_id] = $_row[$value_key];
}
@ -421,7 +419,7 @@ class SCCPShowDevices_Response extends SCCPGeneric_Response
return $this->ConvertTableData(
'Devices',
array('mac'),
array('mac'=>'mac','address'=>'address','descr'=>'descr','regstate'=>'status',
array('mac'=>'name','address'=>'address','descr'=>'descr','regstate'=>'status',
'token'=>'token','act'=>'act', 'lines'=>'lines','nat'=>'nat','regtime'=>'regtime')
);
// return $result;
@ -437,7 +435,9 @@ class SCCPShowDevice_Response extends SCCPGeneric_Response
public function getResult()
{
$result = array();
foreach ($this->_events as $trow) {
dbug('keys are',$trow->getKeys());
$result = array_merge($result, $trow->getKeys());
}
$result['Buttons'] = $this->ConvertTableData(

View file

@ -530,8 +530,59 @@ class aminterface
$result = array();
if ($this->_connect_state) {
$_action = new \FreePBX\modules\Sccp_manager\aminterface\SCCPConfigMetaDataAction();
$_response = $this->send($_action);
$result = $_response->getResult();
$metadata = $this->send($_action)->getResult();
}
//return $result;
if ($metadata && array_key_exists("Version", $metadata)) {
$result["Version"] = $metadata["Version"];
$version_parts = explode(".", $metadata["Version"]);
$result["vCode"] = 0;
if ($version_parts[0] == "4") {
switch ($version_parts[1]) {
case "1":
$result["vCode"] = 410;
break;
case "2":
$result["vCode"] = 420;
break;
case 3. . .5:
if($version_parts[2] == "3"){
$result["vCode"] = 433;
} else {
$result["vCode"] = 430;
}
break;
default:
$result["vCode"] = 400;
break;
}
}
/* Revision got replaced by RevisionHash in 10404 (using the hash does not work) */
if (array_key_exists("Revision", $metadata)) {
if (base_convert($metadata["Revision"], 16, 10) == base_convert('702487a', 16, 10)) {
$result["vCode"] = 431;
}
if (base_convert($metadata["Revision"], 16, 10) >= "10403") {
$result["vCode"] = 431;
}
}
if (array_key_exists("RevisionHash", $metadata)) {
$result["RevisionHash"] = $metadata["RevisionHash"];
} else {
$result["RevisionHash"] = '';
}
if (array_key_exists("RevisionNum", $metadata)) {
$result["RevisionNum"] = $metadata["RevisionNum"];
if ($metadata["RevisionNum"] >= "10403") { // new method, RevisionNum is incremental
$result["vCode"] = 432;
}
if ($metadata["RevisionNum"] >= "10491") { // new method, RevisionNum is incremental
$result["vCode"] = 433;
}
}
if (array_key_exists("ConfigureEnabled", $metadata)) {
$result["futures"] = implode(';', $metadata["ConfigureEnabled"]);
}
}
return $result;
}

View file

@ -117,7 +117,10 @@ class srvinterface {
private function sccp_core_commands($params = array()) {
if ($this->ami_mode) {
if (!$this->ami_mode) {
return $this->oldinterface->sccp_core_commands($params);
}
if (!empty($params['cmd'])) {
switch ($params['cmd']) {
case 'reset_phone':
@ -156,9 +159,7 @@ class srvinterface {
break;
}
}
} else {
return $this->oldinterface->sccp_core_commands($params);
}
}
public function sccp_getdevice_info($dev_id) {
@ -190,21 +191,12 @@ class srvinterface {
}
public function sccp_realtime_status() {
if (!$this->ami_mode) {
return $this->oldinterface->sccp_realtime_status();
} else {
if ($this->ami_mode) {
return $this->aminterface->getRealTimeStatus();
/* if (is_array($ast_out)) {
foreach ($ast_out as $aline) {
if (strlen($aline) > 3) {
$ast_key = strstr(trim($aline), ' ', true);
$ast_res[$ast_key] = array('message' => $aline, 'status' => strpos($aline, 'connected') ? 'OK' : 'ERROR');
} else {
return $this->oldinterface->sccp_realtime_status();
}
}
}
return $ast_res;
*/ }
}
public function get_compatible_sccp() {
@ -253,68 +245,10 @@ class srvinterface {
}
function getChanSCCPVersion() {
if (!$this->ami_mode) {
if ($this->ami_mode) {
return $this->aminterface->getSCCPVersion();
} else {
return $this->oldinterface->getChanSCCPVersion();
} else {
$result = array();
$metadata = $this->aminterface->getSCCPVersion();
if ($metadata && array_key_exists("Version", $metadata)) {
$result["Version"] = $metadata["Version"];
$version_parts = explode(".", $metadata["Version"]);
$result["vCode"] = 0;
if ($version_parts[0] == "4") {
switch ($version_parts[1]) {
case "1":
$result["vCode"] = 410;
break;
case "2":
$result["vCode"] = 420;
break;
case 3. . .5:
if($version_parts[2] == "3"){
$result["vCode"] = 433;
} else {
$result["vCode"] = 430;
}
break;
default:
$result["vCode"] = 400;
break;
}
}
/* Revision got replaced by RevisionHash in 10404 (using the hash does not work) */
if (array_key_exists("Revision", $metadata)) {
if (base_convert($metadata["Revision"], 16, 10) == base_convert('702487a', 16, 10)) {
$result["vCode"] = 431;
}
if (base_convert($metadata["Revision"], 16, 10) >= "10403") {
$result["vCode"] = 431;
}
}
if (array_key_exists("RevisionHash", $metadata)) {
$result["RevisionHash"] = $metadata["RevisionHash"];
} else {
$result["RevisionHash"] = '';
}
if (array_key_exists("RevisionNum", $metadata)) {
$result["RevisionNum"] = $metadata["RevisionNum"];
if ($metadata["RevisionNum"] >= "10403") { // new method, RevisionNum is incremental
$result["vCode"] = 432;
}
if ($metadata["RevisionNum"] >= "10491") { // new method, RevisionNum is incremental
$result["vCode"] = 433;
}
}
if (array_key_exists("ConfigureEnabled", $metadata)) {
$result["futures"] = implode(';', $metadata["ConfigureEnabled"]);
}
} else {
return null;
die_freepbx("Version information could not be retrieved from chan-sccp, via astman::SCCPConfigMetaData");
}
return $result;
}
}