From d2cf7d0c7182396780c74f00ae0090445ecfdd21 Mon Sep 17 00:00:00 2001 From: "belevo\\mh" Date: Wed, 19 Nov 2025 13:37:10 +0100 Subject: [PATCH] no message --- VGT_Sub/module.php | 65 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/VGT_Sub/module.php b/VGT_Sub/module.php index bf5cd08..ded3da1 100644 --- a/VGT_Sub/module.php +++ b/VGT_Sub/module.php @@ -8,8 +8,12 @@ class VGT_Sub extends IPSModule { parent::Create(); + // Konfiguration $this->RegisterPropertyString('DeviceID', ''); + // Auf Parent-Connect reagieren (für Re-Subscribe) + $this->RegisterMessage($this->InstanceID, FM_CONNECT); + /** ------------------------------------------- * STATUS Variablen (schreibbar) * ------------------------------------------*/ @@ -55,15 +59,42 @@ class VGT_Sub extends IPSModule { parent::ApplyChanges(); + // Nach Konfigänderung Topics abonnieren + if ($this->HasActiveParent()) { + $this->SubscribeAll(); + } + } + + /** + * Reaktion auf Systemnachrichten (z.B. Parent verbindet sich) + */ + public function MessageSink($TimeStamp, $SenderID, $Message, $Data) + { + parent::MessageSink($TimeStamp, $SenderID, $Message, $Data); + + switch ($Message) { + case FM_CONNECT: + // Parent (MQTT-Client) hat Verbindung aufgebaut -> erneut subscriben + if ($this->HasActiveParent()) { + $this->SendDebug('MQTT', 'FM_CONNECT -> Re-Subscribe Topics', 0); + $this->SubscribeAll(); + } + break; + } + } + + /** + * Alle benötigten Topics abonnieren + */ + private function SubscribeAll(): void + { $device = $this->ReadPropertyString('DeviceID'); if ($device === '') { + $this->SendDebug('MQTT', 'Kein DeviceID gesetzt, keine Subscribes', 0); return; } - // READ: Topics abonnieren $this->Subscribe("feedback-request/$device"); - - // WRITE: Remote-Control-Requests ebenfalls abonnieren $this->Subscribe("remote-control-request/$device"); } @@ -117,6 +148,8 @@ class VGT_Sub extends IPSModule 'Payload' => '' ]; + $this->SendDebug('MQTT', 'Subscribe Topic=' . $topic, 0); + $this->SendDataToParent(json_encode([ 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' ] + $packet)); @@ -141,6 +174,8 @@ class VGT_Sub extends IPSModule 'Payload' => $payload ]; + $this->SendDebug('MQTT', 'Publish Topic=' . $topic . ' Payload=' . $payload, 0); + $this->SendDataToParent(json_encode([ 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' ] + $packet)); @@ -152,18 +187,34 @@ class VGT_Sub extends IPSModule * ------------------------------------------*/ public function ReceiveData($JSONString) { - $data = json_decode($JSONString, true); - if (!is_array($data)) { + // Äußeres JSON vom MQTT-Client + $data = json_decode($JSONString); + if ($data === null) { + $this->SendDebug('MQTT', 'Ungültiges JSON: ' . $JSONString, 0); return; } - $topic = $data['Topic'] ?? ''; - $payload = $data['Payload'] ?? ''; + // Neuere MQTT-Client-Version: Topic/Payload stecken in Buffer als JSON-String + if (isset($data->Buffer)) { + $buffer = json_decode($data->Buffer, true); + } else { + // Fallback: evtl. altes Format + $buffer = json_decode($JSONString, true); + } + + if (!is_array($buffer)) { + $this->SendDebug('MQTT', 'Buffer ist kein Array: ' . print_r($buffer, true), 0); + return; + } + + $topic = $buffer['Topic'] ?? ''; + $payload = $buffer['Payload'] ?? ''; $this->SendDebug('MQTT', 'Receive: Topic=' . $topic . ' Payload=' . $payload, 0); $device = $this->ReadPropertyString('DeviceID'); if ($device === '') { + $this->SendDebug('MQTT', 'Keine DeviceID gesetzt, ignoriere Receive', 0); return; }