From 8efca3ac1cd3ca136591211cb25753cae8f3e28c Mon Sep 17 00:00:00 2001 From: Tcharp38 Date: Tue, 14 Nov 2023 12:09:34 +0100 Subject: [PATCH] AbeilleCmd: refresh from eqLogic DB updates --- core/class/AbeilleCmdQueue.class.php | 40 +++++++++++++++------------- core/php/AbeilleCmd.php | 39 +++++++++++++++++++-------- 2 files changed, 49 insertions(+), 30 deletions(-) diff --git a/core/class/AbeilleCmdQueue.class.php b/core/class/AbeilleCmdQueue.class.php index 0036d0a270..5cc28a6735 100755 --- a/core/class/AbeilleCmdQueue.class.php +++ b/core/class/AbeilleCmdQueue.class.php @@ -748,7 +748,6 @@ function processAcks() { if ($lastSent && ($cmd['waitFor'] == "ACK")) { $removeCmd = true; - $eqStatusChanged = ''; $net = $msg['net']; $addr = $msg['addr']; $eq = &getDevice($net, $addr); // By ref @@ -758,24 +757,27 @@ function processAcks() { cmdLog('debug', " eq=".json_encode($eq)); // Note: TX status makes sense only if device is always listening (rxOnWhenIdle=TRUE) // For other devices any interrogation without waking up device may lead to NO-ACK which is normal - if ($msg['status'] == '00') { // Ok ? - if ($eq['txStatus'] !== 'ok') - $eqStatusChanged = 'ok'; - } else { // NO ACK ? - if ($eq['txStatus'] !== 'noack') - $eqStatusChanged = 'noack'; - } - if ($eq['rxOnWhenIdle'] && ($eqStatusChanged != '')) { - $eq['txStatus'] = $eqStatusChanged; - $msg = array( - // 'src' => 'cmd', - 'type' => 'eqTxStatusUpdate', - 'net' => $net, - 'addr' => $addr, - 'txStatus' => $eqStatusChanged // 'ok', or 'noack' - ); - msgToAbeille($msg); - cmdLog2('debug', $addr, " ${net}-${addr} status changed to '${eqStatusChanged}'"); + if (isset($eq['rxOnWhenIdle']) && $eq['rxOnWhenIdle']) { + $newTxStatus = ''; + if ($msg['status'] == '00') { // Ok ? + if ($eq['txStatus'] !== 'ok') + $newTxStatus = 'ok'; + } else { // NO ACK ? + if ($eq['txStatus'] !== 'noack') + $newTxStatus = 'noack'; + } + if ($newTxStatus != '') { + $eq['txStatus'] = $newTxStatus; + $msg = array( + // 'src' => 'cmd', + 'type' => 'eqTxStatusUpdate', + 'net' => $net, + 'addr' => $addr, + 'txStatus' => $newTxStatus // 'ok', or 'noack' + ); + msgToAbeille($msg); + cmdLog2('debug', $addr, " ${net}-${addr} status changed to '${newTxStatus}'"); + } } } } diff --git a/core/php/AbeilleCmd.php b/core/php/AbeilleCmd.php index 3017c59ec0..b59fd0c79e 100644 --- a/core/php/AbeilleCmd.php +++ b/core/php/AbeilleCmd.php @@ -56,12 +56,16 @@ function &getDevice($net, $addr) { // Note: A delay is required prior to this if DB has to be updated (createDevice() in Abeille.class) function updateDeviceFromDB($eqId) { $eqLogic = eqLogic::byId($eqId); + if (!is_object($eqLogic)) { + logMessage('debug', " ERROR: updateDeviceFromDB(): Equipment ID ${eqId} does not exist"); + return; + } $eqLogicId = $eqLogic->getLogicalId(); list($net, $addr) = explode("/", $eqLogicId); $ieee = $eqLogic->getConfiguration('IEEE', ''); if (!isset($GLOBALS['devices'][$net][$addr]) && ($ieee == '')) { - logMessage('debug', " updateDeviceFromDB() WARNING: Unknown addr ${addr} and IEEE is undefined"); + logMessage('debug', " ERROR: updateDeviceFromDB(): Unknown addr '${net}/${addr}' and IEEE is undefined"); return; } @@ -75,7 +79,7 @@ function updateDeviceFromDB($eqId) { $found = true; $GLOBALS['devices'][$net][$addr] = $GLOBALS['devices'][$net][$addr2]; unset($GLOBALS['devices'][$net][$addr2]); - logMessage('debug', " Device ID ${eqId} address changed from ${addr2} to ${addr}."); + logMessage('debug', " Device ID ${eqId} address changed from '${net}/${addr2}' to '${net}/${addr}'."); break; } } @@ -90,23 +94,36 @@ function updateDeviceFromDB($eqId) { $found = true; $GLOBALS['devices'][$net][$addr] = $GLOBALS['devices'][$net2][$addr2]; unset($GLOBALS['devices'][$net2][$addr2]); - logMessage('debug', " Device ID ${eqId} migrated from ${net2}/${addr2} to ${net2}/${addr}."); + logMessage('debug', " Device ID ${eqId} migrated from '${net2}/${addr2}' to '${net}/${addr}'."); break; } } } } + // Whatever found or new... updating infos used by cmd process + if (!isset($GLOBALS['devices'][$net])) + $GLOBALS['devices'][$net] = []; + $eqModel = $eqLogic->getConfiguration('ab::eqModel', []); $zigbee = $eqLogic->getConfiguration('ab::zigbee', []); $rwOnWhenIdle = isset($zigbee['rwOnWhenIdle']) ? $zigbee['rwOnWhenIdle'] : 0; - $GLOBALS['devices'][$net][$addr]['rwOnWhenIdle'] = $rwOnWhenIdle ? true : false; - // $GLOBALS['devices'][$net][$addr]['tuyaEF00'] = $eqLogic->getConfiguration('ab::tuyaEF00', null); - // parserLog('debug', " 'tuyaEF00' updated to ".json_encode($GLOBALS['devices'][$net][$addr]['tuyaEF00'])); - // $GLOBALS['devices'][$net][$addr]['xiaomi'] = $eqLogic->getConfiguration('ab::xiaomi', null); - // parserLog('debug', " 'xiaomi' updated to ".json_encode($GLOBALS['devices'][$net][$addr]['xiaomi'])); - // $GLOBALS['devices'][$net][$addr]['customization'] = $eqLogic->getConfiguration('ab::customization', null); - // parserLog('debug', " 'customization' updated to ".json_encode($GLOBALS['devices'][$net][$addr]['customization'])); - // TO BE COMPLETED if any other key info + $eq = array( + 'ieee' => $ieee, + 'txStatus' => $eqLogic->getStatus('ab::txAck', 'ok'), // Transmit status: 'ok' or 'noack' + 'jsonId' => isset($eqModel['id']) ? $eqModel['id'] : '', + 'jsonLocation' => isset($eqModel['location']) ? $eqModel['location'] : 'Abeille', + 'rxOnWhenIdle' => $rwOnWhenIdle ? true : false + ); + if ($eq['jsonId'] != '') { + // Read JSON to get list of commands to execute + $model = AbeilleTools::getDeviceModel('', $eq['jsonId'], $eq['jsonLocation']); + if ($model !== false) { + $eq['mainEp'] = isset($model['mainEP']) ? $model['mainEP'] : "01"; + $eq['commands'] = isset($model['commands']) ? $model['commands'] : []; + } + } + + $GLOBALS['devices'][$net][$addr] = $eq; } /* Send msg to 'xToCmd' queue. */