From e603c510617d3eef7c011488bc5d0a1cd8dcd314 Mon Sep 17 00:00:00 2001 From: Eva Date: Wed, 13 Nov 2024 16:08:23 +0000 Subject: [PATCH] made `:LABEL` update `.EGU` --- src/fastcs_pandablocks/handlers.py | 11 ++++++++--- src/fastcs_pandablocks/panda/fields.py | 24 ++++++++++++++++++------ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/fastcs_pandablocks/handlers.py b/src/fastcs_pandablocks/handlers.py index 222bbbf..820e4ee 100644 --- a/src/fastcs_pandablocks/handlers.py +++ b/src/fastcs_pandablocks/handlers.py @@ -1,3 +1,4 @@ +from dataclasses import asdict from typing import Any from fastcs.attributes import Attribute, AttrR, AttrW, Handler, Sender, Updater @@ -30,13 +31,17 @@ def __init__(self, panda_name: PandaName): class EguSender(Sender): - def __init__(self, attr_to_update: Attribute): + def __init__(self, panda_name: PandaName, attr_to_update: Attribute): """Update the attr""" + self.panda_name = panda_name self.attr_to_update = attr_to_update async def put(self, controller: Any, attr: AttrW, value: str) -> None: - # TODO find out how to update attr_to_update's EGU with the value - ... + await controller.put_value_to_panda(self.panda_name, value) + kwargs = asdict(self.attr_to_update.datatype) + kwargs["units"] = value + new_attribute_datatype = type(self.attr_to_update.datatype)(**kwargs) + self.attr_to_update.update_datatype(new_attribute_datatype) class CaptureHandler(Handler): diff --git a/src/fastcs_pandablocks/panda/fields.py b/src/fastcs_pandablocks/panda/fields.py index 32c8006..1e03272 100644 --- a/src/fastcs_pandablocks/panda/fields.py +++ b/src/fastcs_pandablocks/panda/fields.py @@ -126,8 +126,12 @@ def __init__( initial_values: RawInitialValuesType, ): super().__init__(panda_name) + + units_panda_name = panda_name + PandaName(sub_field="units") + initial_units = initial_values[units_panda_name] + self.top_level_attribute = AttrRW( - Float(), + Float(units=initial_units), handler=DefaultFieldHandler(panda_name), description=_strip_description(field_info.description), group=WidgetGroup.PARAMETERS.value, @@ -135,7 +139,7 @@ def __init__( ) self._additional_attributes["units"] = AttrW( String(), - handler=EguSender(self.top_level_attribute), + handler=EguSender(units_panda_name, self.top_level_attribute), group=WidgetGroup.PARAMETERS.value, allowed_values=field_info.units_labels, ) @@ -149,8 +153,12 @@ def __init__( initial_values: RawInitialValuesType, ): super().__init__(panda_name) + + units_panda_name = panda_name + PandaName(sub_field="units") + initial_units = initial_values[units_panda_name] + self.top_level_attribute = AttrR( - Float(), + Float(units=initial_units), handler=DefaultFieldUpdater( panda_name=panda_name, ), @@ -160,7 +168,7 @@ def __init__( ) self._additional_attributes["units"] = AttrW( String(), - handler=EguSender(self.top_level_attribute), + handler=EguSender(units_panda_name, self.top_level_attribute), group=WidgetGroup.OUTPUTS.value, allowed_values=field_info.units_labels, ) @@ -171,11 +179,15 @@ def __init__( self, panda_name: PandaName, field_info: SubtypeTimeFieldInfo, - initial_value: RawInitialValuesType, + initial_values: RawInitialValuesType, ): super().__init__(panda_name) + + units_panda_name = panda_name + PandaName(sub_field="units") + initial_units = initial_values[units_panda_name] + self.top_level_attribute = AttrW( - Float(), + Float(units=initial_units), handler=DefaultFieldSender(panda_name), description=_strip_description(field_info.description), group=WidgetGroup.OUTPUTS.value,