This commit is contained in:
2025-06-24 14:16:28 +02:00
parent af7497466d
commit 56485a73eb
2 changed files with 53 additions and 56 deletions

View File

@@ -14,7 +14,6 @@
</style> </style>
</head> </head>
<body> <body>
Hallo
<div id="pv_visu"> <div id="pv_visu">
<div class="label" id="prodLabel"></div> <div class="label" id="prodLabel"></div>
<div class="bar-container"> <div class="bar-container">
@@ -38,14 +37,14 @@
} }
function handleMessage(msg) { function handleMessage(msg) {
try { try {
var data = (typeof msg === 'string') ? JSON.parse(msg) : msg; var d = (typeof msg === 'string') ? JSON.parse(msg) : msg;
Apply(data); Apply(d);
} catch(e) { } catch (e) {
console.error('PV_Visu handleMessage error:', e, msg); console.error('PV_Visu handleMessage error', e, msg);
} }
} }
// Bind to HTML-SDK // Register HTML-SDK handler
if (typeof registerMessageHandler === 'function') { if (typeof registerMessageHandler === 'function') {
registerMessageHandler(handleMessage); registerMessageHandler(handleMessage);
} }
</script> </script>

View File

@@ -2,7 +2,6 @@
declare(strict_types=1); declare(strict_types=1);
class PV_Visu extends IPSModule class PV_Visu extends IPSModule
{ {
public function Create() public function Create()
@@ -14,13 +13,36 @@ class PV_Visu extends IPSModule
$this->RegisterPropertyInteger('VarFeedIn', 0); $this->RegisterPropertyInteger('VarFeedIn', 0);
$this->RegisterPropertyInteger('VarGrid', 0); $this->RegisterPropertyInteger('VarGrid', 0);
// HTML-SDK Tile aktivieren // HTML-SDK Tile aktivieren
$this->SetVisualizationType(1); $this->SetVisualizationType(3);
} }
public function ApplyChanges(): void
public function ApplyChanges(): void
{ {
parent::ApplyChanges(); parent::ApplyChanges();
$this->GetVisualizationTile(); // 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);
}
}
}
public function MessageSink(int $TimeStamp, int $SenderID, int $Message, $Data): void
{
if ($Message === VM_UPDATE) {
// bei jeder Aktualisierung einer Zählervariable neu senden
$this->UpdateData();
}
}(int $TimeStamp, int $SenderID, int $Message, $Data): void
{
if ($Message === VM_UPDATE) {
// bei jeder Aktualisierung einer Zählervariable neu senden
$this->UpdateData();
}
} }
/** /**
@@ -28,38 +50,9 @@ class PV_Visu extends IPSModule
*/ */
public function GetVisualizationTile(): string public function GetVisualizationTile(): string
{ {
$start = strtotime('today 00:00'); // Initial senden
$end = time(); $this->UpdateData();
// HTML laden
$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;
$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),
],
];
// Daten als JSON-String übergeben
$json = json_encode($data);
$this->UpdateVisualizationValue($json);
// HTML-Template laden und zurückgeben
$htmlPath = __DIR__ . '/module.html'; $htmlPath = __DIR__ . '/module.html';
if (!file_exists($htmlPath)) { if (!file_exists($htmlPath)) {
$this->LogMessage("module.html nicht gefunden in $htmlPath", KL_ERROR); $this->LogMessage("module.html nicht gefunden in $htmlPath", KL_ERROR);
@@ -69,9 +62,9 @@ class PV_Visu extends IPSModule
} }
/** /**
* Callback vom HTML: sendet frische Daten * Callback vom HTML: frische Daten senden
*/ */
public function RequestAction($Ident, $Value): void public function RequestAction(string $Ident, $Value): void
{ {
if ($Ident === 'update') { if ($Ident === 'update') {
$this->UpdateData(); $this->UpdateData();
@@ -81,21 +74,21 @@ class PV_Visu extends IPSModule
} }
/** /**
* Aktualisiert Daten und sendet an Tile * Berechnet Tagesdaten und schickt sie an die Visualisierung
*/ */
public function UpdateData(): void public function UpdateData(): void
{ {
$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 / $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;
$data = [ $data = [
@@ -111,12 +104,12 @@ class PV_Visu extends IPSModule
], ],
]; ];
$json = json_encode($data); // Als JSON-String senden
$this->UpdateVisualizationValue($json); $this->UpdateVisualizationValue(json_encode($data));
} }
/** /**
* Holt Tages-Aggregat aus dem IPS-Archiv * Holt aggregierte Tageswerte (Summe) aus dem Archiv
*/ */
private function GetDailyTotal(int $varID, int $start, int $end): float private function GetDailyTotal(int $varID, int $start, int $end): float
{ {
@@ -128,6 +121,11 @@ class PV_Visu extends IPSModule
return 0.0; return 0.0;
} }
$values = AC_GetAggregatedValues($archives[0], $varID, 1, $start, $end, 1); $values = AC_GetAggregatedValues($archives[0], $varID, 1, $start, $end, 1);
return empty($values) ? 0.0 : (float) $values[0]['Avg']; if (empty($values)) {
return 0.0;
}
// Für Zähler den Summenwert nutzen
return (float)$values[0]['Sum'];
} }
} }
?>