Tests / test (push) Failing after 49s
Merge the V4 development history with current manager, SDL and setpoint fixes. Retain guarded trial behavior and consolidate controls in Prognose / Forecast. Exclude the unverified accounting-evidence change; no runtime deployment or new dispatch permission. Validated: 357 PHPUnit tests / 1758 assertions, 150 PHP syntax checks, 310 backend tests, 16 portal tests and isolated UI/receiver checks. develop and beta publication explicitly approved by Daniel Haefliger.
131 lines
7.0 KiB
PHP
131 lines
7.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
namespace Belevo\EnelixEMS;
|
|
|
|
require_once __DIR__.'/NetzfahrplanV4Rueckmeldung.php';
|
|
require_once __DIR__.'/NetzfahrplanV4Geraeteabruf.php';
|
|
|
|
/** Shared confirmed physical reading used by receiver and battery before a trial command. */
|
|
trait BatterieNetzfahrplanV4RueckmeldungTrait
|
|
{
|
|
private function registriereV4Rueckmeldung(): void
|
|
{
|
|
$this->RegisterPropertyString('NetzfahrplanV4RueckmeldungKonfiguration', '{}');
|
|
$this->RegisterTimer('NetzfahrplanV4RueckmeldungAktualisieren', 0,
|
|
"IPS_RequestAction(\$_IPS['TARGET'], 'NetzfahrplanV4RueckmeldungAktualisieren', 0);");
|
|
}
|
|
|
|
private function konfiguriereV4Rueckmeldung(): void
|
|
{
|
|
$this->SetBuffer('V4ConfirmedFeedback', '');
|
|
$this->SetBuffer('V4ConfirmedFeedbackConfig', '');
|
|
$this->SetBuffer('V4ConfirmedFeedbackMono', '');
|
|
$configured = !in_array(trim($this->ReadPropertyString('NetzfahrplanV4RueckmeldungKonfiguration')), ['', '{}'], true);
|
|
$this->SetTimerInterval('NetzfahrplanV4RueckmeldungAktualisieren', $configured ? 1000 : 0);
|
|
}
|
|
|
|
/** No transport I/O under the battery control lock. Original sample times never move. */
|
|
private function v4BatterieRueckmeldung(): array
|
|
{
|
|
$json = $this->ReadPropertyString('NetzfahrplanV4RueckmeldungKonfiguration');
|
|
$sample = json_decode($this->GetBuffer('V4ConfirmedFeedback'), true);
|
|
$mono = (float) $this->GetBuffer('V4ConfirmedFeedbackMono');
|
|
$now = time();$age = hrtime(true) / 1e9 - $mono;
|
|
if (!is_array($sample) || !is_int($sample['checkedAt'] ?? null)
|
|
|| $sample['checkedAt'] > $now || $now - $sample['checkedAt'] > 2
|
|
|| $age < 0 || $age > 2
|
|
|| $this->GetBuffer('V4ConfirmedFeedbackConfig') !== hash('sha256', $json)) {
|
|
throw new \RuntimeException('feedback_confirmed_cache_stale_or_invalid');
|
|
}
|
|
$config = json_decode($json, true, 32, JSON_THROW_ON_ERROR);
|
|
if (($config['batteryInstanceId'] ?? null) !== $this->InstanceID
|
|
|| !in_array($config['managerId'] ?? 0, $this->zugeordneteManagerIDs(), true)) {
|
|
throw new \RuntimeException('feedback_cache_binding_invalid');
|
|
}
|
|
foreach ($config['sources'] as $source) {
|
|
if (!IPS_VariableExists($source['variableId'])) throw new \RuntimeException('feedback_variable_missing');
|
|
$object = IPS_GetObject($source['variableId']);
|
|
if ($object['ParentID'] !== $source['parentId'] || $object['ObjectIdent'] !== $source['ident']) {
|
|
throw new \RuntimeException('feedback_cache_source_changed');
|
|
}
|
|
}
|
|
return $sample;
|
|
}
|
|
|
|
/** Independent acquisition; never waits behind another read or takes the actuator lock. */
|
|
private function aktualisiereV4Rueckmeldung(): void
|
|
{
|
|
$json=$this->ReadPropertyString('NetzfahrplanV4RueckmeldungKonfiguration');
|
|
if (strlen($json)>65536) throw new \RuntimeException('feedback_configuration_too_large');
|
|
$c=json_decode($json,true,32,JSON_THROW_ON_ERROR);
|
|
if (!is_array($c) || ($c['batteryInstanceId']??null)!==$this->InstanceID) throw new \RuntimeException('feedback_battery_binding_invalid');
|
|
if (!in_array($c['managerId']??0,$this->zugeordneteManagerIDs(),true)) throw new \RuntimeException('feedback_manager_binding_invalid');
|
|
|
|
$lock='ENELIX.V4.Feedback.'.$this->InstanceID;
|
|
if (!IPS_SemaphoreEnter($lock,0)) return;
|
|
try {
|
|
$reader=static function(int $id):array {
|
|
if (!IPS_VariableExists($id)) throw new \RuntimeException('feedback_variable_missing');
|
|
$v=IPS_GetVariable($id);$o=IPS_GetObject($id);$value=GetValue($id);
|
|
return ['value'=>$value,'updated'=>$v['VariableUpdated'],'changed'=>$v['VariableChanged'],
|
|
'parentID'=>$o['ParentID'],'ident'=>$o['ObjectIdent']];
|
|
};
|
|
$result=null;
|
|
for ($attempt=1;$attempt<=3;++$attempt) {
|
|
try {
|
|
$result=NetzfahrplanV4Geraeteabruf::snapshot(
|
|
$c,
|
|
static function(int $id):string {
|
|
if (!IPS_InstanceExists($id)) throw new \RuntimeException('feedback_parent_instance_missing');
|
|
return (string)(IPS_GetInstance($id)['ModuleInfo']['ModuleID']??'');
|
|
},
|
|
static function(int $id,string $module):bool {
|
|
if ($module===NetzfahrplanV4Geraeteabruf::MBUS_DEVICE) {
|
|
if (!function_exists('MBUS_UpdateValues') && !function_exists(__NAMESPACE__.'\\MBUS_UpdateValues')) throw new \RuntimeException('feedback_mbus_refresh_unavailable');
|
|
return MBUS_UpdateValues($id)===true;
|
|
}
|
|
if (in_array($module,[NetzfahrplanV4Geraeteabruf::MODBUS_DEVICE,NetzfahrplanV4Geraeteabruf::MODBUS_ADDRESS],true)) {
|
|
if (!function_exists('ModBus_RequestRead') && !function_exists(__NAMESPACE__.'\\ModBus_RequestRead')) throw new \RuntimeException('feedback_modbus_refresh_unavailable');
|
|
return ModBus_RequestRead($id)===true;
|
|
}
|
|
return false;
|
|
},
|
|
$reader,
|
|
static fn():int=>time(),
|
|
static function(int $microseconds):void { usleep($microseconds); }
|
|
);
|
|
break;
|
|
} catch (\InvalidArgumentException $e) {
|
|
if (!str_starts_with($e->getMessage(),'feedback_source_changed_after_confirmed_read:') || $attempt>=3) throw $e;
|
|
usleep(150000);
|
|
}
|
|
}
|
|
if (!is_array($result)) throw new \RuntimeException('feedback_snapshot_unavailable');
|
|
$this->SetBuffer('V4ConfirmedFeedback',json_encode($result,JSON_THROW_ON_ERROR));
|
|
$this->SetBuffer('V4ConfirmedFeedbackConfig', hash('sha256', $json));
|
|
$this->SetBuffer('V4ConfirmedFeedbackMono', (string) (hrtime(true) / 1e9));
|
|
} catch (\Throwable $error) {
|
|
$this->SetBuffer('V4ConfirmedFeedback', '');
|
|
throw $error;
|
|
} finally {
|
|
IPS_SemaphoreLeave($lock);
|
|
}
|
|
}
|
|
|
|
/** Read only. A returned current value is NOT actuator permission. */
|
|
public function GetV4BatterieRueckmeldung(): string
|
|
{
|
|
try {
|
|
try { $r=$this->v4BatterieRueckmeldung(); }
|
|
catch (\Throwable) {
|
|
$this->aktualisiereV4Rueckmeldung();
|
|
$r=$this->v4BatterieRueckmeldung();
|
|
}
|
|
}
|
|
catch (\Throwable $e) { $r=['kind'=>'local_physical_feedback','status'=>'unavailable','reason'=>substr($e->getMessage(),0,200),
|
|
'batteryW'=>null,'gridW'=>null,'usableForTrial'=>false,'canDispatch'=>false,'deviceReadConfirmed'=>false]; }
|
|
return json_encode($r,JSON_THROW_ON_ERROR);
|
|
}
|
|
}
|