From 1a88308d451558d29a8f3e1787f152854659bb82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fliger?= Date: Wed, 18 Jun 2025 11:50:50 +0200 Subject: [PATCH] no message --- PV_Visu/module.html | 69 +++++++----------- PV_Visu/module.php | 165 ++++++++++++++++++++++---------------------- 2 files changed, 108 insertions(+), 126 deletions(-) diff --git a/PV_Visu/module.html b/PV_Visu/module.html index 1ef797c..731e189 100644 --- a/PV_Visu/module.html +++ b/PV_Visu/module.html @@ -3,57 +3,40 @@ -
-
-
-
+
+
+
+
+
+
+
+
+
+
+
- -
-
-
-
-
- \ No newline at end of file diff --git a/PV_Visu/module.php b/PV_Visu/module.php index 71d4ad9..f8ad573 100644 --- a/PV_Visu/module.php +++ b/PV_Visu/module.php @@ -1,84 +1,32 @@ RegisterPropertyInteger('VarProduction', 0); $this->RegisterPropertyInteger('VarConsumption', 0); $this->RegisterPropertyInteger('VarFeedIn', 0); $this->RegisterPropertyInteger('VarGrid', 0); + // HTML-SDK Tile aktivieren $this->SetVisualizationType(3); } - public function ApplyChanges() + public function ApplyChanges(): void { parent::ApplyChanges(); } - -public function GetVisualizationTile() -{ - // 1) Aktuelle Tagesdaten berechnen (gleiche Logik wie in 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); - - $prodCons = $prod > 0 ? $cons / $prod * 100 : 0; - $prodFeed = $prod > 0 ? $feed / $prod * 100 : 0; - $consPV = $cons > 0 ? min($prod, $cons) / $cons * 100 : 0; - $consGrid = $cons > 0 ? $grid / $cons * 100 : 0; - - $initialData = [ - 'prodCons' => round($prodCons, 1), - 'prodFeed' => round($prodFeed, 1), - '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), - ], - ]; - - // 2) JS-Snippet zum Injizieren der initialen Daten - $message = ''; - - // 3) Modul-HTML laden - $html = file_get_contents(__DIR__ . '/module.html'); - if ($html === false) { - $this->LogMessage("module.html nicht gefunden", KL_ERROR); - return ''; - } - - // 4) HTML + initiales Daten-Script zurückliefern - return $html . $message; -} /** - * Callback aus dem HTML: Daten neu berechnen und senden + * Liefert das HTML-Template und injiziert initiale Daten per handleMessage */ - public function RequestAction($Ident, $Value) - { - if ($Ident === 'update') { - $this->UpdateData(); - } else { - throw new Exception("Unknown Ident"); - } - } - - /** - * Tägliche Summen holen, Quoten berechnen und ans Frontend senden - */ - public function UpdateData() + public function GetVisualizationTile(): string { + // Tageswerte berechnen $start = strtotime('today 00:00'); $end = time(); @@ -92,48 +40,99 @@ public function GetVisualizationTile() $feed = $this->GetDailyTotal($feedID, $start, $end); $grid = $this->GetDailyTotal($gridID, $start, $end); - // Quoten in Prozent - $prodCons = ($prod > 0) ? ($cons / $prod) * 100 : 0; - $prodFeed = ($prod > 0) ? ($feed / $prod) * 100 : 0; - $consPV = ($cons > 0) ? min($prod, $cons) / $cons * 100 : 0; - $consGrid = ($cons > 0) ? ($grid / $cons) * 100 : 0; + // Prozent-Quoten + $prodCons = $prod > 0 ? ($cons / $prod) * 100 : 0; + $prodFeed = $prod > 0 ? ($feed / $prod) * 100 : 0; + $consPV = $cons > 0 ? min($prod, $cons) / $cons * 100 : 0; + $consGrid = $cons > 0 ? ($grid / $cons) * 100 : 0; + + $initialData = [ + 'prodCons' => round($prodCons, 1), + 'prodFeed' => round($prodFeed, 1), + '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), + ], + ]; + + // Script zur Injektion der initialen Daten + $message = ''; + + // HTML-Template laden + $htmlPath = __DIR__ . '/module.html'; + if (!file_exists($htmlPath)) { + $this->LogMessage("module.html nicht gefunden in $htmlPath", KL_ERROR); + return ''; + } + $html = file_get_contents($htmlPath); + + return $html . $message; + } + + /** + * Callback vom HTML: sendet frische Daten + */ + public function RequestAction($Ident, $Value): void + { + if ($Ident === 'update') { + $this->UpdateData(); + } else { + throw new \UnexpectedValueException("Unknown Ident $Ident"); + } + } + + /** + * Aktualisiert Daten und sendet an Tile + */ + public function UpdateData(): void + { + // identisch zu GetVisualizationTile-Berechnung + $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); + + $prodCons = $prod > 0 ? ($cons / $prod) * 100 : 0; + $prodFeed = $prod > 0 ? ($feed / $prod) * 100 : 0; + $consPV = $cons > 0 ? min($prod, $cons) / $cons * 100 : 0; + $consGrid = $cons > 0 ? ($grid / $cons) * 100 : 0; $data = [ 'prodCons' => round($prodCons, 1), 'prodFeed' => round($prodFeed, 1), - 'consPV' => round($consPV, 1), + '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), + ], ]; $this->UpdateVisualizationValue($data); } /** - * Aggregierte Tageswerte aus dem Archiv + * Holt Tages-Aggregat aus dem IPS-Archiv */ - private function GetDailyTotal(int $varID, int $start, int $end) + private function GetDailyTotal(int $varID, int $start, int $end): float { if ($varID <= 0) { return 0.0; } - // Erstes Archivmodul finden $archives = IPS_GetInstanceListByModuleID('{43192F11-5B02-4B5D-9B53-8B4DBD4769E9}'); if (empty($archives)) { return 0.0; } - $archiveID = $archives[0]; - $values = AC_GetAggregatedValues($archiveID, $varID, 1, $start, $end, 1); - if (empty($values)) { - return 0.0; - } - return (float)$values[0]['Avg']; + $values = AC_GetAggregatedValues($archives[0], $varID, 1, $start, $end, 1); + return empty($values) ? 0.0 : (float) $values[0]['Avg']; } - -} -?> \ No newline at end of file +} \ No newline at end of file