class Controller: """ Drives the server side of TARS application. Controls the signals through GPIO pins based on the messages received from the client connection. """ def __init__(self): self.soc = ServerSocket() #self.gpio_handler = GPIOHandler() self.camera = ServerCameraHandler() def parse_message(self, msg): content = msg.split("|") result = re.match(DIRECTION_TUPLE_PATTERN, content[1]) direction = list(result.groups()) for i in xrange(0, len(direction)): direction[i] = int(direction[i]) settings = (tuple(direction), int(content[2]), float(content[3])) return settings def run(self): try: while True: msg = self.soc.listen() if msg == STARTUP: print "startup" #self.gpio_handler.startup() elif msg.startswith(MOTOR_CHANGE): settings = self.parse_message(msg) #self.gpio_handler.motor_change(settings) elif msg == DETECT_OBSTACLE: #self.soc.reply("@" + str(self.gpio_handler.detect_obstacle())) if randint(0,2): self.soc.reply("@False") else: self.soc.reply("@True") elif msg == CLICK_PICTURE: self.soc.reply(str(self.camera.click_picture())) elif msg in {STANDBY, SOCKET_ERROR}: self.soc.standby() elif msg == SHUTDOWN: #self.gpio_handler.shutdown() pass except KeyboardInterrupt: self.soc.shutdown() #self.gpio_handler.shutdown() sys.exit()
def __init__(self): self.soc = ServerSocket() #self.gpio_handler = GPIOHandler() self.camera = ServerCameraHandler()