class Switch(BaseSwitch): """ This class wrapped opiom command to emulate a switch the configuration may look like this: opiom: $opiom_name register: IMA mask: 0x3 shift: 1 states: - label: OPEN value: 1 - label: CLOSED value: 0 - label: MUSST value: 2 - label: COUNTER_CARD value: 3 """ def __init__(self,name,config): BaseSwitch.__init__(self,name,config) self.__opiom = None self.__register = None self.__mask = None self.__shift = None self.__states = OrderedDict() if OrderedDict else dict() def _init(self): config = self.config self.__opiom = config['opiom'] self.__register = config['register'] self.__mask = config['mask'] self.__shift = config['shift'] for state in config['states']: label = state['label'] value = state['value'] self.__states[label] = value def _set(self,state): value = self.__states.get(state) if value is None: raise RuntimeError("State %s don't exist" % state) mask = self.__mask << self.__shift value <<= self.__shift cmd = '%s 0x%x 0x%x' % (self.__register,value,mask) self.__opiom.comm_ack(cmd) def _get(self): cmd = '?%s' % self.__register value = int(self.__opiom.comm_ack(cmd),base=16) value >>= self.__shift value &= self.__mask for label,state_value in self.__states.iteritems(): if state_value == value: return label return 'UNKNOWN' def _states_list(self): return self.__states.keys()
class Switch(BaseSwitch): """ This class wrapped opiom command to emulate a switch the configuration may look like this: opiom: $opiom_name register: IMA mask: 0x3 shift: 1 states: - label: OPEN value: 1 - label: CLOSED value: 0 - label: MUSST value: 2 - label: COUNTER_CARD value: 3 """ def __init__(self, name, config): BaseSwitch.__init__(self, name, config) self.__opiom = None self.__register = None self.__mask = None self.__shift = None self.__states = OrderedDict() if OrderedDict else dict() def _init(self): config = self.config self.__opiom = config['opiom'] self.__register = config['register'] self.__mask = config['mask'] self.__shift = config['shift'] for state in config['states']: label = state['label'] value = state['value'] self.__states[label] = value def _set(self, state): value = self.__states.get(state) if value is None: raise RuntimeError("State %s don't exist" % state) mask = self.__mask << self.__shift value <<= self.__shift cmd = '%s 0x%x 0x%x' % (self.__register, value, mask) self.__opiom.comm_ack(cmd) def _get(self): cmd = '?%s' % self.__register value = int(self.__opiom.comm_ack(cmd), base=16) value >>= self.__shift value &= self.__mask for label, state_value in self.__states.iteritems(): if state_value == value: return label return 'UNKNOWN' def _states_list(self): return self.__states.keys()
class Multiplexer: def __init__(self, name, config_tree): self.name = name self._boards = OrderedDict() self.__outputs = OrderedDict() all_config = static.get_config() for board in config_tree['boards']: board_name = board.get('name') self._boards[board_name] = all_config.get(board_name) for output_config in config_tree['outputs']: output = Output(self, output_config) self.__outputs[output.name()] = output self.__stat = HashObjSetting('multiplexer.%s' % name) self.__debug = False def setDebug(self, flag): self.__debug = flag is True for opiom in self._boards.itervalues(): opiom.setDebug(self.__debug) def getDebug(self): return self.__debug def getOutputList(self): return self.__outputs.keys() def getPossibleValues(self, output_key): output_key = output_key.upper() return self.__outputs[output_key].getSwitchList() def getKeyAndName(self): return dict([(key, output.comment()) for key, output in self.__outputs.iteritems()]) def getName(self, output_key): output_key = output_key.upper() return self.__outputs[output_key].comment() def switch(self, output_key, input_key, synchronous=False): output_key = output_key.upper() input_key = input_key.upper() if self.__debug: print "Multiplexer.switch %s to %s" % (output_key, input_key) try: output = self.__outputs[output_key] except KeyError: raise ValueError("Multiplexer don't have the ouput %s" % output_key) else: try: output.switch(input_key, synchronous) except ValueError, err: raise ValueError("%s is not available for output %s" % (str(err), output_key))
def __move(*args, **kwargs): wait, relative = kwargs.get('wait', True), kwargs.get('relative', False) motor_pos = OrderedDict() for m, p in zip(__get_objects_iter(*args[::2]), args[1::2]): motor_pos[m] = p group = Group(*motor_pos.keys()) group.move(motor_pos, wait=wait, relative=relative) return group, motor_pos
class Multiplexer: def __init__(self,name,config_tree) : self.name = name self._boards = OrderedDict() self.__outputs = OrderedDict() all_config = static.get_config() for board in config_tree['boards']: board_name = board.get('name') self._boards[board_name] = all_config.get(board_name) for output_config in config_tree['outputs']: output = Output(self,output_config) self.__outputs[output.name()] = output self.__stat = HashObjSetting('multiplexer.%s' % name) self.__debug= False def setDebug(self, flag): self.__debug= flag is True for opiom in self._boards.itervalues(): opiom.setDebug(self.__debug) def getDebug(self): return self.__debug def getOutputList(self) : return self.__outputs.keys() def getPossibleValues(self,output_key) : output_key = output_key.upper() return self.__outputs[output_key].getSwitchList() def getKeyAndName(self) : return dict([(key,output.comment()) for key,output in self.__outputs.iteritems()]) def getName(self,output_key) : output_key = output_key.upper() return self.__outputs[output_key].comment() def switch(self,output_key,input_key,synchronous = False): output_key = output_key.upper() input_key = input_key.upper() if self.__debug: print "Multiplexer.switch %s to %s"%(output_key, input_key) try: output = self.__outputs[output_key] except KeyError: raise ValueError("Multiplexer don't have the ouput %s" % output_key) else: try: output.switch(input_key,synchronous) except ValueError,err: raise ValueError("%s is not available for output %s" % (str(err),output_key))
class Switch(BaseSwitch): """ This class wrapped musst command to emulate a switch. the configuration may look like this: musst: $musst_name states: - label: OPEN set_cmd: "#BTRIG 1" test_cmd: "?BTRIG" test_cmd_reply: "1" - label: CLOSED set_cmd: "#BTRIG 0" test_cmd: "?BTRIG" test_cmd_reply: "0" """ def __init__(self, name, config): BaseSwitch.__init__(self, name, config) self.__musst = None self.__states = OrderedDict() self.__state_test = OrderedDict() def _init(self): config = self.config self.__musst = config['musst'] for state in config['states']: label = state['label'] cmd = state['set_cmd'] self.__states[label] = cmd t1 = {config['test_cmd_reply']: label} t = self.__state_test.setdefault(config['test_cmd'], result) if t1 != t: t.update(t1) def _set(self, state): cmd = self.__states.get(state) if cmd is None: raise RuntimeError("State %s don't exist" % state) self.__musst.putget(cmd) def _get(self): for test_cmd, test_reply in self.__state_test.iteritems(): reply = self.__musst.putget(test_cmd) state = test_reply.get(reply) if state is not None: return state return 'UNKNOWN' def _states_list(self): return self.__states.keys()
class Switch(BaseSwitch): """ This class wrapped musst command to emulate a switch. the configuration may look like this: musst: $musst_name states: - label: OPEN set_cmd: "#BTRIG 1" test_cmd: "?BTRIG" test_cmd_reply: "1" - label: CLOSED set_cmd: "#BTRIG 0" test_cmd: "?BTRIG" test_cmd_reply: "0" """ def __init__(self,name,config): BaseSwitch.__init__(self,name,config) self.__musst = None self.__states = OrderedDict() self.__state_test = OrderedDict() def _init(self): config = self.config self.__musst = config['musst'] for state in config['states']: label = state['label'] cmd = state['set_cmd'] self.__states[label] = cmd t1 = {config['test_cmd_reply'] : label} t = self.__state_test.setdefault(config['test_cmd'],result) if t1 != t: t.update(t1) def _set(self,state): cmd = self.__states.get(state) if cmd is None: raise RuntimeError("State %s don't exist" % state) self.__musst.putget(cmd) def _get(self): for test_cmd,test_reply in self.__state_test.iteritems(): reply = self.__musst.putget(test_cmd) state = test_reply.get(reply) if state is not None: return state return 'UNKNOWN' def _states_list(self): return self.__states.keys()
class Output: class Node: def __init__(self, board, register, shift, mask, value, parentNode): self.__board = board self.__register = register.upper() shift = shift self.__mask = mask << shift self.__value = value << shift self.__parent = parentNode def switch(self, synchronous): if self.__parent: self.__parent.switch(False) op = self.__board cmd = '%s 0x%x 0x%x' % (self.__register, self.__value, self.__mask) if synchronous: op.comm_ack(cmd) else: op.comm(cmd) def isActive(self, opiom_registers): activeFlag = True if self.__parent: activeFlag = self.__parent.isActive(opiom_registers) registerValue = opiom_registers[self.__board][self.__register] return activeFlag and ((registerValue & self.__mask) == self.__value) def __init__(self, multiplex, config_dict): config = OrderedDict(config_dict) self.__multiplex = multiplex self.__name = config.pop('label') self.__comment = config.pop('comment', '') self.__nodes = OrderedDict() self.__build_values(config) def name(self): return self.__name def comment(self): return self.__comment def getSwitchList(self): return self.__nodes.keys() def switch(self, switchValue, synchronous): switchValue = switchValue.upper() try: node = self.__nodes[switchValue] except KeyError: raise ValueError(switchValue) node.switch(synchronous) def getStat(self, opiom_register): for key, node in self.__nodes.iteritems(): if node.isActive(opiom_register): return key def __build_values(self, config, parentNode=None): cfg = dict() for key in ('board', 'register', 'shift', 'mask'): value = config.pop(key, None) if value is None: raise RuntimeError( "multiplexer: doesn't have '%s' keyword defined for %s switch" % (key, self.__comment or self.__name)) cfg[key] = value board = self.__multiplex._boards.get(cfg['board']) if board is None: raise RuntimeError( "multiplexer: can't find the board %s in config" % cfg['board']) register = cfg['register'] shift = cfg['shift'] mask = cfg['mask'] chain = config.pop('chain', None) # check if chained for key, value in config.iteritems(): key = key.upper() self.__nodes[key] = Output.Node(board, register, shift, mask, value, parentNode) if chain is not None: value = chain.pop('chained_value') parentNode = Output.Node(board, register, shift, mask, value, parentNode) self.__build_values(chain, parentNode)
class Output: class Node: def __init__(self,board,register,shift,mask,value,parentNode) : self.__board = board self.__register = register.upper() shift = shift self.__mask = mask << shift self.__value = value << shift self.__parent = parentNode def switch(self,synchronous) : if self.__parent: self.__parent.switch(False) op = self.__board cmd = '%s 0x%x 0x%x' % (self.__register,self.__value,self.__mask) if synchronous: op.comm_ack(cmd) else: op.comm(cmd) def isActive(self,opiom_registers) : activeFlag = True if self.__parent: activeFlag = self.__parent.isActive(opiom_registers) registerValue = opiom_registers[self.__board][self.__register] return activeFlag and ((registerValue & self.__mask) == self.__value) def __init__(self,multiplex,config_dict) : config = OrderedDict(config_dict) self.__multiplex = multiplex self.__name = config.pop('label') self.__comment = config.pop('comment','') self.__nodes = OrderedDict() self.__build_values(config) def name(self): return self.__name def comment(self) : return self.__comment def getSwitchList(self) : return self.__nodes.keys() def switch(self,switchValue,synchronous) : switchValue = switchValue.upper() try: node = self.__nodes[switchValue] except KeyError: raise ValueError(switchValue) node.switch(synchronous) def getStat(self,opiom_register) : for key,node in self.__nodes.iteritems() : if node.isActive(opiom_register) : return key def __build_values(self,config,parentNode = None) : cfg = dict() for key in ('board','register','shift','mask') : value = config.pop(key,None) if value is None: raise RuntimeError("multiplexer: doesn't have '%s' keyword defined for %s switch" % (key,self.__comment or self.__name)) cfg[key] = value board = self.__multiplex._boards.get(cfg['board']) if board is None: raise RuntimeError("multiplexer: can't find the board %s in config" % cfg['board']) register= cfg['register'] shift= cfg['shift'] mask= cfg['mask'] chain= config.pop('chain',None) # check if chained for key,value in config.iteritems() : key = key.upper() self.__nodes[key] = Output.Node(board,register,shift,mask,value,parentNode) if chain is not None: value = chain.pop('chained_value') parentNode = Output.Node(board,register,shift,mask,value,parentNode) self.__build_values(chain,parentNode)