diff --git a/PV_Visu/module.html b/PV_Visu/module.html index 2b86a3b..7aa4fe5 100644 --- a/PV_Visu/module.html +++ b/PV_Visu/module.html @@ -1,25 +1,25 @@ +
-
Produktion
+
Produktion (PV → Verbrauch / Netz)
@@ -27,7 +27,7 @@
-
Verbrauch
+
Verbrauch (PV / Netz)
@@ -37,32 +37,34 @@
\ No newline at end of file + + + diff --git a/PV_Visu/module.php b/PV_Visu/module.php index a8c4b3c..af0ac90 100644 --- a/PV_Visu/module.php +++ b/PV_Visu/module.php @@ -5,80 +5,68 @@ class PV_Visu extends IPSModule public function Create() { parent::Create(); - // Vier Zähler-Variablen + $this->RegisterPropertyInteger('VarProduction', 0); $this->RegisterPropertyInteger('VarConsumption', 0); $this->RegisterPropertyInteger('VarFeedIn', 0); $this->RegisterPropertyInteger('VarGrid', 0); - // HTML-SDK Tile aktivieren - $this->SetVisualizationType(1); + + // Variable zur Übergabe der Daten an das HTML + $this->RegisterVariableString('JSONData', 'Visualisierungsdaten', '', 0); + IPS_SetHidden($this->GetIDForIdent('JSONData'), true); + + $this->SetVisualizationType(1); // HTML SDK Tile } - public function ApplyChanges() + public function ApplyChanges() { parent::ApplyChanges(); - // Auf Änderungen der Zähler-Variablen reagieren + foreach (['VarProduction', 'VarConsumption', 'VarFeedIn', 'VarGrid'] as $prop) { $vid = $this->ReadPropertyInteger($prop); if ($vid > 0) { - // Register event for variable update $this->RegisterMessage($vid, VM_UPDATE); } } + + $this->UpdateData(); // Initial } public function MessageSink($TimeStamp, $SenderID, $Message, $Data) { if ($Message === VM_UPDATE) { - // bei jeder Aktualisierung einer Zählervariable neu senden $this->UpdateData(); } } - /** - * Liefert das HTML-Template und injiziert initiale Daten - */ public function GetVisualizationTile() { - $initialHandling = ''; - - // Füge statisches HTML aus Datei hinzu - $module = file_get_contents(__DIR__ . '/module.html'); - - // Gebe alles zurück. - // Wichtig: $initialHandling nach hinten, da die Funktion handleMessage ja erst im HTML definiert wird - return $module . $initialHandling; - + $initialData = ''; + $html = file_get_contents(__DIR__ . '/module.html'); + return $html . $initialData; } - /** - * Callback vom HTML: sendet frische Daten - */ public function RequestAction($Ident, $Value) { if ($Ident === 'update') { - $this->UpdateData(); - } else { - throw new \UnexpectedValueException("Unknown Ident $Ident"); + return $this->UpdateData(); // Rückgabe für die Visualisierung } + throw new \Exception("Unknown Ident: $Ident"); } - /** - * Aktualisiert Daten und sendet an Tile - */ public function UpdateData() { $start = strtotime('today 00:00'); $end = time(); - $prod = $this->GetDailyTotal($this->ReadPropertyInteger('VarProduction'), $start, $end); - $cons = $this->GetDailyTotal($this->ReadPropertyInteger('VarConsumption'), $start, $end); - $feed = $this->GetDailyTotal($this->ReadPropertyInteger('VarFeedIn'), $start, $end); - $grid = $this->GetDailyTotal($this->ReadPropertyInteger('VarGrid'), $start, $end); + $prod = $this->GetDailyTotal($this->ReadPropertyInteger('VarProduction'), $start, $end); + $cons = $this->GetDailyTotal($this->ReadPropertyInteger('VarConsumption'), $start, $end); + $feed = $this->GetDailyTotal($this->ReadPropertyInteger('VarFeedIn'), $start, $end); + $grid = $this->GetDailyTotal($this->ReadPropertyInteger('VarGrid'), $start, $end); - $prodCons = $prod > 0 ? (($cons-$grid) / $prod) * 100 : 0; + $prodCons = $prod > 0 ? (($cons - $grid) / $prod) * 100 : 0; $prodFeed = $prod > 0 ? 100 - $prodCons : 0; - $consPV = $cons > 0 ? min($prod, ($cons-$grid)) / $cons * 100 : 0; + $consPV = $cons > 0 ? min($prod, ($cons - $grid)) / $cons * 100 : 0; $consGrid = $cons > 0 ? 100 - $consPV : 0; $data = [ @@ -87,34 +75,26 @@ class PV_Visu extends IPSModule 'consPV' => round($consPV, 1), 'consGrid' => round($consGrid, 1), 'value' => [ - 'prod' => round($prod, 2), - 'cons' => round($cons, 2), - 'feed' => round($feed, 2), - 'grid' => round($grid, 2), + 'prod' => round($prod, 2), + 'cons' => round($cons, 2), + 'feed' => round($feed, 2), + 'grid' => round($grid, 2), ], ]; $json = json_encode($data); - $this->UpdateVisualizationValue($json); + SetValueString($this->GetIDForIdent('JSONData'), $json); + return $data; // Wichtig für JS-Aufruf } - /** - * Holt Tages-Aggregat aus dem IPS-Archiv - */ private function GetDailyTotal(int $varID, int $start, int $end) { - if ($varID <= 0) { - return 0.0; - } - $archives = IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}'); - if (empty($archives)) { - return 0.0; - } - $values = AC_GetAggregatedValues($archives[0], $varID, 1, $start, $end, 1); - if (empty($values)) { - return 0.0; - } - // Für Zähler den Summenwert nutzen - return (float)$values[0]['Avg']; + if ($varID <= 0) return 0.0; + + $archiveID = @IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0]; + if (!$archiveID) return 0.0; + + $values = @AC_GetAggregatedValues($archiveID, $varID, 1, $start, $end, 1); + return isset($values[0]['Avg']) ? (float)$values[0]['Avg'] : 0.0; } } \ No newline at end of file