no message

This commit is contained in:
dh
2025-06-24 14:20:32 +02:00
parent 9a24283330
commit b4a9bd12db
+65 -57
View File
@@ -2,6 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
class PV_Visu extends IPSModule class PV_Visu extends IPSModule
{ {
public function Create() public function Create()
@@ -16,61 +17,16 @@ class PV_Visu extends IPSModule
$this->SetVisualizationType(1); $this->SetVisualizationType(1);
} }
public function ApplyChanges(): void public function ApplyChanges(): void
{ {
parent::ApplyChanges(); parent::ApplyChanges();
// Auf Änderungen der Zähler-Variablen reagieren $this->GetVisualizationTile();
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();
}
} }
/** /**
* Liefert das HTML-Template und injiziert initiale Daten * Liefert das HTML-Template und injiziert initiale Daten
*/ */
public function GetVisualizationTile(): string public function GetVisualizationTile(): string
{
// Initial senden
$this->UpdateData();
// HTML laden
$htmlPath = __DIR__ . '/module.html';
if (!file_exists($htmlPath)) {
$this->LogMessage("module.html nicht gefunden in $htmlPath", KL_ERROR);
return '';
}
return file_get_contents($htmlPath);
}
/**
* Callback vom HTML: frische Daten senden
*/
public function RequestAction(string $Ident, $Value): void
{
if ($Ident === 'update') {
$this->UpdateData();
} else {
throw new \UnexpectedValueException("Unknown Ident $Ident");
}
}
/**
* Berechnet Tagesdaten und schickt sie an die Visualisierung
*/
public function UpdateData(): void
{ {
$start = strtotime('today 00:00'); $start = strtotime('today 00:00');
$end = time(); $end = time();
@@ -80,9 +36,10 @@ class PV_Visu extends IPSModule
$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);
// 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;
$data = [ $data = [
@@ -98,12 +55,68 @@ class PV_Visu extends IPSModule
], ],
]; ];
// Als JSON-String senden // Daten als JSON-String übergeben
$this->UpdateVisualizationValue(json_encode($data)); $json = json_encode($data);
$this->UpdateVisualizationValue($json);
// HTML-Template laden und zurückgeben
$htmlPath = __DIR__ . '/module.html';
if (!file_exists($htmlPath)) {
$this->LogMessage("module.html nicht gefunden in $htmlPath", KL_ERROR);
return '';
}
return file_get_contents($htmlPath);
} }
/** /**
* Holt aggregierte Tageswerte (Summe) aus dem Archiv * 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
{
$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 = [
'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),
],
];
$json = json_encode($data);
$this->UpdateVisualizationValue($json);
}
/**
* Holt Tages-Aggregat aus dem IPS-Archiv
*/ */
private function GetDailyTotal(int $varID, int $start, int $end): float private function GetDailyTotal(int $varID, int $start, int $end): float
{ {
@@ -115,11 +128,6 @@ 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);
if (empty($values)) { return empty($values) ? 0.0 : (float) $values[0]['Avg'];
return 0.0;
} }
// Für Zähler den Summenwert nutzen }a
return (float)$values[0]['Sum'];
}
}
?>