102 lines
4.1 KiB
PHP
102 lines
4.1 KiB
PHP
<?php
|
|
|
|
class EnergyCounterChild extends IPSModule
|
|
{
|
|
private const ENERGY_PROFILE = 'EnergyCounter.kWh';
|
|
private const COST_PROFILE = 'EnergyCounter.EUR';
|
|
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
// Diese Eigenschaften werden vom Hauptmodul verwaltet und absichtlich
|
|
// nicht im Formular angeboten. Die Childinstanz bleibt reine Anzeige.
|
|
$this->RegisterPropertyInteger('ParentInstanceID', 0);
|
|
$this->RegisterPropertyString('CounterKey', '');
|
|
$this->RegisterPropertyInteger('SourceVariableID', 0);
|
|
|
|
$this->RegisterPropertyBoolean('ShowTotalEnergy', true);
|
|
$this->RegisterPropertyBoolean('ShowTotalCost', true);
|
|
$this->RegisterPropertyBoolean('ShowDayEnergy', true);
|
|
$this->RegisterPropertyBoolean('ShowDayCost', true);
|
|
$this->RegisterPropertyBoolean('ShowMonthEnergy', true);
|
|
$this->RegisterPropertyBoolean('ShowMonthCost', true);
|
|
|
|
$this->EnsureProfiles();
|
|
$this->RegisterValueVariables();
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
|
|
$this->EnsureProfiles();
|
|
$this->RegisterValueVariables();
|
|
$this->ApplyVisibility();
|
|
$this->SetStatus(102);
|
|
}
|
|
|
|
/** Setzt die Gesamtenergie über das Hauptmodul auf den aktuellen Zählerstand. */
|
|
public function SetTotalEnergyToCurrentSource(): bool
|
|
{
|
|
$parentInstanceID = $this->ReadPropertyInteger('ParentInstanceID');
|
|
$counterKey = $this->ReadPropertyString('CounterKey');
|
|
if ($parentInstanceID <= 0 || $counterKey === '') {
|
|
IPS_LogMessage('EnergyCounterChild', 'Gesamtenergie kann nicht gesetzt werden: Zuordnung zum Hauptmodul fehlt.');
|
|
return false;
|
|
}
|
|
|
|
$function = 'ECM_SetCounterTotalEnergyFromSource';
|
|
if (!function_exists($function)) {
|
|
IPS_LogMessage('EnergyCounterChild', 'Gesamtenergie kann nicht gesetzt werden: Hauptmodul-Funktion ist nicht verfügbar.');
|
|
return false;
|
|
}
|
|
|
|
return $function($parentInstanceID, $counterKey);
|
|
}
|
|
|
|
private function RegisterValueVariables()
|
|
{
|
|
$this->RegisterVariableFloat('TotalEnergy', $this->Translate('Total Energy'), self::ENERGY_PROFILE, 10);
|
|
$this->RegisterVariableFloat('TotalCost', $this->Translate('Total Cost'), self::COST_PROFILE, 20);
|
|
$this->RegisterVariableFloat('DayEnergy', $this->Translate('Daily Energy'), self::ENERGY_PROFILE, 30);
|
|
$this->RegisterVariableFloat('DayCost', $this->Translate('Daily Cost'), self::COST_PROFILE, 40);
|
|
$this->RegisterVariableFloat('MonthEnergy', $this->Translate('Monthly Energy'), self::ENERGY_PROFILE, 50);
|
|
$this->RegisterVariableFloat('MonthCost', $this->Translate('Monthly Cost'), self::COST_PROFILE, 60);
|
|
}
|
|
|
|
private function ApplyVisibility()
|
|
{
|
|
$visibility = [
|
|
'TotalEnergy' => $this->ReadPropertyBoolean('ShowTotalEnergy'),
|
|
'TotalCost' => $this->ReadPropertyBoolean('ShowTotalCost'),
|
|
'DayEnergy' => $this->ReadPropertyBoolean('ShowDayEnergy'),
|
|
'DayCost' => $this->ReadPropertyBoolean('ShowDayCost'),
|
|
'MonthEnergy' => $this->ReadPropertyBoolean('ShowMonthEnergy'),
|
|
'MonthCost' => $this->ReadPropertyBoolean('ShowMonthCost')
|
|
];
|
|
|
|
foreach ($visibility as $ident => $visible) {
|
|
$variableID = $this->GetIDForIdent($ident);
|
|
if ($variableID > 0) {
|
|
IPS_SetHidden($variableID, !$visible);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function EnsureProfiles()
|
|
{
|
|
if (!IPS_VariableProfileExists(self::ENERGY_PROFILE)) {
|
|
IPS_CreateVariableProfile(self::ENERGY_PROFILE, 2);
|
|
IPS_SetVariableProfileText(self::ENERGY_PROFILE, '', ' kWh');
|
|
IPS_SetVariableProfileDigits(self::ENERGY_PROFILE, 3);
|
|
}
|
|
|
|
if (!IPS_VariableProfileExists(self::COST_PROFILE)) {
|
|
IPS_CreateVariableProfile(self::COST_PROFILE, 2);
|
|
IPS_SetVariableProfileText(self::COST_PROFILE, '', ' €');
|
|
IPS_SetVariableProfileDigits(self::COST_PROFILE, 2);
|
|
}
|
|
}
|
|
}
|