no message

This commit is contained in:
dh
2025-07-03 15:39:55 +02:00
parent 0a60647df0
commit ed3dc27878
2 changed files with 64 additions and 82 deletions
+29 -27
View File
@@ -1,25 +1,25 @@
<!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<style> <style>
html, body { margin: 0; padding: 4px; background: transparent; } html, body { margin: 0; padding: 8px; background: transparent; font-family: sans-serif; color: #ffffff; }
#pv_visu { padding-top: 10px; }
.bar-block { margin-bottom: 20px; } .bar-block { margin-bottom: 20px; }
.bar-title { font-size: 1.4em; font-weight: bold; margin-bottom: 4px; color: #ffffff; } .bar-title { font-size: 1.2em; font-weight: bold; margin-bottom: 6px; }
.bar-container { width: 100%; background: #eee; border-radius: 4px; overflow: hidden; height: 24px; position: relative; } .bar-container { width: 100%; background: #ddd; border-radius: 4px; overflow: hidden; height: 24px; position: relative; }
.bar { height: 100%; float: left; 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 span { position: absolute; width: 100%; text-align: center; line-height: 24px; font-size: 0.8em; color: #fff; }
.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; }
.value-text { font-size: 1.2em; margin-top: 4px; color: #ffffff; } .value-text { font-size: 0.95em; margin-top: 4px; }
</style> </style>
</head> </head>
<body> <body>
<div id="pv_visu"> <div id="pv_visu">
<div class="bar-block"> <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-container">
<div class="bar bar-cons" id="barCons"><span id="barConsText"></span></div> <div class="bar bar-cons" id="barCons"><span id="barConsText"></span></div>
<div class="bar bar-feed" id="barFeed"><span id="barFeedText"></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 class="value-text" id="prodValues"></div>
</div> </div>
<div class="bar-block"> <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-container">
<div class="bar bar-pv" id="barPV"><span id="barPVText"></span></div> <div class="bar bar-pv" id="barPV"><span id="barPVText"></span></div>
<div class="bar bar-grid" id="barGrid"><span id="barGridText"></span></div> <div class="bar bar-grid" id="barGrid"><span id="barGridText"></span></div>
@@ -37,32 +37,34 @@
</div> </div>
<script> <script>
function Apply(data) { function Apply(data) {
// Set widths
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('barPV').style.width = data.consPV + '%'; document.getElementById('barPV').style.width = data.consPV + '%';
document.getElementById('barGrid').style.width = data.consGrid + '%'; document.getElementById('barGrid').style.width = data.consGrid + '%';
// Set text inside bars
document.getElementById('barConsText').innerText = data.prodCons + '%'; document.getElementById('barConsText').innerText = data.prodCons + '%';
document.getElementById('barFeedText').innerText = data.prodFeed + '%'; document.getElementById('barFeedText').innerText = data.prodFeed + '%';
document.getElementById('barPVText').innerText = data.consPV + '%'; document.getElementById('barPVText').innerText = data.consPV + '%';
document.getElementById('barGridText').innerText = data.consGrid + '%'; document.getElementById('barGridText').innerText = data.consGrid + '%';
// Set detailed values below
document.getElementById('prodValues').innerText = document.getElementById('prodValues').innerText =
'Eigenverbrauch: ' + data.consPV/100*data.value.cons + ' kWh, ' + 'Gesamt: ' + data.value.prod + ' kWh, davon Eigenverbrauch: ' + (data.consPV/100*data.value.cons).toFixed(2) + ' kWh, Einspeisung: ' + data.value.feed + ' kWh';
'Einspeisung: ' + data.value.feed + ' kWh';
document.getElementById('consValues').innerText = document.getElementById('consValues').innerText =
'PV-Anteil: ' + data.consPV/100*data.value.cons + ' kWh, ' + 'Gesamt: ' + data.value.cons + ' kWh, davon PV: ' + (data.consPV/100*data.value.cons).toFixed(2) + ' kWh, Netz: ' + data.value.grid + ' kWh';
'Netz-Anteil: ' + data.value.grid + ' kWh';
} }
function handleMessage(msg) { function handleMessage(msg) {
try { try {
var data = (typeof msg === 'string') ? JSON.parse(msg) : msg; const data = typeof msg === 'string' ? JSON.parse(msg) : msg;
Apply(data); Apply(data);
} catch(e) { } catch (e) {
console.error('PV_Visu handleMessage error:', e, msg); console.error('Fehler beim Verarbeiten der Daten:', e, msg);
} }
} if (typeof registerMessageHandler === 'function') { }
if (typeof registerMessageHandler === 'function') {
registerMessageHandler(handleMessage); registerMessageHandler(handleMessage);
} }
</script> </script>
</body>
</html>
+35 -55
View File
@@ -5,80 +5,68 @@ 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(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(); parent::ApplyChanges();
// Auf Änderungen der Zähler-Variablen reagieren
foreach (['VarProduction', 'VarConsumption', 'VarFeedIn', 'VarGrid'] as $prop) { foreach (['VarProduction', 'VarConsumption', 'VarFeedIn', 'VarGrid'] as $prop) {
$vid = $this->ReadPropertyInteger($prop); $vid = $this->ReadPropertyInteger($prop);
if ($vid > 0) { if ($vid > 0) {
// Register event for variable update
$this->RegisterMessage($vid, VM_UPDATE); $this->RegisterMessage($vid, VM_UPDATE);
} }
} }
$this->UpdateData(); // Initial
} }
public function MessageSink($TimeStamp, $SenderID, $Message, $Data) public function MessageSink($TimeStamp, $SenderID, $Message, $Data)
{ {
if ($Message === VM_UPDATE) { if ($Message === VM_UPDATE) {
// bei jeder Aktualisierung einer Zählervariable neu senden
$this->UpdateData(); $this->UpdateData();
} }
} }
/**
* Liefert das HTML-Template und injiziert initiale Daten
*/
public function GetVisualizationTile() public function GetVisualizationTile()
{ {
$initialHandling = '<script>handleMessage(' . json_encode($this->UpdateData()) . ');</script>'; $initialData = '<script>handleMessage(' . json_encode($this->UpdateData()) . ');</script>';
$html = file_get_contents(__DIR__ . '/module.html');
// Füge statisches HTML aus Datei hinzu return $html . $initialData;
$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;
} }
/**
* Callback vom HTML: sendet frische Daten
*/
public function RequestAction($Ident, $Value) public function RequestAction($Ident, $Value)
{ {
if ($Ident === 'update') { if ($Ident === 'update') {
$this->UpdateData(); return $this->UpdateData(); // Rückgabe für die Visualisierung
} else {
throw new \UnexpectedValueException("Unknown Ident $Ident");
} }
throw new \Exception("Unknown Ident: $Ident");
} }
/**
* Aktualisiert Daten und sendet an Tile
*/
public function UpdateData() public function UpdateData()
{ {
$start = strtotime('today 00:00'); $start = strtotime('today 00:00');
$end = time(); $end = time();
$prod = $this->GetDailyTotal($this->ReadPropertyInteger('VarProduction'), $start, $end); $prod = $this->GetDailyTotal($this->ReadPropertyInteger('VarProduction'), $start, $end);
$cons = $this->GetDailyTotal($this->ReadPropertyInteger('VarConsumption'), $start, $end); $cons = $this->GetDailyTotal($this->ReadPropertyInteger('VarConsumption'), $start, $end);
$feed = $this->GetDailyTotal($this->ReadPropertyInteger('VarFeedIn'), $start, $end); $feed = $this->GetDailyTotal($this->ReadPropertyInteger('VarFeedIn'), $start, $end);
$grid = $this->GetDailyTotal($this->ReadPropertyInteger('VarGrid'), $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; $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; $consGrid = $cons > 0 ? 100 - $consPV : 0;
$data = [ $data = [
@@ -87,34 +75,26 @@ class PV_Visu extends IPSModule
'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),
], ],
]; ];
$json = json_encode($data); $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) private function GetDailyTotal(int $varID, int $start, int $end)
{ {
if ($varID <= 0) { if ($varID <= 0) return 0.0;
return 0.0;
} $archiveID = @IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0];
$archives = IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}'); if (!$archiveID) return 0.0;
if (empty($archives)) {
return 0.0; $values = @AC_GetAggregatedValues($archiveID, $varID, 1, $start, $end, 1);
} return isset($values[0]['Avg']) ? (float)$values[0]['Avg'] : 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'];
} }
} }