Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make device_factory decorator to ease making and connecting ophyd-async Devices #483

Open
coretl opened this issue Apr 29, 2024 · 2 comments · May be fixed by #597
Open

Make device_factory decorator to ease making and connecting ophyd-async Devices #483

coretl opened this issue Apr 29, 2024 · 2 comments · May be fixed by #597
Assignees

Comments

@coretl
Copy link
Collaborator

coretl commented Apr 29, 2024

#415 (comment) summarizes a discussion on Device connection.
bluesky/ophyd-async#265 will implement the ophyd-async supporting features of idempotent connect. The strategy we would like for dodal is:

  • Each beamline module decorates its device factories with a device_factory
  • This factory makes, names, caches and optionally connects a device
  • In script use can pass connect=True to the device to connect in the foreground, requires a RunEngine to have been made
  • In blueapi use can find all the eager devices and connect them at startup
  • Can also connect them in a plan with the ensure_connected plan stub

This ticket is for:

New ADR

Describing how Devices should be connected in dodal, from scripts and under blueapi

dodal.beamlines.beamline_utils

D = TypeVar(bound=Device)
class DeviceFactory(Protocol, Generic[D]):
    def __call__(self, connect: bool = False, timeout: float = DEFAULT_TIMEOUT) -> D:
        ...

_factory_made_devices: Dict[DeviceFactory, Device] = {}
_device_is_lazy: Dict[DeviceFactory, bool] = {}

def device_factory(lazy=False, set_name=True):
    def wrapper(f) -> DeviceFactory[D]:
        def factory(connect=False, timeout: float = DEFAULT_TIMEOUT):
            device = _factory_made_devices.get(f)
            if not device:
                device = f()
                if set_name:
                    device.set_name(f.__name__)
                _factory_made_devices[f] = device
            if connect: 
                call_in_bluesky_event_loop(device.connect(timeout=timeout))
            return device
        _device_is_lazy[factory] = lazy
        factory.__name__ = f.__name__
        return factory
    return wrapper

def get_device_factories() -> Dict[DeviceFactory, bool]:
    return _device_is_lazy.copy()

Need to add type hints to this so it knows the type of device it is making from the function it is decorating and check with @callumforrester that get_device_factories is what blueapi actually needs

Acceptance Criteria

  • ADR written so we know why this was done
  • device_factory available to use in beamline modules
  • i22.py updated to use it
@stan-dot
Copy link
Contributor

stan-dot commented Jun 6, 2024

so the idea is that all device()... line in i22.py or other beamline gets this decorator, right?

@coretl
Copy link
Collaborator Author

coretl commented Jun 7, 2024

so the idea is that all device()... line in i22.py or other beamline gets this decorator, right?

yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants