no message

This commit is contained in:
mh
2025-11-19 14:25:35 +01:00
parent ab2bd46426
commit 89e6b67e49
+22 -140
View File
@@ -1,111 +1,61 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
class VGT_Sub extends IPSModule class VGT_Sub extends IPSModule
{ {
public function Create() public function Create()
{ {
parent::Create(); parent::Create();
// Konfiguration
$this->RegisterPropertyString('DeviceID', ''); $this->RegisterPropertyString('DeviceID', '');
// Auf Parent-Connect reagieren (für Re-Subscribe)
$this->RegisterMessage($this->InstanceID, FM_CONNECT);
/** ------------------------------------------- /** -------------------------------------------
* STATUS Variablen (schreibbar) * STATUS Variablen (schreibbar)
* ------------------------------------------*/ * ------------------------------------------*/
$this->RegisterVariableInteger('PowerProduction', 'Power Production', '', 10); $this->RegisterVariableInteger('PowerProduction', 'Power Production', '', 10);
$this->EnableAction('PowerProduction'); $this->EnableAction('PowerProduction');
$this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 11); $this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 11);
$this->EnableAction('IsReady'); $this->EnableAction('IsReady');
$this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 12); $this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 12);
$this->EnableAction('IsRunning'); $this->EnableAction('IsRunning');
$this->RegisterVariableInteger('StateOfCharge', 'State of Charge', '', 13); $this->RegisterVariableInteger('StateOfCharge', 'State of Charge', '', 13);
$this->EnableAction('StateOfCharge'); $this->EnableAction('StateOfCharge');
$this->RegisterVariableInteger('MinSOC', 'Min SOC', '', 14); $this->RegisterVariableInteger('MinSOC', 'Min SOC', '', 14);
$this->EnableAction('MinSOC'); $this->EnableAction('MinSOC');
$this->RegisterVariableInteger('MaxSOC', 'Max SOC', '', 15); $this->RegisterVariableInteger('MaxSOC', 'Max SOC', '', 15);
$this->EnableAction('MaxSOC'); $this->EnableAction('MaxSOC');
/** ------------------------------------------- /** -------------------------------------------
* REMOTE CONTROL Variablen (read-only) * REMOTE CONTROL Variablen (read-only)
* ------------------------------------------*/ * ------------------------------------------*/
$this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 20); $this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 20);
$this->RegisterVariableString('Strategy', 'Strategy', '', 21); $this->RegisterVariableString('Strategy', 'Strategy', '', 21);
// Debug // Debug
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 30); $this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 30);
/** ------------------------------------------- /** -------------------------------------------
* FEEDBACK REQUEST Variablen (read-only) * FEEDBACK REQUEST Variablen (read-only)
* ------------------------------------------*/ * ------------------------------------------*/
$this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request Payload', '', 40); $this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request Payload', '', 40);
} }
public function ApplyChanges() public function ApplyChanges()
{ {
parent::ApplyChanges(); 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'); $device = $this->ReadPropertyString('DeviceID');
if ($device === '') { if ($device === '') {
$this->SendDebug('MQTT', 'Kein DeviceID gesetzt, keine Subscribes', 0);
return; return;
} }
// READ: Topics abonnieren
$this->Subscribe("feedback-request/$device"); $this->Subscribe("feedback-request/$device");
// WRITE: Remote-Control-Requests ebenfalls abonnieren
$this->Subscribe("remote-control-request/$device"); $this->Subscribe("remote-control-request/$device");
} }
/** ------------------------------------------- /** -------------------------------------------
* VARIABLE WRITE SUPPORT * VARIABLE WRITE SUPPORT
* ------------------------------------------*/ * ------------------------------------------*/
public function RequestAction($Ident, $Value) public function RequestAction($Ident, $Value)
{ {
$this->SetValue($Ident, $Value); $this->SetValue($Ident, $Value);
$device = $this->ReadPropertyString('DeviceID'); $device = $this->ReadPropertyString('DeviceID');
if ($device !== '') { if ($device !== '') {
switch ($Ident) { switch ($Ident) {
@@ -129,7 +79,6 @@ class VGT_Sub extends IPSModule
} }
} }
/** ------------------------------------------- /** -------------------------------------------
* MQTT SUBSCRIBE * MQTT SUBSCRIBE
* ------------------------------------------*/ * ------------------------------------------*/
@@ -139,7 +88,6 @@ class VGT_Sub extends IPSModule
$this->SendDebug('MQTT', 'Subscribe ohne aktiven Parent: ' . $topic, 0); $this->SendDebug('MQTT', 'Subscribe ohne aktiven Parent: ' . $topic, 0);
return; return;
} }
$packet = [ $packet = [
'PacketType' => 8, // SUBSCRIBE 'PacketType' => 8, // SUBSCRIBE
'QualityOfService' => 0, 'QualityOfService' => 0,
@@ -147,15 +95,11 @@ class VGT_Sub extends IPSModule
'Topic' => $topic, 'Topic' => $topic,
'Payload' => '' 'Payload' => ''
]; ];
$this->SendDebug('MQTT', 'Subscribe Topic=' . $topic, 0);
$this->SendDataToParent(json_encode([ $this->SendDataToParent(json_encode([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
] + $packet)); ] + $packet));
} }
/** ------------------------------------------- /** -------------------------------------------
* MQTT PUBLISH * MQTT PUBLISH
* ------------------------------------------*/ * ------------------------------------------*/
@@ -165,7 +109,6 @@ class VGT_Sub extends IPSModule
$this->SendDebug('MQTT', 'Publish ohne aktiven Parent: ' . $topic, 0); $this->SendDebug('MQTT', 'Publish ohne aktiven Parent: ' . $topic, 0);
return; return;
} }
$packet = [ $packet = [
'PacketType' => 3, // PUBLISH 'PacketType' => 3, // PUBLISH
'QualityOfService' => 0, 'QualityOfService' => 0,
@@ -173,78 +116,36 @@ class VGT_Sub extends IPSModule
'Topic' => $topic, 'Topic' => $topic,
'Payload' => $payload 'Payload' => $payload
]; ];
$this->SendDebug('MQTT', 'Publish Topic=' . $topic . ' Payload=' . $payload, 0);
$this->SendDataToParent(json_encode([ $this->SendDataToParent(json_encode([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
] + $packet)); ] + $packet));
} }
/** ------------------------------------------- /** -------------------------------------------
* RECEIVE DATA * RECEIVE DATA
* ------------------------------------------*/ * ------------------------------------------*/
public function ReceiveData($JSONString) public function ReceiveData($JSONString)
{ {
$this->SendDebug('MQTT', "RAW Incoming JSON: " . $JSONString, 0); $data = json_decode($JSONString, true);
if (!is_array($data)) {
// ---------------------------------------------
// 1) Äußeres JSON vom MQTT-Client
// ---------------------------------------------
$data = json_decode($JSONString);
if ($data === null) {
$this->SendDebug('MQTT', '❌ JSON ungültig (outer decode)', 0);
return; return;
} }
$topic = $data['Topic'] ?? '';
// --------------------------------------------- $payload = $data['Payload'] ?? '';
// 2) Buffer extrahieren $this->SendDebug('MQTT', 'Receive: Topic=' . $topic . ' Payload=' . $payload, 0);
// ---------------------------------------------
if (isset($data->Buffer)) {
$this->SendDebug('MQTT', "Buffer gefunden (json-string): " . $data->Buffer, 0);
$buffer = json_decode($data->Buffer, true);
} else {
$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);
return;
}
// ---------------------------------------------
// 3) Topic / Payload lesen
// ---------------------------------------------
$topic = $buffer['Topic'] ?? '';
$payload = $buffer['Payload'] ?? '';
$this->SendDebug('MQTT', "📩 Receive: Topic='$topic' Payload='$payload'", 0);
$device = $this->ReadPropertyString('DeviceID'); $device = $this->ReadPropertyString('DeviceID');
if ($device === '') { if ($device === '') {
$this->SendDebug('MQTT', '⚠️ DeviceID leer → ignoriert', 0);
return; return;
} }
/** -------------------------------------------
$this->SendDebug('MQTT', "DeviceID aktiv: $device", 0); * 1️⃣ FEEDBACK REQUEST
* ------------------------------------------*/
// =====================================================================
// 4) FEEDBACK REQUEST
// =====================================================================
if ($topic === "feedback-request/$device") { if ($topic === "feedback-request/$device") {
$this->SendDebug('MQTT', "➡️ Feedback-Request erkannt!", 0);
$this->SetValue('FeedbackRequestPayload', $payload); $this->SetValue('FeedbackRequestPayload', $payload);
$json = json_decode($payload, true); $json = json_decode($payload, true);
if (!is_array($json)) { if (!is_array($json)) {
$this->SendDebug('MQTT', "⚠️ payload JSON ungültig → verwende leeres array", 0);
$json = []; $json = [];
} }
// Antwort erzeugen
$response = array_merge($json, [ $response = array_merge($json, [
"power_production" => $this->GetValue('PowerProduction'), "power_production" => $this->GetValue('PowerProduction'),
"is_ready" => $this->GetValue('IsReady'), "is_ready" => $this->GetValue('IsReady'),
@@ -253,44 +154,25 @@ class VGT_Sub extends IPSModule
"min_soc" => $this->GetValue('MinSOC'), "min_soc" => $this->GetValue('MinSOC'),
"max_soc" => $this->GetValue('MaxSOC') "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; return;
} }
/** -------------------------------------------
// ===================================================================== * 2️⃣ REMOTE CONTROL REQUEST
// 5) REMOTE CONTROL REQUEST * ------------------------------------------*/
// =====================================================================
if ($topic === "remote-control-request/$device") { if ($topic === "remote-control-request/$device") {
$this->SendDebug('MQTT', "➡️ Remote-Control-Request erkannt!", 0);
$this->SetValue('RemoteControlPayload', $payload); $this->SetValue('RemoteControlPayload', $payload);
$json = json_decode($payload, true); $json = json_decode($payload, true);
if (!is_array($json)) { if (is_array($json)) {
$this->SendDebug('MQTT', "⚠️ payload JSON ungültig → keine RC-Daten", 0); if (array_key_exists('power_setpoint', $json)) {
return; $this->SetValue('PowerSetpoint', (int)$json['power_setpoint']);
}
if (array_key_exists('strategy', $json)) {
$this->SetValue('Strategy', (string)$json['strategy']);
}
} }
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); $this->Publish("remote-control-response/$device", $payload);
return; return;
} }
// =====================================================================
// 6) Unbekanntes Topic
// =====================================================================
$this->SendDebug('MQTT', "⚠️ Unbekanntes Topic empfangen → ignoriert", 0);
} }
} }