no message

This commit is contained in:
dh
2025-11-21 15:14:39 +01:00
parent 7e5be3ddfc
commit 21f1466bfc
+97 -61
View File
@@ -1,38 +1,45 @@
<?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();
$this->RegisterPropertyString('DeviceID', ''); $this->RegisterPropertyString('DeviceID', '');
/** ------------------------------------------- /** -------------------------------------------
* 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 (readonly)
* ------------------------------------------*/ * ------------------------------------------*/
$this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 20); $this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 20);
$this->RegisterVariableString('Strategy', 'Strategy', '', 21); $this->RegisterVariableString('Strategy', 'Strategy', '', 21);
// Debug
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 30); $this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 30);
/** ------------------------------------------- /** -------------------------------------------
* FEEDBACK REQUEST Variablen (read-only) * FEEDBACK REQUEST (readonly)
* ------------------------------------------*/ * ------------------------------------------*/
$this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request Payload', '', 40); $this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request Payload', '', 40);
} }
@@ -40,112 +47,133 @@ class VGT_Sub extends IPSModule
public function ApplyChanges() public function ApplyChanges()
{ {
parent::ApplyChanges(); parent::ApplyChanges();
$device = $this->ReadPropertyString('DeviceID'); $this->ConnectParent('{DBDA3967-E416-4354-A5C9-2FB9E7E2F5B3}'); // MQTT Client Gateway
if ($device === '') {
return;
}
// READ: Topics abonnieren
$this->Subscribe("feedback-request/$device");
// WRITE: Remote-Control-Requests ebenfalls abonnieren
$this->Subscribe("remote-control-request/$device");
} }
/** ------------------------------------------- /** ----------------------------------------------------------
* VARIABLE WRITE SUPPORT * REQUEST ACTION → Statusvariablen schreibbar machen
* ------------------------------------------*/ * ----------------------------------------------------------*/
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 !== '') {
switch ($Ident) { if ($device === '') {
case 'PowerProduction': return;
case 'IsReady': }
case 'IsRunning':
case 'StateOfCharge': switch ($Ident) {
case 'MinSOC': case 'PowerProduction':
case 'MaxSOC': case 'IsReady':
$payload = json_encode([ case 'IsRunning':
'power_production' => $this->GetValue('PowerProduction'), case 'StateOfCharge':
'is_ready' => $this->GetValue('IsReady'), case 'MinSOC':
'is_running' => $this->GetValue('IsRunning'), case 'MaxSOC':
'state_of_charge' => $this->GetValue('StateOfCharge'),
'min_soc' => $this->GetValue('MinSOC'), $payload = json_encode([
'max_soc' => $this->GetValue('MaxSOC') 'power_production' => $this->GetValue('PowerProduction'),
]); 'is_ready' => $this->GetValue('IsReady'),
$this->Publish("status/$device", $payload); 'is_running' => $this->GetValue('IsRunning'),
break; 'state_of_charge' => $this->GetValue('StateOfCharge'),
} 'min_soc' => $this->GetValue('MinSOC'),
'max_soc' => $this->GetValue('MaxSOC')
]);
$this->Publish("status/$device", $payload);
break;
} }
} }
/** ------------------------------------------- /** ----------------------------------------------------------
* MQTT SUBSCRIBE * SUBSCRIBE (nur bei „MQTT_CONNECTED“)
* ------------------------------------------*/ * ----------------------------------------------------------*/
private function Subscribe(string $topic): void private function Subscribe(string $topic): void
{ {
if (!$this->HasActiveParent()) { if (!$this->HasActiveParent()) {
$this->SendDebug('MQTT', 'Subscribe ohne aktiven Parent: ' . $topic, 0); $this->SendDebug('MQTT', 'Subscribe ohne aktiven Parent: ' . $topic, 0);
return; return;
} }
$packet = [
'PacketType' => 8, // SUBSCRIBE $this->SendDebug('MQTT', 'Subscribe → ' . $topic, 0);
'QualityOfService' => 0,
'Retain' => false,
'Topic' => $topic,
'Payload' => ''
];
$this->SendDataToParent(json_encode([ $this->SendDataToParent(json_encode([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' 'DataID' => '{7F2A28E2-ABFA-4C77-B43F-042E1ED9F2D2}',
] + $packet)); 'PacketType' => 8, // SUBSCRIBE
'QualityOfService' => 0,
'Retain' => false,
'Topic' => $topic,
'Payload' => ''
]));
} }
/** ------------------------------------------- /** ----------------------------------------------------------
* MQTT PUBLISH * PUBLISH
* ------------------------------------------*/ * ----------------------------------------------------------*/
private function Publish(string $topic, string $payload): void private function Publish(string $topic, string $payload): void
{ {
if (!$this->HasActiveParent()) { if (!$this->HasActiveParent()) {
$this->SendDebug('MQTT', 'Publish ohne aktiven Parent: ' . $topic, 0); $this->SendDebug('MQTT', 'Publish ohne aktiven Parent: ' . $topic, 0);
return; return;
} }
$packet = [
'PacketType' => 3, // PUBLISH $this->SendDebug('MQTT', 'Publish → Topic=' . $topic . ' Payload=' . $payload, 0);
$this->SendDataToParent(json_encode([
'DataID' => '{7F2A28E2-ABFA-4C77-B43F-042E1ED9F2D2}',
'PacketType' => 3,
'QualityOfService' => 0, 'QualityOfService' => 0,
'Retain' => false, 'Retain' => false,
'Topic' => $topic, 'Topic' => $topic,
'Payload' => $payload 'Payload' => $payload
]; ]));
$this->SendDataToParent(json_encode([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
] + $packet));
} }
/** ------------------------------------------- /** ----------------------------------------------------------
* RECEIVE DATA * RECEIVE DATA
* ------------------------------------------*/ * ----------------------------------------------------------*/
public function ReceiveData($JSONString) public function ReceiveData($JSONString)
{ {
$data = json_decode($JSONString, true); $data = json_decode($JSONString, true);
if (!is_array($data)) { if (!is_array($data)) {
return; return;
} }
$topic = $data['Topic'] ?? '';
// --- MQTT CONNECTED EVENT ---
if (isset($data['Event']) && $data['Event'] === 'MQTT_CONNECTED') {
$this->SendDebug('MQTT', 'MQTT_CONNECTED → re-subscribing…', 0);
$device = $this->ReadPropertyString('DeviceID');
if ($device !== '') {
$this->Subscribe("feedback-request/$device");
$this->Subscribe("remote-control-request/$device");
}
return;
}
// --- Normale MQTT Messages ---
$topic = $data['Topic'] ?? '';
$payload = $data['Payload'] ?? ''; $payload = $data['Payload'] ?? '';
$this->SendDebug('MQTT', 'Receive: Topic=' . $topic . ' Payload=' . $payload, 0);
$this->SendDebug('MQTT', "Receive: Topic=$topic Payload=$payload", 0);
$device = $this->ReadPropertyString('DeviceID'); $device = $this->ReadPropertyString('DeviceID');
if ($device === '') { if ($device === '') {
return; return;
} }
/** ------------------------------------------- /** -------------------------------------------
* 1️⃣ FEEDBACK REQUEST * 1️⃣ FEEDBACK REQUEST
* ------------------------------------------*/ * ------------------------------------------*/
if ($topic === "feedback-request/$device") { if ($topic === "feedback-request/$device") {
$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)) {
$json = []; $json = [];
} }
$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'),
@@ -154,25 +182,33 @@ 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)); $this->Publish("feedback-response/$device", json_encode($response));
return; return;
} }
/** ------------------------------------------- /** -------------------------------------------
* 2️⃣ REMOTE CONTROL REQUEST * 2️⃣ REMOTE CONTROL REQUEST
* ------------------------------------------*/ * ------------------------------------------*/
if ($topic === "remote-control-request/$device") { if ($topic === "remote-control-request/$device") {
$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)) {
if (array_key_exists('power_setpoint', $json)) { if (array_key_exists('power_setpoint', $json)) {
$this->SetValue('PowerSetpoint', (int)$json['power_setpoint']); $this->SetValue('PowerSetpoint', (int) $json['power_setpoint']);
} }
if (array_key_exists('strategy', $json)) { if (array_key_exists('strategy', $json)) {
$this->SetValue('Strategy', (string)$json['strategy']); $this->SetValue('Strategy', (string) $json['strategy']);
} }
} }
// Antwort spiegeln
$this->Publish("remote-control-response/$device", $payload); $this->Publish("remote-control-response/$device", $payload);
return; return;
} }
} }
} }