diff --git a/VGT_Sub/module.php b/VGT_Sub/module.php index 7002774..c22474b 100644 --- a/VGT_Sub/module.php +++ b/VGT_Sub/module.php @@ -2,16 +2,16 @@ declare(strict_types=1); -class VGT_Sub extends IPSModule +class EMS_MQTT_Device extends IPSModule { public function Create() { parent::Create(); - // Device-ID + // Device ID $this->RegisterPropertyString("DeviceID", ""); - // 6 editable values (response payload) + // Editable response values $this->RegisterVariableFloat("PowerProduction", "Power Production", "", 10); $this->EnableAction("PowerProduction"); @@ -30,14 +30,16 @@ class VGT_Sub extends IPSModule $this->RegisterVariableInteger("MaxSOC", "Max SOC", "", 60); $this->EnableAction("MaxSOC"); - // Remote-Control incoming (read-only) + + // Remote-Control incoming values (read-only) $this->RegisterVariableFloat("RC_PowerSetpoint", "RC Power Setpoint", "", 70); $this->RegisterVariableString("RC_Strategy", "RC Strategy", "", 80); - // Parent MQTT Splitter + // Connect to MQTT client splitter $this->ConnectParent('{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}'); } + public function ApplyChanges() { parent::ApplyChanges(); @@ -46,7 +48,7 @@ class VGT_Sub extends IPSModule $device = $this->ReadPropertyString("DeviceID"); - // Subscribes + // Subscribe to both incoming topics $topics = [ "feedback-request/" . $device, "remote-control-request/" . $device @@ -54,53 +56,46 @@ class VGT_Sub extends IPSModule foreach ($topics as $t) { $this->SendDataToParent(json_encode([ - "DataID" => "{043E0F88-B2B0-4DF4-B1A6-64FB1C385D3C}", - "PacketType" => 8, // SUBSCRIBE - "Topic" => $t + "DataID" => "{043E0F88-B2B0-4DF4-B1A6-64FB1C385D3C}", + "PacketType" => 8, // SUBSCRIBE + "Topic" => $t ])); } } - // --------------------------------------------------------------------- - // ACTION HANDLER - // --------------------------------------------------------------------- + // Handle UI actions public function RequestAction($Ident, $Value) { SetValue($this->GetIDForIdent($Ident), $Value); } - // --------------------------------------------------------------------- - // RECEIVE FROM MQTT - // --------------------------------------------------------------------- + + // MQTT RECEIVE public function ReceiveData($JSONString) { $data = json_decode($JSONString); + if (!isset($data->Topic) || !isset($data->Payload)) { return; } - $topic = $data->Topic; + $topic = $data->Topic; $payload = $data->Payload; - $deviceID = $this->ReadPropertyString("DeviceID"); + $device = $this->ReadPropertyString("DeviceID"); - // Topics - $feedbackReq = "feedback-request/" . $deviceID; - $remoteReq = "remote-control-request/" . $deviceID; - - if ($topic === $feedbackReq) { + if ($topic === "feedback-request/" . $device) { $this->HandleFeedbackRequest(); } - if ($topic === $remoteReq) { + if ($topic === "remote-control-request/" . $device) { $this->HandleRemoteControlRequest($payload); } } - // --------------------------------------------------------------------- - // HANDLE FEEDBACK REQUEST -> SEND STATE JSON - // --------------------------------------------------------------------- + + // FEEDBACK REQUEST → reply with state JSON private function HandleFeedbackRequest() { $response = [ @@ -113,15 +108,11 @@ class VGT_Sub extends IPSModule ]; $topic = "feedback-response/" . $this->ReadPropertyString("DeviceID"); - $this->SendToMQTT($topic, json_encode($response)); } - // --------------------------------------------------------------------- - // HANDLE REMOTE CONTROL REQUEST - // payload: - // { "power_setpoint": 150, "strategy": "activation" } - // --------------------------------------------------------------------- + + // REMOTE CONTROL REQUEST → write values + respond private function HandleRemoteControlRequest(string $payload) { $data = json_decode($payload, true); @@ -129,7 +120,6 @@ class VGT_Sub extends IPSModule return; } - // Write values to variables if (isset($data["power_setpoint"])) { SetValue($this->GetIDForIdent("RC_PowerSetpoint"), $data["power_setpoint"]); } @@ -138,41 +128,40 @@ class VGT_Sub extends IPSModule SetValue($this->GetIDForIdent("RC_Strategy"), $data["strategy"]); } - // Respond with same JSON + // Respond with same payload $topic = "remote-control-response/" . $this->ReadPropertyString("DeviceID"); $this->SendToMQTT($topic, json_encode($data)); } - // --------------------------------------------------------------------- - // MQTT SEND HELPERS - // --------------------------------------------------------------------- + + // MQTT SEND private function SendToMQTT(string $topic, string $payload) { - $this->SendDataToParent(json_encode([ - "DataID" => "{043E0F88-B2B0-4DF4-B1A6-64FB1C385D3C}", - "PacketType" => 3, + $packet = [ + "DataID" => "{043E0F88-B2B0-4DF4-B1A6-64FB1C385D3C}", + "PacketType" => 3, // PUBLISH "QualityOfService" => 0, - "Retain" => false, - "Topic" => $topic, - "Payload" => $payload - ])); + "Retain" => false, + "Topic" => $topic, + "Payload" => $payload + ]; + + $this->SendDataToParent(json_encode($packet)); } - // --------------------------------------------------------------------- + // CONFIG FORM - // --------------------------------------------------------------------- public function GetConfigurationForm() { return json_encode([ "elements" => [ [ - "type" => "ValidationTextBox", - "name" => "DeviceID", + "type" => "ValidationTextBox", + "name" => "DeviceID", "caption" => "Device ID" ] ] ]); } } - ?> \ No newline at end of file