added sync from linked variable
This commit is contained in:
1 parent
faa518ccd8
commit
ac0621ffe4
6 files changed
+89
-5
No files matched your search
@@ -83,7 +83,7 @@ class EnergyCounterMain extends IPSModule
|
||||
}
|
||||
|
||||
IPS_SetName($childID, $counter['Name']);
|
||||
$this->ConfigureChild($childID, $key);
|
||||
$this->ConfigureChild($childID, $key, $counter['VariableID']);
|
||||
$children[$key] = $childID;
|
||||
}
|
||||
|
||||
@@ -177,6 +177,60 @@ class EnergyCounterMain extends IPSModule
|
||||
$this->ResetEnergy('monthEnergy');
|
||||
}
|
||||
|
||||
/**
|
||||
* Übernimmt den aktuellen Rohzählerstand als Gesamtenergie eines einzelnen
|
||||
* Counters. Diese Methode wird über den Initialisierungsbutton des Childs
|
||||
* aufgerufen, die Zustandsführung bleibt aber im Hauptmodul.
|
||||
*/
|
||||
public function SetCounterTotalEnergyFromSource(string $counterKey): bool
|
||||
{
|
||||
// Noch nicht verarbeitete Änderungen sollen Tages- und Monatswerte
|
||||
// weiterhin korrekt fortschreiben, bevor nur der Gesamtwert gesetzt wird.
|
||||
$this->UpdateValues();
|
||||
if (!$this->EnterLock()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$counter = null;
|
||||
foreach ($this->GetCounters() as $configuredCounter) {
|
||||
if ($configuredCounter['Key'] === $counterKey) {
|
||||
$counter = $configuredCounter;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($counter === null || !IPS_VariableExists($counter['VariableID'])) {
|
||||
$this->Log('Gesamtenergie konnte nicht gesetzt werden: Quellzähler für ' . $counterKey . ' ist nicht verfügbar.');
|
||||
return false;
|
||||
}
|
||||
|
||||
$sourceValue = GetValue($counter['VariableID']);
|
||||
if (!is_numeric($sourceValue)) {
|
||||
$this->Log('Gesamtenergie konnte nicht gesetzt werden: Quellzähler für ' . $counter['Name'] . ' enthält keinen numerischen Wert.');
|
||||
return false;
|
||||
}
|
||||
|
||||
$states = $this->ReadJsonAttribute('CounterState', []);
|
||||
$state = $this->NormalizeState($states[$counterKey] ?? [], date('Y-m-d'), date('Y-m'));
|
||||
$currentValue = (float) $sourceValue;
|
||||
$state['initialized'] = true;
|
||||
$state['lastRaw'] = $currentValue;
|
||||
$state['totalEnergy'] = max(0.0, $currentValue);
|
||||
$states[$counterKey] = $state;
|
||||
$this->WriteAttributeString('CounterState', json_encode($states));
|
||||
|
||||
$children = $this->ReadJsonAttribute('CounterChildren', []);
|
||||
$childID = isset($children[$counterKey]) ? (int) $children[$counterKey] : 0;
|
||||
if ($childID > 0 && IPS_InstanceExists($childID)) {
|
||||
$this->WriteChildValues($childID, $state, $counter['PricePerKWh']);
|
||||
}
|
||||
|
||||
return true;
|
||||
} finally {
|
||||
$this->LeaveLock();
|
||||
}
|
||||
}
|
||||
|
||||
private function ResetEnergy(string $field)
|
||||
{
|
||||
// Vor dem Reset wird ein möglicherweise noch nicht verarbeiteter Anstieg
|
||||
@@ -333,7 +387,7 @@ class EnergyCounterMain extends IPSModule
|
||||
return ($instance['ModuleInfo']['ModuleID'] ?? '') === self::CHILD_MODULE_ID;
|
||||
}
|
||||
|
||||
private function ConfigureChild(int $childID, string $key)
|
||||
private function ConfigureChild(int $childID, string $key, int $sourceVariableID)
|
||||
{
|
||||
$configuration = json_decode(IPS_GetConfiguration($childID), true);
|
||||
if (!is_array($configuration)) {
|
||||
@@ -341,6 +395,7 @@ class EnergyCounterMain extends IPSModule
|
||||
}
|
||||
$configuration['ParentInstanceID'] = $this->InstanceID;
|
||||
$configuration['CounterKey'] = $key;
|
||||
$configuration['SourceVariableID'] = $sourceVariableID;
|
||||
IPS_SetConfiguration($childID, json_encode($configuration));
|
||||
IPS_ApplyChanges($childID);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user