From 33667256ecbef0a4e1f1d530fbea188335d21c6b Mon Sep 17 00:00:00 2001 From: "belevo\\mh" Date: Tue, 20 Jan 2026 10:18:51 +0100 Subject: [PATCH] no message --- PV_Forecast/form.json | 39 ++++++++-------------- PV_Forecast/module.php | 75 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 75 insertions(+), 39 deletions(-) diff --git a/PV_Forecast/form.json b/PV_Forecast/form.json index 15a8fd3..5292c4e 100644 --- a/PV_Forecast/form.json +++ b/PV_Forecast/form.json @@ -1,38 +1,27 @@ { "elements": [ { - "type": "ValidationTextBox", - "name": "URL", - "caption": "Solcast URL" - }, - { - "type": "SelectVariable", - "name": "ActualVariableID", - "caption": "Ist-Produktion Variable (Leistung, ideal W oder kW)" + "type": "Select", + "name": "RefreshMode", + "caption": "Aktualisierungsmodus", + "options": [ + { "caption": "Alle X Minuten", "value": "interval" }, + { "caption": "Einmal täglich (Uhrzeit)", "value": "daily" } + ] }, { "type": "NumberSpinner", "name": "RefreshMinutes", - "caption": "Refresh (Minuten)", + "caption": "Intervall (Minuten)", "minimum": 1, - "maximum": 240 + "maximum": 240, + "visible": true }, { - "type": "CheckBox", - "name": "ActualIsWatt", - "caption": "Istwerte sind in Watt (in kW umrechnen)" - } - ], - "actions": [ - { - "type": "Button", - "caption": "Forecast jetzt aktualisieren", - "onClick": "IPS_RequestAction($id, 'UpdateForecast', 0);" - }, - { - "type": "Label", - "caption": "Hinweis: Istwerte werden aus dem Archiv gelesen. Variable muss im Archiv geloggt werden." + "type": "TimeSpinner", + "name": "RefreshTime", + "caption": "Tägliche Uhrzeit", + "visible": true } ] } - diff --git a/PV_Forecast/module.php b/PV_Forecast/module.php index 7fa97e2..366acc0 100644 --- a/PV_Forecast/module.php +++ b/PV_Forecast/module.php @@ -10,7 +10,9 @@ class PV_Forecast extends IPSModule $this->RegisterPropertyString("URL", ""); $this->RegisterPropertyInteger("ActualVariableID", 0); + $this->RegisterPropertyString("RefreshMode", "interval"); // interval | daily $this->RegisterPropertyInteger("RefreshMinutes", 5); + $this->RegisterPropertyInteger("RefreshTime", 6 * 3600); // 06:00 in Sekunden $this->RegisterPropertyBoolean("ActualIsWatt", true); $this->RegisterTimer("UpdateForecastTimer", 0, 'IPS_RequestAction($_IPS["TARGET"], "UpdateForecast", 0);'); @@ -18,27 +20,41 @@ class PV_Forecast extends IPSModule $this->RegisterHook("/hook/solcastcompare"); } - public function ApplyChanges() - { - parent::ApplyChanges(); + public function ApplyChanges() + { + parent::ApplyChanges(); - $this->SetVisualizationType(1); + $this->SetVisualizationType(1); - $mins = max(1, (int)$this->ReadPropertyInteger("RefreshMinutes")); - $this->SetTimerInterval("UpdateForecastTimer", $mins * 60 * 1000); + $mode = $this->ReadPropertyString("RefreshMode"); + + if ($mode === "interval") { + // 🔁 Intervall-Modus + $mins = max(1, (int)$this->ReadPropertyInteger("RefreshMinutes")); + $this->SetTimerInterval( + "UpdateForecastTimer", + $mins * 60 * 1000 + ); + } else { + // ⏰ Tageszeit-Modus + $this->SetTimerInterval( + "UpdateForecastTimer", + 60 * 1000 // jede Minute prüfen + ); + } + + $this->RegisterHook("/hook/solcastcompare"); + } - $this->RegisterHook("/hook/solcastcompare"); - } public function RequestAction($Ident, $Value) { - switch ($Ident) { - case "UpdateForecast": - $this->UpdateForecast(); - break; - default: - throw new Exception("Unknown Ident: " . $Ident); + if ($Ident === "UpdateForecast") { + $this->HandleScheduledUpdate(); + return; } + + throw new Exception("Unknown Ident: " . $Ident); } private function UpdateForecast() @@ -217,6 +233,37 @@ class PV_Forecast extends IPSModule $list = IPS_GetInstanceListByModuleID(self::ARCHIVE_GUID); return (is_array($list) && count($list) > 0) ? (int)$list[0] : 0; } + + private function HandleScheduledUpdate() + { + $mode = $this->ReadPropertyString("RefreshMode"); + + if ($mode === "interval") { + // 🔁 immer aktualisieren + $this->UpdateForecast(); + return; + } + + // ⏰ Tageszeit-Modus + $targetSec = (int)$this->ReadPropertyInteger("RefreshTime"); + $now = time(); + + // Sekunden seit Mitternacht + $nowSec = + (int)date("H", $now) * 3600 + + (int)date("i", $now) * 60; + + // Schon heute gelaufen? + $lastRun = (int)$this->GetBuffer("LastDailyRun"); + $today = strtotime("today"); + + if ($nowSec >= $targetSec && $lastRun < $today) { + $this->SendDebug("Scheduler", "Tägliches Update ausgelöst", 0); + $this->UpdateForecast(); + $this->SetBuffer("LastDailyRun", (string)$now); + } + } + private function RegisterHook(string $Hook) {