diff --git a/Energy_Pie/form.json b/Energy_Pie/form.json
index 4355cb9..a8b8127 100644
--- a/Energy_Pie/form.json
+++ b/Energy_Pie/form.json
@@ -3,20 +3,6 @@
{ "type": "SelectVariable", "name": "VarProduction", "caption": "Produktion (kWh)" },
{ "type": "SelectVariable", "name": "VarConsumption", "caption": "Verbrauch (kWh)" },
{ "type": "SelectVariable", "name": "VarFeedIn", "caption": "Einspeisung (kWh)" },
- { "type": "SelectVariable", "name": "VarGrid", "caption": "Bezug Netz (kWh)" },
-
- { "type": "Label", "caption": "" },
-
- {
- "type": "Select",
- "name": "DefaultRange",
- "caption": "Standard-Zeitraum",
- "options": [
- { "caption": "Tag", "value": "day" },
- { "caption": "Woche", "value": "week" },
- { "caption": "Monat", "value": "month" },
- { "caption": "Gesamt", "value": "total" }
- ]
- }
+ { "type": "SelectVariable", "name": "VarGrid", "caption": "Bezug Netz (kWh)" }
]
-}
+}
\ No newline at end of file
diff --git a/Energy_Pie/module.php b/Energy_Pie/module.php
index 57513ca..bf98628 100644
--- a/Energy_Pie/module.php
+++ b/Energy_Pie/module.php
@@ -2,8 +2,9 @@
declare(strict_types=1);
-class Energy_Pie extends IPSModule
+class EnergyPie extends IPSModule
{
+ // Attribute keys for UI state
private const ATTR_RANGE = 'Range';
private const ATTR_DATE = 'Date';
@@ -17,13 +18,14 @@ class Energy_Pie extends IPSModule
$this->RegisterPropertyInteger('VarFeedIn', 0);
$this->RegisterPropertyInteger('VarGrid', 0);
+ // Default range for first startup
$this->RegisterPropertyString('DefaultRange', 'day');
- // UI state (internal, no object tree clutter)
+ // Persisted UI state
$this->RegisterAttributeString(self::ATTR_RANGE, 'day');
$this->RegisterAttributeString(self::ATTR_DATE, date('Y-m-d'));
- // Enable HTML-SDK visualization
+ // Enable individual visualization (HTML-SDK)
$this->SetVisualizationType(1);
}
@@ -31,165 +33,155 @@ class Energy_Pie extends IPSModule
{
parent::ApplyChanges();
- if ($this->ReadAttributeString(self::ATTR_RANGE) === '') {
+ // First-time init: if range is empty, set it to property default
+ $range = $this->ReadAttributeString(self::ATTR_RANGE);
+ if ($range === '') {
$this->WriteAttributeString(self::ATTR_RANGE, $this->ReadPropertyString('DefaultRange'));
}
- $date = $this->ReadAttributeString(self::ATTR_DATE);
- if ($date === '' || !$this->isValidDate($date)) {
- $this->WriteAttributeString(self::ATTR_DATE, date('Y-m-d'));
- }
-
+ // Push initial view data
$this->RecalculateAndPush();
}
public function GetVisualizationTile(): string
{
$path = __DIR__ . '/module.html';
- return file_exists($path)
- ? file_get_contents($path)
- : '
module.html fehlt
';
+ if (!file_exists($path)) {
+ return 'module.html fehlt
';
+ }
+ return file_get_contents($path);
}
public function RequestAction($Ident, $Value): void
{
switch ($Ident) {
-
case 'SetRange':
$range = (string)$Value;
if (!in_array($range, ['day', 'week', 'month', 'total'], true)) {
- return;
+ throw new Exception('Invalid range: ' . $range);
}
$this->WriteAttributeString(self::ATTR_RANGE, $range);
- if ($range === 'week') {
- $this->snapDateToWeekMonday();
- }
+ $this->RecalculateAndPush();
break;
case 'SetDate':
+ // Expect YYYY-MM-DD from HTML
$date = (string)$Value;
if (!$this->isValidDate($date)) {
- return;
+ throw new Exception('Invalid date: ' . $date);
}
$this->WriteAttributeString(self::ATTR_DATE, $date);
- if ($this->ReadAttributeString(self::ATTR_RANGE) === 'week') {
- $this->snapDateToWeekMonday();
- }
+ $this->RecalculateAndPush();
break;
case 'Prev':
case 'Next':
case 'Today':
$this->ShiftDate($Ident);
- if ($this->ReadAttributeString(self::ATTR_RANGE) === 'week') {
- $this->snapDateToWeekMonday();
- }
+ $this->RecalculateAndPush();
break;
case 'Refresh':
+ $this->RecalculateAndPush();
break;
default:
- return;
+ throw new Exception('Unknown Ident: ' . $Ident);
}
-
- $this->RecalculateAndPush();
}
- /* ========================================================= */
- /* ===================== CORE LOGIC ======================== */
- /* ========================================================= */
+ /**
+ * Compute values (later from archive), then push to tile via UpdateVisualizationValue.
+ */
+ private function RecalculateAndPush(): void
+ {
+ $range = $this->ReadAttributeString(self::ATTR_RANGE);
+ $date = $this->ReadAttributeString(self::ATTR_DATE);
-private function RecalculateAndPush(): void
-{
- $range = $this->ReadAttributeString(self::ATTR_RANGE);
- $date = $this->ReadAttributeString(self::ATTR_DATE);
+ [$tStart, $tEnd] = $this->getRange($range, $date);
- [$tStart, $tEnd] = $this->getRange($range, $date);
+ // --- PLACEHOLDER CALCULATION ---
+ // Replace these with your later delta calculation from logged data:
+ // $prod = $this->readDelta($this->ReadPropertyInteger('VarProduction'), $tStart, $tEnd);
+ // etc.
+ $prod = 12.3;
+ $cons = 8.7;
+ $feed = 3.1;
+ $grid = 5.6;
- // Deltas aus dem Archiv
- $prod = $this->readDelta($this->ReadPropertyInteger('VarProduction'), $tStart, $tEnd);
- $feed = $this->readDelta($this->ReadPropertyInteger('VarFeedIn'), $tStart, $tEnd);
- $grid = $this->readDelta($this->ReadPropertyInteger('VarGrid'), $tStart, $tEnd);
+ // If you prefer, you can already set them to 0.0 for "no calc yet"
+ // $prod = $cons = $feed = $grid = 0.0;
- // Hausverbrauch = Produktion - Einspeisung + Netz
- $house = $prod - $feed + $grid;
+ $payload = [
+ 'range' => $range,
+ 'date' => $date,
+ 'tStart' => $tStart,
+ 'tEnd' => $tEnd,
+ 'values' => [
+ 'Produktion' => (float)$prod,
+ 'Verbrauch' => (float)$cons,
+ 'Einspeisung' => (float)$feed,
+ 'Netz' => (float)$grid
+ ]
+ ];
- // optionaler Schutz (z.B. wenn Logs/Resets Mist bauen)
- if ($house < 0) {
- $house = 0.0;
+ $this->UpdateVisualizationValue($payload);
}
- // Debug ins Log, damit du die Rechnung sofort prüfen kannst
- $this->LogMessage(
- sprintf(
- "EnergyPie | Range=%s Date=%s | %s -> %s | Prod=%.3f Feed=%.3f Grid=%.3f House=%.3f",
- $range,
- $date,
- date('Y-m-d H:i:s', $tStart),
- date('Y-m-d H:i:s', $tEnd),
- $prod,
- $feed,
- $grid,
- $house
- ),
- KL_NOTIFY
- );
-
- $payload = [
- 'range' => $range,
- 'date' => $date,
- 'tStart' => $tStart,
- 'tEnd' => $tEnd,
- 'values' => [
- 'Produktion' => (float)$prod,
- 'Einspeisung' => (float)$feed,
- 'Netz' => (float)$grid,
- 'Hausverbrauch'=> (float)$house
- ]
- ];
-
- $this->UpdateVisualizationValue(json_encode($payload, JSON_THROW_ON_ERROR));
-}
-
-
- /* ========================================================= */
- /* ===================== TIME RANGES ======================= */
- /* ========================================================= */
-
+ /**
+ * Range rules:
+ * - day: chosen date 00:00:00 .. next day 00:00:00
+ * - week: Monday 00:00:00 .. next Monday 00:00:00 (Mo–So)
+ * - month: first of month 00:00:00 .. first of next month 00:00:00
+ * - total: 0 .. now (placeholder)
+ */
private function getRange(string $range, string $dateYmd): array
{
$now = time();
if ($range === 'total') {
+ // Later: use oldest log timestamp as start
return [0, $now];
}
- $base = strtotime($dateYmd . ' 00:00:00') ?: strtotime('today');
+ $base = strtotime($dateYmd . ' 00:00:00');
+ if ($base === false) {
+ // fallback to today
+ $base = strtotime(date('Y-m-d') . ' 00:00:00');
+ }
switch ($range) {
case 'day':
- return [$base, $base + 86400];
+ $start = $base;
+ $end = $start + 86400;
+ return [$start, $end];
case 'week':
- $dow = (int)date('N', $base); // 1=Mon
- $start = $base - (($dow - 1) * 86400);
- return [$start, $start + 7 * 86400];
+ // date('N'): 1=Mon .. 7=Sun
+ $dow = (int)date('N', $base);
+ $start = $base - (($dow - 1) * 86400); // Monday
+ $end = $start + (7 * 86400); // next Monday
+ return [$start, $end];
case 'month':
$start = strtotime(date('Y-m-01 00:00:00', $base));
- return [$start, strtotime('+1 month', $start)];
+ $end = strtotime(date('Y-m-01 00:00:00', strtotime('+1 month', $start)));
+ return [$start, $end];
default:
return [$base, $base + 86400];
}
}
+ /**
+ * Buttons for quick navigation.
+ */
private function ShiftDate(string $action): void
{
$range = $this->ReadAttributeString(self::ATTR_RANGE);
if ($range === 'total') {
+ // total ignores date
return;
}
@@ -198,80 +190,46 @@ private function RecalculateAndPush(): void
return;
}
- $base = strtotime($this->ReadAttributeString(self::ATTR_DATE) . ' 00:00:00') ?: strtotime('today');
- $dir = ($action === 'Prev') ? -1 : 1;
+ $date = $this->ReadAttributeString(self::ATTR_DATE);
+ $base = strtotime($date . ' 00:00:00');
+ if ($base === false) {
+ $base = strtotime(date('Y-m-d') . ' 00:00:00');
+ }
- match ($range) {
- 'day' => $base = strtotime(($dir === -1 ? '-1 day' : '+1 day'), $base),
- 'week' => $base = strtotime(($dir === -7 ? '-7 day' : '+7 day'), $base),
- 'month' => $base = strtotime(($dir === -1 ? '-1 month' : '+1 month'), $base),
- default => null
- };
+ $sign = ($action === 'Prev') ? -1 : 1;
+
+ switch ($range) {
+ case 'day':
+ $base = strtotime(($sign === -1 ? '-1 day' : '+1 day'), $base);
+ break;
+
+ case 'week':
+ $base = strtotime(($sign === -1 ? '-7 day' : '+7 day'), $base);
+ break;
+
+ case 'month':
+ $base = strtotime(($sign === -1 ? '-1 month' : '+1 month'), $base);
+ break;
+ }
$this->WriteAttributeString(self::ATTR_DATE, date('Y-m-d', $base));
}
- private function snapDateToWeekMonday(): void
- {
- $base = strtotime($this->ReadAttributeString(self::ATTR_DATE) . ' 00:00:00') ?: strtotime('today');
- $dow = (int)date('N', $base);
- $this->WriteAttributeString(self::ATTR_DATE, date('Y-m-d', $base - (($dow - 1) * 86400)));
- }
-
- /* ========================================================= */
- /* ===================== ARCHIVE READ ====================== */
- /* ========================================================= */
-
- private function readDelta(int $varId, int $tStart, int $tEnd): float
- {
- if ($varId <= 0 || !IPS_VariableExists($varId)) {
- return 0.0;
- }
-
- $archiveID = IPS_GetInstanceListByModuleID('{43192F0B-135B-4CE7-A0A7-1475603F3060}')[0] ?? 0;
- if ($archiveID <= 0) {
- return 0.0;
- }
-
- $values = AC_GetLoggedValues($archiveID, $varId, $tStart - 86400, $tEnd + 86400, 0);
- if (empty($values)) {
- return 0.0;
- }
-
- usort($values, static fn($a, $b) => $a['TimeStamp'] <=> $b['TimeStamp']);
-
- $vStart = null;
- $vEnd = null;
-
- foreach ($values as $v) {
- if ($v['TimeStamp'] <= $tStart) {
- $vStart = (float)$v['Value'];
- }
- if ($v['TimeStamp'] <= $tEnd) {
- $vEnd = (float)$v['Value'];
- }
- if ($v['TimeStamp'] > $tEnd) {
- break;
- }
- }
-
- if ($vStart === null || $vEnd === null) {
- return 0.0;
- }
-
- $diff = $vEnd - $vStart;
- return ($diff < 0) ? 0.0 : round($diff, 3);
- }
-
- /* ========================================================= */
-
private function isValidDate(string $ymd): bool
{
+ // expects YYYY-MM-DD
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $ymd)) {
return false;
}
[$y, $m, $d] = array_map('intval', explode('-', $ymd));
return checkdate($m, $d, $y);
}
+
+ // Optional helper for the "actions" test button in form.json
+ public function TestPush(): void
+ {
+ $this->RecalculateAndPush();
+ }
}
+
?>
\ No newline at end of file