def top_secret(): """Flask API to obtain the location of the cargo ship and the message. """ sat1 = Satelite('kenobi', 0, 0) sat2 = Satelite('skywalker', 3, 0) sat3 = Satelite('sato', 5, 3) trilateration = Trilateration([sat1, sat2, sat3]) decoder = MessageDecoder() if request.method == 'POST': try: request_data = request.get_json() satellites = request_data["satellites"] trilat_distances = list() received_msgs = list() for satellite in satellites: trilat_distances.append(satellite['distance']) received_msgs.append(satellite['message']) ship_pos = trilateration.get_location(trilat_distances) message_decoded = decoder.get_messages(received_msgs) response = { "position": { "x": str(ship_pos[0]), "y": str(ship_pos[1]) }, "message": message_decoded } except Exception: response = { "msg": "The message or the position of the ship \ could not be determined. Please check the payload." } return response, STATUS_ERROR return response, STATUS_OK return 'Invalid request.', 404
def main(datafile=DEF_FILE_NAME): data = read_data(datafile) drone_data = read_drone_data() drone_locs = {} for index, row in drone_data.iterrows(): drone_locs[row["drone_id"]] = [float(row["x_m"]), float(row["y_m"]), float(row["z_m"])] time_grouped = group_data(data[PI_MAC], 0) sensors_per_time = [] for time_group in time_grouped: ap_ids = [] sensors = [] used_sensors = set() sorted_data = sorted(time_group[1], key=lambda x: x[2]) for sensor in sorted_data: if(sensor[1] in used_sensors): continue used_sensors.add(sensor[1]) try: loc = drone_locs[sensor[1]] except KeyError: continue ap_ids.append(sensor[1]) sensors.append({'x': loc[0], 'y': loc[1], 'z': loc[2], 'signal': int(sensor[2])}) if count_unique_aps(ap_ids) > 3: sensors_per_time.append([time_group[0], sensors]) else: print('JAMMER') for param in range(0, 301, 5): gamma = param / 100 with open("../results/trilateration/results_trilateration_{}_{}.csv".format(SECONDS_GROUPING, gamma), "a") as f: writer = csv.writer(f, delimiter=";") writer.writerow(["sourcemac", "timestamp", "x_m", "y_m", "z_m", "eta"]) trilateration = Trilateration(gamma) for row in sensors_per_time: est = trilateration.get_location(row[1]) writer.writerow([PI_MAC, row[0]] + list(est.x))
def get_position(): wifi_data = WifiSignalParse() global AP_map wifi_data.receive_data(AP_map) trilateration = Trilateration() pos, routers, estimates = trilateration.trilaterate(wifi_data, AP_map) return { 'pos': { 'x': pos[0], 'y': pos[1], }, 'routers': routers, 'estimates': estimates }
class Main: """A class for establishing connections""" def start_db_hdlr(self, db): self.db_hdlr = DatabaseHandler(db) self.db_hdlr.flush_rss_readings() self.db_hdlr.check_pos_tbl() self.db_hdlr.check_rsl_tbl() def start_net_srv(self): self.net_srv = NetworkServer() self.net_srv.start() def start_serial_conn(self, port, timeout): self.serial_conn = SerialConnection(port, timeout) self.serial_conn.open() self.serial_conn.flush_input() def start_trilat(self): self.trilat = Trilateration() self.trilat.start() def run(self): try: while True: data = self.serial_conn.read().strip() tag_id = data[:4] rss = data[4:] self.db_hdlr.ins_reading(2, tag_id, rss) except KeyboardInterrupt: self.db_hdlr.close_db() self.net_srv.stop() self.net_srv.join(60) self.trilat.stop() self.trilat.join(20) self.serial_conn.close()
def start_trilat(self): self.trilat = Trilateration() self.trilat.start()