no message

This commit is contained in:
2025-06-18 11:50:50 +02:00
parent f0ea20a36c
commit 1a88308d45
2 changed files with 108 additions and 126 deletions

View File

@@ -3,57 +3,40 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<style> <style>
.bar-container { html, body { margin: 0; padding: 4px; background: transparent; }
width: 100%; .bar-container { width: 100%; background: #eee; border-radius: 4px; overflow: hidden; height: 20px; margin-bottom: 10px; }
background: #eee;
border-radius: 4px;
overflow: hidden;
height: 18px;
margin-bottom: 8px;
}
.bar { height: 100%; float: left; } .bar { height: 100%; float: left; }
.bar-cons { background: #4CAF50; } .bar-cons { background: #4CAF50; }
.bar-feed { background: #8BC34A; } .bar-feed { background: #8BC34A; }
.bar-pv { background: #FF9800; } .bar-pv { background: #FF9800; }
.bar-grid { background: #FF5722; } .bar-grid { background: #FF5722; }
.label { .label { font-size: 0.9em; margin-bottom: 4px; color: #333; }
font-size: 0.9em;
margin: 4px 0;
}
</style> </style>
</head> </head>
<body> <body>
<div class="label" id="prodLabel"></div> <div id="pv_visu">
<div class="bar-container"> <div class="label" id="prodLabel"></div>
<div class="bar bar-cons" id="barCons"></div> <div class="bar-container">
<div class="bar bar-feed" id="barFeed"></div> <div class="bar bar-cons" id="barCons"></div>
<div class="bar bar-feed" id="barFeed"></div>
</div>
<div class="label" id="consLabel"></div>
<div class="bar-container">
<div class="bar bar-pv" id="barPV"></div>
<div class="bar bar-grid" id="barGrid"></div>
</div>
</div> </div>
<div class="label" id="consLabel"></div>
<div class="bar-container">
<div class="bar bar-pv" id="barPV"></div>
<div class="bar bar-grid" id="barGrid"></div>
</div>
<script> <script>
function Apply(data) { function Apply(data) {
document.getElementById('prodLabel').innerText = document.getElementById('prodLabel').innerText = 'Produktion: ' + data.value.prod + ' kWh';
"Produktion: " + data.value.prod + " kWh"; document.getElementById('barCons').style.width = data.prodCons + '%';
document.getElementById('barCons').style.width = data.prodCons + "%"; document.getElementById('barFeed').style.width = data.prodFeed + '%';
document.getElementById('barFeed').style.width = data.prodFeed + "%"; document.getElementById('consLabel').innerText = 'Verbrauch: ' + data.value.cons + ' kWh';
document.getElementById('barPV').style.width = data.consPV + '%';
document.getElementById('consLabel').innerText = document.getElementById('barGrid').style.width = data.consGrid + '%';
"Verbrauch: " + data.value.cons + " kWh";
document.getElementById('barPV').style.width = data.consPV + "%";
document.getElementById('barGrid').style.width = data.consGrid + "%";
} }
function handleMessage(msg) { Apply(msg); }
function handleMessage(msg) { // Kein initialer requestAction nötig, weil GetVisualizationTile bereits Daten injiziert
if (msg) Apply(msg);
}
// Initial-Update anstoßen
requestAction('update', true);
</script> </script>
</body> </body>
</html> </html>

View File

@@ -1,84 +1,32 @@
<?php <?php
declare(strict_types=1);
class PV_Visu extends IPSModule class PV_Visu extends IPSModule
{ {
public function Create() public function Create()
{ {
parent::Create(); parent::Create();
// Vier Zähler-Variablen
$this->RegisterPropertyInteger('VarProduction', 0); $this->RegisterPropertyInteger('VarProduction', 0);
$this->RegisterPropertyInteger('VarConsumption', 0); $this->RegisterPropertyInteger('VarConsumption', 0);
$this->RegisterPropertyInteger('VarFeedIn', 0); $this->RegisterPropertyInteger('VarFeedIn', 0);
$this->RegisterPropertyInteger('VarGrid', 0); $this->RegisterPropertyInteger('VarGrid', 0);
// HTML-SDK Tile aktivieren
$this->SetVisualizationType(3); $this->SetVisualizationType(3);
} }
public function ApplyChanges() public function ApplyChanges(): void
{ {
parent::ApplyChanges(); 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 = '<script>'
. 'handleMessage(' . json_encode($initialData) . ');'
. '</script>';
// 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) public function GetVisualizationTile(): string
{
if ($Ident === 'update') {
$this->UpdateData();
} else {
throw new Exception("Unknown Ident");
}
}
/**
* Tägliche Summen holen, Quoten berechnen und ans Frontend senden
*/
public function UpdateData()
{ {
// Tageswerte berechnen
$start = strtotime('today 00:00'); $start = strtotime('today 00:00');
$end = time(); $end = time();
@@ -92,48 +40,99 @@ public function GetVisualizationTile()
$feed = $this->GetDailyTotal($feedID, $start, $end); $feed = $this->GetDailyTotal($feedID, $start, $end);
$grid = $this->GetDailyTotal($gridID, $start, $end); $grid = $this->GetDailyTotal($gridID, $start, $end);
// Quoten in Prozent // Prozent-Quoten
$prodCons = ($prod > 0) ? ($cons / $prod) * 100 : 0; $prodCons = $prod > 0 ? ($cons / $prod) * 100 : 0;
$prodFeed = ($prod > 0) ? ($feed / $prod) * 100 : 0; $prodFeed = $prod > 0 ? ($feed / $prod) * 100 : 0;
$consPV = ($cons > 0) ? min($prod, $cons) / $cons * 100 : 0; $consPV = $cons > 0 ? min($prod, $cons) / $cons * 100 : 0;
$consGrid = ($cons > 0) ? ($grid / $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 = '<script>handleMessage(' . json_encode($initialData) . ');</script>';
// 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 = [ $data = [
'prodCons' => round($prodCons, 1), 'prodCons' => round($prodCons, 1),
'prodFeed' => round($prodFeed, 1), 'prodFeed' => round($prodFeed, 1),
'consPV' => round($consPV, 1), 'consPV' => round($consPV, 1),
'consGrid' => round($consGrid, 1), 'consGrid' => round($consGrid, 1),
'value' => [ 'value' => [
'prod' => round($prod, 2), 'prod' => round($prod, 2),
'cons' => round($cons, 2), 'cons' => round($cons, 2),
'feed' => round($feed, 2), 'feed' => round($feed, 2),
'grid' => round($grid, 2) 'grid' => round($grid, 2),
] ],
]; ];
$this->UpdateVisualizationValue($data); $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) { if ($varID <= 0) {
return 0.0; return 0.0;
} }
// Erstes Archivmodul finden
$archives = IPS_GetInstanceListByModuleID('{43192F11-5B02-4B5D-9B53-8B4DBD4769E9}'); $archives = IPS_GetInstanceListByModuleID('{43192F11-5B02-4B5D-9B53-8B4DBD4769E9}');
if (empty($archives)) { if (empty($archives)) {
return 0.0; return 0.0;
} }
$archiveID = $archives[0]; $values = AC_GetAggregatedValues($archives[0], $varID, 1, $start, $end, 1);
$values = AC_GetAggregatedValues($archiveID, $varID, 1, $start, $end, 1); return empty($values) ? 0.0 : (float) $values[0]['Avg'];
if (empty($values)) {
return 0.0;
}
return (float)$values[0]['Avg'];
} }
}
}
?>