def _route_db_comm_protocol(self, raw_data_encoded): """Routing of the DroneBridge communication protocol packets""" status = False extracted_info = comm_message_extract_info( raw_data_encoded) # returns json bytes [0] and crc bytes [1] try: loaded_json = json.loads(extracted_info[0].decode()) except UnicodeDecodeError: print(self.tag + "Invalid command: Could not decode json message") return False except ValueError: print(self.tag + "ValueError on decoding extracted_info[0]") return False if loaded_json[ 'destination'] == 1 and self.comm_direction == DBDir.DB_TO_UAV and check_package_good( extracted_info): message = self._process_db_comm_protocol_type(loaded_json) if message != "": status = self.sendto_smartphone(message, self.APP_PORT_COMM) else: status = True elif loaded_json['destination'] == 2 and check_package_good( extracted_info): if self.comm_direction == DBDir.DB_TO_UAV: response_drone = self._redirect_comm_to_drone(raw_data_encoded) if response_drone != False and response_drone != None: message = self._process_db_comm_protocol_type(loaded_json) self.sendto_smartphone(message, self.APP_PORT_COMM) status = self.sendto_smartphone(response_drone, self.APP_PORT_COMM) else: message = self._process_db_comm_protocol_type(loaded_json) sentbytes = self.sendto_groundstation( message, DBPort.DB_PORT_COMMUNICATION.value) if sentbytes == None: status = True elif loaded_json['destination'] == 3: if self.comm_direction == DBDir.DB_TO_UAV: status = self.sendto_uav(raw_data_encoded, DBPort.DB_PORT_COMMUNICATION.value) else: change_settings_gopro(loaded_json) elif loaded_json['destination'] == 4: if self.comm_direction == DBDir.DB_TO_UAV: status = self.sendto_smartphone(raw_data_encoded, self.APP_PORT_COMM) else: print(self.tag + "DB_COMM_PROTO: Unknown message destination") return status
def _route_db_comm_protocol(self, raw_data_encoded): status = False extracted_info = comm_message_extract_info( raw_data_encoded) # returns json bytes and crc bytes loaded_json = json.loads(extracted_info[0].decode()) if loaded_json[ 'destination'] == 1 and self.comm_direction == TO_DRONE and check_package_good( extracted_info): message = self._process_db_comm_protocol_type(loaded_json) if message != "": status = self.sendto_smartphone(message, self.COMM_PORT_SMARTPHONE) else: status = True elif loaded_json['destination'] == 2 and check_package_good( extracted_info): if self.comm_direction == TO_DRONE: response_drone = self._redirect_comm_to_drone(raw_data_encoded) if response_drone != False and response_drone != None: message = self._process_db_comm_protocol_type(loaded_json) self.sendto_smartphone(message, self.COMM_PORT_SMARTPHONE) status = self.sendto_smartphone(response_drone, self.COMM_PORT_SMARTPHONE) else: message = self._process_db_comm_protocol_type(loaded_json) sentbytes = self.sendto_groundstation(message, PORT_COMMUNICATION) if sentbytes == None: status = True elif loaded_json['destination'] == 3: if self.comm_direction == TO_DRONE: status = self._sendto_drone(raw_data_encoded, PORT_COMMUNICATION) else: change_settings_gopro(loaded_json) elif loaded_json['destination'] == 4: if self.comm_direction == TO_DRONE: status = self.sendto_smartphone(raw_data_encoded, self.COMM_PORT_SMARTPHONE) else: print(self.tag + "DB_COMM_PROTO: Unknown message destination") return status
def _route_db_comm_protocol(self, raw_data_encoded): """Routing of the DroneBridge communication protocol packets. Only write to local settings if we get a positive response from the drone! Ping requests are a exception!""" status = False extracted_info = comm_message_extract_info(raw_data_encoded) # returns json bytes [0] and crc bytes [1] try: loaded_json = json.loads(extracted_info[0].decode()) except UnicodeDecodeError: print(self.tag + "Invalid command: Could not decode json message") return False except ValueError: print(self.tag + "ValueError on decoding extracted_info[0]") return False # Check CRC if not comm_crc_correct(extracted_info): message = new_error_response_message('Bad CRC', self.comm_direction.value, loaded_json['id']) if self.comm_direction == DBDir.DB_TO_UAV: self.sendto_smartphone(message, DBPort.DB_PORT_COMMUNICATION.value) else: self.sendto_groundstation(message, DBPort.DB_PORT_COMMUNICATION.value) return False # Process communication protocol if loaded_json['destination'] == 1 and self.comm_direction == DBDir.DB_TO_UAV: message = self._process_db_comm_protocol_type(loaded_json) if message != "": status = self.sendto_smartphone(message, self.APP_PORT_COMM) else: status = True elif loaded_json['destination'] == 2: if self.comm_direction == DBDir.DB_TO_UAV: # Always process ping requests right away! Do not wait for UAV response! if loaded_json['type'] == DBCommProt.DB_TYPE_PING_REQUEST.value: message = self._process_db_comm_protocol_type(loaded_json) status = self.sendto_smartphone(message, self.APP_PORT_COMM) response_drone = self._redirect_comm_to_drone(raw_data_encoded) if type(response_drone) is bytearray: status = self.sendto_smartphone(response_drone, self.APP_PORT_COMM) else: response_drone = self._redirect_comm_to_drone(raw_data_encoded) if type(response_drone) is bytearray: message = self._process_db_comm_protocol_type(loaded_json) self.sendto_smartphone(message, self.APP_PORT_COMM) status = self.sendto_smartphone(response_drone, self.APP_PORT_COMM) else: message = new_error_response_message('UAV was unreachable - command not executed', DBCommProt.DB_ORIGIN_GND.value, loaded_json['id']) self.sendto_smartphone(message, self.APP_PORT_COMM) else: message = self._process_db_comm_protocol_type(loaded_json) sentbytes = self.sendto_groundstation(message, DBPort.DB_PORT_COMMUNICATION.value) if sentbytes == None: status = True elif loaded_json['destination'] == 3: if self.comm_direction == DBDir.DB_TO_UAV: status = self.sendto_uav(raw_data_encoded, DBPort.DB_PORT_COMMUNICATION.value) else: change_settings_gopro(loaded_json) elif loaded_json['destination'] == 4: if self.comm_direction == DBDir.DB_TO_UAV: status = self.sendto_smartphone(raw_data_encoded, self.APP_PORT_COMM) elif loaded_json['destination'] == 5: if self.comm_direction == DBDir.DB_TO_UAV: status = self.sendto_uav(raw_data_encoded, DBPort.DB_PORT_COMMUNICATION.value) else: message = self._process_db_comm_protocol_type(loaded_json) if self.sendto_groundstation(message, DBPort.DB_PORT_COMMUNICATION.value) == None: status = True else: print(self.tag + "DB_COMM_PROTO: Unknown message destination") return status