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"); } } 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); } // 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); 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)); } public function GetCurrentData(bool $Peak) { $this->SetValue("Is_Peak_Shaving", $Peak); $verbraucherListe = json_decode($this->ReadPropertyString("Verbraucher_Liste"), true); $kombinationen = $this->berechneKombinationen($verbraucherListe); $this->SetValue("PowerSteps", implode(", ", $kombinationen)); } public function CheckIdle($power) { $lastpower = GetValue($this->GetIDForIdent("Aktuelle_Leistung")); if ($lastpower != $power) { $this->SetValue("Idle", false); $this->SetValue("IdleCounter",$this->ReadPropertyInteger("IdleCounterMax")); } $idleCounter = $this->GetValue("IdleCounter"); if ($idleCounter > 0) { $this->SetValue("Idle", false); $this->SetValue("IdleCounter", $idleCounter - 1); } else { $this->SetValue("Idle", true); } } } ?>