diff --git a/Shelly_Parser_MQTT/module.php b/Shelly_Parser_MQTT/module.php index 6e8241c..647cff1 100644 --- a/Shelly_Parser_MQTT/module.php +++ b/Shelly_Parser_MQTT/module.php @@ -16,7 +16,6 @@ class Shelly_Parser_MQTT extends IPSModule // Auf alle Topics hören $this->Subscribe('#'); - // Debug Switch $this->RegisterPropertyBoolean('Debug', false); } @@ -28,22 +27,6 @@ class Shelly_Parser_MQTT extends IPSModule $this->Subscribe('#'); } - /* --------------------------------------------------------- - * DEBUG WRAPPER - * ---------------------------------------------------------*/ - private function Log($title, $msg) - { - if (!$this->ReadPropertyBoolean('Debug')) { - return; - } - - // IPS Log - IPS_LogMessage("ShellyMQTT - $title", $msg); - - // Instance Debug - $this->SendDebug($title, $msg, 0); - } - /* --------------------------------------------------------- * MQTT SUBSCRIBE * ---------------------------------------------------------*/ @@ -60,8 +43,6 @@ class Shelly_Parser_MQTT extends IPSModule $this->SendDataToParent(json_encode([ 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' ] + $packet)); - - $this->Log("Subscribe", "Topic: $topic"); } /* --------------------------------------------------------- @@ -81,7 +62,7 @@ class Shelly_Parser_MQTT extends IPSModule 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' ] + $packet)); - $this->Log("Publish", "$topic → $payload"); + $this->SendDebug("Publish", "$topic → $payload", 0); } /* --------------------------------------------------------- @@ -89,7 +70,7 @@ class Shelly_Parser_MQTT extends IPSModule * ---------------------------------------------------------*/ public function RequestAction($Ident, $Value) { - $this->Log("RequestAction", "$Ident → " . json_encode($Value)); + $this->SendDebug('RequestAction', "$Ident → " . json_encode($Value), 0); if (!str_contains($Ident, '_output_')) { throw new Exception("Unknown Ident: " . $Ident); @@ -119,18 +100,18 @@ class Shelly_Parser_MQTT extends IPSModule ] ]); - $this->Log("MQTT SEND", "$topic : $payload"); + $this->SendDebug('MQTT SEND', "$topic : $payload", 0); // absenden $this->Publish($topic, $payload); } /* --------------------------------------------------------- - * RECEIVE MQTT + * RECEIVE MQTT DATA * ---------------------------------------------------------*/ public function ReceiveData($JSONString) { - $this->Log("ReceiveData Raw", $JSONString); + $this->SendDebug('ReceiveData', $JSONString, 0); $data = json_decode($JSONString, true); if (!is_array($data)) { @@ -144,8 +125,6 @@ class Shelly_Parser_MQTT extends IPSModule return; } - $this->Log("ReceiveTopic", "$topic → $payload"); - $parts = explode('/', $topic); $deviceID = $parts[0] ?? ''; @@ -174,19 +153,15 @@ class Shelly_Parser_MQTT extends IPSModule { $value = ($payload === 'true' || $payload === '1'); - $this->Log("DeviceOnline", "$deviceID = " . json_encode($value)); - $varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online'); SetValue($varID, $value); } /* --------------------------------------------------------- - * RPC-EVENTS + * RPC-EVENTS VERARBEITEN * ---------------------------------------------------------*/ private function HandleRPC(string $deviceID, string $payload): void { - $this->Log("HandleRPC", "$deviceID : $payload"); - $json = json_decode($payload, true); if (!is_array($json)) { return; @@ -211,9 +186,7 @@ class Shelly_Parser_MQTT extends IPSModule * -------------------------*/ foreach ($mapped['outputs'] as $index => $value) { $ident = $deviceID . '_output_' . $index; - $name = "Output $index"; - - $this->Log("RPC Output", "$ident = " . json_encode($value)); + $name = 'Output ' . $index; $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); SetValue($varID, $value); @@ -224,9 +197,7 @@ class Shelly_Parser_MQTT extends IPSModule * -------------------------*/ foreach ($mapped['inputs'] as $index => $value) { $ident = $deviceID . '_input_' . $index; - $name = "Input $index"; - - $this->Log("RPC Input", "$ident = " . json_encode($value)); + $name = 'Input ' . $index; $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); SetValue($varID, $value); @@ -236,15 +207,13 @@ class Shelly_Parser_MQTT extends IPSModule * TEMPERATUR * -------------------------*/ if ($mapped['temperature'] !== null) { - $this->Log("RPC Temperature", json_encode($mapped['temperature'])); - $tempID = $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur'); SetValue($tempID, $mapped['temperature']); } } /* --------------------------------------------------------- - * VARIABLE-HELPER + * HELPER: BOOLEAN VARIABLE * ---------------------------------------------------------*/ private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int { @@ -256,7 +225,7 @@ class Shelly_Parser_MQTT extends IPSModule if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) { - // OUTPUT → immer Action setzen + // OUTPUT → immer CustomAction setzen if (str_contains($ident, '_output_')) { IPS_SetVariableCustomAction($cid, $this->InstanceID); @@ -285,12 +254,16 @@ class Shelly_Parser_MQTT extends IPSModule return $varID; } + /* --------------------------------------------------------- + * FLOAT VARIABLE + * ---------------------------------------------------------*/ private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int { $folderID = $this->GetDeviceFolder($deviceID); foreach (IPS_GetChildrenIDs($folderID) as $cid) { - if (IPS_GetObject($cid)['ObjectIdent'] === $ident) { + $obj = IPS_GetObject($cid); + if ($obj['ObjectIdent'] === $ident) { return $cid; } } @@ -300,12 +273,16 @@ class Shelly_Parser_MQTT extends IPSModule return $id; } + /* --------------------------------------------------------- + * STRING VARIABLE + * ---------------------------------------------------------*/ private function EnsureStringVariable(string $deviceID, string $ident, string $name): int { $folderID = $this->GetDeviceFolder($deviceID); foreach (IPS_GetChildrenIDs($folderID) as $cid) { - if (IPS_GetObject($cid)['ObjectIdent'] === $ident) { + $obj = IPS_GetObject($cid); + if ($obj['ObjectIdent'] === $ident) { return $cid; } } @@ -316,18 +293,20 @@ class Shelly_Parser_MQTT extends IPSModule } /* --------------------------------------------------------- - * DEVICE FOLDER + * GERÄTE-ORDNER * ---------------------------------------------------------*/ private function GetDeviceFolder(string $deviceID): int { $folderIdent = 'folder_' . $deviceID; foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) { - if (IPS_GetObject($cid)['ObjectIdent'] === $folderIdent) { + $obj = IPS_GetObject($cid); + if ($obj['ObjectIdent'] === $folderIdent && $obj['ObjectType'] === OBJECTTYPE_CATEGORY) { return $cid; } } + // neu anlegen $folderID = IPS_CreateCategory(); IPS_SetParent($folderID, $this->InstanceID); IPS_SetName($folderID, $deviceID); @@ -337,7 +316,7 @@ class Shelly_Parser_MQTT extends IPSModule } /* --------------------------------------------------------- - * VARIABLE FINDER + * HELPER: VARIABLE FINDEN * ---------------------------------------------------------*/ private function FindVariableByIdent(string $Ident) {