def load(self): ''' Loads up the settings from gado.conf and instantiates objects for: The Scanner The Robot The Webcam ''' self.load_settings() self.scanner = Scanner(**self.s) self.robot = Robot(**self.s) self.camera = Webcam(**self.s)
def mainloop(self): ''' Repeating loop that constantly checks to see if the in-queue (the Gui) has any new messages for the system A Series of if/elses is then used to determine the appropriate action to take for each message received ''' #Set database interface, the system-to-gui queue and the robot as local vars dbi = self.dbi q = self.q_out robot = self.robot #add_to_queue(q, messages.READY) #Loop until told to stop while True: expecting_return = False try: #Grab a message from the gui-to-system queue (if on exists) msg = fetch_from_queue(self.q_in) #Add an new artifact set to the local database if msg[0] == messages.ADD_ARTIFACT_SET_LIST: i = dbi.add_artifact_set(**msg[1]) #Grab the full lits of artifact sets and pass it back #using the system-to-gui queue elif msg[0] == messages.ARTIFACT_SET_LIST: expecting_return = messages.ARTIFACT_SET_LIST i = dbi.artifact_set_list() add_to_queue(q, messages.ARTIFACT_SET_LIST, i) #Delete a specified artifact set elif msg[0] == messages.DELETE_ARTIFACT_SET_LIST: dbi.delete_artifact_set(msg[1]) #Grab the user's settings and either update the settings for #the robot, scanner and webcam; or reinstantiate those objects #with the new setttings elif msg[0] == messages.RELOAD_SETTINGS: settings = import_settings() self.s = settings self.robot.updateSettings(**settings) del self.camera self.camera = Webcam(**settings) del self.scanner self.scanner = Scanner(**settings) self._load_settings(**settings) #Restart the robot elif msg[0] == messages.RESET: self.robot.reset() #Attempt to connect to the robot (via serial port) #Pass back to Gui whether or not it was sucessfull elif msg[0] == messages.ROBOT_CONNECT: expecting_return = messages.ROBOT_CONNECT success = self.connect() self.logger.info('ROBOT_CONNECT: %s' % success) add_to_queue(q, messages.ROBOT_CONNECT, robot.connected()) #Delete the current scanner object (if it exists) #and instantiate a new one # #Pass back to Gui whether or not this was sucessfull elif msg[0] == messages.SCANNER_CONNECT: expecting_return = messages.SCANNER_CONNECT try: del self.scanner except: self.logger.exception("Exception while connecting to scanner") pass self.scanner = Scanner(**import_settings()) add_to_queue(q, messages.SCANNER_CONNECT, self.scanner.connected()) elif msg[0] == messages.SCANNER_CONNECTED: add_to_queue(q, messages.SCANNER_CONNECTED, self.scanner.connected()) #Scan an inmage using the scanner elif msg[0] == messages.SCANNER_PICTURE: expecting_return = messages.SCANNER_PICTURE self.scanner.scanImage(self.s['scanned_image']) add_to_queue(q, messages.SCANNER_PICTURE, self.s['scanned_image']) #Set the passed in artifact set as the currently selected elif msg[0] == messages.SET_SELECTED_ARTIFACT_SET: self.selected_set = msg[1] #Starts the robot's overall procedure for moving/scanning artifacts elif msg[0] == messages.START: self.start() #Grabs a list of available webcams and returns them to the Gui thread elif msg[0] == messages.WEBCAM_LISTING: self.logger.debug('WEBCAM_LISTING') expecting_return = messages.WEBCAM_LISTING opts = self.camera.options() self.logger.debug('WEBCAM_LISTING - %s' % opts) add_to_queue(q, messages.WEBCAM_LISTING, opts) #Attempt to connect to the selected webcam elif msg[0] == messages.WEBCAM_CONNECT: self.logger.debug('WEBCAM_CONNECT switch made it') expecting_return = messages.WEBCAM_CONNECT #If the webcam object exists if self.camera: self.logger.info('Camera already exists') #And it is connected if self.camera.connected(): self.logger.info('Already connected to the webcam') #Relay that information back to the Gui thread add_to_queue(q, messages.WEBCAM_CONNECT, self.camera.connected()) return #Otherwise, disconnect else: self.camera.disconnect() #Reinstantiate the webcam object with the user settings self.camera = Webcam(**import_settings()) #Connect to webcam and let the Gui thread know if it was sucessfull self.logger.info('self.camera.connected() %s' % self.camera.connected()) add_to_queue(q, messages.WEBCAM_CONNECT, self.camera.connected()) elif msg[0] == messages.WEBCAM_CONNECTED: add_to_queue(q, messages.WEBCAM_CONNECTED, self.camera.connected()) #Take a picture using the currently connected webcam elif msg[0] == messages.WEBCAM_PICTURE: expecting_return = messages.WEBCAM_PICTURE #Save the picture as the temp image name self.camera.savePicture(self.s['webcam_image']) #Pass that name back to the Gui thread add_to_queue(q, messages.WEBCAM_PICTURE, self.s['webcam_image']) #Grab a list of artifact sets, with weights so we can figure out the most #recently used elif msg[0] == messages.WEIGHTED_ARTIFACT_SET_LIST: expecting_return = messages.WEIGHTED_ARTIFACT_SET_LIST li = self.dbi.weighted_artifact_set_list() add_to_queue(q, messages.WEIGHTED_ARTIFACT_SET_LIST, arguments=li) #Kill the program!!! elif msg[0] == messages.MAIN_ABANDON_SHIP: add_to_queue(q, messages.GUI_LISTENER_DIE) add_to_queue(self.q_in, messages.MAIN_ABANDON_SHIP) return elif msg[0] == messages.STOP or msg[0] == messages.LAST_ARTIFACT or msg[0] == messages.RESET: # These commands are only relevant if the robot is already running. pass #Pass the robot object to the Gui thread elif msg[0] == messages.GIVE_ME_A_ROBOT: add_to_queue(q, messages.GIVE_ME_A_ROBOT, self.robot) else: # add it back to the in queue, was somebody else waiting for that message? add_to_queue(self.q_in, msg[0], (msg[1] if len(msg) > 1 else None)) #Something went horribly wrong, if the Gui thread is expecting a response, #Tell it things went poorly except: self.logger.exception("EXCEPTION GENERATED") #raise if expecting_return: add_to_queue(q, expecting_return)