diff --git a/VGT_Sub/module.php b/VGT_Sub/module.php index afc2ab1..01748aa 100644 --- a/VGT_Sub/module.php +++ b/VGT_Sub/module.php @@ -1,224 +1,151 @@ RegisterPropertyString('DeviceID', ''); - - // Status Variablen (schreibbar) + /** ------------------------------------------- + * STATUS Variablen (schreibbar) + * ------------------------------------------*/ $this->RegisterVariableInteger('PowerProduction', 'Power Production', '', 10); $this->EnableAction('PowerProduction'); - $this->RegisterVariableBoolean('IsReady', 'Is Ready', '', 11); $this->EnableAction('IsReady'); - $this->RegisterVariableBoolean('IsRunning', 'Is Running', '', 12); $this->EnableAction('IsRunning'); - $this->RegisterVariableInteger('StateOfCharge', 'State of Charge', '', 13); $this->EnableAction('StateOfCharge'); - $this->RegisterVariableInteger('MinSOC', 'Min SOC', '', 14); $this->EnableAction('MinSOC'); - $this->RegisterVariableInteger('MaxSOC', 'Max SOC', '', 15); $this->EnableAction('MaxSOC'); - // Remote-control (read-only) + /** ------------------------------------------- + * REMOTE CONTROL Variablen (read-only) + * ------------------------------------------*/ $this->RegisterVariableInteger('PowerSetpoint', 'Power Setpoint', '', 20); $this->RegisterVariableString('Strategy', 'Strategy', '', 21); + // Debug $this->RegisterVariableString('RemoteControlPayload', 'Remote Control Payload', '', 30); - // Feedback-Request (read-only) + /** ------------------------------------------- + * FEEDBACK REQUEST Variablen (read-only) + * ------------------------------------------*/ $this->RegisterVariableString('FeedbackRequestPayload', 'Feedback Request Payload', '', 40); } public function ApplyChanges() { parent::ApplyChanges(); - - // Parent verbinden - $this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}'); - - // Subscribes erst, wenn Parent aktiv wird - $this->Subscribed = false; - - // Statusänderungen überwachen - $this->RegisterMessage($this->InstanceID, IM_CHANGESTATUS); - - $parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0; - if ($parent > 0 && IPS_InstanceExists($parent)) { - $this->RegisterMessage($parent, IM_CHANGESTATUS); - } - } - - /* --------------------------------------------------------- - * MESSAGE SINK – reagiert auf Parent-Aktivierung - * ---------------------------------------------------------*/ - public function MessageSink($TimeStamp, $SenderID, $Message, $Data) - { - if ($Message !== IM_CHANGESTATUS) { - return; - } - - $new = $Data[0] ?? 0; - - // Eigene Instanz aktiv - if ($SenderID === $this->InstanceID && $new === IS_ACTIVE) { - $this->TrySubscribe(); - } - - // Parent aktiv - $parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0; - if ($SenderID === $parent && $new === IS_ACTIVE) { - $this->TrySubscribe(); - } - } - - /* --------------------------------------------------------- - * Prüfen ob Parent aktiv ist - * ---------------------------------------------------------*/ - private function HasActiveParent(): bool - { - $parent = @IPS_GetInstance($this->InstanceID)['ConnectionID'] ?? 0; - - if ($parent === 0 || !IPS_InstanceExists($parent)) { - return false; - } - return IPS_GetInstance($parent)['InstanceStatus'] === IS_ACTIVE; - } - - /* --------------------------------------------------------- - * Safe Sender – garantiert keine Fehler mehr - * ---------------------------------------------------------*/ - private function SafeSendToParent(array $packet) - { - if (!$this->HasActiveParent()) { - $this->SendDebug("MQTT", "Parent not active – send skipped", 0); - return; - } - - @ $this->SendDataToParent(json_encode($packet)); - } - - /* --------------------------------------------------------- - * Subscribe (safe) - * ---------------------------------------------------------*/ - private function TrySubscribe() - { - if ($this->Subscribed) { - return; - } - $device = $this->ReadPropertyString('DeviceID'); if ($device === '') { return; } - - $this->Subscribed = true; - + // READ: Topics abonnieren $this->Subscribe("feedback-request/$device"); + // WRITE: Remote-Control-Requests ebenfalls abonnieren $this->Subscribe("remote-control-request/$device"); } - private function Subscribe(string $topic) + /** ------------------------------------------- + * VARIABLE WRITE SUPPORT + * ------------------------------------------*/ + public function RequestAction($Ident, $Value) { - $this->SafeSendToParent([ - 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}', - 'PacketType' => 8, + $this->SetValue($Ident, $Value); + $device = $this->ReadPropertyString('DeviceID'); + if ($device !== '') { + switch ($Ident) { + case 'PowerProduction': + case 'IsReady': + case 'IsRunning': + case 'StateOfCharge': + case 'MinSOC': + case 'MaxSOC': + $payload = json_encode([ + 'power_production' => $this->GetValue('PowerProduction'), + 'is_ready' => $this->GetValue('IsReady'), + 'is_running' => $this->GetValue('IsRunning'), + 'state_of_charge' => $this->GetValue('StateOfCharge'), + 'min_soc' => $this->GetValue('MinSOC'), + 'max_soc' => $this->GetValue('MaxSOC') + ]); + $this->Publish("status/$device", $payload); + break; + } + } + } + + /** ------------------------------------------- + * MQTT SUBSCRIBE + * ------------------------------------------*/ + private function Subscribe(string $topic): void + { + if (!$this->HasActiveParent()) { + $this->SendDebug('MQTT', 'Subscribe ohne aktiven Parent: ' . $topic, 0); + return; + } + $packet = [ + 'PacketType' => 8, // SUBSCRIBE 'QualityOfService' => 0, 'Retain' => false, 'Topic' => $topic, 'Payload' => '' - ]); + ]; + $this->SendDataToParent(json_encode([ + 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' + ] + $packet)); } - /* --------------------------------------------------------- - * Publish (safe) - * ---------------------------------------------------------*/ - private function Publish(string $topic, string $payload) + /** ------------------------------------------- + * MQTT PUBLISH + * ------------------------------------------*/ + private function Publish(string $topic, string $payload): void { - $this->SafeSendToParent([ - 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}', - 'PacketType' => 3, + if (!$this->HasActiveParent()) { + $this->SendDebug('MQTT', 'Publish ohne aktiven Parent: ' . $topic, 0); + return; + } + $packet = [ + 'PacketType' => 3, // PUBLISH 'QualityOfService' => 0, 'Retain' => false, 'Topic' => $topic, 'Payload' => $payload - ]); + ]; + $this->SendDataToParent(json_encode([ + 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' + ] + $packet)); } - /* --------------------------------------------------------- - * RequestAction - * ---------------------------------------------------------*/ - public function RequestAction($Ident, $Value) - { - $this->SetValue($Ident, $Value); - - $device = $this->ReadPropertyString('DeviceID'); - if ($device === '') { - return; - } - - switch ($Ident) { - case 'PowerProduction': - case 'IsReady': - case 'IsRunning': - case 'StateOfCharge': - case 'MinSOC': - case 'MaxSOC': - - $payload = json_encode([ - 'power_production' => $this->GetValue('PowerProduction'), - 'is_ready' => $this->GetValue('IsReady'), - 'is_running' => $this->GetValue('IsRunning'), - 'state_of_charge' => $this->GetValue('StateOfCharge'), - 'min_soc' => $this->GetValue('MinSOC'), - 'max_soc' => $this->GetValue('MaxSOC') - ]); - - $this->Publish("status/$device", $payload); - break; - } - } - - /* --------------------------------------------------------- - * ReceiveData - * ---------------------------------------------------------*/ + /** ------------------------------------------- + * RECEIVE DATA + * ------------------------------------------*/ public function ReceiveData($JSONString) { - $this->TrySubscribe(); - $data = json_decode($JSONString, true); if (!is_array($data)) { return; } - $topic = $data['Topic'] ?? ''; $payload = $data['Payload'] ?? ''; - - $this->SendDebug('MQTT', "Receive: $topic => $payload", 0); - + $this->SendDebug('MQTT', 'Receive: Topic=' . $topic . ' Payload=' . $payload, 0); $device = $this->ReadPropertyString('DeviceID'); if ($device === '') { return; } - - /* --- Feedback Request --- */ + /** ------------------------------------------- + * 1️⃣ FEEDBACK REQUEST + * ------------------------------------------*/ if ($topic === "feedback-request/$device") { - $this->SetValue('FeedbackRequestPayload', $payload); - $json = json_decode($payload, true); - if (!is_array($json)) $json = []; - + if (!is_array($json)) { + $json = []; + } $response = array_merge($json, [ "power_production" => $this->GetValue('PowerProduction'), "is_ready" => $this->GetValue('IsReady'), @@ -227,30 +154,25 @@ class VGT_Sub extends IPSModule "min_soc" => $this->GetValue('MinSOC'), "max_soc" => $this->GetValue('MaxSOC') ]); - $this->Publish("feedback-response/$device", json_encode($response)); return; } - - /* --- Remote Control Request --- */ + /** ------------------------------------------- + * 2️⃣ REMOTE CONTROL REQUEST + * ------------------------------------------*/ if ($topic === "remote-control-request/$device") { - $this->SetValue('RemoteControlPayload', $payload); - $json = json_decode($payload, true); if (is_array($json)) { - if (isset($json['power_setpoint'])) { + if (array_key_exists('power_setpoint', $json)) { $this->SetValue('PowerSetpoint', (int)$json['power_setpoint']); } - if (isset($json['strategy'])) { + if (array_key_exists('strategy', $json)) { $this->SetValue('Strategy', (string)$json['strategy']); } } - $this->Publish("remote-control-response/$device", $payload); return; } } } - -?>