From b223f944b81ba9f028c967f06d73f600e635e9a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fliger?= Date: Thu, 13 Nov 2025 08:04:40 +0100 Subject: [PATCH] no message --- Int_VGT/form.json | 8 +--- Int_VGT/module.json | 2 +- Int_VGT/module.php | 93 ++++++++++++++++++++++++--------------------- 3 files changed, 51 insertions(+), 52 deletions(-) diff --git a/Int_VGT/form.json b/Int_VGT/form.json index db8fe2b..52a6438 100644 --- a/Int_VGT/form.json +++ b/Int_VGT/form.json @@ -1,11 +1,5 @@ { "elements": [ - { - "type": "SelectInstance", - "name": "MQTTServer", - "caption": "MQTT Server", - "filter": "moduleID:{043EA491-84DA-4DFD-B9D4-44B4E63F23D1}" - }, { "type": "ValidationTextBox", "name": "DeviceID", @@ -15,7 +9,7 @@ "actions": [ { "type": "Label", - "label": "Dieses Modul erzeugt automatisch MQTT Server Devices für die beiden Topics:" + "label": "Dieses Modul registriert automatisch die MQTT Topics:" }, { "type": "Label", diff --git a/Int_VGT/module.json b/Int_VGT/module.json index 57124e8..baff49a 100644 --- a/Int_VGT/module.json +++ b/Int_VGT/module.json @@ -5,7 +5,7 @@ "vendor": "", "aliases": ["INT VGT Integration"], "parentRequirements": [ - "{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}" + "{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}" ], "childRequirements": [], "implemented": [], diff --git a/Int_VGT/module.php b/Int_VGT/module.php index 7feda2f..ccb64aa 100644 --- a/Int_VGT/module.php +++ b/Int_VGT/module.php @@ -8,18 +8,15 @@ class Int_VGT extends IPSModule { parent::Create(); - // Properties - $this->RegisterPropertyInteger("MQTTServer", 0); $this->RegisterPropertyString("DeviceID", ""); - // Create script handler only once + // Create handler script if (!@$this->GetIDForIdent("ReceiveScript")) { - $scriptID = IPS_CreateScript(0); - IPS_SetName($scriptID, "INTVGT_ReceiveHandler"); - IPS_SetIdent($scriptID, "ReceiveScript"); - IPS_SetParent($scriptID, $this->InstanceID); - - IPS_SetScriptContent($scriptID, $this->GenerateReceiveScript()); + $id = IPS_CreateScript(0); + IPS_SetName($id, "INTVGT_ReceiveHandler"); + IPS_SetIdent($id, "ReceiveScript"); + IPS_SetParent($id, $this->InstanceID); + IPS_SetScriptContent($id, $this->GenerateReceiveScript()); } } @@ -27,84 +24,92 @@ class Int_VGT extends IPSModule { parent::ApplyChanges(); - $mqttServer = $this->ReadPropertyInteger("MQTTServer"); - $deviceId = $this->ReadPropertyString("DeviceID"); + $gatewayID = $this->GetGatewayID(); + $deviceID = $this->ReadPropertyString("DeviceID"); - if ($mqttServer == 0 || $deviceId == "") { - $this->SetStatus(104); // missing config + if ($gatewayID == 0 || $deviceID == "") { + $this->SetStatus(104); return; } - // Create MQTT Server Devices - $this->RegisterMQTTDevice("FeedbackRequest", "feedback-request/" . $deviceId); - $this->RegisterMQTTDevice("RemoteControlResponse", "remote-control-response/" . $deviceId); + // Create MQTT devices + $this->RegisterMQTTDevice("FeedbackRequest", + "feedback-request/" . $deviceID); + + $this->RegisterMQTTDevice("RemoteControlResponse", + "remote-control-response/" . $deviceID); $this->SetStatus(102); } private function RegisterMQTTDevice(string $ident, string $topic) { - $instanceID = @$this->GetIDForIdent($ident); + $guidMQTTDevice = "{043EA491-84DA-4DFD-B9D4-44B4E63F23D1}"; - if ($instanceID === false) { - $instanceID = IPS_CreateInstance("{058A5647-0C2F-4D7D-9F4B-50A28C7D83BA}"); // MQTT Server Device - IPS_SetParent($instanceID, $this->InstanceID); - IPS_SetIdent($instanceID, $ident); - IPS_SetName($instanceID, "MQTT Device: " . $ident); + $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); } - IPS_SetProperty($instanceID, "Topic", $topic); - IPS_SetProperty($instanceID, "Server", $this->ReadPropertyInteger("MQTTServer")); - IPS_SetProperty($instanceID, "Script", $this->GetIDForIdent("ReceiveScript")); - IPS_ApplyChanges($instanceID); + IPS_SetProperty($id, "Active", true); + IPS_SetProperty($id, "Topic", $topic); + IPS_SetProperty($id, "Script", $this->GetIDForIdent("ReceiveScript")); + IPS_ApplyChanges($id); - return $instanceID; + return $id; + } + + private function GetGatewayID(): int + { + $instance = IPS_GetInstance($this->InstanceID); + return $instance['ConnectionID']; } - /** Script to be executed when MQTT message is received */ private function GenerateReceiveScript(): string { - return << \$_IPS['Topic'], "Value" => \$_IPS['VALUE'] ])); -SCR; +PHP; } - /** Called by ReceiveScript */ public function RequestAction($ident, $value) { - if ($ident == "OnReceive") { + if ($ident === "OnReceive") { $data = json_decode($value, true); $this->OnReceiveMQTT($data["Topic"], $data["Value"]); } } - /** THIS IS YOUR CALLBACK FOR INCOMING MQTT MESSAGES */ private function OnReceiveMQTT(string $topic, string $value) { - IPS_LogMessage("Int_VGT", "Received on $topic: $value"); + IPS_LogMessage("Int_VGT", "Received: $topic -> $value"); - // Here you implement later your handling logic - // Example: respond to feedback-request - $mqttServer = $this->ReadPropertyInteger("MQTTServer"); + $gatewayID = $this->GetGatewayID(); + // Auto responses (you can modify later) if (str_contains($topic, "feedback-request")) { - $response = json_encode([ - "status" => "ok", - "timestamp" => time() - ]); - MQTT_Publish($mqttServer, str_replace("request", "response", $topic), $response, 0, false); + MQTT_Publish($gatewayID, + str_replace("request", "response", $topic), + json_encode(["status" => "ok"]) + ); } if (str_contains($topic, "remote-control-response")) { - // Echo back the same payload - MQTT_Publish($mqttServer, str_replace("response", "feedback", $topic), $value, 0, false); + MQTT_Publish($gatewayID, + str_replace("response", "feedback", $topic), + $value + ); } } }