def init_device(self): super().init_device() # PROTECTED REGION ID(EventReceiver.init_device) ENABLED START # self.logger = logging.getLogger(__name__) self._dev_factory = DevFactory() self.dev = None self.attr_EventReceived = False
def wait_for_events(proxy): dev_states = [] start_time = time.time() dev_factory = DevFactory() minCounter = dev_factory.get_device("test/counter/minutes") ssCounter = dev_factory.get_device("test/counter/seconds") lastValuemin, lastValuesec = minCounter.value, ssCounter.value logging.info("%s:%s", lastValuemin, lastValuesec) while proxy.State() == DevState.RUNNING or proxy.State() == DevState.ALARM: dev_state = proxy.state() if dev_state not in dev_states: logging.info("State %s", dev_state) dev_states.append(dev_state) elapsed_time = time.time() - start_time if elapsed_time > TIMEOUT: pytest.fail("Timeout occurred while executing the test") # to avoid the segmentation fault in simulation mode, # tests must run in less than 10ss # https://gitlab.com/tango-controls/cppTango/-/issues/843 tmpValuemin, tmpValuesec = minCounter.value, ssCounter.value if not ((tmpValuemin == lastValuemin) and (tmpValuesec == lastValuesec)): logging.info("%s:%s", tmpValuemin, tmpValuesec) lastValuemin, lastValuesec = tmpValuemin, tmpValuesec time.sleep(0.001) assert proxy.state() == DevState.OFF assert DevState.ALARM in dev_states
def wait_for_events(proxy): dev_factory = DevFactory() tabatasCounter = dev_factory.get_device("test/counter/tabatas") dev_states = [] run_states = [] start_time = time.time() while not tabatasCounter.value <= 0 or proxy.State() == DevState.ON: dev_state = proxy.state() run_state = proxy.running_state if dev_state not in dev_states: logging.info("Device: %s %s", dev_state, run_state) dev_states.append(dev_state) if run_state not in run_states: logging.info("Device: %s %s", dev_state, run_state) run_states.append(run_state) elapsed_time = time.time() - start_time if elapsed_time > TIMEOUT: pytest.fail("Timeout occurred while executing the test") # to avoid the segmentation fault in simulation mode, # tests must run in less than 10ss # https://gitlab.com/tango-controls/cppTango/-/issues/843 time.sleep(0.01) assert proxy.state() == DevState.OFF assert DevState.ON in dev_states assert RunningState.PREPARE in run_states assert RunningState.WORK in run_states assert RunningState.REST in run_states
def test_type_spectrum(tango_context): logging.info("%s", tango_context) dev_factory = DevFactory() event_receiver = dev_factory.get_device("test/eventreceiver/1") logging.info("%s", event_receiver.read_attribute("TestSpectrumType").value) assert not isinstance( event_receiver.read_attribute("TestSpectrumType").value, tuple )
def init_device(self): """Initialises the attributes and properties of the Motor.""" super().init_device() # PROTECTED REGION ID(Motor.init_device) ENABLED START # self.logger = logging.getLogger(__name__) self.logger.info("set_change_event on PerformanceValue") self.set_change_event("PerformanceValue", False, True) self._dev_factory = DevFactory() self.powerSupply = None
def test_event_received(tango_context): logging.info("%s", tango_context) dev_factory = DevFactory() event_receiver = dev_factory.get_device("test/eventreceiver/1") for i in range(30): time.sleep(1) logging.info("waiting for event %s", i) if event_receiver.read_attribute("EventReceived").value: break assert event_receiver.read_attribute("EventReceived").value is True
def test_timer(tango_context): logging.info("%s", tango_context) dev_factory = DevFactory() proxy = dev_factory.get_device("test/timer/1") setup_timer(proxy) proxy.ResetCounters() proxy.Start() with pytest.raises(Exception): proxy.Start() assert proxy.State() == DevState.RUNNING wait_for_events(proxy) assert proxy.State() == DevState.OFF
def test_async_tabata_command_inout_asynch(tango_context): logging.info("%s", tango_context) dev_factory = DevFactory() proxy = dev_factory.get_device("test/asynctabata/1") proxy.set_timeout_millis(3000) setup_tabata(proxy) cmd_id = proxy.command_inout_asynch("ResetCounters") cmd_res = proxy.command_inout_reply(cmd_id, timeout=3000) logging.info("%s", cmd_res) proxy.command_inout_asynch("Run") wait_for_events(proxy) assert proxy.State() == DevState.OFF
def test_async_tabata_futures(tango_context): logging.info("%s", tango_context) dev_factory = DevFactory() proxy = dev_factory.get_device("test/asynctabata/1", tango.GreenMode.Futures) # proxy.set_timeout_millis(30000) setup_tabata(proxy) proxy.ResetCounters(wait=True) res = proxy.Run(wait=False, timeout=None) logging.info("%s", res) wait_for_events(proxy) assert proxy.State() == DevState.OFF
def init_device(self): """Initialises the attributes and properties of the Timer.""" Device.init_device(self) # PROTECTED REGION ID(Timer.init_device) ENABLED START # self.logger = logging.getLogger(__name__) self._lock = threading.Lock() self._dev_factory = DevFactory() self._start_minutes = 0 self._start_seconds = 0 self.subscribed = False self.set_state(DevState.OFF) self.worker_thread = None
async def init_device(self): """Initialises the attributes and properties of the AsyncTabata.""" await Device.init_device(self) # PROTECTED REGION ID(AsyncTabata.init_device) ENABLED START # self.logger = logging.getLogger(__name__) self._lock = threading.Lock() self._dev_factory = DevFactory() self._prepare = 10 self._work = 20 self._rest = 10 self._cycles = 8 self._tabatas = 1 self._running_state = RunningState.PREPARE self.subscribed = False self.set_state(DevState.OFF)
def test_fatabata(tango_context): logging.info("%s", tango_context) dev_factory = DevFactory() tabata = dev_factory.get_device("test/tabata/1") tabata.ResetCounters() time.sleep( 3 ) # it takes time to propagate; forwarded attributes are not recommended. fatabata = dev_factory.get_device("test/fatabata/1") prepare = dev_factory.get_device("test/counter/prepare") work = dev_factory.get_device("test/counter/work") rest = dev_factory.get_device("test/counter/rest") cycles = dev_factory.get_device("test/counter/cycles") tabatas = dev_factory.get_device("test/counter/tabatas") assert fatabata.prepare == prepare.value assert fatabata.work == work.value assert fatabata.rest == rest.value assert fatabata.cycle == cycles.value assert fatabata.tabata == tabatas.value
class Motor(Device): """ Motor training example """ # PROTECTED REGION ID(Motor.class_variable) ENABLED START # # PROTECTED REGION END # // Motor.class_variable # ---------- # Attributes # ---------- PerformanceValue = attribute( dtype="DevDouble", polling_period=3000, rel_change=5, abs_change=5, ) # --------------- # General methods # --------------- def init_device(self): """Initialises the attributes and properties of the Motor.""" super().init_device() # PROTECTED REGION ID(Motor.init_device) ENABLED START # self.logger = logging.getLogger(__name__) self.logger.info("set_change_event on PerformanceValue") self.set_change_event("PerformanceValue", False, True) self._dev_factory = DevFactory() self.powerSupply = None # PROTECTED REGION END # // Motor.init_device def always_executed_hook(self): """Method always executed before any TANGO command is executed.""" # PROTECTED REGION ID(Motor.always_executed_hook) ENABLED START # try: if self.powerSupply is None: self.logger.info("Connect to power Supply device") self.powerSupply = self._dev_factory.get_device( "test/powersupply/1") except Exception as ex: self.logger.info("Unexpected error on DeviceProxy creation %s", str(ex)) # PROTECTED REGION END # // Motor.always_executed_hook def delete_device(self): """Hook to delete resources allocated in init_device. This method allows for any memory or other resources allocated in the init_device method to be released. This method is called by the device destructor and by the device Init command. """ # PROTECTED REGION ID(Motor.delete_device) ENABLED START # # PROTECTED REGION END # // Motor.delete_device # ------------------ # Attributes methods # ------------------ def read_PerformanceValue(self): # PROTECTED REGION ID(Motor.PerformanceValue_read) ENABLED START # # import debugpy; debugpy.debug_this_thread() return random.uniform(0, 1) # PROTECTED REGION END # // Motor.PerformanceValue_read # -------- # Commands # -------- @command() @DebugIt() def TurnOn(self): # PROTECTED REGION ID(Motor.TurnOn) ENABLED START # try: power_state = self.powerSupply.state() if power_state != DevState.ON: self.powerSupply.turn_on() except Exception as ex: self.logger.info("No power state %s", ex) self.set_state(DevState.ON) # PROTECTED REGION END # // Motor.TurnOn @command() @DebugIt() def TurnOff(self): # PROTECTED REGION ID(Motor.TurnOff) ENABLED START # self.set_state(DevState.OFF) self.logger.info("Motor Off") # PROTECTED REGION END # // Motor.TurnOff @command() @DebugIt() def Start(self): # PROTECTED REGION ID(Motor.Start) ENABLED START # self.set_state(DevState.RUNNING) self.logger.info("Motor Running")
class EventReceiver(SKABaseDevice): """ EventReceiver Training Example """ __metaclass__ = DeviceMeta # PROTECTED REGION ID(EventReceiver.class_variable) ENABLED START # # PROTECTED REGION END # // EventReceiver.class_variable # ---------- # Attributes # ---------- EventReceived = attribute(dtype="bool", ) TestSpectrumType = attribute( dtype=("uint16", ), max_dim_x=200, ) # --------------- # General methods # --------------- def init_device(self): super().init_device() # PROTECTED REGION ID(EventReceiver.init_device) ENABLED START # self.logger = logging.getLogger(__name__) self._dev_factory = DevFactory() self.dev = None self.attr_EventReceived = False # PROTECTED REGION END # // EventReceiver.init_device def always_executed_hook(self): # PROTECTED REGION ID(EventReceiver.always_executed_hook) # ENABLED START # try: if self.dev is None: self.logger.info("Connect to motor device") self.dev = self._dev_factory.get_device("test/motor/1") self.attr_EventReceived = False self.logger.info("subscribe_event on PerformanceValue") self.dev.subscribe_event( "PerformanceValue", tango.EventType.CHANGE_EVENT, self.HandleEvent, stateless=True, ) except Exception as ex: self.logger.info("Unexpected error: %s", str(ex)) # PROTECTED REGION END # # // EventReceiver.always_executed_hook def delete_device(self): # PROTECTED REGION ID(EventReceiver.delete_device) ENABLED START # pass # PROTECTED REGION END # // EventReceiver.delete_device # ------------------ # Attributes methods # ------------------ def read_EventReceived(self): # PROTECTED REGION ID(EventReceiver.EventReceived_read) ENABLED START # try: return self.attr_EventReceived except Exception as ex: self.logger.info( "Unexpected error on (self.attr_EventReceived = False): %s", str(ex), ) # PROTECTED REGION END # // EventReceiver.EventReceived_read def read_TestSpectrumType(self): # PROTECTED REGION ID(EventReceiver.TestSpectrumType_read) ENABLED START # noqa: E501 self.TestSpectrumType = [1, 2, 3] return self.TestSpectrumType # PROTECTED REGION END # // EventReceiver.TestSpectrumType_read # -------- # Commands # -------- def HandleEvent(self, args): try: self.logger.info( "Event arrived on PerformanceValue value= %s", str(self.dev.PerformanceValue), ) self.logger.info("args = %s", str(args)) self.attr_EventReceived = True except Exception as ex: self.logger.info( "Unexpected error on (self.attr_EventReceived = False): %s", str(ex), )
def test_set_attr(tango_context): logging.info("%s", tango_context) dev_factory = DevFactory() proxy = dev_factory.get_device("test/tabata/1") check_set_attr(proxy)
def test_async_set_attr(tango_context): logging.info("%s", tango_context) dev_factory = DevFactory() proxy = dev_factory.get_device("test/asynctabata/1", tango.GreenMode.Futures) check_set_attr(proxy)
def motor(tango_context): """Create DeviceProxy for tests""" logging.info("%s", tango_context) dev_factory = DevFactory() return dev_factory.get_device("test/motor/1")