Files
Symcon_Belevo_Energiemanage…/Verbraucher_extern/module.php
2025-04-14 09:47:11 +02:00

148 lines
5.1 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
class Verbraucher_extern extends IPSModule
{
private $timerID;
public function Create()
{
parent::Create();
// Verbraucherspezifische Properties
$this->RegisterPropertyString("Verbraucher_Liste", "");
$this->RegisterPropertyInteger("Is_Peak", 0);
$this->RegisterPropertyInteger("delta_power", 0);
$this->RegisterPropertyInteger("Interval", 5); // Recheninterval
// Variabeln für Kommunkation mit Manager
$this->RegisterVariableInteger("Sperre_Prio", "Sperre_Prio");
$this->RegisterVariableInteger("PV_Prio", "PV_Prio");
$this->RegisterVariableBoolean("Idle", "Idle", "", 0);
$this->RegisterVariableInteger("Aktuelle_Leistung", "Aktuelle_Leistung", "", 0);
$this->RegisterVariableFloat("Bezogene_Energie", "Bezogene_Energie", "", 0);
$this->RegisterVariableString("PowerSteps", "PowerSteps");
$this->RegisterVariableInteger("Power", "Power");
$this->RegisterVariableBoolean("Is_Peak_Shaving", "Is_Peak_Shaving");
$this->RegisterVariableInteger("Leistung_Delta", "Leistung_Delta", "", 0);
// Hilfsvariabeln für Idle zustand
$this->RegisterPropertyInteger("IdleCounterMax", 2);
$this->RegisterVariableInteger("IdleCounter", "IdleCounter", "", 0);
$this->SetValue("IdleCounter", 0);
// Initialisiere Idle
$this->SetValue("Idle", true);
$this->RegisterTimer("Timer_Do_UserCalc_Verb",$this->ReadPropertyInteger("Interval")*1000,"IPS_RequestAction(" .$this->InstanceID .', "Do_UserCalc", "");');
}
public function ApplyChanges()
{
parent::ApplyChanges();
$this->SetTimerInterval("Timer_Do_UserCalc_Verb",$this->ReadPropertyInteger("Interval")*1000);
}
public function RequestAction($Ident, $Value)
{
switch ($Ident) {
case "SetAktuelle_Leistung":
$this->SetValue("Power", (int)$Value);
break;
case "GetCurrentData":
$this->SetValue("Is_Peak_Shaving", (bool)$Value);
break;
case "Do_UserCalc":
$this->SetAktuelle_Leistung($this->GetValue("Power"));
$this->GetCurrentData($this->GetValue("Is_Peak_Shaving"));
break;
default:
throw new Exception("Invalid Ident");
}
}
// Methode zum Setzen des aktuellen Stromverbrauchs
public function SetAktuelle_Leistung(float $power) {
$this->CheckIdle($power);
$verbraucherListe = json_decode($this->ReadPropertyString("Verbraucher_Liste"), true);
$kombinationen = $this->kombinationenRekursiv($verbraucherListe, $power);
        // Setze die Werte in Write_Var
foreach ($verbraucherListe as $verbraucher) {
$writeVarID = $verbraucher['Write_Var'];
$pNenn = $verbraucher['P_Nenn'];
$this->SetValue($writeVarID, in_array($pNenn, $kombinationen) ? 1 : 0);
}
$this->SetValue("Leistung_Delta", $power - array_sum($kombinationen));
}
    // Methode zum Abrufen der aktuellen Daten
public function GetCurrentData(bool $Peak)
{
        // Setze die Is_Peak_Shaving Variable
$this->SetValue("Is_Peak_Shaving", $Peak);
$verbraucherListe = json_decode($this->ReadPropertyString("Verbraucher_Liste"), true);
$kombinationen = $this->berechneKombinationen($verbraucherListe);
$this->SetValue("PowerSteps", implode(", ", $kombinationen));
}
private function berechneKombinationen(array $verbraucherListe, float $power = null)
{
$kombinationen = [];
$this->kombinationenRekursiv($verbraucherListe, [], $kombinationen, $power);
return $kombinationen;
}
private function kombinationenRekursiv(array $verbraucherListe, array $aktuelleKombi, array &$kombinationen, float $power = null)
{
if (empty($verbraucherListe)) {
$summe = array_sum($aktuelleKombi);
if ($power === null || $summe <= $power) {
$kombinationen[] = $summe;
}
return;
}
$verbraucher = array_shift($verbraucherListe);
if ($this->GetValue($verbraucher['Read_Var']) == 1) {
$this->kombinationenRekursiv($verbraucherListe, array_merge($aktuelleKombi, [$verbraucher['P_Nenn']]), $kombinationen, $power);
}
$this->kombinationenRekursiv($verbraucherListe, $aktuelleKombi, $kombinationen, $power);
}
public function CheckIdle($power)
{
$lastpower = GetValue($this->GetIDForIdent("Aktuelle_Leistung"));
if ($lastpower != $power) {
$this->SetValue("Idle", false);
$this->SetValue("IdleCounter",$this->ReadPropertyInteger("IdleCounterMax"));
}
// IdleCounter auslesen und verarbeiten
$idleCounter = $this->GetValue("IdleCounter");
if ($idleCounter > 0) {
$this->SetValue("Idle", false);
$this->SetValue("IdleCounter", $idleCounter - 1);
} else {
$this->SetValue("Idle", true);
}
}
}
?>