diff --git a/Int_VGT/module.php b/Int_VGT/module.php index ef9c0b2..e0f4e9e 100644 --- a/Int_VGT/module.php +++ b/Int_VGT/module.php @@ -41,10 +41,16 @@ class Int_VGT extends IPSModule $this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 20); $this->RegisterVariableString('Strategy', 'Strategy', '', 21); - // Für Debugging + // Debug $this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 30); + /** ------------------------------------------- + * FEEDBACK REQUEST Variablen (read-only) + * ------------------------------------------*/ + $this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request Payload', '', 40); + + /** ------------------------------------------- * MQTT SERVER verbinden * ------------------------------------------*/ @@ -56,18 +62,18 @@ class Int_VGT extends IPSModule { parent::ApplyChanges(); - // Sicherheitshalber erneut verbinden $this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}'); $device = $this->ReadPropertyString('DeviceID'); if ($device === '') { - $this->SendDebug('ApplyChanges', 'Keine DeviceID gesetzt', 0); return; } - // Subscriptions setzen + // READ $this->Subscribe("feedback-request/$device"); - $this->Subscribe("remote-control-response/$device"); + + // WRITE + $this->Subscribe("remote-control-request/$device"); } @@ -85,10 +91,8 @@ class Int_VGT extends IPSModule * ------------------------------------------*/ private function Subscribe(string $topic): void { - $this->SendDebug('Subscribe', $topic, 0); - $packet = [ - 'PacketType' => 8, // SUBSCRIBE + 'PacketType' => 8, 'QualityOfService' => 0, 'Retain' => false, 'Topic' => $topic, @@ -106,10 +110,8 @@ class Int_VGT extends IPSModule * ------------------------------------------*/ private function Publish(string $topic, string $payload): void { - $this->SendDebug('Publish', "Topic: $topic Payload: $payload", 0); - $packet = [ - 'PacketType' => 3, // PUBLISH + 'PacketType' => 3, 'QualityOfService' => 0, 'Retain' => false, 'Topic' => $topic, @@ -128,42 +130,54 @@ class Int_VGT extends IPSModule public function ReceiveData($JSONString) { $data = json_decode($JSONString, true); - if (!is_array($data)) { - return; - } + if (!is_array($data)) return; $topic = $data['Topic'] ?? ''; $payload = $data['Payload'] ?? ''; $device = $this->ReadPropertyString('DeviceID'); - $this->SendDebug('Receive', "Topic: $topic Payload: $payload", 0); - /** ------------------------------------------- - * 1️⃣ FEEDBACK REQUEST → Statusvariablen senden + * 1️⃣ FEEDBACK REQUEST + * request: feedback-request/deviceId + * response: feedback-response/deviceId * ------------------------------------------*/ if ($topic === "feedback-request/$device") { - $response = [ + // 1. Payload speichern + $this->SetValue('FeedbackRequestPayload', $payload); + + // 2. JSON interpretieren (falls vorhanden) + $json = json_decode($payload, true); + if (!is_array($json)) { + $json = []; + } + + // 3. Antwort-Mix erstellen (Payload + Status) + $response = array_merge($json, [ "power_production" => $this->GetValue('PowerProduction'), "is_ready" => $this->GetValue('IsReady'), "is_running" => $this->GetValue('IsRunning'), "state_of_charge" => $this->GetValue('StateOfCharge'), "min_soc" => $this->GetValue('MinSOC'), "max_soc" => $this->GetValue('MaxSOC') - ]; + ]); + // 4. Antwort senden $this->Publish("feedback-response/$device", json_encode($response)); return; } /** ------------------------------------------- - * 2️⃣ REMOTE CONTROL → write-only Variablen + * 2️⃣ REMOTE CONTROL REQUEST + * request: remote-control-request/deviceId + * response: remote-control-response/deviceId * ------------------------------------------*/ - if ($topic === "remote-control-response/$device") { + if ($topic === "remote-control-request/$device") { + // 1. Payload speichern $this->SetValue('RemoteControlPayload', $payload); $json = json_decode($payload, true); @@ -178,16 +192,9 @@ class Int_VGT extends IPSModule } } + // 2. Echo Response + $this->Publish("remote-control-response/$device", $payload); return; } } - - - /** ------------------------------------------- - * OPTIONAL CALLBACK - * ------------------------------------------*/ - protected function HandleRemoteControl(string $payload): void - { - $this->SendDebug('RemoteControl', $payload, 0); - } }