no message

This commit is contained in:
2025-07-03 15:39:55 +02:00
parent 0a60647df0
commit ed3dc27878
2 changed files with 64 additions and 82 deletions

View File

@@ -1,25 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
html, body { margin: 0; padding: 4px; background: transparent; }
#pv_visu { padding-top: 10px; }
html, body { margin: 0; padding: 8px; background: transparent; font-family: sans-serif; color: #ffffff; }
.bar-block { margin-bottom: 20px; }
.bar-title { font-size: 1.4em; font-weight: bold; margin-bottom: 4px; color: #ffffff; }
.bar-container { width: 100%; background: #eee; border-radius: 4px; overflow: hidden; height: 24px; position: relative; }
.bar-title { font-size: 1.2em; font-weight: bold; margin-bottom: 6px; }
.bar-container { width: 100%; background: #ddd; border-radius: 4px; overflow: hidden; height: 24px; position: relative; }
.bar { height: 100%; float: left; position: relative; }
.bar span { position: absolute; width: 100%; text-align: center; line-height: 24px; font-size: 0.8em; color: #fff; }
.bar-cons { background: #4CAF50; }
.bar-feed { background: #8BC34A; }
.bar-pv { background: #FF9800; }
.bar-grid { background: #FF5722; }
.value-text { font-size: 1.2em; margin-top: 4px; color: #ffffff; }
.bar-cons { background: #4CAF50; }
.bar-feed { background: #8BC34A; }
.bar-pv { background: #FF9800; }
.bar-grid { background: #FF5722; }
.value-text { font-size: 0.95em; margin-top: 4px; }
</style>
</head>
<body>
<div id="pv_visu">
<div class="bar-block">
<div class="bar-title">Produktion</div>
<div class="bar-title">Produktion (PV → Verbrauch / Netz)</div>
<div class="bar-container">
<div class="bar bar-cons" id="barCons"><span id="barConsText"></span></div>
<div class="bar bar-feed" id="barFeed"><span id="barFeedText"></span></div>
@@ -27,7 +27,7 @@
<div class="value-text" id="prodValues"></div>
</div>
<div class="bar-block">
<div class="bar-title">Verbrauch</div>
<div class="bar-title">Verbrauch (PV / Netz)</div>
<div class="bar-container">
<div class="bar bar-pv" id="barPV"><span id="barPVText"></span></div>
<div class="bar bar-grid" id="barGrid"><span id="barGridText"></span></div>
@@ -37,32 +37,34 @@
</div>
<script>
function Apply(data) {
// Set widths
document.getElementById('barCons').style.width = data.prodCons + '%';
document.getElementById('barFeed').style.width = data.prodFeed + '%';
document.getElementById('barPV').style.width = data.consPV + '%';
document.getElementById('barGrid').style.width = data.consGrid + '%';
// Set text inside bars
document.getElementById('barConsText').innerText = data.prodCons + '%';
document.getElementById('barFeedText').innerText = data.prodFeed + '%';
document.getElementById('barPVText').innerText = data.consPV + '%';
document.getElementById('barGridText').innerText = data.consGrid + '%';
// Set detailed values below
document.getElementById('barConsText').innerText = data.prodCons + '%';
document.getElementById('barFeedText').innerText = data.prodFeed + '%';
document.getElementById('barPVText').innerText = data.consPV + '%';
document.getElementById('barGridText').innerText = data.consGrid + '%';
document.getElementById('prodValues').innerText =
'Eigenverbrauch: ' + data.consPV/100*data.value.cons + ' kWh, ' +
'Einspeisung: ' + data.value.feed + ' kWh';
'Gesamt: ' + data.value.prod + ' kWh, davon Eigenverbrauch: ' + (data.consPV/100*data.value.cons).toFixed(2) + ' kWh, Einspeisung: ' + data.value.feed + ' kWh';
document.getElementById('consValues').innerText =
'PV-Anteil: ' + data.consPV/100*data.value.cons + ' kWh, ' +
'Netz-Anteil: ' + data.value.grid + ' kWh';
'Gesamt: ' + data.value.cons + ' kWh, davon PV: ' + (data.consPV/100*data.value.cons).toFixed(2) + ' kWh, Netz: ' + data.value.grid + ' kWh';
}
function handleMessage(msg) {
try {
var data = (typeof msg === 'string') ? JSON.parse(msg) : msg;
const data = typeof msg === 'string' ? JSON.parse(msg) : msg;
Apply(data);
} catch(e) {
console.error('PV_Visu handleMessage error:', e, msg);
} catch (e) {
console.error('Fehler beim Verarbeiten der Daten:', e, msg);
}
} if (typeof registerMessageHandler === 'function') {
}
if (typeof registerMessageHandler === 'function') {
registerMessageHandler(handleMessage);
}
</script>
</script>
</body>
</html>

View File

@@ -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 = '<script>handleMessage(' . json_encode($this->UpdateData()) . ');</script>';
// 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 = '<script>handleMessage(' . json_encode($this->UpdateData()) . ');</script>';
$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;
}
}