no message

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