From 1ff2e06bf801c7225971518cd7c06d1ea34ca3cc Mon Sep 17 00:00:00 2001 From: DanielHaefliger Date: Fri, 28 Nov 2025 08:58:52 +0100 Subject: [PATCH] no message --- VGT_Sub/module.json | 20 +++++------ VGT_Sub/module.php | 82 ++++++++++++++++++++++++++------------------- 2 files changed, 57 insertions(+), 45 deletions(-) diff --git a/VGT_Sub/module.json b/VGT_Sub/module.json index 3c89b03..ebe1cb5 100644 --- a/VGT_Sub/module.json +++ b/VGT_Sub/module.json @@ -1,12 +1,12 @@ { - "id": "{0F001E51-E914-6444-B635-D46DDE0C3810}", - "name": "VGT_Sub", - "type": 3, - "vendor": "Belevo", - "aliases": ["VGT MQTT Client"], - "prefix": "VGT", - "parentRequirements": [], - "childRequirements": [], - "implemented": ["{018EF6B5-AB94-40C6-AA53-46943E824ACF}"], - "version": "1.0" + "id": "{0F001E51-E914-6444-B635-D46DDE0C3810}", + "name": "VGT_Sub", + "type": 3, + "vendor": "Belevo", + "aliases": ["VGT MQTT Client"], + "prefix": "VGT", + "parentRequirements": [], + "childRequirements": [], + "implemented": [], + "version": "1.0" } diff --git a/VGT_Sub/module.php b/VGT_Sub/module.php index 6887100..15c92b3 100644 --- a/VGT_Sub/module.php +++ b/VGT_Sub/module.php @@ -7,8 +7,14 @@ class VGT_Sub extends IPSModule public function Create() { parent::Create(); + $this->RegisterPropertyString('DeviceID', ''); + $this->RegisterVariableString("InputJSON", "MQTT Input", "", 1); + $this->EnableAction("InputJSON"); + + $this->RegisterVariableString("OutputJSON", "MQTT Output", "", 2); + $this->RegisterVariableFloat('PowerProduction', 'Power Production', '', 10); $this->EnableAction('PowerProduction'); @@ -34,30 +40,38 @@ class VGT_Sub extends IPSModule IPS_SetVariableCustomAction($this->GetIDForIdent('Strategy'), 0); } - public function ForwardData($JSONString) + public function RequestAction($Ident, $Value) { - $data = json_decode($JSONString, true); - - if (!isset($data['Topic']) || !isset($data['Payload'])) { - return ''; + if ($Ident === "InputJSON") { + $this->ProcessIncoming($Value); + return; } - $topic = $data['Topic']; - $payload = $data['Payload']; + SetValue($this->GetIDForIdent($Ident), $Value); + } + + private function ProcessIncoming($jsonString) + { + $data = @json_decode($jsonString, true); + if (!is_array($data) || !isset($data['topic']) || !isset($data['payload'])) { + return; + } + + $topic = $data['topic']; + $payload = $data['payload']; + $device = $this->ReadPropertyString('DeviceID'); if ($topic === "feedback-request/$device") { - $this->handleFeedback($device); + $this->SendFeedback($device); } if ($topic === "remote-control-request/$device") { - $this->handleRemote($device, $payload); + $this->ProcessRemote($device, $payload); } - - return ''; } - private function handleFeedback($device) + private function SendFeedback($device) { $response = [ "power_production" => GetValue($this->GetIDForIdent('PowerProduction')), @@ -68,39 +82,37 @@ class VGT_Sub extends IPSModule "max_soc" => GetValue($this->GetIDForIdent('MaxSoc')) ]; - $this->publish("feedback-response/$device", json_encode($response)); + SetValueString( + $this->GetIDForIdent("OutputJSON"), + json_encode([ + "topic" => "feedback-response/$device", + "payload" => json_encode($response) + ]) + ); } - private function handleRemote($device, $payload) + private function ProcessRemote($device, $payload) { - $in = json_decode($payload, true); - if (!is_array($in)) { + $json = @json_decode($payload, true); + if (!is_array($json)) { return; } - if (isset($in['power_setpoint'])) { - SetValue($this->GetIDForIdent('PowerSetpoint'), (int)$in['power_setpoint']); + if (isset($json['power_setpoint'])) { + SetValue($this->GetIDForIdent('PowerSetpoint'), (int)$json['power_setpoint']); } - if (isset($in['strategy'])) { - SetValueString($this->GetIDForIdent('Strategy'), (string)$in['strategy']); + if (isset($json['strategy'])) { + SetValueString($this->GetIDForIdent('Strategy'), (string)$json['strategy']); } - $this->publish("remote-control-response/$device", json_encode($in)); - } - - private function publish($topic, $payload) - { - $this->SendDataToParent(json_encode([ - "DataID" => "{018EF6B5-AB94-40C6-AA53-46943E824ACF}", - "Topic" => $topic, - "Payload" => $payload - ])); - } - - public function RequestAction($Ident, $Value) - { - SetValue($this->GetIDForIdent($Ident), $Value); + SetValueString( + $this->GetIDForIdent("OutputJSON"), + json_encode([ + "topic" => "remote-control-response/$device", + "payload" => json_encode($json) + ]) + ); } }