no message

This commit is contained in:
dh
2025-11-27 14:46:09 +01:00
parent 2914c0bdc8
commit 9ac109f895
+73 -77
View File
@@ -4,126 +4,124 @@ declare(strict_types=1);
class VGT_Sub extends IPSModule class VGT_Sub extends IPSModule
{ {
private bool $Subscribed = false;
public function Create() public function Create()
{ {
parent::Create(); parent::Create();
$this->RegisterPropertyString("DeviceID", ""); // MQTT CLIENT NICHT Splitter!
$this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
// --- Variablen für Feedback --- $this->RegisterPropertyString('DeviceID', '');
$this->RegisterVariableFloat("PowerProduction", "Power Production", "", 10);
$this->RegisterVariableBoolean("IsReady", "Is Ready", "", 11);
$this->RegisterVariableBoolean("IsRunning", "Is Running", "", 12);
$this->RegisterVariableFloat("StateOfCharge", "State of Charge", "", 13);
$this->RegisterVariableFloat("MinSOC", "Min SOC", "", 14);
$this->RegisterVariableFloat("MaxSOC", "Max SOC", "", 15);
// --- Variablen für Remote Control --- // Feedback Variablen
$this->RegisterVariableFloat("PowerSetpoint", "Power Setpoint", "", 20); $this->RegisterVariableFloat('PowerProduction', 'Power Production', '', 10);
$this->RegisterVariableString("Strategy", "Strategy", "", 21); $this->RegisterVariableFloat('StateOfCharge', 'State of Charge', '', 11);
$this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 12);
$this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 13);
$this->RegisterVariableFloat('MinSOC', 'Min SOC', '', 14);
$this->RegisterVariableFloat('MaxSOC', 'Max SOC', '', 15);
// Debug-Variablen // Remote Control
$this->RegisterVariableString("FeedbackRequestPayload", "Feedback Request", "", 90); $this->RegisterVariableFloat('PowerSetpoint', 'Power Setpoint', '', 20);
$this->RegisterVariableString("RemoteControlPayload", "Remote Control Request", "", 91); $this->RegisterVariableString('Strategy', 'Strategy', '', 21);
// MQTT Splitter (NUR dieser ist korrekt) // Debug
$this->ConnectParent("{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}"); $this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request', '', 90);
$this->RegisterVariableString('RemoteControlPayload', 'Remote Control', '', 91);
} }
public function ApplyChanges() public function ApplyChanges()
{ {
parent::ApplyChanges(); parent::ApplyChanges();
$this->ConnectParent("{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}"); $this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}');
$this->Subscribed = false;
} }
public function RequestAction($Ident, $Value) private function Log($t, $m)
{ {
SetValue($this->GetIDForIdent($Ident), $Value); IPS_LogMessage("VGT_Sub/$t", $m);
$this->SendDebug($t, $m, 0);
} }
private function SafeSend(array $p)
/* =====================================================================================
* MQTT Funktionen
* ===================================================================================== */
private function Subscribe(string $topic)
{ {
$this->SendDataToParent(json_encode([ $parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0;
"DataID" => "{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}", if ($parent === 0) return;
"Buffer" => [ if (IPS_GetInstance($parent)['InstanceStatus'] !== 102) return;
"Topic" => $topic, @$this->SendDataToParent(json_encode($p));
"QualityOfService" => 0 }
]
])); private function EnsureSubscribe()
{
if ($this->Subscribed) return;
$this->Subscribed = true;
$this->SafeSend([
'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}',
'PacketType' => 8,
'QualityOfService' => 0,
'Retain' => false,
'Topic' => '#',
'Payload' => ''
]);
$this->Log("Subscribe", "#");
} }
private function Publish(string $topic, string $payload) private function Publish(string $topic, string $payload)
{ {
$this->SendDataToParent(json_encode([ $this->SafeSend([
"DataID" => "{F7A0DD2E-7684-95C0-64C2-D2A9DC47577B}", 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}',
"Buffer" => [ 'PacketType' => 3,
"Command" => "PUBLISH", 'QualityOfService' => 0,
"Topic" => $topic, 'Retain' => false,
"Payload" => $payload, 'Topic' => $topic,
"Retain" => false, 'Payload' => $payload
"QualityOfService" => 0 ]);
]
])); $this->Log("Publish", "$topic$payload");
} }
/* =====================================================================================
* ReceiveData erkennt NUR das tatsächliche Splitter-Format
* ===================================================================================== */
public function ReceiveData($JSONString) public function ReceiveData($JSONString)
{ {
$this->EnsureSubscribe();
$data = json_decode($JSONString, true); $data = json_decode($JSONString, true);
if (!isset($data["Buffer"])) return; if (!is_array($data)) return;
$buf = $data["Buffer"]; $topic = $data['Topic'] ?? '';
$payload = $data['Payload'] ?? '';
/* CONNECTED? → Dann subscriben */ if ($topic === '') return;
if (isset($buf["Message"]) && $buf["Message"] === "MQTT_CONNECTED") {
$dev = $this->ReadPropertyString("DeviceID"); $this->Log("RX", "$topic$payload");
if ($dev) {
$this->Subscribe("feedback-request/$dev");
$this->Subscribe("remote-control-request/$dev");
}
return;
}
/* Normale MQTT Message */ $device = $this->ReadPropertyString('DeviceID');
if (!isset($buf["Topic"]) || !isset($buf["Payload"])) return;
$topic = $buf["Topic"]; /* ================== FEEDBACK ================== */
$payload = $buf["Payload"];
$device = $this->ReadPropertyString("DeviceID");
/* =====================================================================================
* FEEDBACK REQUEST
* ===================================================================================== */
if ($topic === "feedback-request/$device") { if ($topic === "feedback-request/$device") {
$this->SetValue("FeedbackRequestPayload", $payload); $this->SetValue('FeedbackRequestPayload', $payload);
$response = [ $resp = [
"power_production" => GetValueFloat($this->GetIDForIdent("PowerProduction")), "power_production" => GetValueFloat($this->GetIDForIdent("PowerProduction")),
"is_ready" => GetValueBoolean($this->GetIDForIdent("IsReady")), "is_ready" => GetValueBoolean($this->GetIDForIdent("IsReady")),
"is_running" => GetValueBoolean($this->GetIDForIdent("IsRunning")), "is_running" => GetValueBoolean($this->GetIDForIdent("IsRunning")),
"state_of_charge" => GetValueFloat($this->GetIDForIdent("StateOfCharge")), "state_of_charge" => GetValueFloat($this->GetIDForIdent("StateOfCharge")),
"min_soc" => GetValueFloat($this->GetIDForIdent("MinSOC")), "min_soc" => GetValueFloat($this->GetIDForIdent("MinSOC")),
"max_soc" => GetValueFloat($this->GetIDForIdent("MaxSOC")), "max_soc" => GetValueFloat($this->GetIDForIdent("MaxSOC"))
]; ];
$this->Publish("feedback-response/$device", json_encode($response)); $this->Publish("feedback-response/$device", json_encode($resp));
return; return;
} }
/* ===================================================================================== /* ================== REMOTE CONTROL ================== */
* REMOTE CONTROL REQUEST
* ===================================================================================== */
if ($topic === "remote-control-request/$device") { if ($topic === "remote-control-request/$device") {
$this->SetValue("RemoteControlPayload", $payload); $this->SetValue("RemoteControlPayload", $payload);
@@ -131,13 +129,11 @@ class VGT_Sub extends IPSModule
$json = json_decode($payload, true); $json = json_decode($payload, true);
if (!is_array($json)) return; if (!is_array($json)) return;
if (isset($json["power_setpoint"])) { if (isset($json["power_setpoint"]))
SetValueFloat($this->GetIDForIdent("PowerSetpoint"), floatval($json["power_setpoint"])); SetValueFloat($this->GetIDForIdent("PowerSetpoint"), floatval($json["power_setpoint"]));
}
if (isset($json["strategy"])) { if (isset($json["strategy"]))
SetValueString($this->GetIDForIdent("Strategy"), strval($json["strategy"])); SetValueString($this->GetIDForIdent("Strategy"), $json["strategy"]);
}
$this->Publish("remote-control-response/$device", json_encode($json)); $this->Publish("remote-control-response/$device", json_encode($json));
return; return;