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"); if ($gatewayID == 0 || $deviceID == "") { $this->SetStatus(104); return; } // 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) { $guidMQTTDevice = "{043EA491-84DA-4DFD-B9D4-44B4E63F23D1}"; $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($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"]); } } private function OnReceiveMQTT(string $topic, string $value) { IPS_LogMessage("Int_VGT", "Received: $topic -> $value"); $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 ); } } } ?>