Refactor on_trigger/complete in psi_detector_base
Issue
Currently, the on_trigger and on_complete from custom prepare do not support to create a DeviceStatus object and pass it back to BEC. This means that if waiting for a signal is implemented, the current code needs to be blocking. However, this means that BEC will potentially wait for each device being triggered since this is called in a loop https://gitlab.psi.ch/bec/bec/-/blob/main/bec_server/bec_server/device_server/device_server.py?ref_type=heads#L210. This is not ideal, and should be optimised. Since BEC is receiving the DeviceStatus, and will wait for this to resolve after the scan (see method https://gitlab.psi.ch/bec/bec/-/blob/main/bec_server/bec_server/scan_server/scan_worker.py?ref_type=heads#L589), we can adapt the methods such that we actually return the DeviceStatus and wait for signals to change within a thread.
Proposed solution
Both methods could be adapted as following:
def trigger(self) -> DeviceStatus:
"""Trigger the detector, called from BEC."""
status = self.custom_prepare.on_trigger()
if status:
return start
status = DeviceStatus()
status.set_finished()
return status
This would give us the flexibility to decide whether on_trigger can return a status object:
def on_trigger() -> None | DeviceStatus:
...
To make it a bit easier for people to work with DeviceStatus objects and threading, we could refactor the wait_for_signal
, to make it easier to deal with threads in the on_complete/trigger methods.