From c3796d0800696c80549f95e8e6d3f33445e78fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fliger?= Date: Tue, 24 Jun 2025 11:27:43 +0200 Subject: [PATCH] no message --- PV_Visu/module.html | 14 ++++++++++-- PV_Visu/module.php | 52 ++++++++++++++++++++------------------------- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/PV_Visu/module.html b/PV_Visu/module.html index 0f1b3b6..7e32f63 100644 --- a/PV_Visu/module.html +++ b/PV_Visu/module.html @@ -35,8 +35,18 @@ document.getElementById('barPV').style.width = data.consPV + '%'; document.getElementById('barGrid').style.width = data.consGrid + '%'; } - function handleMessage(msg) { Apply(msg); } - // Kein requestAction-Aufruf hier nötig + function handleMessage(msg) { + try { + var data = (typeof msg === 'string') ? JSON.parse(msg) : msg; + Apply(data); + } catch(e) { + console.error('PV_Visu handleMessage error:', e, msg); + } + } + // Bind to HTML-SDK + if (typeof registerMessageHandler === 'function') { + registerMessageHandler(handleMessage); + } \ No newline at end of file diff --git a/PV_Visu/module.php b/PV_Visu/module.php index ad4d55e..56a660f 100644 --- a/PV_Visu/module.php +++ b/PV_Visu/module.php @@ -2,6 +2,7 @@ declare(strict_types=1); + class PV_Visu extends IPSModule { public function Create() @@ -16,30 +17,23 @@ class PV_Visu extends IPSModule $this->SetVisualizationType(3); } - public function ApplyChanges() + public function ApplyChanges(): void { parent::ApplyChanges(); - $this->RequestAction("update", 1); } /** - * Liefert das HTML-Template und injiziert initiale Tagesdaten + * Liefert das HTML-Template und injiziert initiale Daten */ - public function GetVisualizationTile() + public function GetVisualizationTile(): string { - // Tageswerte berechnen $start = strtotime('today 00:00'); $end = time(); - $prodID = $this->ReadPropertyInteger('VarProduction'); - $consID = $this->ReadPropertyInteger('VarConsumption'); - $feedID = $this->ReadPropertyInteger('VarFeedIn'); - $gridID = $this->ReadPropertyInteger('VarGrid'); - - $prod = $this->GetDailyTotal($prodID, $start, $end); - $cons = $this->GetDailyTotal($consID, $start, $end); - $feed = $this->GetDailyTotal($feedID, $start, $end); - $grid = $this->GetDailyTotal($gridID, $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); // Prozent-Quoten $prodCons = $prod > 0 ? ($cons / $prod) * 100 : 0; @@ -47,7 +41,7 @@ class PV_Visu extends IPSModule $consPV = $cons > 0 ? min($prod, $cons) / $cons * 100 : 0; $consGrid = $cons > 0 ? ($grid / $cons) * 100 : 0; - $initialData = [ + $data = [ 'prodCons' => round($prodCons, 1), 'prodFeed' => round($prodFeed, 1), 'consPV' => round($consPV, 1), @@ -60,24 +54,23 @@ class PV_Visu extends IPSModule ], ]; - // Script zur Injektion der initialen Daten - $message = ''; + // Daten als JSON-String übergeben + $json = json_encode($data); + $this->UpdateVisualizationValue($json); - // HTML-Template laden + // HTML-Template laden und zurückgeben $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; + return file_get_contents($htmlPath); } /** * Callback vom HTML: sendet frische Daten */ - public function RequestAction($Ident, $Value) + public function RequestAction($Ident, $Value): void { if ($Ident === 'update') { $this->UpdateData(); @@ -89,15 +82,15 @@ class PV_Visu extends IPSModule /** * Aktualisiert Daten und sendet an Tile */ - public function UpdateData() + public function UpdateData(): void { $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 / $prod) * 100 : 0; $prodFeed = $prod > 0 ? ($feed / $prod) * 100 : 0; @@ -117,13 +110,14 @@ class PV_Visu extends IPSModule ], ]; - $this->UpdateVisualizationValue($data); + $json = json_encode($data); + $this->UpdateVisualizationValue($json); } /** * 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;