Skip to content

Commit 1ee25c6

Browse files
author
James Souter
committed
use AttributeIO/Ref in docs/snippets/dynamic.py
1 parent 5fdb5e7 commit 1ee25c6

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

docs/snippets/dynamic.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
from pydantic import BaseModel, ConfigDict, ValidationError
88

9-
from fastcs.attributes import AttrHandlerRW, Attribute, AttrR, AttrRW, AttrW
9+
from fastcs.attribute_io import AttributeIO
10+
from fastcs.attribute_io_ref import AttributeIORef
11+
from fastcs.attributes import Attribute, AttrR, AttrRW, AttrW
1012
from fastcs.connections import IPConnection, IPConnectionSettings
1113
from fastcs.controller import Controller, SubController
12-
from fastcs.datatypes import Bool, DataType, Float, Int, String
14+
from fastcs.datatypes import Bool, DataType, Float, Int, String, T
1315
from fastcs.launch import FastCS
1416
from fastcs.transport.epics.ca.options import EpicsCAOptions
1517
from fastcs.transport.epics.options import EpicsIOCOptions
@@ -46,52 +48,52 @@ def create_attributes(parameters: dict[str, Any]) -> dict[str, Attribute]:
4648
print(f"Failed to validate parameter '{parameter}'\n{e}")
4749
continue
4850

49-
handler = TemperatureControllerHandler(parameter.command)
51+
io_ref = TemperatureControllerAttributeIORef(command_name=parameter.command)
5052
match parameter.access_mode:
5153
case "r":
52-
attributes[name] = AttrR(parameter.fastcs_datatype, handler=handler)
54+
attributes[name] = AttrR(parameter.fastcs_datatype, io_ref=io_ref)
5355
case "rw":
54-
attributes[name] = AttrRW(parameter.fastcs_datatype, handler=handler)
56+
attributes[name] = AttrRW(parameter.fastcs_datatype, io_ref=io_ref)
5557

5658
return attributes
5759

5860

59-
@dataclass
60-
class TemperatureControllerHandler(AttrHandlerRW):
61+
@dataclass(kw_only=True)
62+
class TemperatureControllerAttributeIORef(AttributeIORef):
6163
command_name: str
6264
update_period: float | None = 0.2
63-
_controller: TemperatureController | None = None
65+
66+
67+
class TemperatureControllerAttributeIO(
68+
AttributeIO[T, TemperatureControllerAttributeIORef]
69+
):
70+
def __init__(self, connection: IPConnection):
71+
self._connection = connection
6472

6573
async def update(self, attr: AttrR):
66-
response = await self.controller.connection.send_query(
67-
f"{self.command_name}?\r\n"
68-
)
74+
response = await self._connection.send_query(f"{attr.io_ref.command_name}?\r\n")
6975
value = response.strip("\r\n")
7076

7177
await attr.set(attr.dtype(value))
7278

7379
async def put(self, attr: AttrW, value: Any):
74-
await self.controller.connection.send_command(
75-
f"{self.command_name}={value}\r\n"
76-
)
80+
await self._connection.send_command(f"{attr.io_ref.command_name}={value}\r\n")
7781

7882

7983
class TemperatureRampController(SubController):
80-
def __init__(self, index: int, connection: IPConnection):
81-
super().__init__(f"Ramp {index}")
82-
83-
self.connection = connection
84+
def __init__(self, index: int, io: AttributeIO):
85+
super().__init__(f"Ramp {index}", ios=[io])
8486

8587
async def initialise(self, parameters: dict[str, Any]):
8688
self.attributes.update(create_attributes(parameters))
8789

8890

8991
class TemperatureController(Controller):
9092
def __init__(self, settings: IPConnectionSettings):
91-
super().__init__()
92-
9393
self._ip_settings = settings
9494
self.connection = IPConnection()
95+
self._temperature_io = TemperatureControllerAttributeIO(self.connection)
96+
super().__init__(ios=[self._temperature_io])
9597

9698
async def connect(self):
9799
await self.connection.connect(self._ip_settings)
@@ -105,7 +107,7 @@ async def initialise(self):
105107
self.attributes.update(create_attributes(api))
106108

107109
for idx, ramp_parameters in enumerate(ramps_api):
108-
ramp_controller = TemperatureRampController(idx + 1, self.connection)
110+
ramp_controller = TemperatureRampController(idx + 1, self._temperature_io)
109111
self.register_sub_controller(f"Ramp{idx + 1:02d}", ramp_controller)
110112
await ramp_controller.initialise(ramp_parameters)
111113

0 commit comments

Comments
 (0)