diff --git a/MQTTBatterySDL/module.php b/MQTTBatterySDL/module.php index d888ae6..a89973b 100644 --- a/MQTTBatterySDL/module.php +++ b/MQTTBatterySDL/module.php @@ -408,25 +408,45 @@ class MQTTBatterySDL extends IPSModule } } - private function PublishMQTT(string $topic, string $payload) - { - sleep(1); - $this->SendDebug( - 'PublishMQTT', - $topic . ' => ' . $payload, - 0 - ); - $this->SendDataToParent(json_encode([ - 'DataID' => '{018EF6B5-AB94-40C6-AA53-46943E824ACF}', + + + private function PublishMQTT(string $topic, string $payload) + { + $this->SafeSend([ + 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}', 'PacketType' => 3, - 'Payload' => $payload, 'QualityOfService' => 0, 'Retain' => false, - 'Topic' => $topic - ])); + 'Topic' => $topic, + 'Payload' => $payload + ]); + + $this->SendDebug('PublishMQTT', $topic . ' → ' . $payload, 0); } + + + + + + private function SafeSend(array $packet) +{ + $parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0; + + if ($parent === 0 || !IPS_InstanceExists($parent)) { + $this->SendDebug('SafeSend', 'Kein Parent vorhanden', 0); + return; + } + + if (IPS_GetInstance($parent)['InstanceStatus'] !== 102) { + $this->SendDebug('SafeSend', 'Parent nicht aktiv', 0); + return; + } + + @$this->SendDataToParent(json_encode($packet)); +} + private function IsValidVariable(int $id) { return (