def _receive(self): # Wait for incoming messages for 0.1 seconds recv_timeout = 0.1 # Receive timeout 0.1 seconds data = "" self.client_sock.settimeout(recv_timeout) try: data = self.client_sock.recv(1024).decode('utf-8') except bluetooth.btcommon.BluetoothError: # Recieved when server responds to shutdown pass except OSError: pass self.client_sock.settimeout(0) if self.restart_demanded: # Restart requested self.client_sock.close() del self.client_sock return "RESTART" elif self.exit_demanded: # Shutdown requested self.client_sock.close() del self.client_sock return "EXIT" if data: data = literal_eval(data) bt_in_task = BT_task(data[0], data[1]) self.queue_handler.post_in_queue(bt_in_task) print("Bt client received: ", str(data[1])) return ""
def pop_outgoing(): """ Called from server. Pops next outgoing BT_task from answer queue, tasks already processed by main unit. Returns popped task. """ answer_queue = open("bt_answers.txt", "rb") task = None task_q = [] while (True): try: task_i = pickle.load(answer_queue) task_q.append(task_i) except EOFError: break if task_q: task = BT_task(task_q[0].cmd_id, task_q[0].data) del task_q[0] answer_queue = open("bt_answers.txt", "wb") for task_i in task_q: pickle.dump(task_i, answer_queue) answer_queue.close() return task
def pop_incoming(): """ Called from main unit. Pops and returns next BT_task from commands queue, to be processed by the main unit. """ command_queue = open("bt_commands.txt", "rb") task = None task_q = [] while (True): try: task_i = pickle.load(command_queue) task_q.append(task_i) except EOFError: break if task_q: task = BT_task(task_q[0].cmd_id, task_q[0].data) del task_q[0] command_queue = open("bt_commands.txt", "wb") for task_i in task_q: pickle.dump(task_i, command_queue) command_queue.close() return task
def _send(self): bt_out_task = self.queue_handler.pop_out_queue() if bt_out_task: self.current_out_task = bt_out_task try: self.client_sock.send(str(bt_out_task.cmd_id)) except bluetooth.btcommon.BluetoothError: pass except OSError: pass if bt_out_task.cmd_id == protocol.BT_SERVER_SHUTDOWN: self.exit_demanded = True elif bt_out_task.cmd_id == protocol.BT_SERVER_RESTART: self.restart_demanded = True else: self.current_out_task = BT_task(0, 0)
def pop_outgoing(): answer_queue = open("bt_answers.txt", "rb") task = None task_q = [] while (True): try: task_i = pickle.load(answer_queue) task_q.append(task_i) except EOFError: break if task_q: task = BT_task(task_q[0].cmd_id, task_q[0].data) del task_q[0] answer_queue = open("bt_answers.txt", "wb") for task_i in task_q: pickle.dump(task_i, answer_queue) answer_queue.close() return task
def pop_incoming(): command_queue = open("bt_commands.txt", "rb") task = None task_q = [] while (True): try: task_i = pickle.load(command_queue) task_q.append(task_i) except EOFError: break if task_q: task = BT_task(task_q[0].cmd_id, task_q[0].data) del task_q[0] command_queue = open("bt_commands.txt", "wb") for task_i in task_q: pickle.dump(task_i, command_queue) command_queue.close() return task
def bt_shutdown(): EventBus.post(BLUETOOTH_ADDR, BT_task(BT_SERVER_SHUTDOWN))
def bt_forward_left(): EventBus.post(BLUETOOTH_ADDR, BT_task(BT_DRIVE_FORWARD_LEFT))
def bt_turn_left(): EventBus.post(BLUETOOTH_ADDR, BT_task(BT_TURN_LEFT))
def bt_forward_right(): EventBus.post(BLUETOOTH_ADDR, BT_task(BT_DRIVE_FORWARD_RIGHT))
def bt_turn_right(): EventBus.post(BLUETOOTH_ADDR, BT_task(BT_TURN_RIGHT))
def bt_return_map_data(data): EventBus.post(BLUETOOTH_ADDR, BT_task(BT_RETURN_MAP_DATA, data))
def bt_switch_to_manual(): EventBus.post(BLUETOOTH_ADDR, BT_task(MANUAL_MODE))
def bt_return_sensor_data(data): EventBus.post(BLUETOOTH_ADDR, BT_task(BT_RETURN_SENSOR_DATA, data))
def bt_request_servo_data(): EventBus.post(BLUETOOTH_ADDR, BT_task(BT_REQUEST_SERVO_DATA))
def bt_request_sensor_data(): EventBus.post(BLUETOOTH_ADDR, BT_task(BT_REQUEST_SENSOR_DATA))
def request_ip(): EventBus.post(BLUETOOTH_ADDR, BT_task(REQUEST_PI_IP))
def notify_mode_changed(new_mode): EventBus.post(BLUETOOTH_ADDR, BT_task(CMD_MODE_SET, new_mode))
def bt_restart(): EventBus.post(BLUETOOTH_ADDR, BT_task(BT_SERVER_RESTART))
def bt_return_ip(ip): EventBus.post(BLUETOOTH_ADDR, BT_task(RETURN_PI_IP, ip))
def bt_switch_to_auto(): EventBus.post(BLUETOOTH_ADDR, BT_task(AUTONOMOUS_MODE))
def bt_drive_back(): EventBus.post(BLUETOOTH_ADDR, BT_task(BT_DRIVE_BACK))
def bt_request_map_data(): EventBus.post(BLUETOOTH_ADDR, BT_task(BT_REQUEST_MAP_DATA))
def bt_return_servo_data(data): EventBus.post(BLUETOOTH_ADDR, BT_task(BT_RETURN_SERVO_DATA, data))
def post_to_incoming(self): """ Puts saved incoming data to queue to robot. """ bt_task_handler.post_incoming(BT_task(self.incoming_data, ""))
def bt_drive_forward(): EventBus.post(BLUETOOTH_ADDR, BT_task(BT_DRIVE_FORWARD))