Files
2026-05-27 11:41:25 +02:00

200 lines
6.5 KiB
PHP

<?php
class Peakshaving extends IPSModule
{
public function Create()
{
parent::Create();
// Ziel-Instanzen und Property-Namen
$this->RegisterPropertyInteger('CapacityTargetInstance', 0);
$this->RegisterPropertyString('CapacityTargetProperty', 'AufdasNachladen');
$this->RegisterPropertyInteger('PowerTargetInstance', 0);
$this->RegisterPropertyString('PowerTargetProperty', 'Peakleistung');
// Monatswerte
$this->RegisterPropertyFloat('CapacityJanuary', 11);
$this->RegisterPropertyFloat('PowerJanuary', 42000);
$this->RegisterPropertyFloat('CapacityFebruary', 11);
$this->RegisterPropertyFloat('PowerFebruary', 42000);
$this->RegisterPropertyFloat('CapacityMarch', 8);
$this->RegisterPropertyFloat('PowerMarch', 43000);
$this->RegisterPropertyFloat('CapacityApril', 8);
$this->RegisterPropertyFloat('PowerApril', 44000);
$this->RegisterPropertyFloat('CapacitySummer', 8);
$this->RegisterPropertyFloat('PowerSummer', 44000);
$this->RegisterPropertyFloat('CapacityOctober', 12);
$this->RegisterPropertyFloat('PowerOctober', 43000);
$this->RegisterPropertyFloat('CapacityNovember', 12);
$this->RegisterPropertyFloat('PowerNovember', 43000);
$this->RegisterPropertyFloat('CapacityDecember', 12);
$this->RegisterPropertyFloat('PowerDecember', 43000);
// Statusvariablen
$this->RegisterVariableFloat('ActiveCapacity', 'Kapazität Peakshaving', '~Intensity.100', 10);
$this->RegisterVariableFloat('ActivePower', 'Leistung Peakshaving', '~Watt', 20);
$this->EnableAction('ActiveCapacity');
$this->EnableAction('ActivePower');
// Einmal täglich prüfen/übernehmen
$this->RegisterTimer('DailyUpdate', 0, 'GEF_ApplyCurrentValues($_IPS[\'TARGET\']);');
}
public function ApplyChanges()
{
parent::ApplyChanges();
$this->SetTimerInterval('DailyUpdate', 24 * 60 * 60 * 1000);
if (IPS_GetName($this->InstanceID) == '') {
IPS_SetName($this->InstanceID, 'Peakshaving');
}
}
public function RequestAction($Ident, $Value)
{
switch ($Ident) {
case 'ActiveCapacity':
SetValueFloat($this->GetIDForIdent('ActiveCapacity'), (float)$Value);
$this->ApplyCapacityToTarget((float)$Value);
break;
case 'ActivePower':
SetValueFloat($this->GetIDForIdent('ActivePower'), (float)$Value);
$this->ApplyPowerToTarget((float)$Value);
break;
default:
throw new Exception('Ungültiger Ident: ' . $Ident);
}
}
public function ApplyCurrentValues()
{
$values = $this->GetValuesForCurrentMonth();
SetValueFloat($this->GetIDForIdent('ActiveCapacity'), $values['capacity']);
SetValueFloat($this->GetIDForIdent('ActivePower'), $values['power']);
$this->ApplyCapacityToTarget($values['capacity']);
$this->ApplyPowerToTarget($values['power']);
return 'Peakshaving Werte wurden übernommen.';
}
private function GetValuesForCurrentMonth()
{
$month = (int)date('n');
switch ($month) {
case 1:
return [
'capacity' => $this->ReadPropertyFloat('CapacityJanuary'),
'power' => $this->ReadPropertyFloat('PowerJanuary')
];
case 2:
return [
'capacity' => $this->ReadPropertyFloat('CapacityFebruary'),
'power' => $this->ReadPropertyFloat('PowerFebruary')
];
case 3:
return [
'capacity' => $this->ReadPropertyFloat('CapacityMarch'),
'power' => $this->ReadPropertyFloat('PowerMarch')
];
case 4:
return [
'capacity' => $this->ReadPropertyFloat('CapacityApril'),
'power' => $this->ReadPropertyFloat('PowerApril')
];
case 5:
case 6:
case 7:
case 8:
case 9:
return [
'capacity' => $this->ReadPropertyFloat('CapacitySummer'),
'power' => $this->ReadPropertyFloat('PowerSummer')
];
case 10:
return [
'capacity' => $this->ReadPropertyFloat('CapacityOctober'),
'power' => $this->ReadPropertyFloat('PowerOctober')
];
case 11:
return [
'capacity' => $this->ReadPropertyFloat('CapacityNovember'),
'power' => $this->ReadPropertyFloat('PowerNovember')
];
case 12:
return [
'capacity' => $this->ReadPropertyFloat('CapacityDecember'),
'power' => $this->ReadPropertyFloat('PowerDecember')
];
}
return [
'capacity' => 0,
'power' => 0
];
}
private function ApplyCapacityToTarget(float $value)
{
$instanceID = $this->ReadPropertyInteger('CapacityTargetInstance');
$property = $this->ReadPropertyString('CapacityTargetProperty');
$this->ApplyPropertyToInstance($instanceID, $property, $value);
}
private function ApplyPowerToTarget(float $value)
{
$instanceID = $this->ReadPropertyInteger('PowerTargetInstance');
$property = $this->ReadPropertyString('PowerTargetProperty');
$this->ApplyPropertyToInstance($instanceID, $property, $value);
}
private function ApplyPropertyToInstance(int $instanceID, string $property, float $value)
{
if ($instanceID <= 0) {
return;
}
if (!IPS_InstanceExists($instanceID)) {
$this->SendDebug('Peakshaving', 'Ziel-Instanz existiert nicht: ' . $instanceID, 0);
return;
}
if ($property === '') {
$this->SendDebug('Peakshaving', 'Kein Property-Name gesetzt.', 0);
return;
}
IPS_SetProperty($instanceID, $property, $value);
if (!IPS_ApplyChanges($instanceID)) {
$this->SendDebug(
'Peakshaving',
'IPS_ApplyChanges fehlgeschlagen für Instanz ' . $instanceID . ', Property ' . $property,
0
);
}
}
}
?>