From f6b7b9e66438e8e4019232696f7b61f65209b3f7 Mon Sep 17 00:00:00 2001 From: daniel Tartavel Date: Thu, 24 Feb 2022 21:46:10 +0100 Subject: [PATCH] - indexFriendlyName modified\n- debugging --- class/db.php | 1 + class/main.php | 1 - db_functions.php | 162 ++++++------------------------- hooks/scripts/availability.php | 2 +- topics_callbacks/linky2mqtt.php | 3 +- topics_callbacks/pws2mqtt.php | 2 +- topics_callbacks/zigbee2mqtt.php | 15 ++- 7 files changed, 41 insertions(+), 145 deletions(-) diff --git a/class/db.php b/class/db.php index d8bf66a..226b090 100644 --- a/class/db.php +++ b/class/db.php @@ -43,6 +43,7 @@ class db extends mysqli if (array_key_exists($property, $properties2log)) { //echo "logging in database"; + //var_dump($device); $ieeeAddress = $device->ieeeAddress; //print_r($ieeeAddress); $query = "INSERT INTO logs (device, property, value) VALUES('" . $this->protect($ieeeAddress) . "', '" . $this->protect($property) . "', '" . $this->protect($value) . "')"; diff --git a/class/main.php b/class/main.php index 9e014e6..8a880e0 100644 --- a/class/main.php +++ b/class/main.php @@ -31,7 +31,6 @@ class device public $description; public $functions; public $payload; - public $availability; public $toConfirm; public $triggerDevice; public $properties = array(); diff --git a/db_functions.php b/db_functions.php index df372a2..cbbc54a 100644 --- a/db_functions.php +++ b/db_functions.php @@ -53,22 +53,22 @@ function mkDevicesDB($topic, $json, $group=false) //print_r($device); $device["device"]->groupID = $jsonDevice->id; $indexDevices[$device["device"]->groupID] = & $device["device"]; - $indexFriendlyNames[$fn] = & $device["device"]; + $indexFriendlyNames[$topic][$fn] = & $device["device"]; }else { - addDevice($device["device"], $fn, $jsonDevice); + addDevice($topic, $device["device"], $fn, $jsonDevice); } } $dbInit += 1; fwrite($logFh, "################################START##################################################"); fwrite($logFh, var_export($devices, true)); fwrite($logFh, "################################END##################################################"); - + fwrite($logFh, var_export($indexFriendlyNames, true)); logger(DEBUG, _("Devices DB made"), __FILE__ . ":" . __LINE__); //print_r($devices); } -function addDevice(& $device, $fn, $jsonDevice ) +function addDevice($topic, & $device, $fn, $jsonDevice ) { global $listProperties, $listPropertiesKeys, $hooks, $indexDevices, $indexFriendlyNames; $device->type = $jsonDevice->type; @@ -86,7 +86,7 @@ function addDevice(& $device, $fn, $jsonDevice ) //indexing device $indexDevices[$device->ieeeAddress] = & $device; - $indexFriendlyNames[$fn] = & $device; + $indexFriendlyNames[$topic][$fn] = & $device; } function searchPropertyKey($fn, &$device, $inputObject, $listPropertiesKeys) @@ -157,18 +157,18 @@ function changeDevice($topic, $fn, &$device, $payloadArray) { //echo "==================== New ChangeDevice =====================" .EOL; - iterateDevice($topic, $fn, $device, $device, $payloadArray); + iterateDevice($topic, $fn, $device, $device->properties, $payloadArray); }else { logger(ERROR, $fn . _(" => payloadArray is empty!"), __FILE__ . ":" . __LINE__); } - } -function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $propertyTree="") +function iterateDevice($topic, $fn, $parentDevice, &$properties, $payloadArray, $propertyTree="") { global $changed, $mohaDB, $testMode; - $deviceType = (is_a($device, "device")); // = true if object + //if (is_a($device, "device")) // = true if object + //echo "==================== New iterate =====================" .EOL; //echo "deviceType = "; var_dump($deviceType); echo EOL; //echo "device =>";print_r($device);echo EOL; @@ -180,123 +180,23 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro //echo "value =>"; var_dump($value); echo EOL; //echo "type : " . gettype($value) .EOL; $valueType = gettype($value); - if ($valueType == "object") + + if ($valueType == "array") { - //logger(DEBUG, _("valueType == object"), __FILE__ . ":" . __LINE__); - + logger(DEBUG,_("valueType == array"), __FILE__ . ":" . __LINE__); $propertyTree .= $key . "/"; - //echo "PropertyTree " . $propertyTree . EOL; - //echo " is Object" . EOL; - if ($deviceType === true ) - { - //echo "deviceType = true" . EOL; - if (!array_key_exists($key, $device->properties)) - { - //echo "Property do not exists" . EOL; - $device->properties[$key] = new stdClass; - } - //echo "iterating" . EOL; - //echo "===============>"; - - iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree); - }elseif(is_array($device)) - { - //echo "is array"; - if (!array_key_exists($key, $device)) - { - $device[$key] = new stdClass; - } - //echo "iterating" . EOL; - iterateDevice($topic, $fn, $parentDevice, $device[$key], $value, $propertyTree); - }elseif(is_object($device)) - { - if (!property_exists($device, $key)) - { - $device->$key = new stdClass; - } - iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree); - } - - }elseif ($valueType == "array") - { - //logger(DEBUG,_("valueType == array"), __FILE__ . ":" . __LINE__); - $propertyTree .= $key . "/"; - if ($deviceType === true ) - { - $device->properties[$key] = array(); - iterateDevice($topic, $fn, $parentDevice, $device->properties[$key], $value, $propertyTree); - }elseif(is_array($device)) - { - $device[$key] = array(); - iterateDevice($topic, $fn, $parentDevice, $device[$key], $value, $propertyTree); - }elseif(is_object($device)) - { - $device->$key = array(); - iterateDevice($topic, $fn, $parentDevice, $device->$key, $value, $propertyTree); - } - + $properties[$key] = array(); + iterateDevice($topic, $fn, $parentDevice, $properties[$key], $value, $propertyTree); }else { - //logger(DEBUG,_("valueType is ") . $valueType, __FILE__ . ":" . __LINE__); - - //var_dump($device);echo EOL; - if (is_a($device, "device")) + logger(DEBUG,_("valueType is ") . $valueType, __FILE__ . ":" . __LINE__); + //var_dump($properties);echo EOL; + if (!array_key_exists($key, $properties)) { - //logger(DEBUG,_("This is an object 'device'"), __FILE__ . ":" . __LINE__); - if(array_key_exists($key, $device->properties)) - { - if ($device->properties[$key]["value"] != $value) - { - changeValue($device->properties[$key]["value"], $value, $parentDevice, $propertyTree, $key); - } - }else - { - $device->properties[$key] = array("value" => $value); - $device->properties[$key]["functions"] = array(); - } - }elseif (is_array($device)) - { - //logger(DEBUG,_("deviceType") . $deviceType, __FILE__ . ":" . __LINE__); - if (array_key_exists($key, $device)) - { - if (is_array($device[$key])) - { - if ($device[$key]["value"] != $value) - { - changeValue($device->$key["value"], $value, $parentDevice, $propertyTree, $key); - } - }else - { - changeValue($device[$key], $value, $parentDevice, $propertyTree, $key); - } - }else - { - $device[$key] = array("value" => $value); - $device[$key]["functions"] = array(); - } - }elseif (is_object($device)) - { - if(property_exists($device, $key)) - { - if (is_array($device->$key)) - { - if ($device->$key["value"] != $value) - { - changeValue($device->$key["value"], $value, $parentDevice, $propertyTree, $key); - } - }else - { - if ($device->$key != $value) - { - changeValue($device->$key, $value, $parentDevice, $propertyTree, $key); - } - } - }else - { - $device->$key = array("value" => $value); - $device->$key["functions"] = array(); - } + $properties[$key] = array("value" => $value); + $properties[$key]["functions"] = array(); } + changeValue($properties[$key], $value, $parentDevice, $propertyTree, $key); } } } @@ -331,11 +231,10 @@ function getDevicesValues($topic) } } -function getDeviceByFriendlyname(&$device, $topic, $fn, $payloadArray, $create = false) +/*function getDeviceByFriendlyname(&$device, $topic, $fn, $payloadArray, $create = false) { global $devices, $indexDevices, $indexFriendlyNames; $n = explode("/", $fn); - //echo "topic => ". $topic . EOL; $device = &$devices[$topic]; foreach($n as $value) { @@ -352,7 +251,6 @@ function getDeviceByFriendlyname(&$device, $topic, $fn, $payloadArray, $create = logger(ERROR, sprintf(_(" device with friendlyname %s not found"), $fn), __FILE__ . ":" . __LINE__); return false; } - } if (! array_key_exists("device", $device)) { @@ -364,28 +262,28 @@ function getDeviceByFriendlyname(&$device, $topic, $fn, $payloadArray, $create = $device["device"]->ieeeAddress = $payloadArray->ieeeAddress; $device["device"]->friendlyname = $fn; $indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; - $indexFriendlyNames[$fn] = & $device["device"]; + $indexFriendlyNames[$topic][$fn] = & $device["device"]; } }else { logger(INFO, sprintf(_(" device with friendlyname %s exists"), $fn), __FILE__ . ":" . __LINE__); } + //var_dump($device); return true; -} +}*/ -function changeValue(&$oldValue, $value, &$parentDevice, $propertyTree, $key) +function changeValue(&$property, $value, &$parentDevice, $propertyTree, $key) { global $mohaDB; - $object = $parentDevice->properties[$key]; //$changed[$fn]["key"] = $key; //$changed[$fn]["value"] = $value; - logger(INFO, sprintf(_("Logging Device %s property %s, %s"), $parentDevice->friendlyName, $propertyTree . $key, bool2string($value)), __FILE__ . ":" . __LINE__); - $mohaDB->logProperty($parentDevice, $key, $value, $oldValue); - $oldvalue = $value; - if (!empty($object["functions"])) + logger(INFO, sprintf(_("Logging Device property %s, %s"), $propertyTree . $key, bool2string($value)), __FILE__ . ":" . __LINE__); + $mohaDB->logProperty($parentDevice, $key, $value, $property["value"]); + $property["value"] = $value; + if (!empty($property["functions"])) { logger(DEBUG,_("executing notifications functions"), __FILE__ . ":" . __LINE__); - foreach($object["functions"] as $function) + foreach($property["functions"] as $function) { $function($parentDevice, $key, $value); } diff --git a/hooks/scripts/availability.php b/hooks/scripts/availability.php index f3f55a7..fc6dd12 100644 --- a/hooks/scripts/availability.php +++ b/hooks/scripts/availability.php @@ -17,7 +17,7 @@ class availability global $indexDevices; foreach ($indexDevices as $value) { - $value->availability["functions"][] = array($this,"callback"); + $value->properties["availability"]["functions"][] = array($this,"callback"); } } diff --git a/topics_callbacks/linky2mqtt.php b/topics_callbacks/linky2mqtt.php index 6535a9c..10940ee 100644 --- a/topics_callbacks/linky2mqtt.php +++ b/topics_callbacks/linky2mqtt.php @@ -21,8 +21,7 @@ $topics["linky2mqtt"]->callback = function($topic, $message) $device["device"]->ieeeAddress = $payloadArray["ieeeAddress"]; $device["device"]->type = $payloadArray["type"]; $indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; - $indexFriendlyNames[$fn] = & $device["device"]; - + $indexFriendlyNames[$topicName][$fn] = & $device["device"]; }else { $device = &$device[$fn]; diff --git a/topics_callbacks/pws2mqtt.php b/topics_callbacks/pws2mqtt.php index c74e84a..eea5925 100644 --- a/topics_callbacks/pws2mqtt.php +++ b/topics_callbacks/pws2mqtt.php @@ -22,7 +22,7 @@ $topics["pws2mqtt"]->callback = function($topic, $message) $device["device"]->type = $payloadArray["type"]; $device["device"]->topic = $topicName; $indexDevices[$device["device"]->ieeeAddress] = & $device["device"]; - $indexFriendlyNames[$fn] = & $device["device"]; + $indexFriendlyNames[$topicName][$fn] = & $device["device"]; }else { diff --git a/topics_callbacks/zigbee2mqtt.php b/topics_callbacks/zigbee2mqtt.php index cb95baf..c980ee9 100644 --- a/topics_callbacks/zigbee2mqtt.php +++ b/topics_callbacks/zigbee2mqtt.php @@ -5,7 +5,7 @@ $devices["zigbee2mqtt"] = array(); $topics["zigbee2mqtt"]->callback = function($topic, $message) { - global $topics, $devices, $included, $logFh; + global $topics, $devices, $included, $logFh, $indexFriendlyNames; if ($topic[1] == "bridge") { @@ -46,7 +46,7 @@ $topics["zigbee2mqtt"]->callback = function($topic, $message) $topic = explode ("/", $message->topic, 2); // get topic name $topicName = $topic[0]; $fn = $topic[1]; - $device = null; + $device = new device; $fnTree = explode ("/" , $topic[1]); // get friendlyname echo $topicName . " => " . $fn . EOL; @@ -64,25 +64,24 @@ $topics["zigbee2mqtt"]->callback = function($topic, $message) $fn = implode("/", $fnTree); $fn = implode("/", $fnTree); $payloadArray = array($fnTreeEnd => $message->payload); - //TODO payl - $payloadArray = array($fnTreeEnd => $message->payload); //TODO payload can be a json object break; default: $payloadArray = json_decode($message->payload,true); } logger(DEBUG, _("friendlyname = ") . $fn, __FILE__ . ":" . __LINE__); - if ((getDeviceByFriendlyname($device, $topicName, $fn, $payloadArray, true)) === false) + if (!array_key_exists($fn, $indexFriendlyNames[$topicName])) { logger(ERROR, sprintf(_("device with friendlyname %s not found"), $fn), __FILE__ . ":" . __LINE__); + }else + { + changeDevice($topicName, $fn, $indexFriendlyNames[$topicName][$fn], $payloadArray); } - print_r($device); - changeDevice($topicName, $fn, $device["device"], $payloadArray); + }else { logger(DEBUG, _("Zigbee2mqtt doing nothing !?")); } - };