diff --git a/MQTTBatterySDL/module.php b/MQTTBatterySDL/module.php index 1d8de02..5a01818 100644 --- a/MQTTBatterySDL/module.php +++ b/MQTTBatterySDL/module.php @@ -42,7 +42,16 @@ class MQTTBatterySDL extends IPSModule { parent::ApplyChanges(); - $this->SetReceiveDataFilter('.*'); + $suffix = $this->ReadPropertyString('TopicSuffix'); + + if ($suffix == '') { + $this->SetStatus(IS_INACTIVE); + return; + } + + $filter = '.*"Topic":"(feedback-request|remote-control-request)\/' . preg_quote($suffix, '/') . '".*'; + + $this->SetReceiveDataFilter($filter); $this->SetStatus(IS_ACTIVE); } @@ -327,9 +336,44 @@ class MQTTBatterySDL extends IPSModule return $json; } - public function ReceiveData($JSONString) +public function ReceiveData($JSONString) { - IPS_LogMessage('MQTTBatterySDL', $JSONString); + $data = json_decode($JSONString, true); + + $this->SendDebug('ReceiveData', $JSONString, 0); + + if (!is_array($data)) { + return; + } + + $topic = $data['Topic'] ?? ''; + $payload = $data['Payload'] ?? ''; + + $suffix = $this->ReadPropertyString('TopicSuffix'); + + if ($topic === 'feedback-request/' . $suffix) { + $json = $this->BuildReadResponse(); + + $this->PublishMQTT( + 'feedback-response/' . $suffix, + $json + ); + + return; + } + + if ($topic === 'remote-control-request/' . $suffix) { + $json = $this->HandleRemoteControlJSON($payload); + + if ($json !== null) { + $this->PublishMQTT( + 'remote-control-response/' . $suffix, + $json + ); + } + + return; + } } private function IsValidVariable(int $id)