def __init__(self, name, controller): ''' Initialise comms thread and setup serial ''' comms.interface.interface.__init__(self, name, controller) section = 'Comms_Serial' s = self._settings s.port = config.get(section, 'port') s.baudrate = int(config.get(section, 'baudrate')) s.bytesize = int(config.get(section, 'bytesize')) s.parity = config.get(section, 'parity') s.stopbits = int(config.get(section, 'stopbits')) s.timeout = int(config.get(section, 'timeout')) s.xonxoff = config.getBool(section, 'xonxoff') s.rtscts = config.getBool(section, 'rtscts') # Process data # Cast a numeric port identifier to an int try: s.port = int(s.port) except ValueError: # Wasn't numerical, pass pass s.parity = s.parity[0:1] # Load protocol self.getProtocol() self.start()
def _actionLog(self, data): ''' Sends log message to appropriate handler ''' if self.log_verboseness == None: # Config has not yet been loaded self.log_verboseness = self.log_levels[config.get('Logging', 'verboseness')] self.log_to_file = config.getBool('Logging', 'to_file') self.log_to_terminal = config.getBool('Logging', 'to_terminal') if self.log_levels[data[2]] < self.log_verboseness: # Not logging this level message return if self.log_to_file: self._actionLogFile(data) if self.log_to_terminal: self._actionLogTerminal(data)
def __init__(self, name, controller): ''' Initialise comms thread and setup serial ''' comms.interface.interface.__init__(self, name, controller) section = 'Comms_Serial' s = self._settings s.port = config.get(section, 'port') s.baudrate = int(config.get(section, 'baudrate')) s.bytesize = int(config.get(section, 'bytesize')) s.parity = config.get(section, 'parity') s.stopbits = int(config.get(section, 'stopbits')) s.timeout = int(config.get(section, 'timeout')) s.xonxoff = config.getBool(section, 'xonxoff') s.rtscts = config.getBool(section, 'rtscts') s.parity = s.parity[0:1] # Load protocol self.getProtocol() self.start()
def run(self): ''' Perform controller logic ''' # Check config regarding logging queue sizes log_actions = False if config.getBool('Logging', 'log_queues'): log_actions = True # Queue sizes (for logging) actionQueueSize = 0 actionQueueLowPrioritySize = 0 actionQueueBlockingSize = 0 # Continue running until no other threads are left except # this one and the main thread while threading.activeCount() > 2: # If no actions in queue, block if not self._actionQueue: self._checkBlock() # Loop through actions while self._actionQueue or self._actionQueueLowPriority or self._actionQueueBlocking: # Log the size of the queues if they have changed if log_actions: if actionQueueSize != len(self._actionQueue) or actionQueueLowPrioritySize != len(self._actionQueueLowPriority) or actionQueueBlockingSize != len(self._actionQueueBlocking): self._log('DEBUG', 'Size of action queues, blocking: %s, actions: %s, low-priority: %s' % (len(self._actionQueueBlocking), len(self._actionQueue), len(self._actionQueueLowPriority))) actionQueueSize = len(self._actionQueue) actionQueueLowPrioritySize = len(self._actionQueueLowPriority) actionQueueBlockingSize = len(self._actionQueueBlocking) # Actions in _actionQueueBlocking are top priority so always # get done before low priority actions if self._actionQueueBlocking: queue = self._actionQueueBlocking if log_actions: actionQueueBlockingSize -= 1 elif self._actionQueue: queue = self._actionQueue if log_actions: actionQueueSize -= 1 else: queue = self._actionQueueLowPriority if log_actions: actionQueueLowPrioritySize -= 1 # Grab oldest action in queue action = queue.pop(0) # Split data into relevant chunks if len(action) == 3: (action, data, block_id) = action else: block_id = None (action, data) = action # Log action #if action != 'Log': # self._log('DEBUG', 'Performing %s action' % action) # Check where action is located if '.' in action: # Run method in action module loc = action.rfind('.') module = action[0:loc] method = action[loc+1:] action_method = getattr(__import__(module, globals(), locals(), 'actions'), 'actions') result = getattr(action_method, method)(self, data).run() else: # Run actions internal method action = '_action'+action result = getattr(self, action)(data) # Save result and release lock for blocking actions if block_id: self._actionBlockingData[block_id] = result self._actionBlockingBlocks[block_id].set() # Print one last log message instead of running _final() # like other threads, which would just create a log action # that would never got run self._log('DEBUG', 'Shutting down controller thread')