feat(v4): confirm feedback with device reads
This commit is contained in:
@@ -4,8 +4,9 @@ declare(strict_types=1);
|
||||
namespace Belevo\EnelixEMS;
|
||||
|
||||
require_once __DIR__.'/NetzfahrplanV4Rueckmeldung.php';
|
||||
require_once __DIR__.'/NetzfahrplanV4Geraeteabruf.php';
|
||||
|
||||
/** Shared physical reading used by receiver and battery immediately before a trial command. */
|
||||
/** Shared confirmed physical reading used by receiver and battery before a trial command. */
|
||||
trait BatterieNetzfahrplanV4RueckmeldungTrait
|
||||
{
|
||||
private function registriereV4Rueckmeldung(): void
|
||||
@@ -20,11 +21,41 @@ trait BatterieNetzfahrplanV4RueckmeldungTrait
|
||||
$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');
|
||||
return NetzfahrplanV4Rueckmeldung::read($c,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']];
|
||||
},time());
|
||||
|
||||
$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. */
|
||||
@@ -32,7 +63,7 @@ trait BatterieNetzfahrplanV4RueckmeldungTrait
|
||||
{
|
||||
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]; }
|
||||
'batteryW'=>null,'gridW'=>null,'usableForTrial'=>false,'canDispatch'=>false,'deviceReadConfirmed'=>false]; }
|
||||
return json_encode($r,JSON_THROW_ON_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user