diff --git a/Shelly_Parser_MQTT/libs/ShellyParser.php b/Shelly_Parser_MQTT/libs/ShellyParser.php index 22c7f1c..aed068e 100644 --- a/Shelly_Parser_MQTT/libs/ShellyParser.php +++ b/Shelly_Parser_MQTT/libs/ShellyParser.php @@ -15,9 +15,12 @@ class ShellyParser return 'unknown'; } - // alles nach "shelly" - $rest = substr($src, 6); // z.B. "1g4-12345" oder "plusplugs-xyz" + // nach 'shelly' den Rest nehmen + $rest = substr($src, 6); // z.B. "1g4-12345" oder "mini3-abc" + + // alles vor dem ersten "-" ist der Typ $parts = explode('-', $rest); + return $parts[0] ?? 'unknown'; } diff --git a/Shelly_Parser_MQTT/module.php b/Shelly_Parser_MQTT/module.php index 5d13574..823ab30 100644 --- a/Shelly_Parser_MQTT/module.php +++ b/Shelly_Parser_MQTT/module.php @@ -139,14 +139,16 @@ class Shelly_Parser_MQTT extends IPSModule * ---------------------------------------------------------*/ private function HandleOnline(string $deviceID, string $payload): void { - $ident = $deviceID . '_online'; - $name = "Online"; + $value = ($payload === 'true' || $payload === '1'); - $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); - $this->SetValue($ident, $payload === 'true' || $payload === '1'); + $this->SetValue( + $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online'), + $value + ); } + /* --------------------------------------------------------- * RPC-EVENTS * ---------------------------------------------------------*/ @@ -158,43 +160,44 @@ private function HandleRPC(string $deviceID, string $payload): void } $src = $json['src'] ?? ''; - if (!str_starts_with($src, 'shelly')) { - return; // kein Shelly-Gerät - } - - // Typ extrahieren - $type = ShellyParser::ExtractType($src); - $typeIdent = $deviceID . '_type'; - $this->EnsureStringVariable($deviceID, $typeIdent, 'Typ'); - $this->SetValue($typeIdent, $type); - - $params = $json['params'] ?? []; - if (!is_array($params)) { + if (!is_string($src) || !str_starts_with($src, 'shelly')) { return; } + // Modell Typ extrahieren + $type = ShellyParser::ExtractType($src); + + // Typ Variable + $this->SetValue( + $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ'), + $type + ); + + // PARAMETER MAPPEN + $params = $json['params'] ?? []; $mapped = ShellyParser::MapParams($params); // INPUT - if (array_key_exists('input', $mapped)) { - $ident = $deviceID . '_input'; - $this->EnsureBooleanVariable($deviceID, $ident, 'Input'); - $this->SetValue($ident, (bool)$mapped['input']); + if (isset($mapped['input'])) { + $this->SetValue( + $this->EnsureBooleanVariable($deviceID, $deviceID . '_input', 'Input'), + (bool)$mapped['input'] + ); } // OUTPUT - if (array_key_exists('output', $mapped)) { - $ident = $deviceID . '_output'; - $varID = $this->EnsureBooleanVariable($deviceID, $ident, 'Output'); - $this->EnableAction($ident); - $this->SetValue($ident, (bool)$mapped['output']); + if (isset($mapped['output'])) { + $varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_output', 'Output'); + $this->EnableAction($deviceID . '_output'); + $this->SetValue($varID, (bool)$mapped['output']); } - // TEMPERATUR - if (array_key_exists('temperature', $mapped)) { - $ident = $deviceID . '_temperature'; - $this->EnsureFloatVariable($deviceID, $ident, 'Temperatur'); - $this->SetValue($ident, (float)$mapped['temperature']); + // TEMPERATURE + if (isset($mapped['temperature'])) { + $this->SetValue( + $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur'), + (float)$mapped['temperature'] + ); } } @@ -206,11 +209,17 @@ private function HandleRPC(string $deviceID, string $payload): void { $folderID = $this->GetDeviceFolder($deviceID); - $varID = @$this->GetIDForIdent($ident); - if ($varID === false) { - $varID = $this->RegisterVariableBoolean($ident, $name); - IPS_SetParent($varID, $folderID); + // Immer im Ordner suchen + foreach (IPS_GetChildrenIDs($folderID) as $cid) { + if (IPS_GetObject($cid)['ObjectIdent'] === $ident) { + return $cid; + } } + + // Neue Variable anlegen + $varID = $this->RegisterVariableBoolean($ident, $name); + IPS_SetParent($varID, $folderID); + return $varID; } @@ -218,11 +227,15 @@ private function HandleRPC(string $deviceID, string $payload): void { $folderID = $this->GetDeviceFolder($deviceID); - $varID = @$this->GetIDForIdent($ident); - if ($varID === false) { - $varID = $this->RegisterVariableFloat($ident, $name); - IPS_SetParent($varID, $folderID); + foreach (IPS_GetChildrenIDs($folderID) as $cid) { + if (IPS_GetObject($cid)['ObjectIdent'] === $ident) { + return $cid; + } } + + $varID = $this->RegisterVariableFloat($ident, $name); + IPS_SetParent($varID, $folderID); + return $varID; } @@ -230,32 +243,37 @@ private function HandleRPC(string $deviceID, string $payload): void { $folderID = $this->GetDeviceFolder($deviceID); - $varID = @$this->GetIDForIdent($ident); - if ($varID === false) { - $varID = $this->RegisterVariableString($ident, $name); - IPS_SetParent($varID, $folderID); + foreach (IPS_GetChildrenIDs($folderID) as $cid) { + if (IPS_GetObject($cid)['ObjectIdent'] === $ident) { + return $cid; + } } + + $varID = $this->RegisterVariableString($ident, $name); + IPS_SetParent($varID, $folderID); + return $varID; } + private function GetDeviceFolder(string $deviceID): int { $folderIdent = 'folder_' . $deviceID; - $folderID = @$this->GetIDForIdent($folderIdent); + $folderID = @IPS_GetObjectIDByIdent($folderIdent); if ($folderID !== false) { return $folderID; } - // Ordner erzeugen $folderID = IPS_CreateCategory(); IPS_SetParent($folderID, $this->InstanceID); - IPS_SetIdent($folderID, $folderIdent); IPS_SetName($folderID, $deviceID); + IPS_SetIdent($folderID, $folderIdent); return $folderID; } + } ?>