Skip to content

Commit

Permalink
WIP: deciding on certain features in backend conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
evalott100 committed Aug 16, 2024
1 parent 1d1953d commit 1b203e9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
9 changes: 2 additions & 7 deletions src/ophyd_async/core/_soft_signal_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ def make_initial_value(self, datatype: Optional[Type[T]]) -> T:


class SoftProtocolDatatypeAbstractionConverter(SoftConverter):
"""
No conversion is necessary for ProtocolDatatypeAbstraction datatypes in soft
signals.
"""

def __init__(self, datatype: Type[ProtocolDatatypeAbstraction]):
self.datatype = datatype

Expand All @@ -141,7 +136,8 @@ def reading(self, value: T, timestamp: float, severity: int) -> Reading:
return super().reading(value, timestamp, severity)

def value(self, value: Any) -> Any:
if not issubclass(type(value), ProtocolDatatypeAbstraction):
if not isinstance(value, self.datatype):
# For the case where we
value = self.datatype.convert_from_protocol_datatype(value)
return value

Expand Down Expand Up @@ -197,7 +193,6 @@ def __init__(
self.datatype = datatype
self._initial_value = initial_value
self._metadata = metadata or {}
self.converter_factory = SoftSignalConverterFactory
self.converter: SoftConverter = SoftSignalConverterFactory.make_converter(
datatype
)
Expand Down
5 changes: 3 additions & 2 deletions src/ophyd_async/epics/signal/_p4p.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ def value(self, value: Value):
return self.datatype.convert_from_protocol_datatype(value.todict())

def write_value(self, value):
return value.convert_to_protocol_datatype()
if isinstance(value, self.datatype):
return value.convert_to_protocol_datatype()
return value


class PvaConverterFactory(BackendConverterFactory):
Expand Down Expand Up @@ -428,7 +430,6 @@ def __init__(self, datatype: Optional[Type[T]], read_pv: str, write_pv: str):
self.read_pv = read_pv
self.write_pv = write_pv
self.initial_values: Dict[str, Any] = {}
self.converter_factory = PvaConverterFactory()
self.converter: PvaConverter = DisconnectedPvaConverter()
self.subscription: Optional[Subscription] = None

Expand Down
17 changes: 17 additions & 0 deletions tests/core/test_device_save_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
set_signal_values,
walk_rw_signals,
)
from ophyd_async.core._signal_backend import ProtocolDatatypeAbstraction
from ophyd_async.epics.signal import epics_signal_r, epics_signal_rw


Expand Down Expand Up @@ -54,6 +55,18 @@ class MyEnum(str, Enum):
three = "three"


class SomeProtocolDatatypeAbstraction(ProtocolDatatypeAbstraction):
def __init__(self, value: int):
self.value = value

def convert_to_protocol_datatype(self) -> int:
return self.value - 1

@classmethod
def convert_from_protocol_datatype(cls, value: int) -> "SomeProtocolDatatypeAbstraction":
return cls(value + 1)


class DummyDeviceGroupAllTypes(Device):
def __init__(self, name: str):
self.pv_int: SignalRW = epics_signal_rw(int, "PV1")
Expand All @@ -73,6 +86,7 @@ def __init__(self, name: str):
self.pv_array_float64 = epics_signal_rw(npt.NDArray[np.float64], "PV14")
self.pv_array_npstr = epics_signal_rw(npt.NDArray[np.str_], "PV15")
self.pv_array_str = epics_signal_rw(Sequence[str], "PV16")
self.pv_protocol_device_abstraction = epics_signal_rw(SomeProtocolDatatypeAbstraction, "PV17")


@pytest.fixture
Expand Down Expand Up @@ -155,6 +169,9 @@ async def test_save_device_all_types(RE: RunEngine, device_all_types, tmp_path):
await device_all_types.pv_array_str.set(
["one", "two", "three"],
)
await device_all_types.pv_protocol_device_abstraction.set(
SomeProtocolDatatypeAbstraction(1)
)

# Create save plan from utility functions
def save_my_device():
Expand Down
1 change: 1 addition & 0 deletions tests/test_data/test_yaml_save.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
pv_float: 1.234
pv_int: 1
pv_str: test_string
pv_protocol_device_abstraction: 0

0 comments on commit 1b203e9

Please sign in to comment.