no message

This commit is contained in:
dh
2025-11-21 15:04:11 +01:00
parent aba7d82360
commit 13946b4538
+157 -55
View File
@@ -1,25 +1,35 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
class VGT_Sub extends IPSModule class VGT_Sub extends IPSModule
{ {
private bool $Subscribed = false;
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');
@@ -28,6 +38,7 @@ class VGT_Sub extends IPSModule
* ------------------------------------------*/ * ------------------------------------------*/
$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);
@@ -40,24 +51,138 @@ class VGT_Sub extends IPSModule
public function ApplyChanges() public function ApplyChanges()
{ {
parent::ApplyChanges(); parent::ApplyChanges();
// Parent registrieren
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
// Subscribe erst ausführen, wenn Parent wirklich aktiv ist
$this->Subscribed = false;
// Statusänderungen anhören (Instanz + Parent)
$this->RegisterMessage($this->InstanceID, IM_CHANGESTATUS);
$parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0;
if ($parent > 0 && IPS_InstanceExists($parent)) {
$this->RegisterMessage($parent, IM_CHANGESTATUS);
}
}
/* ---------------------------------------------------------
* MESSAGE SINK – reagiert auf Parent-Aktivierung
* ---------------------------------------------------------*/
public function MessageSink($TimeStamp, $SenderID, $Message, $Data)
{
if ($Message !== IM_CHANGESTATUS) {
return;
}
$newStatus = $Data[0] ?? 0;
// Eigene Instanz aktiv
if ($SenderID === $this->InstanceID && $newStatus === IS_ACTIVE) {
$this->TrySubscribe();
return;
}
// Parent aktiv
$parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0;
if ($parent > 0 && $SenderID === $parent && $newStatus === IS_ACTIVE) {
$this->TrySubscribe();
return;
}
}
/* ---------------------------------------------------------
* Prüfen ob Parent aktiv ist
* ---------------------------------------------------------*/
private function HasActiveParent(): bool
{
$parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0;
if ($parent === 0 || !IPS_InstanceExists($parent)) {
return false;
}
return IPS_GetInstance($parent)['InstanceStatus'] === IS_ACTIVE;
}
/* ---------------------------------------------------------
* Safe Sender
* ---------------------------------------------------------*/
private function SafeSendToParent(array $packet)
{
if (!$this->HasActiveParent()) {
$this->SendDebug('MQTT', 'Parent not active → send skipped', 0);
return;
}
@$this->SendDataToParent(json_encode($packet));
}
/* ---------------------------------------------------------
* Safe Subscribe
* ---------------------------------------------------------*/
private function TrySubscribe()
{
if ($this->Subscribed) {
return;
}
$device = $this->ReadPropertyString('DeviceID'); $device = $this->ReadPropertyString('DeviceID');
if ($device === '') { if ($device === '') {
return; return;
} }
// READ: Topics abonnieren
$this->Subscribed = true;
$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 * Subscribe
* ------------------------------------------*/ * ---------------------------------------------------------*/
private function Subscribe(string $topic): void
{
$packet = [
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}',
'PacketType' => 8,
'QualityOfService' => 0,
'Retain' => false,
'Topic' => $topic,
'Payload' => ''
];
$this->SafeSendToParent($packet);
}
/* ---------------------------------------------------------
* Publish
* ---------------------------------------------------------*/
private function Publish(string $topic, string $payload): void
{
$packet = [
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}',
'PacketType' => 3,
'QualityOfService' => 0,
'Retain' => false,
'Topic' => $topic,
'Payload' => $payload
];
$this->SafeSendToParent($packet);
}
/* ---------------------------------------------------------
* RequestAction
* ---------------------------------------------------------*/
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 === '') {
return;
}
switch ($Ident) { switch ($Ident) {
case 'PowerProduction': case 'PowerProduction':
case 'IsReady': case 'IsReady':
@@ -65,6 +190,7 @@ class VGT_Sub extends IPSModule
case 'StateOfCharge': case 'StateOfCharge':
case 'MinSOC': case 'MinSOC':
case 'MaxSOC': case 'MaxSOC':
$payload = json_encode([ $payload = json_encode([
'power_production' => $this->GetValue('PowerProduction'), 'power_production' => $this->GetValue('PowerProduction'),
'is_ready' => $this->GetValue('IsReady'), 'is_ready' => $this->GetValue('IsReady'),
@@ -73,79 +199,47 @@ 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("status/$device", $payload); $this->Publish("status/$device", $payload);
break; break;
} }
} }
}
/** ------------------------------------------- /* ---------------------------------------------------------
* MQTT SUBSCRIBE * ReceiveData
* ------------------------------------------*/ * ---------------------------------------------------------*/
private function Subscribe(string $topic): void
{
if (!$this->HasActiveParent()) {
$this->SendDebug('MQTT', 'Subscribe ohne aktiven Parent: ' . $topic, 0);
return;
}
$packet = [
'PacketType' => 8, // SUBSCRIBE
'QualityOfService' => 0,
'Retain' => false,
'Topic' => $topic,
'Payload' => ''
];
$this->SendDataToParent(json_encode([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
] + $packet));
}
/** -------------------------------------------
* MQTT PUBLISH
* ------------------------------------------*/
private function Publish(string $topic, string $payload): void
{
if (!$this->HasActiveParent()) {
$this->SendDebug('MQTT', 'Publish ohne aktiven Parent: ' . $topic, 0);
return;
}
$packet = [
'PacketType' => 3, // PUBLISH
'QualityOfService' => 0,
'Retain' => false,
'Topic' => $topic,
'Payload' => $payload
];
$this->SendDataToParent(json_encode([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
] + $packet));
}
/** -------------------------------------------
* RECEIVE DATA
* ------------------------------------------*/
public function ReceiveData($JSONString) public function ReceiveData($JSONString)
{ {
// Beim ersten Paket: subscriben
$this->TrySubscribe();
$data = json_decode($JSONString, true); $data = json_decode($JSONString, true);
if (!is_array($data)) { if (!is_array($data)) {
return; return;
} }
$topic = $data['Topic'] ?? ''; $topic = $data['Topic'] ?? '';
$payload = $data['Payload'] ?? ''; $payload = $data['Payload'] ?? '';
$this->SendDebug('MQTT', 'Receive: Topic=' . $topic . ' Payload=' . $payload, 0);
$this->SendDebug('MQTT', 'Receive: ' . $topic . ' → ' . $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 +248,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 (isset($json['power_setpoint'])) {
$this->SetValue('PowerSetpoint', (int)$json['power_setpoint']); $this->SetValue('PowerSetpoint', (int)$json['power_setpoint']);
} }
if (array_key_exists('strategy', $json)) {
if (isset($json['strategy'])) {
$this->SetValue('Strategy', (string)$json['strategy']); $this->SetValue('Strategy', (string)$json['strategy']);
} }
} }
$this->Publish("remote-control-response/$device", $payload); $this->Publish("remote-control-response/$device", $payload);
return; return;
} }
} }
} }
?>