From 585537fe05934a5783c21ac9c414f9d42ee82966 Mon Sep 17 00:00:00 2001 From: Paul Eastham Date: Sat, 12 Aug 2023 22:09:04 -0700 Subject: [PATCH] api alerter --- adsb_alerter.py | 43 +++++++++++++++++++++++++++++++++++-------- appsheet_api.py | 4 ++-- config.py | 4 ++-- flight.py | 2 +- test_tools/replay.py | 4 ++-- 5 files changed, 42 insertions(+), 15 deletions(-) diff --git a/adsb_alerter.py b/adsb_alerter.py index 0383491..e790ee7 100755 --- a/adsb_alerter.py +++ b/adsb_alerter.py @@ -7,22 +7,24 @@ - maybe also log to appsheet? """ -from collections import defaultdict import datetime +import json import requests import time import threading -import pytz import adsb_receiver from dbg import dbg, set_dbg_level, log from bboxes import Bboxes from config import Config +from adsb_receiver import Flights +from flight import Flight, Location import appsheet_api SEND_SLACK = True TZ_CONVERT = 0 # -7 # UTC conversion +API_LOOP_DELAY = 300 # seconds CONFIG = Config() def send_slack(text): @@ -52,6 +54,27 @@ def bbox_change_cb(flight, flight_str): if "Nearby" in flight_str: send_slack(flight_str) +def api_read_loop(bbox_list, bbox_cb): + flights = Flights(bbox_list) + + while True: + headers = { + "X-RapidAPI-Key": CONFIG.private_vars['rapid_api_key'], + "X-RapidAPI-Host": "adsbexchange-com1.p.rapidapi.com" + } + + response = requests.get(CONFIG.private_vars['absbx_url'], headers=headers) + jsondict = response.json() + print(jsondict) + + for ac in jsondict['ac']: + loc_update = Location.from_dict(ac) + print(loc_update) + flights.add_location(loc_update, None, None, bbox_cb) + + print("Sleeping ") + time.sleep(API_LOOP_DELAY) + if __name__ == "__main__": # No-GUI mode, see controller.py for GUI @@ -60,8 +83,10 @@ def bbox_change_cb(flight, flight_str): parser = argparse.ArgumentParser(description="match flights against kml bounding boxes") parser.add_argument("-d", "--debug", action="store_true") parser.add_argument('file', nargs='+', help="kml files to use") - parser.add_argument('--ipaddr', help="IP address to connect to", required=True) - parser.add_argument('--port', help="port to connect to", required=True) + parser.add_argument('--ipaddr', help="IP address to connect to") + parser.add_argument('--port', help="port to connect to" + ) + parser.add_argument('--api', action='store_true', help="use web api instead of direct connect IP") args = parser.parse_args() if args.debug: set_dbg_level(2) @@ -70,7 +95,9 @@ def bbox_change_cb(flight, flight_str): for f in args.file: bboxes_list.append(Bboxes(f)) - listen = adsb_receiver.setup(args.ipaddr, args.port) - - adsb_receiver.flight_read_loop(listen, bboxes_list, None, None, - None, bbox_start_change_cb) + if args.api: + api_read_loop(bboxes_list, bbox_start_change_cb) + else: + listen = adsb_receiver.setup(args.ipaddr, args.port) + adsb_receiver.flight_read_loop(listen, bboxes_list, None, None, + None, bbox_start_change_cb) diff --git a/appsheet_api.py b/appsheet_api.py index d3922bb..b6f1654 100755 --- a/appsheet_api.py +++ b/appsheet_api.py @@ -129,8 +129,8 @@ def delete_aircraft(self, regno): def add_aircraft_from_file(self, fn): with open(fn, 'r') as file: for line in file: - line = line.strip() - if line and line[0] == 'N': + line = line.strip().upper() + if line and (line[0] == 'N' or line[0] == 'C'): print(f"adding {line}") self.add_aircraft(line) diff --git a/config.py b/config.py index 0a43350..042c4e6 100644 --- a/config.py +++ b/config.py @@ -13,5 +13,5 @@ def __init__(self): try: with open(PRIVPATH, "r") as f: self.private_vars = yaml.safe_load(f) - except: - print("No private.yaml found.") + except Exception as e: + print("No private.yaml found, or parse fail: " +str(e)) diff --git a/flight.py b/flight.py index b24f98a..d255709 100644 --- a/flight.py +++ b/flight.py @@ -48,7 +48,7 @@ def from_dict(cl, d: dict): return Location(**nd) def to_str(self): - s = "%s: %d MSL %d deg %f kts %.4f, %.4f" % (self.flight, self.alt_baro, + s = "%s: %d MSL %d deg %.1f kts %.4f, %.4f" % (self.flight, self.alt_baro, self.track, self.gs, self.lat, self.lon) return s diff --git a/test_tools/replay.py b/test_tools/replay.py index 056ee26..c7e8202 100755 --- a/test_tools/replay.py +++ b/test_tools/replay.py @@ -17,10 +17,10 @@ import readsb_parse TZ_CONVERT = -7 # UTC conversion -- stored on disk as UTC but wire uses local -TIME_X = 4.3 # how many "x" versus real time to play back, 5000 max +TIME_X = 4.3 # how many "x" versus real time to play back, 5000 max ANALYZE_LEN_SECS = 60*60*6 +#start_date_string = '2022-09-01 16:20:00+00:00' # UTC start_date_string = '2022-09-01 13:20:00+00:00' # UTC -#start_date_string = '2022-08-23 13:30:00+00:00' # UTC start_date_time = datetime.fromisoformat(start_date_string) # start_date_time = datetime.strptime(start_date_string, '%Y-%m-%d %H:%M:%S')