no message

This commit is contained in:
2025-06-24 11:27:43 +02:00
parent 421a73cead
commit c3796d0800
2 changed files with 35 additions and 31 deletions

View File

@@ -35,8 +35,18 @@
document.getElementById('barPV').style.width = data.consPV + '%';
document.getElementById('barGrid').style.width = data.consGrid + '%';
}
function handleMessage(msg) { Apply(msg); }
// Kein requestAction-Aufruf hier nötig
function handleMessage(msg) {
try {
var data = (typeof msg === 'string') ? JSON.parse(msg) : msg;
Apply(data);
} catch(e) {
console.error('PV_Visu handleMessage error:', e, msg);
}
}
// Bind to HTML-SDK
if (typeof registerMessageHandler === 'function') {
registerMessageHandler(handleMessage);
}
</script>
</body>
</html>

View File

@@ -2,6 +2,7 @@
declare(strict_types=1);
class PV_Visu extends IPSModule
{
public function Create()
@@ -16,30 +17,23 @@ class PV_Visu extends IPSModule
$this->SetVisualizationType(3);
}
public function ApplyChanges()
public function ApplyChanges(): void
{
parent::ApplyChanges();
$this->RequestAction("update", 1);
}
/**
* Liefert das HTML-Template und injiziert initiale Tagesdaten
* Liefert das HTML-Template und injiziert initiale Daten
*/
public function GetVisualizationTile()
public function GetVisualizationTile(): string
{
// Tageswerte berechnen
$start = strtotime('today 00:00');
$end = time();
$prodID = $this->ReadPropertyInteger('VarProduction');
$consID = $this->ReadPropertyInteger('VarConsumption');
$feedID = $this->ReadPropertyInteger('VarFeedIn');
$gridID = $this->ReadPropertyInteger('VarGrid');
$prod = $this->GetDailyTotal($prodID, $start, $end);
$cons = $this->GetDailyTotal($consID, $start, $end);
$feed = $this->GetDailyTotal($feedID, $start, $end);
$grid = $this->GetDailyTotal($gridID, $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);
// Prozent-Quoten
$prodCons = $prod > 0 ? ($cons / $prod) * 100 : 0;
@@ -47,7 +41,7 @@ class PV_Visu extends IPSModule
$consPV = $cons > 0 ? min($prod, $cons) / $cons * 100 : 0;
$consGrid = $cons > 0 ? ($grid / $cons) * 100 : 0;
$initialData = [
$data = [
'prodCons' => round($prodCons, 1),
'prodFeed' => round($prodFeed, 1),
'consPV' => round($consPV, 1),
@@ -60,24 +54,23 @@ class PV_Visu extends IPSModule
],
];
// Script zur Injektion der initialen Daten
$message = '<script>handleMessage(' . json_encode($initialData) . ');</script>';
// Daten als JSON-String übergeben
$json = json_encode($data);
$this->UpdateVisualizationValue($json);
// HTML-Template laden
// 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 '';
}
$html = file_get_contents($htmlPath);
return $html . $message;
return file_get_contents($htmlPath);
}
/**
* Callback vom HTML: sendet frische Daten
*/
public function RequestAction($Ident, $Value)
public function RequestAction($Ident, $Value): void
{
if ($Ident === 'update') {
$this->UpdateData();
@@ -89,15 +82,15 @@ class PV_Visu extends IPSModule
/**
* Aktualisiert Daten und sendet an Tile
*/
public function UpdateData()
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);
$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;
@@ -117,13 +110,14 @@ class PV_Visu extends IPSModule
],
];
$this->UpdateVisualizationValue($data);
$json = json_encode($data);
$this->UpdateVisualizationValue($json);
}
/**
* 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) {
return 0.0;