no message

This commit is contained in:
dh
2025-11-13 08:16:38 +01:00
parent 6c9e5fd4e9
commit 8f3ebda3af
+57 -22
View File
@@ -7,73 +7,108 @@ class Int_VGT extends IPSModule
public function Create() public function Create()
{ {
parent::Create(); parent::Create();
$this->RegisterPropertyString("DeviceID", ""); $this->RegisterPropertyString("DeviceID", "");
} }
public function ApplyChanges() public function ApplyChanges()
{ {
parent::ApplyChanges(); parent::ApplyChanges();
// Verbindung zur ausgewählten Gateway-Instanz
$this->ConnectParent("{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}");
$deviceID = $this->ReadPropertyString("DeviceID");
if ($deviceID === "") { // MQTT Server als Parent verbinden
$this->SetStatus(104); $this->ConnectParent("{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}");
if ($this->ReadPropertyString("DeviceID") == "") {
$this->SetStatus(104); // config error
} else { } else {
$this->SetStatus(102); $this->SetStatus(102); // OK
} }
} }
/**
* Required when using RequestAction
*/
public function RequestAction($Ident, $Value)
{
// Im Moment haben wir keine Action-Variablen
// Wird aber von Symcon benötigt → also leer implementiert
return;
}
/**
* Wird vom MQTT Server aufgerufen wenn Daten ankommen
*/
public function ReceiveData(string $JSONString) public function ReceiveData(string $JSONString)
{ {
$data = json_decode($JSONString, true); $buffer = json_decode($JSONString, true);
if ($data === null) { if ($buffer === null) {
return; IPS_LogMessage("Int_VGT", "Invalid JSON received.");
}
if (!isset($data["Topic"]) || !isset($data["Payload"])) {
return; return;
} }
$topic = $data["Topic"]; if (!isset($buffer['Topic']) || !isset($buffer['Payload'])) {
$payload = $data["Payload"]; IPS_LogMessage("Int_VGT", "Missing keys in buffer.");
return;
}
$topic = $buffer['Topic'];
$payload = $buffer['Payload'];
IPS_LogMessage("Int_VGT", "MQTT RX: {$topic}{$payload}");
IPS_LogMessage("Int_VGT", "Received topic: {$topic}, payload: {$payload}");
$this->HandleIncoming($topic, $payload); $this->HandleIncoming($topic, $payload);
} }
/**
* Verarbeitet eingehende MQTT-Nachrichten
*/
private function HandleIncoming(string $topic, string $payload) private function HandleIncoming(string $topic, string $payload)
{ {
$deviceID = $this->ReadPropertyString("DeviceID"); $deviceID = $this->ReadPropertyString("DeviceID");
if ($deviceID == "") {
return; // no config
}
// 1) Feedback Request
if ($topic === "feedback-request/{$deviceID}") { if ($topic === "feedback-request/{$deviceID}") {
$response = json_encode([ $response = json_encode([
"status" => "ok", "status" => "ok",
"timestamp" => time() "timestamp" => time()
]); ]);
$this->Publish("feedback-response/{$deviceID}", $response); $this->Publish("feedback-response/{$deviceID}", $response);
return; return;
} }
// 2) Remote Control Echo
if ($topic === "remote-control-response/{$deviceID}") { if ($topic === "remote-control-response/{$deviceID}") {
// Echo same payload
$this->Publish("remote-control-feedback/{$deviceID}", $payload); $this->Publish("remote-control-feedback/{$deviceID}", $payload);
return; return;
} }
} }
/**
* Publisht MQTT-Daten über das Gateway
*/
private function Publish(string $topic, string $payload) private function Publish(string $topic, string $payload)
{ {
$message = [
"Topic" => $topic,
"Payload" => $payload,
"Retain" => false,
"QoS" => 0
];
$data = [ $data = [
"DataID" => "{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}", "DataID" => "{7F8A48DD-F8AD-4F9C-8E6B-9E4D92DDB8F9}",
"Packet" => $message "Packet" => [
"Topic" => $topic,
"Payload" => $payload,
"QoS" => 0,
"Retain" => false
]
]; ];
IPS_LogMessage("Int_VGT", "MQTT TX: $topic$payload");
$this->SendDataToParent(json_encode($data)); $this->SendDataToParent(json_encode($data));
} }
} }
?> ?>