From c223a4123ff0d8d68723dc645cc4a9435cfa9c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Tr=C3=B6ger?= Date: Wed, 16 Oct 2024 18:42:19 +0200 Subject: [PATCH] no message --- Configurator/form.json | 12 +++--- Configurator/module.php | 95 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 95 insertions(+), 12 deletions(-) diff --git a/Configurator/form.json b/Configurator/form.json index 2ccfe44..66d7162 100644 --- a/Configurator/form.json +++ b/Configurator/form.json @@ -19,7 +19,7 @@ "delete": true, "rowCount": 5, "sort": { - "column": "name", + "column": "topic", "direction": "ascending" }, "columns": [ @@ -31,7 +31,7 @@ { "caption": "Topic", "name": "topic", - "width": "200px" + "width": "auto" }, { "caption": "Network Address", @@ -56,7 +56,7 @@ { "caption": "Description", "name": "description", - "width": "auto" + "width": "200px" }, { "caption": "Type", @@ -85,7 +85,7 @@ "delete": true, "rowCount": 5, "sort": { - "column": "name", + "column": "topic", "direction": "ascending" }, "columns": [ @@ -98,7 +98,7 @@ { "caption": "Topic", "name": "topic", - "width": "200px" + "width": "auto" }, { "caption": "Devices", @@ -108,7 +108,7 @@ { "caption": "Name", "name": "name", - "width": "auto" + "width": "200px" } ], "values": [] diff --git a/Configurator/module.php b/Configurator/module.php index 28c7659..1e30cb0 100644 --- a/Configurator/module.php +++ b/Configurator/module.php @@ -11,7 +11,29 @@ class Zigbee2MQTTConfigurator extends IPSModule use \Zigbee2MQTT\BufferHelper; use \Zigbee2MQTT\Semaphore; use \Zigbee2MQTT\SendData; - + private static $DeviceValues=[ + 'name' =>'', + 'id' => '', + 'parent' => '', + 'instanceID' => 0, + 'ieee_address' => '', + 'topic' => '', + 'networkAddress' => '', + 'type' => '', + 'vendor' => '', + 'modelID' => '', + 'description' => '', + 'power_source' => '' + ]; + private static $GroupValues=[ + 'name' =>'', + 'id' => '', + 'parent' => '', + 'instanceID' => 0, + 'ID' => 0, + 'topic' => '', + 'DevicesCount' => '' + ]; /** * Create * @@ -133,6 +155,7 @@ public function GetConfigurationForm() } //Devices $ValuesDevices = []; + $ValueId=1; foreach ($Devices as $device) { $Value = []; //Array leeren $instanceID = array_search($device['ieeeAddr'], $IPSDevicesByIEEE); @@ -158,6 +181,8 @@ public function GetConfigurationForm() $Value['name'] = $Name; $Value['instanceID'] = 0; } + $Value['parent']=$this->AddParentElement($ValueId, $ValuesDevices, $Location, self::$DeviceValues); + $Value['id'] = ++$ValueId; $Value['ieee_address'] = $device['ieeeAddr']; $Value['topic'] = $device['friendly_name']; $Value['networkAddress'] = $device['networkAddress']; @@ -184,9 +209,12 @@ public function GetConfigurationForm() $Topic = $IPSDevicesByTopic[$instanceID]; unset($IPSDevicesByTopic[$instanceID]); } - + $Location = explode('/', $Topic); + array_pop($Location); $ValuesDevices[] = [ 'name' => IPS_GetName($instanceID), + 'id' => ++$ValueId, + 'parent' => $this->AddParentElement($ValueId, $ValuesDevices, $Location,self::$DeviceValues), 'instanceID' => $instanceID, 'ieee_address' => $IEEE, 'topic' => $Topic, @@ -200,8 +228,12 @@ public function GetConfigurationForm() ]; } foreach ($IPSDevicesByTopic as $instanceID => $Topic) { + $Location = explode('/', $Topic); + array_pop($Location); $ValuesDevices[] = [ 'name' => IPS_GetName($instanceID), + 'id' => ++$ValueId, + 'parent' => $this->AddParentElement($ValueId, $ValuesDevices, $Location, self::$DeviceValues), 'instanceID' => $instanceID, 'ieee_address' => @IPS_GetProperty($instanceID, 'IEEE'), 'topic' => $Topic, @@ -223,6 +255,7 @@ public function GetConfigurationForm() $this->SendDebug('IPS Group Topic', json_encode($IPSGroupByTopic), 0); $ValuesGroups = []; + $ValueId=1; foreach ($Groups as $group) { $Value = []; //Array leeren $instanceID = array_search($group['ID'], $IPSGroupById); @@ -248,6 +281,8 @@ public function GetConfigurationForm() $Value['name'] = $Name; $Value['instanceID'] = 0; } + $Value['parent']=$this->AddParentElement($ValueId, $ValuesGroups, $Location, self::$GroupValues); + $Value['id'] = ++$ValueId; $Value['ID'] = $group['ID']; $Value['topic'] = $group['friendly_name']; $Value['DevicesCount'] = (string) count($group['devices']); @@ -269,9 +304,12 @@ public function GetConfigurationForm() $Topic = $IPSGroupByTopic[$instanceID]; unset($IPSGroupByTopic[$instanceID]); } - + $Location = explode('/', $Topic); + array_pop($Location); $ValuesGroups[] = [ 'name' => IPS_GetName($instanceID), + 'id' => ++$ValueId, + 'parent' => $this->AddParentElement($ValueId, $ValuesDevices, $Location,self::$GroupValues), 'instanceID' => $instanceID, 'ID' => $ID, 'topic' => $Topic, @@ -280,8 +318,12 @@ public function GetConfigurationForm() ]; } foreach ($IPSGroupByTopic as $instanceID => $Topic) { - $ValuesGroups[] = [ + $Location = explode('/', $Topic); + array_pop($Location); + $ValuesGroups[] = [ 'name' => IPS_GetName($instanceID), + 'id' => ++$ValueId, + 'parent' => $this->AddParentElement($ValueId, $ValuesDevices, $Location, self::$DeviceValues), 'instanceID' => $instanceID, 'ID' => @IPS_GetProperty($instanceID, 'GroupId'), 'topic' => $Topic, @@ -291,9 +333,9 @@ public function GetConfigurationForm() } $Form['actions'][0]['items'][0]['values'] = $ValuesDevices; - $Form['actions'][0]['items'][0]['rowCount'] = (count($ValuesDevices) < 35 ? count($ValuesDevices) : 35); + $Form['actions'][0]['items'][0]['rowCount'] = (count($ValuesDevices) < 20 ? count($ValuesDevices) : 20); $Form['actions'][1]['items'][0]['values'] = $ValuesGroups; - $Form['actions'][1]['items'][0]['rowCount'] = (count($ValuesGroups) < 35 ? count($ValuesGroups) : 35); + $Form['actions'][1]['items'][0]['rowCount'] = (count($ValuesGroups) < 20 ? count($ValuesGroups) : 20); return json_encode($Form); } @@ -361,6 +403,47 @@ public function getGroups() return []; } + private function TopicPathExists(array $Values, string $Topic):int|false { + foreach ($Values as $dir) { + if ($dir['topic'] === $Topic) { + return $dir['id']; + } + } + return false; + } + + private function AddParentElement(int &$Id, array &$Result, array $Topics, array $EmtpyValuesArray):int + { + $parentId = 0; // Für root-Verzeichnisse setzen wir parent auf 0 + $currentPath = ""; // Der aktuelle Pfad wird hier zusammengebaut + + // Durchlaufe jedes Teil des Verzeichnisses + foreach ($Topics as $Topic) { + // Baue den aktuellen Pfad schrittweise auf + $currentPath .= ($currentPath === "" ? "" : "/") . $Topic; + + // Prüfe, ob das Verzeichnis bereits existiert (nur anhand des vollständigen Pfads) + $existingId = $this->TopicPathExists($Result, $currentPath); + + if ($existingId === false) { + // Füge das Verzeichnis dem Ergebnis hinzu + $Result[] = array_merge( $EmtpyValuesArray, [ + 'id' => $Id, + 'topic' => $currentPath, // Fügt den gesamten Pfad hinzu + 'parent' => $parentId, // Für root 0, sonst parentId des vorherigen Verzeichnisses + 'expanded'=>true + ]); + + // Setze den parentId für das nächste Verzeichnis + $parentId = $Id; + $Id++; // Erhöhe die ID für das nächste Verzeichnis + } else { + // Falls das Verzeichnis existiert, setze parentId auf die ID des existierenden Verzeichnisses + $parentId = $existingId; + } + } + return $parentId; + } /** * GetIPSInstancesByIEEE *