diff --git a/VGT_Sub/module.php b/VGT_Sub/module.php index 08c2161..afc2ab1 100644 --- a/VGT_Sub/module.php +++ b/VGT_Sub/module.php @@ -12,9 +12,7 @@ class VGT_Sub extends IPSModule $this->RegisterPropertyString('DeviceID', ''); - /** ------------------------------------------- - * STATUS Variablen (schreibbar) - * ------------------------------------------*/ + // Status Variablen (schreibbar) $this->RegisterVariableInteger('PowerProduction', 'Power Production', '', 10); $this->EnableAction('PowerProduction'); @@ -33,18 +31,12 @@ class VGT_Sub extends IPSModule $this->RegisterVariableInteger('MaxSOC', 'Max SOC', '', 15); $this->EnableAction('MaxSOC'); - /** ------------------------------------------- - * REMOTE CONTROL Variablen (read-only) - * ------------------------------------------*/ + // Remote-control (read-only) $this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 20); $this->RegisterVariableString('Strategy', 'Strategy', '', 21); - - // Debug $this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 30); - /** ------------------------------------------- - * FEEDBACK REQUEST Variablen (read-only) - * ------------------------------------------*/ + // Feedback-Request (read-only) $this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request Payload', '', 40); } @@ -52,13 +44,13 @@ class VGT_Sub extends IPSModule { parent::ApplyChanges(); - // Parent registrieren + // Parent verbinden $this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}'); - // Subscribe erst ausführen, wenn Parent wirklich aktiv ist + // Subscribes erst, wenn Parent aktiv wird $this->Subscribed = false; - // Statusänderungen anhören (Instanz + Parent) + // Statusänderungen überwachen $this->RegisterMessage($this->InstanceID, IM_CHANGESTATUS); $parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0; @@ -76,19 +68,17 @@ class VGT_Sub extends IPSModule return; } - $newStatus = $Data[0] ?? 0; + $new = $Data[0] ?? 0; // Eigene Instanz aktiv - if ($SenderID === $this->InstanceID && $newStatus === IS_ACTIVE) { + if ($SenderID === $this->InstanceID && $new === IS_ACTIVE) { $this->TrySubscribe(); - return; } // Parent aktiv $parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0; - if ($parent > 0 && $SenderID === $parent && $newStatus === IS_ACTIVE) { + if ($SenderID === $parent && $new === IS_ACTIVE) { $this->TrySubscribe(); - return; } } @@ -98,6 +88,7 @@ class VGT_Sub extends IPSModule private function HasActiveParent(): bool { $parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0; + if ($parent === 0 || !IPS_InstanceExists($parent)) { return false; } @@ -105,20 +96,20 @@ class VGT_Sub extends IPSModule } /* --------------------------------------------------------- - * Safe Sender + * Safe Sender – garantiert keine Fehler mehr * ---------------------------------------------------------*/ private function SafeSendToParent(array $packet) { if (!$this->HasActiveParent()) { - $this->SendDebug('MQTT', 'Parent not active → send skipped', 0); + $this->SendDebug("MQTT", "Parent not active – send skipped", 0); return; } - @$this->SendDataToParent(json_encode($packet)); + @ $this->SendDataToParent(json_encode($packet)); } /* --------------------------------------------------------- - * Safe Subscribe + * Subscribe (safe) * ---------------------------------------------------------*/ private function TrySubscribe() { @@ -137,38 +128,31 @@ class VGT_Sub extends IPSModule $this->Subscribe("remote-control-request/$device"); } - /* --------------------------------------------------------- - * Subscribe - * ---------------------------------------------------------*/ - private function Subscribe(string $topic): void + private function Subscribe(string $topic) { - $packet = [ + $this->SafeSendToParent([ 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}', 'PacketType' => 8, 'QualityOfService' => 0, 'Retain' => false, 'Topic' => $topic, 'Payload' => '' - ]; - - $this->SafeSendToParent($packet); + ]); } /* --------------------------------------------------------- - * Publish + * Publish (safe) * ---------------------------------------------------------*/ - private function Publish(string $topic, string $payload): void + private function Publish(string $topic, string $payload) { - $packet = [ + $this->SafeSendToParent([ 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}', 'PacketType' => 3, 'QualityOfService' => 0, 'Retain' => false, 'Topic' => $topic, 'Payload' => $payload - ]; - - $this->SafeSendToParent($packet); + ]); } /* --------------------------------------------------------- @@ -210,7 +194,6 @@ class VGT_Sub extends IPSModule * ---------------------------------------------------------*/ public function ReceiveData($JSONString) { - // Beim ersten Paket: subscriben $this->TrySubscribe(); $data = json_decode($JSONString, true); @@ -221,24 +204,20 @@ class VGT_Sub extends IPSModule $topic = $data['Topic'] ?? ''; $payload = $data['Payload'] ?? ''; - $this->SendDebug('MQTT', 'Receive: ' . $topic . ' → ' . $payload, 0); + $this->SendDebug('MQTT', "Receive: $topic => $payload", 0); $device = $this->ReadPropertyString('DeviceID'); if ($device === '') { return; } - /** ------------------------------------------- - * 1️⃣ FEEDBACK REQUEST - * ------------------------------------------*/ + /* --- Feedback Request --- */ if ($topic === "feedback-request/$device") { $this->SetValue('FeedbackRequestPayload', $payload); $json = json_decode($payload, true); - if (!is_array($json)) { - $json = []; - } + if (!is_array($json)) $json = []; $response = array_merge($json, [ "power_production" => $this->GetValue('PowerProduction'), @@ -253,9 +232,7 @@ class VGT_Sub extends IPSModule return; } - /** ------------------------------------------- - * 2️⃣ REMOTE CONTROL REQUEST - * ------------------------------------------*/ + /* --- Remote Control Request --- */ if ($topic === "remote-control-request/$device") { $this->SetValue('RemoteControlPayload', $payload); @@ -265,7 +242,6 @@ class VGT_Sub extends IPSModule if (isset($json['power_setpoint'])) { $this->SetValue('PowerSetpoint', (int)$json['power_setpoint']); } - if (isset($json['strategy'])) { $this->SetValue('Strategy', (string)$json['strategy']); }