diff --git a/MQTTBatterySDL/module.php b/MQTTBatterySDL/module.php index 0dd1f1f..26f188a 100644 --- a/MQTTBatterySDL/module.php +++ b/MQTTBatterySDL/module.php @@ -38,6 +38,10 @@ class MQTTBatterySDL extends IPSModule // MQTT Parent verbinden $this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}'); + + $this->RegisterTimer('PublishDelay', 0, 'MBSDL_DoDelayedPublish($_IPS["TARGET"]);'); + $this->SetBuffer('PublishTopic', ''); + $this->SetBuffer('PublishPayload', ''); } public function ApplyChanges() @@ -381,7 +385,7 @@ class MQTTBatterySDL extends IPSModule $json = $this->BuildReadResponse(); - $this->PublishViaHelper( + $this->QueuePublish( 'feedback-response/' . $suffix, $json ); @@ -399,7 +403,7 @@ class MQTTBatterySDL extends IPSModule if ($json !== null) { - $this->PublishViaHelper( + $this->QueuePublish( 'remote-control-response/' . $suffix, $json ); @@ -440,6 +444,32 @@ class MQTTBatterySDL extends IPSModule } + private function QueuePublish(string $topic, string $payload) +{ + $this->SetBuffer('PublishTopic', $topic); + $this->SetBuffer('PublishPayload', $payload); + + $this->SetTimerInterval('PublishDelay', 500); +} + +public function DoDelayedPublish() +{ + $this->SetTimerInterval('PublishDelay', 0); + + $topic = $this->GetBuffer('PublishTopic'); + $payload = $this->GetBuffer('PublishPayload'); + + $this->SetBuffer('PublishTopic', ''); + $this->SetBuffer('PublishPayload', ''); + + if ($topic == '' || $payload == '') { + return; + } + + $this->PublishViaHelper($topic, $payload); +} + + private function SafeSend(array $packet) {