class FooDevice(BaseDevice): state = State(default='standby') no_write = Parameter() foo = Quantity(q.m, check=check(source='*', target='moved')) bar = Quantity(q.m) test = Quantity(q.m, fset=_test_setter, fget=_test_getter) def __init__(self, default): super(FooDevice, self).__init__() self._value = default self._param_value = 0 * q.mm self._test_value = 0 * q.mm async def _get_foo(self): return self._value @transition(target='moved') async def _set_foo(self, value): self._value = value async def _get_bar(self): return 5 * q.m param = Parameter(fget=_get_foo, fset=_set_foo) async def _get_param(self): return self._param_value async def _set_param(self, value): self._param_value = value param = Parameter(fget=_get_param, fset=_set_param)
class LensChanger(BaseLensChanger): '''Lens changer class implementation''' objective = Selection(['objective_10x', 'objective_5x'], help='objective', check=check(source='standby', target='standby')) def __init__(self): super(LensChanger, self).__init__() self._objective = 'objective_10x' async def _set_objective(self, objective): self._objective = objective async def _get_objective(self): return self._objective
class AttenuatorBox(base.AttenuatorBox): '''Attenuator class implementation''' attenuator = Selection([None, 'Al_1mm'], check=check(source='standby', target='standby')) def __init__(self): super(AttenuatorBox, self).__init__() self._filter = None async def _set_attenuator(self, attenuator): self._filter = attenuator async def _get_attenuator(self): return self._filter
class LinearMotor(_PositionMixin): """ One-dimensional linear motor. .. attribute:: position Position of the motor in length units. """ def __init__(self): super(LinearMotor, self).__init__() def _get_state(self): raise NotImplementedError state = State(default='standby') position = Quantity(q.mm, help="Position", check=check(source=['hard-limit', 'standby'], target=['hard-limit', 'standby']))
class ContinuousRotationMotor(RotationMotor, _VelocityMixin): """ One-dimensional rotational motor with adjustable velocity. .. attribute:: velocity Current velocity in angle per time unit. """ def __init__(self): super(ContinuousRotationMotor, self).__init__() async def _get_state(self): raise NotImplementedError state = State(default='standby') velocity = Quantity(q.deg / q.s, help="Angular velocity", check=check(source=['hard-limit', 'standby', 'moving'], target=['moving', 'standby']))
class RotationMotor(_PositionMixin): """ One-dimensional rotational motor. .. attribute:: position Position of the motor in angular units. """ async def _get_state(self): raise NotImplementedError state = State(default='standby') position = Quantity(q.deg, help="Angular position", check=check(source=['hard-limit', 'standby'], target=['hard-limit', 'standby'])) def __init__(self): super(RotationMotor, self).__init__()
class ContinuousLinearMotor(LinearMotor): """ One-dimensional linear motor with adjustable velocity. .. attribute:: velocity Current velocity in length per time unit. """ def __init__(self): super(ContinuousLinearMotor, self).__init__() def _get_state(self): raise NotImplementedError def _cancel_velocity(self): self._abort() state = State(default='standby') velocity = Quantity(q.mm / q.s, help="Linear velocity", check=check(source=['hard-limit', 'standby', 'moving'], target=['moving', 'standby']))
class DummyDevice(Device): """A dummy device.""" position = Quantity(unit=q.mm) sleep_time = Quantity(unit=q.s) # Value with a check-decorated setter before it is bound to instance, so still a function value = Parameter() # value with check wrapping the bound method cvalue = Parameter(check=check(source='*', target='standby')) # Value set elsewhere evalue = Parameter(fset=set_evalue, fget=get_evalue, check=check(source='*', target='standby')) slow = Parameter() state = State(default='standby') def __init__(self, slow=None): super(DummyDevice, self).__init__() self._position = 1 * q.mm self._value = 0 self._slow = slow self._sleep_time = 0.5 * q.s async def _get_sleep_time(self): return self._sleep_time async def _set_sleep_time(self, value): self._sleep_time = value async def _get_position(self): return self._position async def _set_position(self, value): self._position = value async def _get_slow(self): try: LOG.log(AIODEBUG, 'get slow start %s', self._slow) await asyncio.sleep((await self.get_sleep_time()).magnitude) LOG.log(AIODEBUG, 'get slow finish %s', self._slow) return self._slow except asyncio.CancelledError: LOG.log(AIODEBUG, 'get slow cancelled %s', self._slow) raise except KeyboardInterrupt: # do not scream LOG.debug("KeyboardInterrupt caught while getting") async def _set_slow(self, value): try: LOG.log(AIODEBUG, 'set slow start %s', value) await asyncio.sleep((await self.get_sleep_time()).magnitude) LOG.log(AIODEBUG, 'set slow finish %s', value) self._slow = value except asyncio.CancelledError: LOG.log(AIODEBUG, 'set slow cancelled %s', value) raise async def _get_value(self): """Get the real value.""" return self._value async def _get_target_value(self): """Get the real value.""" return self._value + 1 @check(source='standby', target=['standby', 'hard-limit']) @transition(immediate='moving', target='standby') async def _set_value(self, value): """The real value setter.""" self._value = value async def _get_cvalue(self): """The real value setter.""" return self._value async def _set_cvalue(self, value): """The real value setter.""" self._value = value @background async def do_nothing(self, value=None): """Do nothing. For testing task canellation.""" await self._do_nothing(value=value) async def _do_nothing(self, value=None): LOG.log(AIODEBUG, f'Start doing nothing: {value}') try: await asyncio.sleep(1) LOG.log(AIODEBUG, f'Stop doing nothing: {value}') return value except asyncio.CancelledError: LOG.log(AIODEBUG, f'Doing nothing cancelled: {value}') raise async def _emergency_stop(self): LOG.debug('Emergency stop on a dummy device') await asyncio.sleep(0.5) self._state_value = 'aborted'
class StatelessDevice(Device): foo = Parameter(check=check(source='*', target='*')) @transition(target='changed') async def change(self): pass