From 58fb1ba99ee85bc55806d17af4deb3aa030e8271 Mon Sep 17 00:00:00 2001 From: "belevo\\mh" Date: Tue, 26 May 2026 10:32:50 +0200 Subject: [PATCH] no message --- MQTTBatterySDL/form.json | 121 ++++++++++++-------------- MQTTBatterySDL/module.php | 179 +++++++++++++------------------------- 2 files changed, 118 insertions(+), 182 deletions(-) diff --git a/MQTTBatterySDL/form.json b/MQTTBatterySDL/form.json index 3efb1b6..e7d7bcf 100644 --- a/MQTTBatterySDL/form.json +++ b/MQTTBatterySDL/form.json @@ -1,66 +1,59 @@ { - "elements": [ - { - "type": "ValidationTextBox", - "name": "TopicSuffix", - "caption": "Topic Suffix / x" - }, - { - "type": "SelectVariable", - "name": "ReqActionID", - "caption": "Ausgabe-Variable / RequestAction" - }, - { - "type": "NumberSpinner", - "name": "TargetSoC", - "caption": "Ziel-Ladestand %", - "minimum": 0, - "maximum": 100 - }, - { - "type": "NumberSpinner", - "name": "ChargePower", - "caption": "Ladeleistung in W", - "minimum": 0, - "maximum": 100000 - }, - { - "type": "NumberSpinner", - "name": "DischargePower", - "caption": "Entladeleistung in W", - "minimum": 0, - "maximum": 100000 - }, - { - "type": "NumberSpinner", - "name": "MaxPowerSetpoint", - "caption": "Max. Power Setpoint", - "minimum": 0, - "maximum": 100000 - } - ], - "actions": [ - { - "type": "Button", - "caption": "Topics neu abonnieren", - "onClick": "MBSDL_Subscribe($id);" - } - ], - "status": [ - { - "code": 101, - "icon": "active", - "caption": "Instanz aktiv" - }, - { - "code": 102, - "icon": "inactive", - "caption": "Topic Suffix fehlt" - }, - { - "code": 103, - "icon": "error", - "caption": "Keine Ausgabe-Variable gewählt" - } - ] + "elements": [ + { + "type": "ValidationTextBox", + "name": "TopicSuffix", + "caption": "Topic Suffix / x" + }, + { + "type": "SelectVariable", + "name": "ReqActionID", + "caption": "Ausgabe-Variable / RequestAction" + }, + { + "type": "SelectVariable", + "name": "SoCID", + "caption": "SoC Variable" + }, + { + "type": "SelectVariable", + "name": "PowerProductionID", + "caption": "Aktuelle Leistung Variable" + }, + { + "type": "NumberSpinner", + "name": "TargetSoC", + "caption": "Ziel-Ladestand %", + "minimum": 0, + "maximum": 100 + }, + { + "type": "NumberSpinner", + "name": "ChargePower", + "caption": "Ladeleistung in W", + "minimum": 0, + "maximum": 100000 + }, + { + "type": "NumberSpinner", + "name": "DischargePower", + "caption": "Entladeleistung in W", + "minimum": 0, + "maximum": 100000 + }, + { + "type": "NumberSpinner", + "name": "MaxPowerSetpoint", + "caption": "Max. Power Setpoint", + "minimum": 0, + "maximum": 100000 + } + ], + "actions": [ + { + "type": "Button", + "caption": "Batterie-Steuerung testen", + "onClick": "MBSDL_RunBatteryControl($id);" + } + ] } \ No newline at end of file diff --git a/MQTTBatterySDL/module.php b/MQTTBatterySDL/module.php index dfc3233..579584b 100644 --- a/MQTTBatterySDL/module.php +++ b/MQTTBatterySDL/module.php @@ -9,47 +9,34 @@ class MQTTBatterySDL extends IPSModule $this->RegisterPropertyString('TopicSuffix', ''); $this->RegisterPropertyInteger('ReqActionID', 0); + $this->RegisterPropertyInteger('SoCID', 0); + $this->RegisterPropertyInteger('PowerProductionID', 0); $this->RegisterPropertyInteger('TargetSoC', 50); $this->RegisterPropertyInteger('ChargePower', 2500); $this->RegisterPropertyInteger('DischargePower', 2500); $this->RegisterPropertyInteger('MaxPowerSetpoint', 10000); - $this->RegisterVariableFloat('PowerProduction', 'Power Production', '', 10); - $this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 20); - $this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 30); - $this->RegisterVariableFloat('StateOfCharge', 'State of Charge', '', 40); - $this->RegisterVariableFloat('MinSoC', 'Min SoC', '', 50); - $this->RegisterVariableFloat('MaxSoC', 'Max SoC', '', 60); + $this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 10); + $this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 20); + $this->RegisterVariableFloat('MinSoC', 'Min SoC', '', 30); + $this->RegisterVariableFloat('MaxSoC', 'Max SoC', '', 40); - $this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 70); + $this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 50); $this->EnableAction('PowerSetpoint'); - $this->RegisterVariableString('Strategy', 'Strategy', '', 80); + $this->RegisterVariableString('Strategy', 'Strategy', '', 60); $this->EnableAction('Strategy'); - $this->RegisterVariableString('LastReadResponse', 'Letzte Lese-Antwort', '', 90); - $this->RegisterVariableString('LastWriteResponse', 'Letzte Steuer-Antwort', '', 100); - - + $this->RegisterVariableString('LastReadResponse', 'Letzte Lese-Antwort', '', 70); + $this->RegisterVariableString('LastWriteResponse', 'Letzte Steuer-Antwort', '', 80); } public function ApplyChanges() { parent::ApplyChanges(); - if ($this->ReadPropertyString('TopicSuffix') == '') { - $this->SetStatus(102); - return; - } - - if ($this->ReadPropertyInteger('ReqActionID') <= 0) { - $this->SetStatus(103); - return; - } - - $this->SetStatus(101); - $this->Subscribe(); + $this->SetStatus(IS_ACTIVE); } public function RequestAction($Ident, $Value) @@ -71,80 +58,77 @@ class MQTTBatterySDL extends IPSModule break; case 'Strategy': - SetValueString($this->GetIDForIdent('Strategy'), $Value); + SetValueString($this->GetIDForIdent('Strategy'), (string)$Value); $this->RunBatteryControl(); break; } } - public function Subscribe() + public function RunBatteryControl() { - $suffix = $this->ReadPropertyString('TopicSuffix'); + $reqActionID = $this->ReadPropertyInteger('ReqActionID'); + $socID = $this->ReadPropertyInteger('SoCID'); + $powerProductionID = $this->ReadPropertyInteger('PowerProductionID'); - if ($suffix == '') { + if (!$this->IsValidVariable($reqActionID)) { + $this->SendDebug('RunBatteryControl', 'Keine gültige Ausgabe-Variable gewählt', 0); return; } - $topics = [ - 'feedback-request/' . $suffix, - 'remote-control-request/' . $suffix - ]; - - foreach ($topics as $topic) { - $this->SendDataToParent(json_encode([ - 'DataID' => '{D6F8C9A1-2A3B-4C5D-9E0F-333333333333}', - 'PacketType' => 8, - 'Topic' => $topic, - 'QoS' => 0 - ])); - } - } - - public function ReceiveData($JSONString) - { - $data = json_decode($JSONString, true); - - if (!is_array($data)) { + if (!$this->IsValidVariable($socID)) { + $this->SendDebug('RunBatteryControl', 'Keine gültige SoC Variable gewählt', 0); return; } - $topic = $data['Topic'] ?? ''; - $payload = $data['Buffer'] ?? ''; + $strategy = GetValueString($this->GetIDForIdent('Strategy')); + $setpoint = GetValue($this->GetIDForIdent('PowerSetpoint')); + $soc = GetValue($socID); - $suffix = $this->ReadPropertyString('TopicSuffix'); + $targetSoC = $this->ReadPropertyInteger('TargetSoC'); + $chargePower = $this->ReadPropertyInteger('ChargePower'); + $dischargePower = $this->ReadPropertyInteger('DischargePower'); - if ($topic == 'feedback-request/' . $suffix) { - $this->HandleReadRequest(); + if ($strategy == 'activate') { + RequestAction($reqActionID, $setpoint * -1); return; } - if ($topic == 'remote-control-request/' . $suffix) { - $this->HandleRemoteControlRequest($payload); + if ($strategy == 'stop') { + RequestAction($reqActionID, 0); + + if ($this->IsValidVariable($powerProductionID)) { + SetValue($powerProductionID, 0); + } + + return; + } + + if ((int)$soc == $targetSoC) { + RequestAction($reqActionID, 0); + return; + } + + if ($soc < $targetSoC) { + RequestAction($reqActionID, abs($chargePower)); + return; + } + + if ($soc > $targetSoC) { + RequestAction($reqActionID, abs($dischargePower) * -1); return; } } - private function Publish(string $topic, string $payload) + public function BuildReadResponse() { - $this->SendDataToParent(json_encode([ - 'DataID' => '{D6F8C9A1-2A3B-4C5D-9E0F-333333333333}', - 'PacketType' => 3, - 'Topic' => $topic, - 'Payload' => $payload, - 'QoS' => 0, - 'Retain' => false - ])); - } - - private function HandleReadRequest() - { - $suffix = $this->ReadPropertyString('TopicSuffix'); + $socID = $this->ReadPropertyInteger('SoCID'); + $powerProductionID = $this->ReadPropertyInteger('PowerProductionID'); $data = [ - 'power_production' => GetValue($this->GetIDForIdent('PowerProduction')), + 'power_production' => $this->IsValidVariable($powerProductionID) ? GetValue($powerProductionID) : 0, 'is_ready' => GetValue($this->GetIDForIdent('IsReady')), 'is_running' => GetValue($this->GetIDForIdent('IsRunning')), - 'state_of_charge' => GetValue($this->GetIDForIdent('StateOfCharge')), + 'state_of_charge' => $this->IsValidVariable($socID) ? GetValue($socID) : 0, 'min_soc' => GetValue($this->GetIDForIdent('MinSoC')), 'max_soc' => GetValue($this->GetIDForIdent('MaxSoC')) ]; @@ -153,10 +137,10 @@ class MQTTBatterySDL extends IPSModule SetValueString($this->GetIDForIdent('LastReadResponse'), $json); - $this->Publish('feedback-response/' . $suffix, $json); + return $json; } - private function HandleRemoteControlRequest(string $payload) + public function HandleRemoteControlJSON(string $payload) { if ($payload == '') { return; @@ -189,8 +173,6 @@ class MQTTBatterySDL extends IPSModule $this->RunBatteryControl(); - $suffix = $this->ReadPropertyString('TopicSuffix'); - $output = [ 'power_setpoint' => GetValue($this->GetIDForIdent('PowerSetpoint')), 'strategy' => GetValueString($this->GetIDForIdent('Strategy')) @@ -200,51 +182,12 @@ class MQTTBatterySDL extends IPSModule SetValueString($this->GetIDForIdent('LastWriteResponse'), $json); - $this->Publish('remote-control-response/' . $suffix, $json); + return $json; } - private function RunBatteryControl() + private function IsValidVariable(int $id) { - $strategy = GetValueString($this->GetIDForIdent('Strategy')); - $setpoint = GetValue($this->GetIDForIdent('PowerSetpoint')); - $soc = GetValue($this->GetIDForIdent('StateOfCharge')); - - $targetSoC = $this->ReadPropertyInteger('TargetSoC'); - $chargePower = $this->ReadPropertyInteger('ChargePower'); - $dischargePower = $this->ReadPropertyInteger('DischargePower'); - $reqActionID = $this->ReadPropertyInteger('ReqActionID'); - - if ($reqActionID <= 0 || !IPS_ObjectExists($reqActionID)) { - $this->SendDebug('RunBatteryControl', 'Keine gültige ReqActionID gewählt', 0); - return; - } - - if ($strategy == 'activate') { - RequestAction($reqActionID, $setpoint * -1); - return; - } - - if ($strategy == 'stop') { - RequestAction($reqActionID, 0); - SetValue($this->GetIDForIdent('PowerProduction'), 0); - return; - } - - if ((int)$soc == $targetSoC) { - RequestAction($reqActionID, 0); - return; - } - - if ($soc < $targetSoC) { - RequestAction($reqActionID, abs($chargePower)); - return; - } - - if ($soc > $targetSoC) { - RequestAction($reqActionID, abs($dischargePower) * -1); - return; - } + return $id > 0 && IPS_ObjectExists($id) && IPS_GetObject($id)['ObjectType'] == 2; } } - -?> \ No newline at end of file +?>