From f3119e7e30b1f78e2648ba1f2eb17073f8257821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fliger?= Date: Wed, 18 Jun 2025 11:33:19 +0200 Subject: [PATCH] no message --- PV_Visu/module.php | 84 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/PV_Visu/module.php b/PV_Visu/module.php index ec41fbf..152bfe1 100644 --- a/PV_Visu/module.php +++ b/PV_Visu/module.php @@ -17,5 +17,89 @@ class PV_Visu extends IPSModule parent::ApplyChanges(); } + + public function GetVisualizationTile(int $InstanceID): string + { + $file = __DIR__ . '/module.html'; + if (!file_exists($file)) { + $this->LogMessage("module.html nicht gefunden in $file", KL_ERROR); + return ''; + } + return $this->Translate(file_get_contents($file)); + } + + /** + * Callback aus dem HTML: Daten neu berechnen und senden + */ + public function RequestAction(string $Ident, $Value): void + { + if ($Ident === 'update') { + $this->UpdateData(); + } else { + throw new Exception("Unknown Ident"); + } + } + + /** + * Tägliche Summen holen, Quoten berechnen und ans Frontend senden + */ + protected function UpdateData(): void + { + $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); + + // 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; + + $data = [ + '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) + ] + ]; + + $this->UpdateVisualizationValue($data); + } + + /** + * Aggregierte Tageswerte aus dem Archiv + */ + 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']; + } + } ?> \ No newline at end of file