no message

This commit is contained in:
2026-02-10 13:31:39 +01:00
parent e21f760e13
commit 015b1fc441
2 changed files with 22 additions and 59 deletions

View File

@@ -8,7 +8,7 @@
], ],
"parentRequirements": ["{F66ADE63-8834-4178-8CA5-AE4465D2E252}"], "parentRequirements": ["{F66ADE63-8834-4178-8CA5-AE4465D2E252}"],
"childRequirements": [], "childRequirements": [],
"implemented": [], "implemented": ["{018EF6B5-AB94-40C6-AA53-46943E824ACF}"],
"prefix": "VGT", "prefix": "VGT",
"version": "1.1" "version": "1.2"
} }

View File

@@ -8,30 +8,22 @@ class VGT_Sub extends IPSModule
{ {
parent::Create(); parent::Create();
// --------------------------------------------------------------------- // --- Eigenschaften ---
// 1. Eigenschaften: Hardware-Verknüpfungen & MQTT Einstellungen
// ---------------------------------------------------------------------
$this->RegisterPropertyString('MQTTBaseTopic', 'Test VGT_Steuerung/Komm'); $this->RegisterPropertyString('MQTTBaseTopic', 'Test VGT_Steuerung/Komm');
// Externe Hardware-Quellen (Lesen)
$this->RegisterPropertyInteger('SourceSoC', 0); $this->RegisterPropertyInteger('SourceSoC', 0);
$this->RegisterPropertyInteger('SourcePowerProd', 0); $this->RegisterPropertyInteger('SourcePowerProd', 0);
$this->RegisterPropertyInteger('SourceIsReady', 0); $this->RegisterPropertyInteger('SourceIsReady', 0);
$this->RegisterPropertyInteger('SourceIsRunning', 0); $this->RegisterPropertyInteger('SourceIsRunning', 0);
$this->RegisterPropertyInteger('SourceMinSoC', 0); $this->RegisterPropertyInteger('SourceMinSoC', 0);
$this->RegisterPropertyInteger('SourceMaxSoC', 0); $this->RegisterPropertyInteger('SourceMaxSoC', 0);
// Hardware-Ziel (Schreiben)
$this->RegisterPropertyInteger('TargetControlVar', 0); $this->RegisterPropertyInteger('TargetControlVar', 0);
// --------------------------------------------------------------------- // --- Variablen ---
// 2. Variablen im Modul erstellen (zur Anzeige & Steuerung)
// ---------------------------------------------------------------------
// Diese Variablen spiegeln den aktuellen Zustand wider
$this->RegisterVariableString('Strategy', 'Strategy', '', 10); $this->RegisterVariableString('Strategy', 'Strategy', '', 10);
$this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 20); $this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 20);
// Visualisierung der gelesenen Hardware-Werte (optional, aber gut zur Kontrolle) // Visu
$this->RegisterVariableFloat('Visu_SoC', 'State of Charge', '~Intensity.100', 30); $this->RegisterVariableFloat('Visu_SoC', 'State of Charge', '~Intensity.100', 30);
$this->RegisterVariableFloat('Visu_Production', 'Power Production', '~Watt', 40); $this->RegisterVariableFloat('Visu_Production', 'Power Production', '~Watt', 40);
$this->RegisterVariableBoolean('Visu_IsReady', 'Is Ready', '~Switch', 50); $this->RegisterVariableBoolean('Visu_IsReady', 'Is Ready', '~Switch', 50);
@@ -44,22 +36,15 @@ class VGT_Sub extends IPSModule
{ {
parent::ApplyChanges(); parent::ApplyChanges();
// --------------------------------------------------------------------- // --- MQTT Filter setzen ---
// 3. MQTT Filter setzen (Abonnement) // Wir abonnieren alles, was mit dem BaseTopic beginnt
// ---------------------------------------------------------------------
// Wir sagen dem Parent (MQTT Splitter): "Schick mir alles, was auf mein Topic kommt"
$topic = $this->ReadPropertyString('MQTTBaseTopic'); $topic = $this->ReadPropertyString('MQTTBaseTopic');
// Regex Filter für ReceiveData: Alles was mit dem Topic beginnt
// Wir escapen Slash /, damit Regex funktioniert
$cleanTopic = preg_quote($topic, '/'); $cleanTopic = preg_quote($topic, '/');
// Filter: Topic muss matchen
// Filterformat für MQTT Splitter
$this->SetReceiveDataFilter(".*\"Topic\":\"" . $cleanTopic . "/.*\".*"); $this->SetReceiveDataFilter(".*\"Topic\":\"" . $cleanTopic . "/.*\".*");
// --------------------------------------------------------------------- // --- Events registrieren ---
// 4. Events registrieren (Hardware überwachen)
// ---------------------------------------------------------------------
// Wenn sich der externe SoC ändert, müssen wir die Logik ausführen
$socID = $this->ReadPropertyInteger('SourceSoC'); $socID = $this->ReadPropertyInteger('SourceSoC');
if (IPS_VariableExists($socID)) { if (IPS_VariableExists($socID)) {
$this->RegisterMessage($socID, VM_UPDATE); $this->RegisterMessage($socID, VM_UPDATE);
@@ -68,49 +53,41 @@ class VGT_Sub extends IPSModule
$this->RegisterMessage($this->GetIDForIdent('PowerSetpoint'), VM_UPDATE); $this->RegisterMessage($this->GetIDForIdent('PowerSetpoint'), VM_UPDATE);
} }
// ------------------------------------------------------------------------- // --- DATEN EMPFANGEN (Vom MQTT Splitter) ---
// MQTT EMPFANG (Ersatz für Events auf MQTT-Variablen)
// -------------------------------------------------------------------------
public function ReceiveData($JSONString) public function ReceiveData($JSONString)
{ {
$data = json_decode($JSONString); $data = json_decode($JSONString);
// UTF-8 Payload decodieren // Der Splitter liefert 'Topic' und 'Payload' (Payload ist meist UTF-8)
$payload = utf8_decode($data->Payload); $payload = utf8_decode($data->Payload);
$topic = $data->Topic; $topic = $data->Topic;
$baseTopic = $this->ReadPropertyString('MQTTBaseTopic'); $baseTopic = $this->ReadPropertyString('MQTTBaseTopic');
// Prüfen: Ist es der Writebefehl? // Debugging (optional, schreibt ins Meldungsfenster)
// $this->SendDebug('MQTT In', "Topic: $topic | Payload: $payload", 0);
// Routing
if ($topic === $baseTopic . '/Writebefehl') { if ($topic === $baseTopic . '/Writebefehl') {
$this->ProcessWriteCommand($payload); $this->ProcessWriteCommand($payload);
} }
// Prüfen: Ist es der Lesebefehl?
elseif ($topic === $baseTopic . '/Lesebefehl') { elseif ($topic === $baseTopic . '/Lesebefehl') {
$this->ProcessReadCommand(); $this->ProcessReadCommand();
} }
} }
// ------------------------------------------------------------------------- // --- LOGIK ---
// LOGIK: Interne Verarbeitung
// -------------------------------------------------------------------------
// Hardware-Überwachung (MessageSink) bleibt gleich
public function MessageSink($TimeStamp, $SenderID, $Message, $Data) public function MessageSink($TimeStamp, $SenderID, $Message, $Data)
{ {
if ($Message == VM_UPDATE) { if ($Message == VM_UPDATE) {
// Visualisierung aktualisieren (SoC im Modul anzeigen)
$socID = $this->ReadPropertyInteger('SourceSoC'); $socID = $this->ReadPropertyInteger('SourceSoC');
if ($SenderID == $socID) { if ($SenderID == $socID) {
$this->SetValue('Visu_SoC', $Data[0]); $this->SetValue('Visu_SoC', $Data[0]);
} }
// Steuerungslogik immer ausführen wenn sich was ändert
$this->ExecuteControlLogic(); $this->ExecuteControlLogic();
} }
} }
// Manuelles Schalten im WebFront
public function RequestAction($Ident, $Value) public function RequestAction($Ident, $Value)
{ {
switch ($Ident) { switch ($Ident) {
@@ -122,17 +99,12 @@ class VGT_Sub extends IPSModule
} }
} }
// -------------------------------------------------------------------------
// CORE LOGIK
// -------------------------------------------------------------------------
private function ProcessWriteCommand($jsonInput) private function ProcessWriteCommand($jsonInput)
{ {
if ($jsonInput == "") return; if ($jsonInput == "") return;
$data = json_decode($jsonInput, true); $data = json_decode($jsonInput, true);
if ($data === null) return; if ($data === null) return;
// 1. Variablen setzen
if (isset($data['power_setpoint'])) { if (isset($data['power_setpoint'])) {
$val = $data['power_setpoint']; $val = $data['power_setpoint'];
if ($val > 6000) $val = 6000; if ($val > 6000) $val = 6000;
@@ -144,10 +116,9 @@ class VGT_Sub extends IPSModule
$this->SetValue('Strategy', $data['strategy']); $this->SetValue('Strategy', $data['strategy']);
} }
// 2. Hardware sofort ansteuern
$this->ExecuteControlLogic(); $this->ExecuteControlLogic();
// 3. Antwort senden (an .../Writebefehl Antwort) // Antwort Senden
$output = [ $output = [
'power_setpoint' => $this->GetValue('PowerSetpoint'), 'power_setpoint' => $this->GetValue('PowerSetpoint'),
'strategy' => $this->GetValue('Strategy') 'strategy' => $this->GetValue('Strategy')
@@ -158,16 +129,13 @@ class VGT_Sub extends IPSModule
private function ProcessReadCommand() private function ProcessReadCommand()
{ {
// Hardware Werte lesen
$data = $this->GatherHardwareData(); $data = $this->GatherHardwareData();
// Lokale Visu-Variablen updaten (damit man im Modul sieht was passiert) $this->SetValue('Visu_Production', $data['raw_prod']);
$this->SetValue('Visu_Production', $data['raw_prod']); // Hilfswert speichern
$this->SetValue('Visu_IsReady', $data['is_ready']); $this->SetValue('Visu_IsReady', $data['is_ready']);
// JSON für MQTT
$mqttData = [ $mqttData = [
'power_production' => -1 * $data['raw_prod'], // Vorzeichen wie im Original 'power_production' => -1 * $data['raw_prod'],
'is_ready' => $data['is_ready'], 'is_ready' => $data['is_ready'],
'is_running' => $data['is_running'], 'is_running' => $data['is_running'],
'state_of_charge' => $data['soc'], 'state_of_charge' => $data['soc'],
@@ -175,7 +143,6 @@ class VGT_Sub extends IPSModule
'max_soc' => $data['max_soc'] 'max_soc' => $data['max_soc']
]; ];
// Senden an .../Lesebefehl Antwort
$this->SendMQTT($this->ReadPropertyString('MQTTBaseTopic') . '/Lesebefehl Antwort', json_encode($mqttData)); $this->SendMQTT($this->ReadPropertyString('MQTTBaseTopic') . '/Lesebefehl Antwort', json_encode($mqttData));
} }
@@ -187,7 +154,6 @@ class VGT_Sub extends IPSModule
$strategy = $this->GetValue('Strategy'); $strategy = $this->GetValue('Strategy');
$setpoint = $this->GetValue('PowerSetpoint'); $setpoint = $this->GetValue('PowerSetpoint');
// SoC holen (Live)
$socID = $this->ReadPropertyInteger('SourceSoC'); $socID = $this->ReadPropertyInteger('SourceSoC');
$soc = IPS_VariableExists($socID) ? GetValue($socID) : 0; $soc = IPS_VariableExists($socID) ? GetValue($socID) : 0;
@@ -196,7 +162,6 @@ class VGT_Sub extends IPSModule
} elseif ($strategy == "stop") { } elseif ($strategy == "stop") {
RequestAction($targetID, 0); RequestAction($targetID, 0);
} else { } else {
// Logic: soc == 50 -> 0
if ((int)$soc == 50) { if ((int)$soc == 50) {
RequestAction($targetID, 0); RequestAction($targetID, 0);
} elseif ($soc < 50) { } elseif ($soc < 50) {
@@ -207,7 +172,6 @@ class VGT_Sub extends IPSModule
} }
} }
// Hilfsfunktion: Hardware Daten sammeln
private function GatherHardwareData() private function GatherHardwareData()
{ {
$safeGet = function($propName) { $safeGet = function($propName) {
@@ -225,10 +189,10 @@ class VGT_Sub extends IPSModule
]; ];
} }
// Hilfsfunktion: MQTT Senden
protected function SendMQTT($Topic, $Payload) protected function SendMQTT($Topic, $Payload)
{ {
$Data['DataID'] = '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}'; // MQTT Client/Splitter GUID // GUID für den Datenaustausch mit MQTT Splitter (TX)
$Data['DataID'] = '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}';
$Data['PacketType'] = 3; // Publish $Data['PacketType'] = 3; // Publish
$Data['QualityOfService'] = 0; $Data['QualityOfService'] = 0;
$Data['Retain'] = false; $Data['Retain'] = false;
@@ -236,7 +200,6 @@ class VGT_Sub extends IPSModule
$Data['Payload'] = $Payload; $Data['Payload'] = $Payload;
$JSON = json_encode($Data); $JSON = json_encode($Data);
// An Parent senden
$this->SendDataToParent($JSON); $this->SendDataToParent($JSON);
} }
} }