no message

This commit is contained in:
belevo\mh
2025-11-19 13:48:54 +01:00
parent d2cf7d0c71
commit ab2bd46426

View File

@@ -187,49 +187,64 @@ class VGT_Sub extends IPSModule
* ------------------------------------------*/
public function ReceiveData($JSONString)
{
// Äußeres JSON vom MQTT-Client
$this->SendDebug('MQTT', "RAW Incoming JSON: " . $JSONString, 0);
// ---------------------------------------------
// 1) Äußeres JSON vom MQTT-Client
// ---------------------------------------------
$data = json_decode($JSONString);
if ($data === null) {
$this->SendDebug('MQTT', 'Ungültiges JSON: ' . $JSONString, 0);
$this->SendDebug('MQTT', '❌ JSON ungültig (outer decode)', 0);
return;
}
// Neuere MQTT-Client-Version: Topic/Payload stecken in Buffer als JSON-String
// ---------------------------------------------
// 2) Buffer extrahieren
// ---------------------------------------------
if (isset($data->Buffer)) {
$this->SendDebug('MQTT', "Buffer gefunden (json-string): " . $data->Buffer, 0);
$buffer = json_decode($data->Buffer, true);
} else {
// Fallback: evtl. altes Format
$this->SendDebug('MQTT', "⚠️ Kein Buffer-Feld gefunden, versuche komplettes JSON", 0);
$buffer = json_decode($JSONString, true);
}
if (!is_array($buffer)) {
$this->SendDebug('MQTT', 'Buffer ist kein Array: ' . print_r($buffer, true), 0);
$this->SendDebug('MQTT', 'Buffer ist kein Array: ' . print_r($buffer, true), 0);
return;
}
// ---------------------------------------------
// 3) Topic / Payload lesen
// ---------------------------------------------
$topic = $buffer['Topic'] ?? '';
$payload = $buffer['Payload'] ?? '';
$this->SendDebug('MQTT', 'Receive: Topic=' . $topic . ' Payload=' . $payload, 0);
$this->SendDebug('MQTT', "📩 Receive: Topic='$topic' Payload='$payload'", 0);
$device = $this->ReadPropertyString('DeviceID');
if ($device === '') {
$this->SendDebug('MQTT', 'Keine DeviceID gesetzt, ignoriere Receive', 0);
$this->SendDebug('MQTT', '⚠️ DeviceID leer → ignoriert', 0);
return;
}
/** -------------------------------------------
* 1⃣ FEEDBACK REQUEST
* ------------------------------------------*/
$this->SendDebug('MQTT', "DeviceID aktiv: $device", 0);
// =====================================================================
// 4) FEEDBACK REQUEST
// =====================================================================
if ($topic === "feedback-request/$device") {
$this->SendDebug('MQTT', "➡️ Feedback-Request erkannt!", 0);
$this->SetValue('FeedbackRequestPayload', $payload);
$json = json_decode($payload, true);
if (!is_array($json)) {
$this->SendDebug('MQTT', "⚠️ payload JSON ungültig → verwende leeres array", 0);
$json = [];
}
// Antwort erzeugen
$response = array_merge($json, [
"power_production" => $this->GetValue('PowerProduction'),
"is_ready" => $this->GetValue('IsReady'),
@@ -239,31 +254,43 @@ class VGT_Sub extends IPSModule
"max_soc" => $this->GetValue('MaxSOC')
]);
$this->Publish("feedback-response/$device", json_encode($response));
$jsonOut = json_encode($response);
$this->SendDebug('MQTT', "⬆️ Sende Feedback-Response: " . $jsonOut, 0);
$this->Publish("feedback-response/$device", $jsonOut);
return;
}
/** -------------------------------------------
* 2 REMOTE CONTROL REQUEST
* ------------------------------------------*/
// =====================================================================
// 5) REMOTE CONTROL REQUEST
// =====================================================================
if ($topic === "remote-control-request/$device") {
$this->SendDebug('MQTT', "➡️ Remote-Control-Request erkannt!", 0);
$this->SetValue('RemoteControlPayload', $payload);
$json = json_decode($payload, true);
if (is_array($json)) {
if (array_key_exists('power_setpoint', $json)) {
$this->SetValue('PowerSetpoint', (int)$json['power_setpoint']);
}
if (array_key_exists('strategy', $json)) {
$this->SetValue('Strategy', (string)$json['strategy']);
}
if (!is_array($json)) {
$this->SendDebug('MQTT', "⚠️ payload JSON ungültig → keine RC-Daten", 0);
return;
}
if (array_key_exists('power_setpoint', $json)) {
$this->SendDebug('MQTT', "Set power_setpoint: " . $json['power_setpoint'], 0);
$this->SetValue('PowerSetpoint', (int)$json['power_setpoint']);
}
if (array_key_exists('strategy', $json)) {
$this->SendDebug('MQTT', "Set strategy: " . $json['strategy'], 0);
$this->SetValue('Strategy', (string)$json['strategy']);
}
$this->SendDebug('MQTT', "⬆️ Sende Remote-Control-Response: " . $payload, 0);
$this->Publish("remote-control-response/$device", $payload);
return;
}
// =====================================================================
// 6) Unbekanntes Topic
// =====================================================================
$this->SendDebug('MQTT', "⚠️ Unbekanntes Topic empfangen → ignoriert", 0);
}
}