diff --git a/Int_VGT/form.json b/Int_VGT/form.json index 52a6438..9f5911e 100644 --- a/Int_VGT/form.json +++ b/Int_VGT/form.json @@ -5,19 +5,5 @@ "name": "DeviceID", "caption": "Device ID" } - ], - "actions": [ - { - "type": "Label", - "label": "Dieses Modul registriert automatisch die MQTT Topics:" - }, - { - "type": "Label", - "label": "→ feedback-request/" - }, - { - "type": "Label", - "label": "→ remote-control-response/" - } ] } diff --git a/Int_VGT/module.json b/Int_VGT/module.json index baff49a..12b86cb 100644 --- a/Int_VGT/module.json +++ b/Int_VGT/module.json @@ -8,6 +8,6 @@ "{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}" ], "childRequirements": [], - "implemented": [], + "implemented": ["MQTTSERVER"], "prefix": "INTVGT" } diff --git a/Int_VGT/module.php b/Int_VGT/module.php index ccb64aa..1d9ff39 100644 --- a/Int_VGT/module.php +++ b/Int_VGT/module.php @@ -9,108 +9,82 @@ class Int_VGT extends IPSModule parent::Create(); $this->RegisterPropertyString("DeviceID", ""); - - // Create handler script - if (!@$this->GetIDForIdent("ReceiveScript")) { - $id = IPS_CreateScript(0); - IPS_SetName($id, "INTVGT_ReceiveHandler"); - IPS_SetIdent($id, "ReceiveScript"); - IPS_SetParent($id, $this->InstanceID); - IPS_SetScriptContent($id, $this->GenerateReceiveScript()); - } } public function ApplyChanges() { parent::ApplyChanges(); - $gatewayID = $this->GetGatewayID(); - $deviceID = $this->ReadPropertyString("DeviceID"); + // MQTT-Server verbinden + $this->ConnectParent("{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}"); - if ($gatewayID == 0 || $deviceID == "") { + // Status prüfen + if ($this->ReadPropertyString("DeviceID") == "") { $this->SetStatus(104); + } else { + $this->SetStatus(102); + } + } + + /** Wird vom MQTT Server aufgerufen */ + public function ReceiveData($JSONString) + { + $data = json_decode($JSONString, true); + + // Beispiel MQTT-Datenformat: + // [ + // "Topic" => "...", + // "Payload" => "..." + // ] + + if (!isset($data["Topic"]) || !isset($data["Payload"])) { return; } - // Create MQTT devices - $this->RegisterMQTTDevice("FeedbackRequest", - "feedback-request/" . $deviceID); + $topic = $data["Topic"]; + $value = $data["Payload"]; - $this->RegisterMQTTDevice("RemoteControlResponse", - "remote-control-response/" . $deviceID); + IPS_LogMessage("Int_VGT", "MQTT RX: $topic → $value"); - $this->SetStatus(102); + $this->HandleIncoming($topic, $value); } - private function RegisterMQTTDevice(string $ident, string $topic) + /** Deine Logik */ + private function HandleIncoming(string $topic, string $value) { - $guidMQTTDevice = "{043EA491-84DA-4DFD-B9D4-44B4E63F23D1}"; + $deviceID = $this->ReadPropertyString("DeviceID"); + $gateway = $this->InstanceID; - $id = @$this->GetIDForIdent($ident); - if ($id === false) { - $id = IPS_CreateInstance($guidMQTTDevice); - IPS_SetParent($id, $this->InstanceID); - IPS_SetIdent($id, $ident); - IPS_SetName($id, "MQTT Device: " . $ident); + // Feedback Request + if ($topic === "feedback-request/$deviceID") { + + $response = json_encode([ + "status" => "ok", + "timestamp" => time() + ]); + + $this->Publish("feedback-response/$deviceID", $response); + return; } - IPS_SetProperty($id, "Active", true); - IPS_SetProperty($id, "Topic", $topic); - IPS_SetProperty($id, "Script", $this->GetIDForIdent("ReceiveScript")); - IPS_ApplyChanges($id); - - return $id; - } - - private function GetGatewayID(): int - { - $instance = IPS_GetInstance($this->InstanceID); - return $instance['ConnectionID']; - } - - private function GenerateReceiveScript(): string - { - return << \$_IPS['Topic'], - "Value" => \$_IPS['VALUE'] -])); - -PHP; - } - - public function RequestAction($ident, $value) - { - if ($ident === "OnReceive") { - $data = json_decode($value, true); - $this->OnReceiveMQTT($data["Topic"], $data["Value"]); + // Remote Control + if ($topic === "remote-control-response/$deviceID") { + $this->Publish("remote-control-feedback/$deviceID", $value); + return; } } - private function OnReceiveMQTT(string $topic, string $value) + /** MQTT Publish Wrapper */ + private function Publish(string $topic, string $payload) { - IPS_LogMessage("Int_VGT", "Received: $topic -> $value"); + $msg = [ + "Topic" => $topic, + "Payload" => $payload, + "Retain" => false, + "QoS" => 0 + ]; - $gatewayID = $this->GetGatewayID(); - - // Auto responses (you can modify later) - if (str_contains($topic, "feedback-request")) { - MQTT_Publish($gatewayID, - str_replace("request", "response", $topic), - json_encode(["status" => "ok"]) - ); - } - - if (str_contains($topic, "remote-control-response")) { - MQTT_Publish($gatewayID, - str_replace("response", "feedback", $topic), - $value - ); - } + $this->SendDataToParent(json_encode(["DataID" => "{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}", "Packet" => $msg])); } }