Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
Nall-chan committed Oct 16, 2024
1 parent 986809c commit c223a41
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 12 deletions.
12 changes: 6 additions & 6 deletions Configurator/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"delete": true,
"rowCount": 5,
"sort": {
"column": "name",
"column": "topic",
"direction": "ascending"
},
"columns": [
Expand All @@ -31,7 +31,7 @@
{
"caption": "Topic",
"name": "topic",
"width": "200px"
"width": "auto"
},
{
"caption": "Network Address",
Expand All @@ -56,7 +56,7 @@
{
"caption": "Description",
"name": "description",
"width": "auto"
"width": "200px"
},
{
"caption": "Type",
Expand Down Expand Up @@ -85,7 +85,7 @@
"delete": true,
"rowCount": 5,
"sort": {
"column": "name",
"column": "topic",
"direction": "ascending"
},
"columns": [
Expand All @@ -98,7 +98,7 @@
{
"caption": "Topic",
"name": "topic",
"width": "200px"
"width": "auto"
},
{
"caption": "Devices",
Expand All @@ -108,7 +108,7 @@
{
"caption": "Name",
"name": "name",
"width": "auto"
"width": "200px"
}
],
"values": []
Expand Down
95 changes: 89 additions & 6 deletions Configurator/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -133,6 +155,7 @@ public function GetConfigurationForm()
}
//Devices
$ValuesDevices = [];
$ValueId=1;
foreach ($Devices as $device) {
$Value = []; //Array leeren
$instanceID = array_search($device['ieeeAddr'], $IPSDevicesByIEEE);
Expand All @@ -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'];
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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']);
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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);
}

Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit c223a41

Please sign in to comment.