RegisterPropertyInteger('VarProduction', 0);
$this->RegisterPropertyInteger('VarConsumption', 0);
$this->RegisterPropertyInteger('VarFeedIn', 0);
$this->RegisterPropertyInteger('VarGrid', 0);
// Default range for first startup
$this->RegisterPropertyString('DefaultRange', 'day');
// Persisted UI state
$this->RegisterAttributeString(self::ATTR_RANGE, 'day');
$this->RegisterAttributeString(self::ATTR_DATE, date('Y-m-d'));
// Enable individual visualization (HTML-SDK)
$this->SetVisualizationType(1);
}
public function ApplyChanges(): void
{
parent::ApplyChanges();
// 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'));
}
// Push initial view data
$this->RecalculateAndPush();
}
public function GetVisualizationTile(): string
{
$path = __DIR__ . '/module.html';
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)) {
throw new Exception('Invalid range: ' . $range);
}
$this->WriteAttributeString(self::ATTR_RANGE, $range);
$this->RecalculateAndPush();
break;
case 'SetDate':
// Expect YYYY-MM-DD from HTML
$date = (string)$Value;
if (!$this->isValidDate($date)) {
throw new Exception('Invalid date: ' . $date);
}
$this->WriteAttributeString(self::ATTR_DATE, $date);
$this->RecalculateAndPush();
break;
case 'Prev':
case 'Next':
case 'Today':
$this->ShiftDate($Ident);
$this->RecalculateAndPush();
break;
case 'Refresh':
$this->RecalculateAndPush();
break;
default:
throw new Exception('Unknown Ident: ' . $Ident);
}
}
/**
* 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);
[$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;
// If you prefer, you can already set them to 0.0 for "no calc yet"
// $prod = $cons = $feed = $grid = 0.0;
$payload = [
'range' => $range,
'date' => $date,
'tStart' => $tStart,
'tEnd' => $tEnd,
'values' => [
'Produktion' => (float)$prod,
'Verbrauch' => (float)$cons,
'Einspeisung' => (float)$feed,
'Netz' => (float)$grid
]
];
$this->UpdateVisualizationValue($payload);
}
/**
* 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');
if ($base === false) {
// fallback to today
$base = strtotime(date('Y-m-d') . ' 00:00:00');
}
switch ($range) {
case 'day':
$start = $base;
$end = $start + 86400;
return [$start, $end];
case 'week':
// 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));
$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;
}
if ($action === 'Today') {
$this->WriteAttributeString(self::ATTR_DATE, date('Y-m-d'));
return;
}
$date = $this->ReadAttributeString(self::ATTR_DATE);
$base = strtotime($date . ' 00:00:00');
if ($base === false) {
$base = strtotime(date('Y-m-d') . ' 00:00:00');
}
$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 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();
}
}
?>