From c60f28945f1b61e43aa565234e1a9a038661efd1 Mon Sep 17 00:00:00 2001 From: "belevo\\mh" Date: Wed, 19 Nov 2025 07:26:17 +0100 Subject: [PATCH] no message --- VGT_Sub/README.md | 0 VGT_Sub/form.json | 9 ++ VGT_Sub/module.json | 17 ++++ VGT_Sub/module.php | 227 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 253 insertions(+) create mode 100644 VGT_Sub/README.md create mode 100644 VGT_Sub/form.json create mode 100644 VGT_Sub/module.json create mode 100644 VGT_Sub/module.php diff --git a/VGT_Sub/README.md b/VGT_Sub/README.md new file mode 100644 index 0000000..e69de29 diff --git a/VGT_Sub/form.json b/VGT_Sub/form.json new file mode 100644 index 0000000..9f5911e --- /dev/null +++ b/VGT_Sub/form.json @@ -0,0 +1,9 @@ +{ + "elements": [ + { + "type": "ValidationTextBox", + "name": "DeviceID", + "caption": "Device ID" + } + ] +} diff --git a/VGT_Sub/module.json b/VGT_Sub/module.json new file mode 100644 index 0000000..8b39024 --- /dev/null +++ b/VGT_Sub/module.json @@ -0,0 +1,17 @@ +{ + "id": "{0F001E51-E914-6444-B635-D46DDE0C3810}", + "name": "VGT_Sub", + "type": 3, + "vendor": "Belevo", + "aliases": [ + "VGT MQTT-Client" + ], + "prefix": "VGT", + "parentRequirements": [ + "{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}" + ], + "childRequirements": [], + "implemented": [ + "{7F7632D9-FA40-4F38-8DEA-C83CD4325A32}" + ] +} diff --git a/VGT_Sub/module.php b/VGT_Sub/module.php new file mode 100644 index 0000000..ac40a48 --- /dev/null +++ b/VGT_Sub/module.php @@ -0,0 +1,227 @@ +RegisterPropertyString('DeviceID', ''); + + /** ------------------------------------------- + * STATUS Variablen (schreibbar) + * ------------------------------------------*/ + + $this->RegisterVariableInteger('PowerProduction', 'Power Production', '', 10); + $this->EnableAction('PowerProduction'); + + $this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 11); + $this->EnableAction('IsReady'); + + $this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 12); + $this->EnableAction('IsRunning'); + + $this->RegisterVariableInteger('StateOfCharge', 'State of Charge', '', 13); + $this->EnableAction('StateOfCharge'); + + $this->RegisterVariableInteger('MinSOC', 'Min SOC', '', 14); + $this->EnableAction('MinSOC'); + + $this->RegisterVariableInteger('MaxSOC', 'Max SOC', '', 15); + $this->EnableAction('MaxSOC'); + + + /** ------------------------------------------- + * REMOTE CONTROL Variablen (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) + * ------------------------------------------*/ + $this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request Payload', '', 40); + + // WICHTIG: Kein ConnectParent mehr hier -> Modul ist nur Client/Device + } + + + public function ApplyChanges() + { + parent::ApplyChanges(); + + // Kein ConnectParent mehr -> Parent wird in der Konsole verknüpft + // (MQTT Server ODER MQTT Client Gateway) + + $device = $this->ReadPropertyString('DeviceID'); + 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 + * ------------------------------------------*/ + public function RequestAction($Ident, $Value) + { + $this->SetValue($Ident, $Value); + + // Optional: Hier könntest du auch direkt publizieren, + // wenn eine Variable geändert wird. + // Beispiel (auskommentiert, nur als Vorlage): + + $device = $this->ReadPropertyString('DeviceID'); + if ($device !== '') { + switch ($Ident) { + case 'PowerProduction': + case 'IsReady': + case 'IsRunning': + case 'StateOfCharge': + case 'MinSOC': + case 'MaxSOC': + $payload = json_encode([ + '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') + ]); + $this->Publish("status/$device", $payload); + break; + } + } + + } + + + /** ------------------------------------------- + * MQTT SUBSCRIBE + * ------------------------------------------*/ + private function Subscribe(string $topic): void + { + $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 + { + $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) + { + $data = json_decode($JSONString, true); + if (!is_array($data)) { + return; + } + + $topic = $data['Topic'] ?? ''; + $payload = $data['Payload'] ?? ''; + + $device = $this->ReadPropertyString('DeviceID'); + if ($device === '') { + return; + } + + /** ------------------------------------------- + * 1️⃣ FEEDBACK REQUEST + * request: feedback-request/deviceId + * response: feedback-response/deviceId + * ------------------------------------------*/ + if ($topic === "feedback-request/$device") { + + // 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 REQUEST + * request: remote-control-request/deviceId + * response: remote-control-response/deviceId + * ------------------------------------------*/ + if ($topic === "remote-control-request/$device") { + + // 1. Payload speichern + $this->SetValue('RemoteControlPayload', $payload); + + $json = json_decode($payload, true); + if (is_array($json)) { + + if (array_key_exists('power_setpoint', $json)) { + $this->SetValue('PowerSetpoint', (int)$json['power_setpoint']); + } + + if (array_key_exists('strategy', $json)) { + $this->SetValue('Strategy', (string)$json['strategy']); + } + } + + // 2. Echo Response + $this->Publish("remote-control-response/$device", $payload); + return; + } + } +}