Skip to content

Commit

Permalink
api alerter
Browse files Browse the repository at this point in the history
  • Loading branch information
eastham committed Aug 13, 2023
1 parent f04efcf commit 585537f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
43 changes: 35 additions & 8 deletions adsb_alerter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
4 changes: 2 additions & 2 deletions appsheet_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
2 changes: 1 addition & 1 deletion flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions test_tools/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 585537f

Please sign in to comment.