no message

This commit is contained in:
2025-11-13 08:08:57 +01:00
parent b223f944b8
commit 5df9907c93
3 changed files with 53 additions and 93 deletions

View File

@@ -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/<DeviceID>"
},
{
"type": "Label",
"label": "→ remote-control-response/<DeviceID>"
}
]
}

View File

@@ -8,6 +8,6 @@
"{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}"
],
"childRequirements": [],
"implemented": [],
"implemented": ["MQTTSERVER"],
"prefix": "INTVGT"
}

View File

@@ -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 <<<PHP
<?php
\$parent = IPS_GetParent(\$_IPS['SELF']);
IPS_RequestAction(\$parent, "OnReceive", json_encode([
"Topic" => \$_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]));
}
}