189 lines
7.0 KiB
PHP
189 lines
7.0 KiB
PHP
<?php
|
|
class Verbraucher_1_Stufig extends IPSModule {
|
|
|
|
private $timerID;
|
|
|
|
public function Create() {
|
|
parent::Create();
|
|
|
|
// Prioritäten
|
|
$this->RegisterVariableInteger("LockPrio", "LockPrio");
|
|
$this->RegisterVariableInteger("UserPrio", "UserPrio");
|
|
|
|
// Energiehandling
|
|
$this->RegisterVariableBoolean("Idle", "Idle", "", 0);
|
|
$this->RegisterVariableBoolean("IstNacht", "IstNacht", "", 0);
|
|
$this->RegisterVariableInteger("CurrentPower", "CurrentPower", "", 0);
|
|
$this->RegisterVariableInteger("DailyOnTime", "DailyOnTime", "", 0);
|
|
$this->RegisterVariableFloat("UsedEnergy", "UsedEnergy", "", 0);
|
|
$this->RegisterVariableString("PowerSteps", "PowerSteps"); // PowerSteps-Variable registrieren
|
|
|
|
// Neue Variable für den Timer-Status
|
|
$this->RegisterVariableBoolean("IsTimerActive", "IsTimerActive", "", 0);
|
|
$this->SetValue("IsTimerActive", false);
|
|
|
|
// Trägheit
|
|
$this->RegisterPropertyInteger("IdleCounterMax", 4);
|
|
$this->RegisterVariableInteger("IdleCounter", "IdleCounter", "", 0);
|
|
$this->SetValue("IdleCounter", 0);
|
|
|
|
$this->RegisterPropertyInteger("BoilerLeistung", 6000); // Standardwert für Volllast
|
|
$this->RegisterPropertyInteger("Mindesttlaufzeit", 4); // Standardwert für Volllast
|
|
$this->RegisterPropertyInteger("Zeit_Zwischen_Zustandswechseln", 1);
|
|
$this->RegisterPropertyInteger("Schaltkontakt1", 0);
|
|
|
|
// Timer für Zeit_Zwischen_Zustandswechseln
|
|
$this->RegisterTimer("ZustandswechselTimer", 0, 'IPS_RequestAction(' . $this->InstanceID . ', "ResetPowerSteps", "");');
|
|
|
|
|
|
|
|
//Initialisieren
|
|
$this->SetValue("Idle", true);
|
|
}
|
|
|
|
public function ApplyChanges() {
|
|
parent::ApplyChanges();
|
|
}
|
|
|
|
// Aktionen verarbeiten
|
|
public function RequestAction($Ident, $Value) {
|
|
switch ($Ident) {
|
|
case "SetCurrentPower":
|
|
$this->SetCurrentPower($Value);
|
|
break;
|
|
case "GetCurrentData":
|
|
return $this->GetCurrentData($Value);
|
|
break;
|
|
case "ResetPowerSteps":
|
|
$this->ResetPowerSteps($Value);
|
|
break;
|
|
default:
|
|
throw new Exception("Invalid Ident");
|
|
}
|
|
}
|
|
|
|
|
|
// Methode zum Setzen der PowerSteps und Timer starten
|
|
public function SetTimerOn() {
|
|
|
|
// Timer setzen, der nach "Zeit_Zwischen_Zustandswechseln" abläuft
|
|
$zeitZwischenZustandswechseln = $this->ReadPropertyInteger("Zeit_Zwischen_Zustandswechseln");
|
|
$this->SetTimerInterval("ZustandswechselTimer", $zeitZwischenZustandswechseln * 60000); // Timer in Millisekunden
|
|
IPS_LogMessage("Verbraucher", "In Set Methode");
|
|
|
|
// Timer-Status auf true setzen
|
|
$this->SetValue("IsTimerActive", true);
|
|
|
|
}
|
|
|
|
// Methode zum Zurücksetzen von PowerSteps nach Ablauf des Timers
|
|
public function ResetPowerSteps() {
|
|
// PowerSteps wieder auf den ursprünglichen Zustand setzen (wie vorherige Funktionalität)
|
|
$this->SetValue("PowerSteps", json_encode([$this->GetValue("CurrentPower")]));
|
|
IPS_LogMessage("Verbraucher", "In Reset Methode");
|
|
|
|
// Timer stoppen
|
|
$this->SetTimerInterval("ZustandswechselTimer", 0);
|
|
|
|
// Timer-Status auf false setzen
|
|
$this->SetValue("IsTimerActive", false);
|
|
}
|
|
// Methode zum Setzen des aktuellen Stromverbrauchs
|
|
public function SetCurrentPower(float $power) {
|
|
$this->CheckIdle($power);
|
|
if($this->GetValue("CurrentPower")!=$power){
|
|
$this->SetTimerOn();
|
|
}
|
|
$this->SetValue("CurrentPower", $power);
|
|
$boilerLeistung = $this->ReadPropertyInteger("BoilerLeistung");
|
|
$schaltkontaktID = $this->ReadPropertyInteger("Schaltkontakt1");
|
|
|
|
if ($power == $boilerLeistung) {
|
|
$schaltkontaktStatus = true;
|
|
} elseif ($power == 0) {
|
|
$schaltkontaktStatus = false;
|
|
} else {
|
|
// Keine Änderung, wenn power nicht 0 oder boilerLeistung entspricht
|
|
return;
|
|
}
|
|
|
|
$currentStatus = GetValue($this->ReadPropertyInteger("Schaltkontakt1"));
|
|
|
|
// Schaltkontaktstatus ändern
|
|
SetValue($this->ReadPropertyInteger("Schaltkontakt1"), $schaltkontaktStatus);
|
|
|
|
if($schaltkontaktStatus){
|
|
$this->SetValue("DailyOnTime", $this->GetValue("DailyOnTime") + 1);
|
|
}
|
|
}
|
|
|
|
// Methode zum Abrufen der aktuellen Daten
|
|
public function GetCurrentData(bool $Peak) {
|
|
$IstNacht = $this->GetValue("IstNacht");
|
|
$NeuesIstNacht = $this->ist_nachts();
|
|
|
|
if($IstNacht == true && $NeuesIstNacht == false){
|
|
$this->SetValue("DailyOnTime", 0);
|
|
}
|
|
|
|
$this->SetValue("IstNacht", $NeuesIstNacht);
|
|
|
|
$DailyOnTime = $this->GetValue("DailyOnTime");
|
|
$Mindestlaufzeit = $this->ReadPropertyInteger("Mindesttlaufzeit") * 60 * 12;
|
|
|
|
// Überprüfen, ob der Timer aktiv ist
|
|
if($this->GetValue("IsTimerActive")) {
|
|
// Timer ist aktiv, PowerSteps setzen
|
|
$this->SetValue("PowerSteps", json_encode([$this->GetValue("CurrentPower")]));
|
|
}
|
|
// Wenn Nacht und Mindestlaufzeit nicht erreicht ist
|
|
else if($NeuesIstNacht && ($DailyOnTime < $Mindestlaufzeit)) {
|
|
if($Peak) {
|
|
$this->SetValue("PowerSteps", json_encode([0, $this->ReadPropertyInteger("BoilerLeistung")]));
|
|
} else {
|
|
$this->SetValue("PowerSteps", json_encode([$this->ReadPropertyInteger("BoilerLeistung")]));
|
|
}
|
|
}
|
|
// Andernfalls
|
|
else {
|
|
if($Peak) {
|
|
$this->SetValue("PowerSteps", json_encode([0]));
|
|
} else {
|
|
$this->SetValue("PowerSteps", json_encode([0, $this->ReadPropertyInteger("BoilerLeistung")]));
|
|
}
|
|
}
|
|
}
|
|
|
|
public function CheckIdle($power){
|
|
$lastpower = GetValue($this->GetIDForIdent("CurrentPower"));
|
|
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);
|
|
}
|
|
}
|
|
|
|
private function ist_nachts() {
|
|
date_default_timezone_set('Europe/Berlin'); // Setze hier deine Zeitzone
|
|
|
|
$aktuelle_zeit = strtotime(date('H:i')); // Aktuelle Zeit in Stunden und Minuten umwandeln
|
|
$start_nacht = strtotime('22:00'); // Startzeit der Nacht (22 Uhr)
|
|
$ende_nacht = strtotime('07:00'); // Endzeit der Nacht (7 Uhr)
|
|
|
|
if ($aktuelle_zeit >= $start_nacht || $aktuelle_zeit < $ende_nacht) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|