no message

This commit is contained in:
2025-11-21 14:55:51 +01:00
parent f1f6943c93
commit aba7d82360

View File

@@ -4,17 +4,17 @@ declare(strict_types=1);
class Int_VGT extends IPSModule class Int_VGT 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');
@@ -33,102 +33,149 @@ class Int_VGT extends IPSModule
$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 (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 (read-only)
* ------------------------------------------*/ * ------------------------------------------*/
$this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request Payload', '', 40); $this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request Payload', '', 40);
/** ------------------------------------------- /** -------------------------------------------
* MQTT SERVER verbinden * MQTT verbinden
* ------------------------------------------*/ * ------------------------------------------*/
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}'); $this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
} }
public function ApplyChanges() public function ApplyChanges()
{ {
parent::ApplyChanges(); parent::ApplyChanges();
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}'); $this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
// Beim Start niemals subscriben erst wenn Daten fließen oder Parent aktiv wird
$this->Subscribed = false;
// Parent-Status überwachen für Re-Subscribe
$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 PARENT STATUS
* ---------------------------------------------------------*/
public function MessageSink($TimeStamp, $SenderID, $Message, $Data)
{
if ($Message === IM_CHANGESTATUS) {
// Eigene Instanz aktiv
if ($SenderID === $this->InstanceID && ($Data[0] ?? 0) === IS_ACTIVE) {
$this->TrySubscribe();
return;
}
// Parent aktiv
$parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0;
if ($parent > 0 && $SenderID === $parent && ($Data[0] ?? 0) === IS_ACTIVE) {
$this->TrySubscribe();
return;
}
}
}
/* ---------------------------------------------------------
* SAFE SEND TO PARENT
* ---------------------------------------------------------*/
private function SafeSendToParent(array $packet)
{
$parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0;
if ($parent === 0 || !IPS_InstanceExists($parent)) {
return;
}
if (IPS_GetInstance($parent)['InstanceStatus'] !== IS_ACTIVE) {
return;
}
@ $this->SendDataToParent(json_encode($packet));
}
/* ---------------------------------------------------------
* Subscribe erst ausführen, wenn Parent sicher aktiv ist
* ---------------------------------------------------------*/
private function TrySubscribe()
{
if ($this->Subscribed) {
return;
}
$device = $this->ReadPropertyString('DeviceID'); $device = $this->ReadPropertyString('DeviceID');
if ($device === '') { if ($device === '') {
return; return;
} }
// READ $this->Subscribed = true;
$this->Subscribe("feedback-request/$device");
// WRITE $this->Subscribe("feedback-request/$device");
$this->Subscribe("remote-control-request/$device"); $this->Subscribe("remote-control-request/$device");
} }
/* ---------------------------------------------------------
/** ------------------------------------------- * REQUEST ACTION
* VARIABLE WRITE SUPPORT * ---------------------------------------------------------*/
* ------------------------------------------*/
public function RequestAction($Ident, $Value) public function RequestAction($Ident, $Value)
{ {
$this->SetValue($Ident, $Value); $this->SetValue($Ident, $Value);
} }
/* ---------------------------------------------------------
/** ------------------------------------------- * SUBSCRIBE (safe)
* MQTT SUBSCRIBE * ---------------------------------------------------------*/
* ------------------------------------------*/
private function Subscribe(string $topic): void private function Subscribe(string $topic): void
{ {
$packet = [ $this->SafeSendToParent([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}',
'PacketType' => 8, 'PacketType' => 8,
'QualityOfService' => 0, 'QualityOfService' => 0,
'Retain' => false, 'Retain' => false,
'Topic' => $topic, 'Topic' => $topic,
'Payload' => '' 'Payload' => ''
]; ]);
$this->SendDataToParent(json_encode([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'
] + $packet));
} }
/* ---------------------------------------------------------
/** ------------------------------------------- * PUBLISH (safe)
* MQTT PUBLISH * ---------------------------------------------------------*/
* ------------------------------------------*/
private function Publish(string $topic, string $payload): void private function Publish(string $topic, string $payload): void
{ {
$packet = [ $this->SafeSendToParent([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}',
'PacketType' => 3, '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)
{ {
// Beim ersten Paket: subscriben
$this->TrySubscribe();
$data = json_decode($JSONString, true); $data = json_decode($JSONString, true);
if (!is_array($data)) return; if (!is_array($data)) return;
@@ -137,24 +184,18 @@ class Int_VGT extends IPSModule
$device = $this->ReadPropertyString('DeviceID'); $device = $this->ReadPropertyString('DeviceID');
/* ---------------------------------------------------------
/** -------------------------------------------
* 1⃣ FEEDBACK REQUEST * 1⃣ FEEDBACK REQUEST
* request: feedback-request/deviceId * ---------------------------------------------------------*/
* response: feedback-response/deviceId
* ------------------------------------------*/
if ($topic === "feedback-request/$device") { if ($topic === "feedback-request/$device") {
// 1. Payload speichern
$this->SetValue('FeedbackRequestPayload', $payload); $this->SetValue('FeedbackRequestPayload', $payload);
// 2. JSON interpretieren (falls vorhanden)
$json = json_decode($payload, true); $json = json_decode($payload, true);
if (!is_array($json)) { if (!is_array($json)) {
$json = []; $json = [];
} }
// 3. Antwort-Mix erstellen (Payload + Status)
$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'),
@@ -164,20 +205,15 @@ class Int_VGT extends IPSModule
"max_soc" => $this->GetValue('MaxSOC') "max_soc" => $this->GetValue('MaxSOC')
]); ]);
// 4. Antwort senden
$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
* request: remote-control-request/deviceId * ---------------------------------------------------------*/
* response: remote-control-response/deviceId
* ------------------------------------------*/
if ($topic === "remote-control-request/$device") { if ($topic === "remote-control-request/$device") {
// 1. Payload speichern
$this->SetValue('RemoteControlPayload', $payload); $this->SetValue('RemoteControlPayload', $payload);
$json = json_decode($payload, true); $json = json_decode($payload, true);
@@ -192,9 +228,10 @@ class Int_VGT extends IPSModule
} }
} }
// 2. Echo Response
$this->Publish("remote-control-response/$device", $payload); $this->Publish("remote-control-response/$device", $payload);
return; return;
} }
} }
} }
?>