70 lines
3.6 KiB
PHP
70 lines
3.6 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', '{}');
|
|
}
|
|
|
|
private function v4BatterieRueckmeldung(): array
|
|
{
|
|
$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,5000)) throw new \RuntimeException('feedback_refresh_busy');
|
|
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=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()
|
|
);
|
|
$this->SetBuffer('V4ConfirmedFeedback',json_encode($result,JSON_THROW_ON_ERROR));
|
|
return $result;
|
|
} finally {
|
|
IPS_SemaphoreLeave($lock);
|
|
}
|
|
}
|
|
|
|
/** Read only. A returned current value is NOT actuator permission. */
|
|
public function GetV4BatterieRueckmeldung(): string
|
|
{
|
|
try { $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);
|
|
}
|
|
}
|