237 lines
8.0 KiB
PHP
237 lines
8.0 KiB
PHP
<?php
|
|
|
|
class Bat_EV_SDL extends IPSModule
|
|
{
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
// Properties
|
|
$this->RegisterPropertyString("Batteries", "[]");
|
|
$this->RegisterPropertyInteger("SDL_Leistung", 0); // W
|
|
$this->RegisterPropertyInteger("UpdateInterval", 5); // Minuten
|
|
|
|
// Status
|
|
$this->RegisterVariableBoolean("State", "Aktiv", "~Switch", 1);
|
|
$this->EnableAction("State");
|
|
|
|
// Prozentwerte nach deiner Skizze:
|
|
// SDL_% = verfügbare SDL-Energie / benötigte SDL-Energie
|
|
// EV_% = verfügbare EV-Energie / max. EV-Energie (SumCap - SumSDLreq)
|
|
$this->RegisterVariableFloat("SDL_Pos", "SDL Energie verfügbar (%)", "", 10);
|
|
$this->RegisterVariableFloat("SoC_EV", "EV Energie verfügbar (%)", "", 11);
|
|
|
|
// Leistungsgrenzen
|
|
$this->RegisterVariableFloat("P_SDL_max", "SDL max (W)", "", 20);
|
|
$this->RegisterVariableFloat("P_SDL_laden", "P SDL laden max (W)", "", 21);
|
|
$this->RegisterVariableFloat("P_SDL_entladen", "P SDL entladen max (W)", "", 22);
|
|
|
|
$this->RegisterVariableFloat("P_EV_max", "EV max (W)", "", 30);
|
|
$this->RegisterVariableFloat("P_EV_laden", "P EV laden max (W)", "", 31);
|
|
$this->RegisterVariableFloat("P_EV_entladen", "P EV entladen max (W)", "", 32);
|
|
|
|
// Debug
|
|
$this->RegisterVariableString("CalcJSON", "Berechnung (JSON)", "", 99);
|
|
|
|
// Timer (Prefix: GEF)
|
|
$this->RegisterTimer("UpdateTimer", 0, 'GEF_Update($_IPS["TARGET"]);');
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
|
|
$intervalMin = (int)$this->ReadPropertyInteger("UpdateInterval");
|
|
$this->SetTimerInterval("UpdateTimer", ($intervalMin > 0) ? $intervalMin * 60 * 1000 : 0);
|
|
|
|
$this->Update();
|
|
}
|
|
|
|
public function RequestAction($Ident, $Value)
|
|
{
|
|
if ($Ident === "State") {
|
|
SetValue($this->GetIDForIdent("State"), (bool)$Value);
|
|
if ((bool)$Value) {
|
|
$this->Update();
|
|
}
|
|
return;
|
|
}
|
|
throw new Exception("Invalid Ident: " . $Ident);
|
|
}
|
|
|
|
public function Update()
|
|
{
|
|
if (!GetValue($this->GetIDForIdent("State"))) {
|
|
return;
|
|
}
|
|
|
|
$batteries = json_decode($this->ReadPropertyString("Batteries"), true);
|
|
if (!is_array($batteries) || count($batteries) === 0) {
|
|
return;
|
|
}
|
|
|
|
$sdlPowerW = max(0, (int)$this->ReadPropertyInteger("SDL_Leistung"));
|
|
$reserveH = 0.5; // 30 Minuten
|
|
$eps = 0.0001;
|
|
|
|
// Summe Batterie-Maxleistungen
|
|
$sumBatPowerW = 0;
|
|
foreach ($batteries as $b) {
|
|
$p = (int)($b["powerbat"] ?? 0);
|
|
if ($p > 0) $sumBatPowerW += $p;
|
|
}
|
|
if ($sumBatPowerW <= 0) return;
|
|
|
|
// SDL begrenzen
|
|
if ($sdlPowerW > $sumBatPowerW) $sdlPowerW = $sumBatPowerW;
|
|
|
|
// Summen Leistung
|
|
$P_SDL_max = 0.0;
|
|
$P_EV_max = 0.0;
|
|
|
|
$P_SDL_laden = 0.0;
|
|
$P_SDL_entladen = 0.0;
|
|
$P_EV_laden = 0.0;
|
|
$P_EV_entladen = 0.0;
|
|
|
|
// Summen Energie für Prozentberechnung (deine Logik)
|
|
$sumCap_kWh = 0.0;
|
|
|
|
$sumSDLreq_kWh = 0.0; // Sum(E_req)
|
|
$sumSDLavail_kWh = 0.0; // Sum(min(E_ist, E_req))
|
|
|
|
$sumEVavail_kWh = 0.0; // Sum(max(E_ist - E_req, 0))
|
|
|
|
$calc = [];
|
|
|
|
foreach ($batteries as $b) {
|
|
|
|
$typ = (string)($b["typ"] ?? "");
|
|
$pBat = (int)($b["powerbat"] ?? 0);
|
|
$cap = (float)($b["capazity"] ?? 0);
|
|
if ($pBat <= 0 || $cap <= 0) continue;
|
|
|
|
$sumCap_kWh += $cap;
|
|
|
|
$socPct = $this->ReadSocPercent((int)($b["soc"] ?? 0));
|
|
$E_ist = $cap * $socPct / 100.0; // kWh
|
|
|
|
// SDL Anteil Leistung proportional
|
|
$pSDL = min(($sdlPowerW * $pBat) / $sumBatPowerW, (float)$pBat);
|
|
if ($pSDL < 0) $pSDL = 0.0;
|
|
|
|
$pEV = max(0.0, $pBat - $pSDL);
|
|
|
|
$P_SDL_max += $pSDL;
|
|
$P_EV_max += $pEV;
|
|
|
|
// E_req für 30 min SDL (kWh)
|
|
$E_req = ($pSDL * $reserveH) / 1000.0;
|
|
|
|
$sumSDLreq_kWh += $E_req;
|
|
|
|
// SDL verfügbar nach deiner Skizze: min(E_ist, E_req)
|
|
$SDL_avail = min($E_ist, $E_req);
|
|
$sumSDLavail_kWh += $SDL_avail;
|
|
|
|
// EV verfügbar nach deiner Skizze: max(E_ist - E_req, 0)
|
|
$EV_avail = max($E_ist - $E_req, 0.0);
|
|
$sumEVavail_kWh += $EV_avail;
|
|
|
|
// Leistungsmaxima (Richtung) nach Reservierungslogik
|
|
// SDL entladen geht nur, wenn genug Energie für 30min vorhanden
|
|
$canSDLdis = ($E_ist + $eps) >= $E_req;
|
|
|
|
// SDL laden geht nur, wenn genug Platz für 30min vorhanden
|
|
$canSDLch = (($cap - $E_ist) + $eps) >= $E_req;
|
|
|
|
if ($canSDLdis) $P_SDL_entladen += $pSDL;
|
|
if ($canSDLch) $P_SDL_laden += $pSDL;
|
|
|
|
// EV entladen geht nur, wenn Energie über SDL-Reserve liegt
|
|
if (($E_ist - $E_req) > $eps) $P_EV_entladen += $pEV;
|
|
|
|
// EV laden (einfach): solange noch Platz ist
|
|
if (($cap - $E_ist) > $eps) $P_EV_laden += $pEV;
|
|
|
|
$calc[] = [
|
|
"typ" => $typ,
|
|
"pBat_W" => $pBat,
|
|
"cap_kWh" => round($cap, 3),
|
|
"soc_pct" => round($socPct, 2),
|
|
"E_ist_kWh" => round($E_ist, 3),
|
|
|
|
"pSDL_W" => round($pSDL, 0),
|
|
"E_req_kWh" => round($E_req, 3),
|
|
"SDL_avail_kWh" => round($SDL_avail, 3),
|
|
|
|
"pEV_W" => round($pEV, 0),
|
|
"EV_avail_kWh" => round($EV_avail, 3),
|
|
|
|
"canSDLcharge" => $canSDLch,
|
|
"canSDLdischarge" => $canSDLdis
|
|
];
|
|
}
|
|
|
|
// EV max Energie = SumCap - SumSDLreq (deine 77 - 16 = 61)
|
|
$EV_max_kWh = max(0.0, $sumCap_kWh - $sumSDLreq_kWh);
|
|
|
|
// SDL% und EV% (deine Darstellung)
|
|
$SDL_pct = ($sumSDLreq_kWh > $eps) ? ($sumSDLavail_kWh / $sumSDLreq_kWh) * 100.0 : 0.0;
|
|
$EV_pct = ($EV_max_kWh > $eps) ? ($sumEVavail_kWh / $EV_max_kWh) * 100.0 : 0.0;
|
|
|
|
// Setzen Variablen
|
|
SetValue($this->GetIDForIdent("SDL_Pos"), round($SDL_pct, 2));
|
|
SetValue($this->GetIDForIdent("SoC_EV"), round($EV_pct, 2));
|
|
|
|
SetValue($this->GetIDForIdent("P_SDL_max"), round($P_SDL_max, 0));
|
|
SetValue($this->GetIDForIdent("P_SDL_laden"), round($P_SDL_laden, 0));
|
|
SetValue($this->GetIDForIdent("P_SDL_entladen"), round($P_SDL_entladen, 0));
|
|
|
|
SetValue($this->GetIDForIdent("P_EV_max"), round($P_EV_max, 0));
|
|
SetValue($this->GetIDForIdent("P_EV_laden"), round($P_EV_laden, 0));
|
|
SetValue($this->GetIDForIdent("P_EV_entladen"), round($P_EV_entladen, 0));
|
|
|
|
// Debug JSON mit Summen
|
|
$out = [
|
|
"SDL_W" => $sdlPowerW,
|
|
"SumBatPower_W" => $sumBatPowerW,
|
|
"Reserve_h" => $reserveH,
|
|
|
|
"SumCap_kWh" => round($sumCap_kWh, 3),
|
|
|
|
"SumSDLreq_kWh" => round($sumSDLreq_kWh, 3),
|
|
"SumSDLavail_kWh" => round($sumSDLavail_kWh, 3),
|
|
"SDL_pct" => round($SDL_pct, 2),
|
|
|
|
"EV_max_kWh" => round($EV_max_kWh, 3),
|
|
"SumEVavail_kWh" => round($sumEVavail_kWh, 3),
|
|
"EV_pct" => round($EV_pct, 2),
|
|
|
|
"P_SDL_max_W" => round($P_SDL_max, 0),
|
|
"P_SDL_laden_W" => round($P_SDL_laden, 0),
|
|
"P_SDL_entladen_W" => round($P_SDL_entladen, 0),
|
|
|
|
"P_EV_max_W" => round($P_EV_max, 0),
|
|
"P_EV_laden_W" => round($P_EV_laden, 0),
|
|
"P_EV_entladen_W" => round($P_EV_entladen, 0),
|
|
|
|
"batteries" => $calc
|
|
];
|
|
|
|
SetValue($this->GetIDForIdent("CalcJSON"), json_encode($out, JSON_PRETTY_PRINT));
|
|
}
|
|
|
|
private function ReadSocPercent(int $varId): float
|
|
{
|
|
if ($varId <= 0 || !IPS_VariableExists($varId)) return 0.0;
|
|
$v = GetValue($varId);
|
|
if (!is_numeric($v)) return 0.0;
|
|
|
|
$f = (float)$v;
|
|
if ($f >= 0.0 && $f <= 1.0) $f *= 100.0;
|
|
|
|
return max(0.0, min(100.0, $f));
|
|
}
|
|
}
|