def MoveMotors(self): """ :rtype: PyTango.DevBoolean """ self.debug_stream("In MoveMotors()") argout = False #----- PROTECTED REGION ID(ManageServer.MoveMotors) ENABLED START -----# g = PyTango.Group("my_group") g.add("simulation/huiling/1") g.add("bottom/hhl/1") # execute a command with the same parameter value for all devices in the group data = PyTango.DeviceData() data.insert(PyTango.DevDouble, 30.000) #g.command_inout("SetPosition", data) # execute a command with different parameter values, one for each device in the group data_list = PyTango.DeviceDataList() data1 = PyTango.DeviceData() data1.insert(PyTango.DevDouble, 30.000) data2 = PyTango.DeviceData() data2.insert(PyTango.DevDouble, -30.000) data_list.append(data1) data_list.append(data2) g.command_inout("SetPosition", data_list) argout=True #----- PROTECTED REGION END -----# // ManageServer.MoveMotors return argout
def get_all_status(self): """Returns a dictionary with the individual status of the inner devices.""" result = {} group = PyTango.Group(self.name) [ group.add(dev) for dev in self.get_device_list() if not dev.startswith('dserver') ] try: answers = group.command_inout('Status', []) for reply in answers: result[reply.dev_name()] = reply.get_data() return result except Exception, e: print 'Unable to read all Status from %s: %s' % ( self.name, str(e)[:100] + '...') return result
def _build_group(definition): """Returns tango.Group object according to defined hierarchy. Used internally by `get_groups_from_json`. """ group_name = definition['group_name'] devices = definition.get('devices', []) subgroups = definition.get('subgroups', []) group = PyTango.Group(group_name) for device_name in devices: group.add(device_name) for subgroup_definition in subgroups: subgroup = _build_group(subgroup_definition) # recurse group.add(subgroup) return group
def get(self, cache=False): door = self._door macro_server = door.macro_server env = door.getEnvironment() ret = dict(ScanDir=env.get('ScanDir'), DataCompressionRank=env.get('DataCompressionRank', 1), PreScanSnapshot=env.get('PreScanSnapshot', [])) scan_file = env.get('ScanFile') if scan_file is None: scan_file = [] elif isinstance(scan_file, (str, unicode)): scan_file = [scan_file] ret['ScanFile'] = scan_file mnt_grps = macro_server.getElementsOfType("MeasurementGroup") mnt_grps_names = [mnt_grp.name for mnt_grp in mnt_grps.values()] mnt_grps_full_names = mnt_grps.keys() active_mnt_grp = env.get('ActiveMntGrp') if active_mnt_grp is None and len(mnt_grps): active_mnt_grp = mnt_grps_names[0] door.putEnvironment('ActiveMntGrp', active_mnt_grp) ret['ActiveMntGrp'] = active_mnt_grp ret['MntGrpConfigs'] = mnt_grp_configs = CaselessDict() if len(mnt_grps) == 0: return ret mnt_grp_grps = PyTango.Group("grp") # use full names cause we may be using a different Tango database mnt_grp_grps.add(mnt_grps_full_names) codec = CodecFactory().getCodec('json') replies = mnt_grp_grps.read_attribute("configuration") for mnt_grp, reply in zip(mnt_grps_names, replies): try: mnt_grp_configs[mnt_grp] = \ codec.decode(('json', reply.get_data().value), ensure_ascii=True)[1] except Exception, e: from taurus.core.util.log import warning warning('Cannot load Measurement group "%s": %s', repr(mnt_grp), repr(e))
class Station_DS (PyTango.Device_4Impl): #--------- Add you global variables here -------------------------- # ----- PROTECTED REGION ID(Station_DS.global_variables) ENABLED START -----# all_states_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] tpm_dict = {} station_devices = PyTango.Group('station') def check_state_flow(self, fnName): """ Checks if the current state the device is in one of the states in a given list of allowed states for a paticular function. :param : Name of command to be executed :type: String :return: True if allowed, false if not. :rtype: PyTango.DevBoolean """ self.debug_stream("In check_state_flow()") argout = False # get allowed states for this command try: fnAllowedStates = self.state_list[fnName] allowed = self.attr_station_state_read in fnAllowedStates if not allowed: self.info_stream("Current state allowed: %s" % allowed) argout = allowed except DevFailed as df: self.info_stream("Failed to check state flow: %s" % df) argout = False finally: return argout # State flow definitions - allowed states for each command state_list = { 'add_tpm': all_states_list, 'remove_tpm': all_states_list, 'connect_tpm': all_states_list, 'run_station_command': all_states_list, 'get_station_state': all_states_list } # ----- PROTECTED REGION END -----# // Station_DS.global_variables def __init__(self,cl, name): PyTango.Device_4Impl.__init__(self,cl,name) self.debug_stream("In __init__()") Station_DS.init_device(self) # ----- PROTECTED REGION ID(Station_DS.__init__) ENABLED START -----# # ----- PROTECTED REGION END -----# // Station_DS.__init__ def delete_device(self): self.debug_stream("In delete_device()") # ----- PROTECTED REGION ID(Station_DS.delete_device) ENABLED START -----# # ----- PROTECTED REGION END -----# // Station_DS.delete_device def init_device(self): self.debug_stream("In init_device()") self.get_device_properties(self.get_device_class()) self.attr_station_state_read = 0 # ----- PROTECTED REGION ID(Station_DS.init_device) ENABLED START -----# self.set_state(DevState.ON) self.set_station_state(BoardState.Init.value) # ----- PROTECTED REGION END -----# // Station_DS.init_device def always_executed_hook(self): self.debug_stream("In always_excuted_hook()") # ----- PROTECTED REGION ID(Station_DS.always_executed_hook) ENABLED START -----# # ----- PROTECTED REGION END -----# // Station_DS.always_executed_hook #----------------------------------------------------------------------------- # Station_DS read/write attribute methods #----------------------------------------------------------------------------- def read_station_state(self, attr): self.debug_stream("In read_station_state()") # ----- PROTECTED REGION ID(Station_DS.station_state_read) ENABLED START -----# attr.set_value(self.attr_station_state_read) self.info_stream(BoardState(self.attr_station_state_read)) # ----- PROTECTED REGION END -----# // Station_DS.station_state_read # ----- PROTECTED REGION ID(Station_DS.initialize_dynamic_attributes) ENABLED START -----# # ----- PROTECTED REGION END -----# // Station_DS.initialize_dynamic_attributes def read_attr_hardware(self, data): self.debug_stream("In read_attr_hardware()") # ----- PROTECTED REGION ID(Station_DS.read_attr_hardware) ENABLED START -----# # ----- PROTECTED REGION END -----# // Station_DS.read_attr_hardware #----------------------------------------------------------------------------- # Station_DS command methods #----------------------------------------------------------------------------- def add_tpm(self, argin): """ Adds the Tango device proxy name to the list of TPMs managed by this station. :param argin: The device proxy name, board type, IP address and port number for communication are supplied as a serialized python dictionary, stored as a string. :type: PyTango.DevString :return: Returns true if operation is successful, false otherwise. :rtype: PyTango.DevBoolean """ self.debug_stream("In add_tpm()") argout = False # ----- PROTECTED REGION ID(Station_DS.add_tpm) ENABLED START -----# state_ok = self.check_state_flow(inspect.stack()[0][3]) if state_ok: try: arguments = pickle.loads(argin) device_proxy_name = arguments['name'] device_type = arguments['type'] device_ip = arguments['ip'] device_port = arguments['port'] sub_dict = {'type': device_type, 'ip': device_ip, 'port': device_port} self.tpm_dict[device_proxy_name] = sub_dict self.station_devices.add(device_proxy_name) argout = True except DevFailed as df: self.debug_stream("Failed to add tpm to station: %s" % df) else: self.debug_stream("Invalid state") # ----- PROTECTED REGION END -----# // Station_DS.add_tpm return argout def remove_tpm(self, argin): """ Removes the Tango device proxy name from the list of TPMs managed by this station. :param argin: The Tango device proxy name of the TPM to remove from the station. :type: PyTango.DevString :return: Returns true if operation is successful, false otherwise. :rtype: PyTango.DevBoolean """ self.debug_stream("In remove_tpm()") argout = False # ----- PROTECTED REGION ID(Station_DS.remove_tpm) ENABLED START -----# state_ok = self.check_state_flow(inspect.stack()[0][3]) if state_ok: try: device_proxy_name = argin del self.tpm_dict[device_proxy_name] self.station_devices.remove(device_proxy_name) argout = True except DevFailed as df: self.debug_stream("Failed to remove tpm from station: %s" % df) else: self.debug_stream("Invalid state") # ----- PROTECTED REGION END -----# // Station_DS.remove_tpm return argout def connect_tpm(self, argin): """ Instructs a particular TPM to connect. :param argin: Tango device proxy name of TPM. :type: PyTango.DevString :return: Returns true if operation is successful, false otherwise. :rtype: PyTango.DevBoolean """ self.debug_stream("In connect_tpm()") argout = False # ----- PROTECTED REGION ID(Station_DS.connect_tpm) ENABLED START -----# state_ok = self.check_state_flow(inspect.stack()[0][3]) if state_ok: try: device_name = argin sub_dict = self.tpm_dict[device_name] # setup device tpm_instance = PyTango.DeviceProxy(device_name) tpm_instance.ip_address = sub_dict['ip'] tpm_instance.port = sub_dict['port'] # Connect to device if not tpm_instance.is_connected: tpm_instance.command_inout("connect") self.info_stream("Connected: %s" % device_name) argout = True except DevFailed as df: self.debug_stream("Failed to connect to all station devices: %s" % df) argout = '' else: self.debug_stream("Invalid state") # ----- PROTECTED REGION END -----# // Station_DS.connect_tpm return argout def set_station_state(self, argin): """ Sets the station status by passing in a value. UNKNOWN = 0 INIT = 1 ON = 2 RUNNING = 3 FAULT = 4 OFF = 5 STANDBY = 6 SHUTTING_DOWN = 7 MAINTENANCE = 8 LOW_POWER = 9 SAFE_STATE = 10 :param argin: Station state. :type: PyTango.DevLong :return: :rtype: PyTango.DevVoid """ self.debug_stream("In set_station_state()") # ----- PROTECTED REGION ID(Station_DS.set_station_state) ENABLED START -----# # ----- PROTECTED REGION END -----# // Station_DS.set_station_state def run_station_command(self, argin): """ This command takes the name of a command and executes it station-wide. The command must exist on all connected devices. :param argin: A pickled string containing: 1) Name of command 2) Arguments for command Arguments for a command have to be supplied as list of dictionaries. If only one item is in the list, the same input is applied to all station commands. If more than one item is in the list, then each item is sent to an individual device (in order of station devices). Therefore the size of the list should be equivalent to the number of devices controlled by the station. :type: PyTango.DevString :return: Returns a set of replies per device in the station, pickled as a string. :rtype: PyTango.DevString """ self.debug_stream("In run_station_command()") argout = '' # ----- PROTECTED REGION ID(Station_DS.run_station_command) ENABLED START -----# state_ok = self.check_state_flow(inspect.stack()[0][3]) if state_ok: try: arguments = pickle.loads(argin) fnName = arguments['fnName'] fnInput = arguments['fnInput'] self.info_stream("Calling TPM function: %s" % fnName) if fnInput is not None: if len(fnInput) > 1 and len(fnInput) != len(self.tpm_dict): raise Exception( 'Number of inputs to station commands not equivalent to number of devices under station control.') # loop over tpms in station command_indexes = [] cnt = 0 for device in self.tpm_dict: tpm_proxy = self.station_devices.get_device(device) self.connect_tpm(device) if fnInput is None: self.info_stream("No input detected...") command_indexes.append(tpm_proxy.command_inout_asynch(fnName)) elif len(fnInput) > 1: self.info_stream("Multiple inputs detected...spreading to boards") pickled_fnInput = pickle.dumps(fnInput[cnt]) command_indexes.append(tpm_proxy.command_inout_asynch(fnName, pickled_fnInput)) else: self.info_stream("Single input detected...unifying calls") pickled_fnInput = pickle.dumps(fnInput[0]) command_indexes.append(tpm_proxy.command_inout_asynch(fnName, pickled_fnInput)) cnt += 1 replies = [None] * len(command_indexes) replies_gathered = 0 while replies_gathered < len(command_indexes): time.sleep(0.1) cnt = 0 for device in self.tpm_dict: self.debug_stream("Waiting for asynchronous reply...") tpm_proxy = self.station_devices.get_device(device) try: #replies[cnt] = tpm_proxy.command_inout_reply(command_indexes[cnt]) replies[cnt] = tpm_proxy.command_inout_reply(command_indexes[cnt]) replies_gathered += 1 self.debug_stream('Received: %s' % replies[cnt]) cnt += 1 except DevFailed as e: #self.debug_stream('Received DevFailed: %s' % e) #if e.args[0]['reason'] != 'API_AsynReplyNotArrived': self.info_stream('Wait for reply again') continue #raise Exception, 'Weird exception received!: %s' % e argout = pickle.dumps(replies) except DevFailed as df: self.debug_stream("Failed to run station-wide command: %s" % df) else: self.debug_stream("Invalid state") # ----- PROTECTED REGION END -----# // Station_DS.run_station_command return argout def get_station_state(self): """ This commands returns a summary of the state of each TPM in the station. :param : :type: PyTango.DevVoid :return: A pickled string storing the state of each TPM in the station. :rtype: PyTango.DevString """ self.debug_stream("In get_station_state()") argout = '' # ----- PROTECTED REGION ID(Station_DS.get_station_state) ENABLED START -----# state_ok = self.check_state_flow(inspect.stack()[0][3]) if state_ok: try: states = {} for device_proxy_name in self.tpm_dict: self.info_stream("Retrieving state from: %s" % device_proxy_name) tpm_instance = PyTango.DeviceProxy(device_proxy_name) tpm_state = tpm_instance.read_attribute("board_state") self.debug_stream("State: %s" % tpm_state.value) states[device_proxy_name] = tpm_state.value argout = pickle.dumps(states) except DevFailed as df: self.debug_stream("Failed to report station board states: %s" % df) argout = '' else: self.debug_stream("Invalid state") # ----- PROTECTED REGION END -----# // Station_DS.get_station_state return argout