no message
This commit is contained in:
67
Energy_Pie/README.md
Normal file
67
Energy_Pie/README.md
Normal file
@@ -0,0 +1,67 @@
|
||||
# Manager_1
|
||||
Beschreibung des Moduls.
|
||||
|
||||
### Inhaltsverzeichnis
|
||||
|
||||
1. [Funktionsumfang](#1-funktionsumfang)
|
||||
2. [Voraussetzungen](#2-voraussetzungen)
|
||||
3. [Software-Installation](#3-software-installation)
|
||||
4. [Einrichten der Instanzen in IP-Symcon](#4-einrichten-der-instanzen-in-ip-symcon)
|
||||
5. [Statusvariablen und Profile](#5-statusvariablen-und-profile)
|
||||
6. [WebFront](#6-webfront)
|
||||
7. [PHP-Befehlsreferenz](#7-php-befehlsreferenz)
|
||||
|
||||
### 1. Funktionsumfang
|
||||
|
||||
*
|
||||
|
||||
### 2. Voraussetzungen
|
||||
|
||||
- IP-Symcon ab Version 7.1
|
||||
|
||||
### 3. Software-Installation
|
||||
|
||||
* Über den Module Store das 'Manager_1'-Modul installieren.
|
||||
* Alternativ über das Module Control folgende URL hinzufügen
|
||||
|
||||
### 4. Einrichten der Instanzen in IP-Symcon
|
||||
|
||||
Unter 'Instanz hinzufügen' kann das 'Manager_1'-Modul mithilfe des Schnellfilters gefunden werden.
|
||||
- Weitere Informationen zum Hinzufügen von Instanzen in der [Dokumentation der Instanzen](https://www.symcon.de/service/dokumentation/konzepte/instanzen/#Instanz_hinzufügen)
|
||||
|
||||
__Konfigurationsseite__:
|
||||
|
||||
Name | Beschreibung
|
||||
-------- | ------------------
|
||||
|
|
||||
|
|
||||
|
||||
### 5. Statusvariablen und Profile
|
||||
|
||||
Die Statusvariablen/Kategorien werden automatisch angelegt. Das Löschen einzelner kann zu Fehlfunktionen führen.
|
||||
|
||||
#### Statusvariablen
|
||||
|
||||
Name | Typ | Beschreibung
|
||||
------ | ------- | ------------
|
||||
| |
|
||||
| |
|
||||
|
||||
#### Profile
|
||||
|
||||
Name | Typ
|
||||
------ | -------
|
||||
|
|
||||
|
|
||||
|
||||
### 6. WebFront
|
||||
|
||||
Die Funktionalität, die das Modul im WebFront bietet.
|
||||
|
||||
### 7. PHP-Befehlsreferenz
|
||||
|
||||
`boolean GEF_BeispielFunktion(integer $InstanzID);`
|
||||
Erklärung der Funktion.
|
||||
|
||||
Beispiel:
|
||||
`GEF_BeispielFunktion(12345);`
|
||||
11
Energy_Pie/form.json
Normal file
11
Energy_Pie/form.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "{{28A05FF5-582D-8F15-4CCF-A18402C4BC2B}}",
|
||||
"name": "Energy_Pie",
|
||||
"type": 3,
|
||||
"vendor": "Custom",
|
||||
"aliases": [
|
||||
"Energy_Pie",
|
||||
"Energie-Kreisdiagramm"
|
||||
],
|
||||
"url": ""
|
||||
}
|
||||
171
Energy_Pie/module.html
Normal file
171
Energy_Pie/module.html
Normal file
@@ -0,0 +1,171 @@
|
||||
<div style="padding:12px;font-family:system-ui,-apple-system,Segoe UI,Roboto,sans-serif;">
|
||||
<div style="display:flex;gap:10px;flex-wrap:wrap;align-items:center;">
|
||||
<label style="display:flex;gap:6px;align-items:center;">
|
||||
<span>Zeitraum</span>
|
||||
<select id="range">
|
||||
<option value="day">Tag</option>
|
||||
<option value="week">Woche (Mo–So)</option>
|
||||
<option value="month">Monat</option>
|
||||
<option value="total">Gesamt</option>
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<label style="display:flex;gap:6px;align-items:center;">
|
||||
<span>Datum</span>
|
||||
<input id="date" type="date" />
|
||||
</label>
|
||||
|
||||
<button id="prev" type="button">◀</button>
|
||||
<button id="today" type="button">Heute</button>
|
||||
<button id="next" type="button">▶</button>
|
||||
</div>
|
||||
|
||||
<div style="margin-top:12px;display:flex;gap:16px;align-items:flex-start;flex-wrap:wrap;">
|
||||
<div style="display:flex;flex-direction:column;gap:6px;">
|
||||
<canvas id="pie" width="280" height="280" style="border-radius:14px;"></canvas>
|
||||
<div id="period" style="font-size:12px;opacity:0.7;"></div>
|
||||
</div>
|
||||
|
||||
<div style="min-width:260px;flex:1;">
|
||||
<div style="font-weight:600;margin-bottom:8px;">Aufteilung</div>
|
||||
<div id="legend" style="display:flex;flex-direction:column;gap:6px;"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const elRange = document.getElementById('range');
|
||||
const elDate = document.getElementById('date');
|
||||
const elPrev = document.getElementById('prev');
|
||||
const elToday = document.getElementById('today');
|
||||
const elNext = document.getElementById('next');
|
||||
const elLegend = document.getElementById('legend');
|
||||
const elPeriod = document.getElementById('period');
|
||||
|
||||
const canvas = document.getElementById('pie');
|
||||
const ctx = canvas.getContext('2d');
|
||||
|
||||
elRange.addEventListener('change', () => requestAction('SetRange', elRange.value));
|
||||
|
||||
// Fire immediately when user picks a date (input + change), with a tiny debounce
|
||||
let dateTimer = null;
|
||||
function pushDateNow() {
|
||||
if (dateTimer) clearTimeout(dateTimer);
|
||||
dateTimer = setTimeout(() => {
|
||||
requestAction('SetDate', elDate.value);
|
||||
}, 80);
|
||||
}
|
||||
elDate.addEventListener('input', pushDateNow);
|
||||
elDate.addEventListener('change', pushDateNow);
|
||||
|
||||
elPrev.addEventListener('click', () => requestAction('Prev', true));
|
||||
elToday.addEventListener('click', () => requestAction('Today', true));
|
||||
elNext.addEventListener('click', () => requestAction('Next', true));
|
||||
|
||||
// Called by Symcon when PHP sends UpdateVisualizationValue($payload)
|
||||
function handleMessage(data) {
|
||||
if (!data) return;
|
||||
|
||||
if (data.range) elRange.value = data.range;
|
||||
if (data.date) elDate.value = data.date;
|
||||
|
||||
const isTotal = (data.range === 'total');
|
||||
elDate.disabled = isTotal;
|
||||
|
||||
if (data.tStart && data.tEnd) {
|
||||
const s = new Date(data.tStart * 1000);
|
||||
const e = new Date(data.tEnd * 1000);
|
||||
elPeriod.textContent = isTotal
|
||||
? 'Zeitraum: Gesamt'
|
||||
: `Zeitraum: ${fmtDateTime(s)} – ${fmtDateTime(e)}`;
|
||||
} else {
|
||||
elPeriod.textContent = '';
|
||||
}
|
||||
|
||||
if (data.values) {
|
||||
drawPie(data.values);
|
||||
drawLegend(data.values);
|
||||
}
|
||||
}
|
||||
|
||||
function fmtDateTime(d) {
|
||||
const yyyy = d.getFullYear();
|
||||
const mm = String(d.getMonth() + 1).padStart(2,'0');
|
||||
const dd = String(d.getDate()).padStart(2,'0');
|
||||
const hh = String(d.getHours()).padStart(2,'0');
|
||||
const mi = String(d.getMinutes()).padStart(2,'0');
|
||||
return `${dd}.${mm}.${yyyy} ${hh}:${mi}`;
|
||||
}
|
||||
|
||||
function drawLegend(values) {
|
||||
const entries = Object.entries(values);
|
||||
const sum = entries.reduce((a,[,v]) => a + (+v || 0), 0);
|
||||
|
||||
elLegend.innerHTML = entries.map(([k,v]) => {
|
||||
const val = (+v || 0);
|
||||
const pct = sum > 0 ? (val / sum * 100) : 0;
|
||||
return `
|
||||
<div style="display:flex;justify-content:space-between;gap:12px;">
|
||||
<div>${escapeHtml(k)}</div>
|
||||
<div style="white-space:nowrap;">${val.toFixed(2)} kWh (${pct.toFixed(1)}%)</div>
|
||||
</div>`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function drawPie(values) {
|
||||
const entries = Object.entries(values).map(([k,v]) => [k, (+v || 0)]);
|
||||
const sum = entries.reduce((a,[,v]) => a + v, 0);
|
||||
|
||||
ctx.clearRect(0,0,canvas.width,canvas.height);
|
||||
|
||||
// Background circle
|
||||
ctx.beginPath();
|
||||
ctx.arc(140,140,125,0,Math.PI*2);
|
||||
ctx.fillStyle = '#f2f2f2';
|
||||
ctx.fill();
|
||||
|
||||
if (sum <= 0) {
|
||||
ctx.fillStyle = '#666';
|
||||
ctx.font = '14px sans-serif';
|
||||
ctx.fillText('Keine Daten', 92, 145);
|
||||
return;
|
||||
}
|
||||
|
||||
let start = -Math.PI / 2;
|
||||
const colors = ['#4e79a7','#f28e2b','#e15759','#76b7b2','#59a14f','#edc948'];
|
||||
|
||||
entries.forEach(([label,val], i) => {
|
||||
if (val <= 0) return;
|
||||
const ang = (val / sum) * Math.PI * 2;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(140,140);
|
||||
ctx.arc(140,140,125,start,start+ang);
|
||||
ctx.closePath();
|
||||
ctx.fillStyle = colors[i % colors.length];
|
||||
ctx.fill();
|
||||
start += ang;
|
||||
});
|
||||
|
||||
// Donut hole
|
||||
ctx.beginPath();
|
||||
ctx.arc(140,140,65,0,Math.PI*2);
|
||||
ctx.fillStyle = '#fff';
|
||||
ctx.fill();
|
||||
|
||||
// Sum in center
|
||||
ctx.fillStyle = '#111';
|
||||
ctx.font = 'bold 16px sans-serif';
|
||||
const text = sum.toFixed(2) + ' kWh';
|
||||
const w = ctx.measureText(text).width;
|
||||
ctx.fillText(text, 140 - w/2, 146);
|
||||
}
|
||||
|
||||
function escapeHtml(s) {
|
||||
return String(s)
|
||||
.replaceAll('&','&')
|
||||
.replaceAll('<','<')
|
||||
.replaceAll('>','>')
|
||||
.replaceAll('"','"')
|
||||
.replaceAll("'",''');
|
||||
}
|
||||
</script>
|
||||
11
Energy_Pie/module.json
Normal file
11
Energy_Pie/module.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "{{97BFC43B-9BE5-AB7D-EC5A-E31BB54878E0}}",
|
||||
"name": "Energy_Pie",
|
||||
"type": 3,
|
||||
"vendor": "Custom",
|
||||
"aliases": [
|
||||
"Energy_Pie",
|
||||
"Energie-Kreisdiagramm"
|
||||
],
|
||||
"url": ""
|
||||
}
|
||||
267
Energy_Pie/module.php
Normal file
267
Energy_Pie/module.php
Normal file
@@ -0,0 +1,267 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
class Energy_Pie extends IPSModule
|
||||
{
|
||||
private const ATTR_RANGE = 'Range';
|
||||
private const ATTR_DATE = 'Date';
|
||||
|
||||
public function Create(): void
|
||||
{
|
||||
parent::Create();
|
||||
|
||||
// Source variables (logged counters, kWh)
|
||||
$this->RegisterPropertyInteger('VarProduction', 0);
|
||||
$this->RegisterPropertyInteger('VarConsumption', 0);
|
||||
$this->RegisterPropertyInteger('VarFeedIn', 0);
|
||||
$this->RegisterPropertyInteger('VarGrid', 0);
|
||||
|
||||
$this->RegisterPropertyString('DefaultRange', 'day');
|
||||
|
||||
// UI state
|
||||
$this->RegisterAttributeString(self::ATTR_RANGE, 'day');
|
||||
$this->RegisterAttributeString(self::ATTR_DATE, date('Y-m-d'));
|
||||
|
||||
// Enable HTML-SDK visualization tile
|
||||
$this->SetVisualizationType(1);
|
||||
}
|
||||
|
||||
public function ApplyChanges(): void
|
||||
{
|
||||
parent::ApplyChanges();
|
||||
|
||||
// Initialize attributes if needed
|
||||
$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 data
|
||||
$this->RecalculateAndPush();
|
||||
}
|
||||
|
||||
public function GetVisualizationTile(): string
|
||||
{
|
||||
$path = __DIR__ . '/module.html';
|
||||
if (!file_exists($path)) {
|
||||
return '<div style="padding:12px;font-family:sans-serif;">module.html fehlt</div>';
|
||||
}
|
||||
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);
|
||||
|
||||
// UX: if switching to week, snap date to Monday (Mo–So)
|
||||
if ($range === 'week') {
|
||||
$this->snapDateToWeekMonday();
|
||||
}
|
||||
|
||||
// For total, date is irrelevant but we keep it stored
|
||||
$this->RecalculateAndPush();
|
||||
break;
|
||||
|
||||
case 'SetDate':
|
||||
$date = (string)$Value;
|
||||
if (!$this->isValidDate($date)) {
|
||||
// ignore invalid date instead of crashing the UI
|
||||
return;
|
||||
}
|
||||
|
||||
$this->WriteAttributeString(self::ATTR_DATE, $date);
|
||||
|
||||
// If currently in week mode, snap to Monday immediately
|
||||
if ($this->ReadAttributeString(self::ATTR_RANGE) === 'week') {
|
||||
$this->snapDateToWeekMonday();
|
||||
}
|
||||
|
||||
$this->RecalculateAndPush();
|
||||
break;
|
||||
|
||||
case 'Prev':
|
||||
case 'Next':
|
||||
case 'Today':
|
||||
$this->ShiftDate($Ident);
|
||||
|
||||
// If week mode, always keep Monday
|
||||
if ($this->ReadAttributeString(self::ATTR_RANGE) === 'week') {
|
||||
$this->snapDateToWeekMonday();
|
||||
}
|
||||
|
||||
$this->RecalculateAndPush();
|
||||
break;
|
||||
|
||||
case 'Refresh':
|
||||
// optional manual refresh (keeps handy for debugging)
|
||||
$this->RecalculateAndPush();
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Exception('Unknown Ident: ' . $Ident);
|
||||
}
|
||||
}
|
||||
|
||||
private function RecalculateAndPush(): void
|
||||
{
|
||||
$range = $this->ReadAttributeString(self::ATTR_RANGE);
|
||||
$date = $this->ReadAttributeString(self::ATTR_DATE);
|
||||
|
||||
[$tStart, $tEnd] = $this->getRange($range, $date);
|
||||
|
||||
// --- PLACEHOLDER ---
|
||||
// Replace later with archive delta calculations:
|
||||
// $prod = $this->readDelta($this->ReadPropertyInteger('VarProduction'), $tStart, $tEnd);
|
||||
// $cons = $this->readDelta($this->ReadPropertyInteger('VarConsumption'), $tStart, $tEnd);
|
||||
// $feed = $this->readDelta($this->ReadPropertyInteger('VarFeedIn'), $tStart, $tEnd);
|
||||
// $grid = $this->readDelta($this->ReadPropertyInteger('VarGrid'), $tStart, $tEnd);
|
||||
$prod = 12.3;
|
||||
$cons = 8.7;
|
||||
$feed = 3.1;
|
||||
$grid = 5.6;
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
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) {
|
||||
$base = strtotime(date('Y-m-d') . ' 00:00:00');
|
||||
}
|
||||
|
||||
switch ($range) {
|
||||
case 'day':
|
||||
$start = $base;
|
||||
$end = $start + 86400;
|
||||
return [$start, $end];
|
||||
|
||||
case 'week':
|
||||
// Mo–So: start = Monday 00:00:00, end = next Monday 00:00:00
|
||||
$dow = (int)date('N', $base); // 1=Mon..7=Sun
|
||||
$start = $base - (($dow - 1) * 86400);
|
||||
$end = $start + (7 * 86400);
|
||||
return [$start, $end];
|
||||
|
||||
case 'month':
|
||||
$start = strtotime(date('Y-m-01 00:00:00', $base));
|
||||
$end = strtotime('+1 month', $start);
|
||||
return [$start, $end];
|
||||
|
||||
default:
|
||||
return [$base, $base + 86400];
|
||||
}
|
||||
}
|
||||
|
||||
private function ShiftDate(string $action): void
|
||||
{
|
||||
$range = $this->ReadAttributeString(self::ATTR_RANGE);
|
||||
|
||||
if ($range === 'total') {
|
||||
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 snapDateToWeekMonday(): void
|
||||
{
|
||||
$date = $this->ReadAttributeString(self::ATTR_DATE);
|
||||
if (!$this->isValidDate($date)) {
|
||||
$date = date('Y-m-d');
|
||||
$this->WriteAttributeString(self::ATTR_DATE, $date);
|
||||
}
|
||||
|
||||
$base = strtotime($date . ' 00:00:00');
|
||||
if ($base === false) {
|
||||
$base = strtotime(date('Y-m-d') . ' 00:00:00');
|
||||
}
|
||||
|
||||
$dow = (int)date('N', $base); // 1=Mon..7=Sun
|
||||
$monday = $base - (($dow - 1) * 86400);
|
||||
|
||||
$this->WriteAttributeString(self::ATTR_DATE, date('Y-m-d', $monday));
|
||||
}
|
||||
|
||||
private function isValidDate(string $ymd): bool
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
public function TestPush(): void
|
||||
{
|
||||
$this->RecalculateAndPush();
|
||||
}
|
||||
}
|
||||
|
||||
// Action helper for form.json button
|
||||
function EP_TestPush(int $id): void
|
||||
{
|
||||
IPS_RequestAction($id, 'Refresh', true);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user