no message

This commit is contained in:
belevo\mh
2025-11-19 13:37:10 +01:00
parent 3bc5e59054
commit d2cf7d0c71

View File

@@ -8,8 +8,12 @@ class VGT_Sub extends IPSModule
{
parent::Create();
// Konfiguration
$this->RegisterPropertyString('DeviceID', '');
// Auf Parent-Connect reagieren (für Re-Subscribe)
$this->RegisterMessage($this->InstanceID, FM_CONNECT);
/** -------------------------------------------
* STATUS Variablen (schreibbar)
* ------------------------------------------*/
@@ -55,15 +59,42 @@ class VGT_Sub extends IPSModule
{
parent::ApplyChanges();
// Nach Konfigänderung Topics abonnieren
if ($this->HasActiveParent()) {
$this->SubscribeAll();
}
}
/**
* Reaktion auf Systemnachrichten (z.B. Parent verbindet sich)
*/
public function MessageSink($TimeStamp, $SenderID, $Message, $Data)
{
parent::MessageSink($TimeStamp, $SenderID, $Message, $Data);
switch ($Message) {
case FM_CONNECT:
// Parent (MQTT-Client) hat Verbindung aufgebaut -> erneut subscriben
if ($this->HasActiveParent()) {
$this->SendDebug('MQTT', 'FM_CONNECT -> Re-Subscribe Topics', 0);
$this->SubscribeAll();
}
break;
}
}
/**
* Alle benötigten Topics abonnieren
*/
private function SubscribeAll(): void
{
$device = $this->ReadPropertyString('DeviceID');
if ($device === '') {
$this->SendDebug('MQTT', 'Kein DeviceID gesetzt, keine Subscribes', 0);
return;
}
// READ: Topics abonnieren
$this->Subscribe("feedback-request/$device");
// WRITE: Remote-Control-Requests ebenfalls abonnieren
$this->Subscribe("remote-control-request/$device");
}
@@ -117,6 +148,8 @@ class VGT_Sub extends IPSModule
'Payload' => ''
];
$this->SendDebug('MQTT', 'Subscribe Topic=' . $topic, 0);
$this->SendDataToParent(json_encode([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
] + $packet));
@@ -141,6 +174,8 @@ class VGT_Sub extends IPSModule
'Payload' => $payload
];
$this->SendDebug('MQTT', 'Publish Topic=' . $topic . ' Payload=' . $payload, 0);
$this->SendDataToParent(json_encode([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
] + $packet));
@@ -152,18 +187,34 @@ class VGT_Sub extends IPSModule
* ------------------------------------------*/
public function ReceiveData($JSONString)
{
$data = json_decode($JSONString, true);
if (!is_array($data)) {
// Äußeres JSON vom MQTT-Client
$data = json_decode($JSONString);
if ($data === null) {
$this->SendDebug('MQTT', 'Ungültiges JSON: ' . $JSONString, 0);
return;
}
$topic = $data['Topic'] ?? '';
$payload = $data['Payload'] ?? '';
// Neuere MQTT-Client-Version: Topic/Payload stecken in Buffer als JSON-String
if (isset($data->Buffer)) {
$buffer = json_decode($data->Buffer, true);
} else {
// Fallback: evtl. altes Format
$buffer = json_decode($JSONString, true);
}
if (!is_array($buffer)) {
$this->SendDebug('MQTT', 'Buffer ist kein Array: ' . print_r($buffer, true), 0);
return;
}
$topic = $buffer['Topic'] ?? '';
$payload = $buffer['Payload'] ?? '';
$this->SendDebug('MQTT', 'Receive: Topic=' . $topic . ' Payload=' . $payload, 0);
$device = $this->ReadPropertyString('DeviceID');
if ($device === '') {
$this->SendDebug('MQTT', 'Keine DeviceID gesetzt, ignoriere Receive', 0);
return;
}