diff --git a/VGT_Sub/module.php b/VGT_Sub/module.php index b3b7735..5a53ea2 100644 --- a/VGT_Sub/module.php +++ b/VGT_Sub/module.php @@ -8,32 +8,32 @@ class VGT_Sub extends IPSModule { parent::Create(); - // Device - $this->RegisterPropertyString('DeviceID', ''); + $this->RegisterPropertyString("DeviceID", ""); - // --- Variablen anlegen --- - $this->RegisterVariableFloat('PowerProduction', 'Power Production', '', 10); - $this->RegisterVariableFloat('StateOfCharge', 'State of Charge', '', 11); - $this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 12); - $this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 13); + // --- Variablen für Feedback --- + $this->RegisterVariableFloat("PowerProduction", "Power Production", "", 10); + $this->RegisterVariableBoolean("IsReady", "Is Ready", "", 11); + $this->RegisterVariableBoolean("IsRunning", "Is Running", "", 12); + $this->RegisterVariableFloat("StateOfCharge", "State of Charge", "", 13); + $this->RegisterVariableFloat("MinSOC", "Min SOC", "", 14); + $this->RegisterVariableFloat("MaxSOC", "Max SOC", "", 15); - $this->RegisterVariableFloat('MinSOC', 'Min SOC', '', 20); - $this->RegisterVariableFloat('MaxSOC', 'Max SOC', '', 21); + // --- Variablen für Remote Control --- + $this->RegisterVariableFloat("PowerSetpoint", "Power Setpoint", "", 20); + $this->RegisterVariableString("Strategy", "Strategy", "", 21); - $this->RegisterVariableFloat('PowerSetpoint', 'Power Setpoint', '', 30); - $this->RegisterVariableString('Strategy', 'Strategy', '', 31); + // Debug-Variablen + $this->RegisterVariableString("FeedbackRequestPayload", "Feedback Request", "", 90); + $this->RegisterVariableString("RemoteControlPayload", "Remote Control Request", "", 91); - $this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request', '', 80); - $this->RegisterVariableString('RemoteControlPayload', 'Remote Control Request', '', 81); - - // MQTT Splitter - $this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}'); + // MQTT Splitter (NUR dieser ist korrekt) + $this->ConnectParent("{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}"); } public function ApplyChanges() { parent::ApplyChanges(); - $this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}'); + $this->ConnectParent("{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}"); } public function RequestAction($Ident, $Value) @@ -41,22 +41,13 @@ class VGT_Sub extends IPSModule SetValue($this->GetIDForIdent($Ident), $Value); } - public function GetConfigurationForm() - { - return json_encode([ - "elements" => [ - [ "type" => "ValidationTextBox", "name" => "DeviceID", "caption" => "Device ID" ] - ] - ]); - } - /* ========================================================= - * MQTT Subscribe (kompatibel zu MQTT-Splitter) - * ========================================================= */ + /* ===================================================================================== + * MQTT Funktionen + * ===================================================================================== */ + private function Subscribe(string $topic) { - if (!$this->HasActiveParent()) return; - $this->SendDataToParent(json_encode([ "DataID" => "{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}", "Buffer" => [ @@ -66,13 +57,8 @@ class VGT_Sub extends IPSModule ])); } - /* ========================================================= - * MQTT Publish (kompatibel zu MQTT-Splitter) - * ========================================================= */ private function Publish(string $topic, string $payload) { - if (!$this->HasActiveParent()) return; - $this->SendDataToParent(json_encode([ "DataID" => "{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}", "Buffer" => [ @@ -85,85 +71,75 @@ class VGT_Sub extends IPSModule ])); } - /* ========================================================= - * MQTT RECEIVE - * ========================================================= */ + + /* ===================================================================================== + * ReceiveData – erkennt NUR das tatsächliche Splitter-Format + * ===================================================================================== */ public function ReceiveData($JSONString) { $data = json_decode($JSONString, true); - if (!isset($data['Buffer'])) return; + if (!isset($data["Buffer"])) return; - $buf = $data['Buffer']; - $device = $this->ReadPropertyString('DeviceID'); + $buf = $data["Buffer"]; - // Wenn der MQTT-Splitter ready ist → abonnieren - if (isset($buf['Message']) && $buf['Message'] === "MQTT_CONNECTED") { - if ($device !== '') { - $this->Subscribe("feedback-request/$device"); - $this->Subscribe("remote-control-request/$device"); + /* CONNECTED? → Dann subscriben */ + if (isset($buf["Message"]) && $buf["Message"] === "MQTT_CONNECTED") { + + $dev = $this->ReadPropertyString("DeviceID"); + if ($dev) { + $this->Subscribe("feedback-request/$dev"); + $this->Subscribe("remote-control-request/$dev"); } return; } - // echte MQTT Nachrichten - if (!isset($buf['Topic']) || !isset($buf['Payload'])) return; + /* Normale MQTT Message */ + if (!isset($buf["Topic"]) || !isset($buf["Payload"])) return; - $topic = $buf['Topic']; - $payload = $buf['Payload']; + $topic = $buf["Topic"]; + $payload = $buf["Payload"]; - /* --------------------------------------------------------- + $device = $this->ReadPropertyString("DeviceID"); + + /* ===================================================================================== * FEEDBACK REQUEST - * --------------------------------------------------------- */ + * ===================================================================================== */ if ($topic === "feedback-request/$device") { - $this->SetValue('FeedbackRequestPayload', $payload); - - $power = GetValueFloat($this->GetIDForIdent('PowerProduction')); - $soc = GetValueFloat($this->GetIDForIdent('StateOfCharge')); - $run = GetValueBoolean($this->GetIDForIdent('IsRunning')); - - $min = GetValueFloat($this->GetIDForIdent('MinSOC')); - $max = GetValueFloat($this->GetIDForIdent('MaxSOC')); - - $isReady = ($run && $soc > $min && $soc < $max); - SetValueBoolean($this->GetIDForIdent('IsReady'), $isReady); + $this->SetValue("FeedbackRequestPayload", $payload); $response = [ - "power_production" => $power, - "state_of_charge" => $soc, - "is_running" => $run, - "is_ready" => $isReady, - "min_soc" => $min, - "max_soc" => $max + "power_production" => GetValueFloat($this->GetIDForIdent("PowerProduction")), + "is_ready" => GetValueBoolean($this->GetIDForIdent("IsReady")), + "is_running" => GetValueBoolean($this->GetIDForIdent("IsRunning")), + "state_of_charge" => GetValueFloat($this->GetIDForIdent("StateOfCharge")), + "min_soc" => GetValueFloat($this->GetIDForIdent("MinSOC")), + "max_soc" => GetValueFloat($this->GetIDForIdent("MaxSOC")), ]; - $this->Publish("feedbackresponse/$device", json_encode($response)); + $this->Publish("feedback-response/$device", json_encode($response)); return; } - /* --------------------------------------------------------- + /* ===================================================================================== * REMOTE CONTROL REQUEST - * --------------------------------------------------------- */ + * ===================================================================================== */ if ($topic === "remote-control-request/$device") { - $this->SetValue('RemoteControlPayload', $payload); + $this->SetValue("RemoteControlPayload", $payload); + $json = json_decode($payload, true); if (!is_array($json)) return; if (isset($json["power_setpoint"])) { - SetValueFloat( - $this->GetIDForIdent('PowerSetpoint'), - floatval($json["power_setpoint"]) - ); + SetValueFloat($this->GetIDForIdent("PowerSetpoint"), floatval($json["power_setpoint"])); } if (isset($json["strategy"])) { - SetValueString( - $this->GetIDForIdent('Strategy'), - strval($json["strategy"]) - ); + SetValueString($this->GetIDForIdent("Strategy"), strval($json["strategy"])); } + $this->Publish("remote-control-response/$device", json_encode($json)); return; } }