diff --git a/VGT_Sub/module.php b/VGT_Sub/module.php index b1d101f..b3b7735 100644 --- a/VGT_Sub/module.php +++ b/VGT_Sub/module.php @@ -8,10 +8,10 @@ class VGT_Sub extends IPSModule { parent::Create(); - // Device-ID + // Device $this->RegisterPropertyString('DeviceID', ''); - // --- Variablen erstellen --- + // --- Variablen anlegen --- $this->RegisterVariableFloat('PowerProduction', 'Power Production', '', 10); $this->RegisterVariableFloat('StateOfCharge', 'State of Charge', '', 11); $this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 12); @@ -20,13 +20,11 @@ class VGT_Sub extends IPSModule $this->RegisterVariableFloat('MinSOC', 'Min SOC', '', 20); $this->RegisterVariableFloat('MaxSOC', 'Max SOC', '', 21); - // Remote Control Variablen $this->RegisterVariableFloat('PowerSetpoint', 'Power Setpoint', '', 30); $this->RegisterVariableString('Strategy', 'Strategy', '', 31); - // Debug / Anzeige - $this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request Payload', '', 90); - $this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 91); + $this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request', '', 80); + $this->RegisterVariableString('RemoteControlPayload', 'Remote Control Request', '', 81); // MQTT Splitter $this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}'); @@ -35,38 +33,26 @@ class VGT_Sub extends IPSModule public function ApplyChanges() { parent::ApplyChanges(); - $this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}'); - - $dev = $this->ReadPropertyString('DeviceID'); - if ($dev !== '') { - $this->Subscribe("feedback-request/$dev"); - $this->Subscribe("remote-control-request/$dev"); - } } public function RequestAction($Ident, $Value) { - // Variablen schreibbar machen SetValue($this->GetIDForIdent($Ident), $Value); - return true; } - /* --------------------------------------------------------- - * CONFIG FORM - * ---------------------------------------------------------*/ public function GetConfigurationForm() { return json_encode([ "elements" => [ - ["type" => "ValidationTextBox", "name" => "DeviceID", "caption" => "Device ID"] + [ "type" => "ValidationTextBox", "name" => "DeviceID", "caption" => "Device ID" ] ] ]); } - /* --------------------------------------------------------- - * MQTT Subscribe - * ---------------------------------------------------------*/ + /* ========================================================= + * MQTT Subscribe (kompatibel zu MQTT-Splitter) + * ========================================================= */ private function Subscribe(string $topic) { if (!$this->HasActiveParent()) return; @@ -80,6 +66,9 @@ class VGT_Sub extends IPSModule ])); } + /* ========================================================= + * MQTT Publish (kompatibel zu MQTT-Splitter) + * ========================================================= */ private function Publish(string $topic, string $payload) { if (!$this->HasActiveParent()) return; @@ -88,31 +77,27 @@ class VGT_Sub extends IPSModule "DataID" => "{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}", "Buffer" => [ "Command" => "PUBLISH", - "Topic" => $topic, + "Topic" => $topic, "Payload" => $payload, - "Retain" => false, + "Retain" => false, "QualityOfService" => 0 ] ])); } - /* --------------------------------------------------------- - * RECEIVE MQTT - * ---------------------------------------------------------*/ + /* ========================================================= + * MQTT RECEIVE + * ========================================================= */ public function ReceiveData($JSONString) { $data = json_decode($JSONString, true); + if (!isset($data['Buffer'])) return; - if (!isset($data['Type'])) return; - - $type = $data['Type']; - $topic = $data['Topic'] ?? ''; - $payload = $data['Payload'] ?? ''; - + $buf = $data['Buffer']; $device = $this->ReadPropertyString('DeviceID'); - // Beim Connect Topics neu abonnieren - if ($type === "MQTT_CONNECTED") { + // 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"); @@ -120,27 +105,29 @@ class VGT_Sub extends IPSModule return; } - if ($type !== "MESSAGE") return; + // echte MQTT Nachrichten + if (!isset($buf['Topic']) || !isset($buf['Payload'])) return; + + $topic = $buf['Topic']; + $payload = $buf['Payload']; /* --------------------------------------------------------- * FEEDBACK REQUEST - * ---------------------------------------------------------*/ + * --------------------------------------------------------- */ if ($topic === "feedback-request/$device") { $this->SetValue('FeedbackRequestPayload', $payload); - // Variablen abrufen $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')); + $min = GetValueFloat($this->GetIDForIdent('MinSOC')); + $max = GetValueFloat($this->GetIDForIdent('MaxSOC')); $isReady = ($run && $soc > $min && $soc < $max); SetValueBoolean($this->GetIDForIdent('IsReady'), $isReady); - // Antwort erstellen $response = [ "power_production" => $power, "state_of_charge" => $soc, @@ -150,22 +137,19 @@ class VGT_Sub extends IPSModule "max_soc" => $max ]; - // Publish an feedbackresponse/deviceid $this->Publish("feedbackresponse/$device", json_encode($response)); return; } /* --------------------------------------------------------- * REMOTE CONTROL REQUEST - * ---------------------------------------------------------*/ + * --------------------------------------------------------- */ if ($topic === "remote-control-request/$device") { $this->SetValue('RemoteControlPayload', $payload); - $json = json_decode($payload, true); if (!is_array($json)) return; - // power_setpoint → Float-Variable if (isset($json["power_setpoint"])) { SetValueFloat( $this->GetIDForIdent('PowerSetpoint'), @@ -173,7 +157,6 @@ class VGT_Sub extends IPSModule ); } - // strategy → String-Variable if (isset($json["strategy"])) { SetValueString( $this->GetIDForIdent('Strategy'),