Browse Source

simplifying rapidadsbx logic

- raising exception if response not 200
- decode json from resposonse lib
- simplifying iteration of planes
- updating plane data in the same loop as querying api

fixes Jxck-S/plane-notify#84
pull/85/head
Mark Bumiller 2 years ago
parent
commit
4d44d58306
  1. 41
      __main__.py
  2. 40
      defRpdADSBX.py

41
__main__.py

@ -190,36 +190,21 @@ try:
print(key, "has", len(sorted_ras[key]), "RAs")
obj.check_new_ras(sorted_ras[key])
obj.expire_ra_types()
icao_key = 'hex'
from defRpdADSBX import pull_rpdadsbx
# print("Planes list: \n"+str((list(planes.keys()))))
# print("\nLen planes:\n"+str(len(planes)))
p = 0
data = dict.fromkeys(['ac'])
# print(data)
while p < len(planes):
planeInfo = pull_rpdadsbx(str(list(planes.keys())[p]))
if p == 0:
data['ac'] = (planeInfo)['ac']
data_indexed = {}
for icao in planes:
plane = planes[icao]
plane_info = pull_rpdadsbx(icao)
if plane_info is not None:
data_indexed[icao.upper()] = plane_info['ac']
if data_indexed[icao.upper()]:
plane.run_adsbx_v2(data_indexed[icao.upper()])
else:
plane.run_empty()
else:
data['ac'].extend((planeInfo)['ac'])
# print("p = "+str(p))
# print(str(list(planes.keys())[p]) + ": " + str(data))
p += 1
if data is not None:
if data['ac'] is not None:
data_indexed = {}
for planeData in data['ac']:
data_indexed[planeData[icao_key].upper()] = planeData
for key, obj in planes.items():
try:
obj.run_adsbx_v2(data_indexed[key.upper()])
except KeyError:
obj.run_empty()
else:
for obj in planes.values():
obj.run_empty()
else:
print(f"No data for icao {icao}. Skipping...")
plane.run_empty()
if not data_indexed:
failed_count += 1
elif source == "OPENS":
from defOpenSky import pull_opensky

40
defRpdADSBX.py

@ -1,5 +1,4 @@
import requests
import json
import configparser
from datetime import datetime
@ -18,29 +17,18 @@ def pull_rpdadsbx(planes):
}
try:
response = requests.get(url, headers = headers, timeout=30)
except Exception as error:
print('err.args:' + str(error.args))
response = None
if response is not None:
try:
data = json.loads(response.text)
except (json.decoder.JSONDecodeError, ValueError) as error_message:
print("Error with JSON")
print(error_message)
data = None
except TypeError as error_message:
print("Type Error", error_message)
data = None
else:
if "msg" in data.keys() and data['msg'] != "No error":
raise ValueError("Error from ADSBX: msg = ", data['msg'])
if "ctime" in data.keys():
data_ctime = float(data['ctime']) / 1000.0
print("Data ctime:",datetime.utcfromtimestamp(data_ctime))
if "now" in data.keys():
data_now = float(data['now']) / 1000.0
print("Data now time:",datetime.utcfromtimestamp(data_now))
response.raise_for_status()
data = response.json()
if "msg" in data.keys() and data['msg'] != "No error":
raise ValueError("Error from ADSBX: msg = ", data['msg'])
if "ctime" in data.keys():
data_ctime = float(data['ctime']) / 1000.0
print("Data ctime:",datetime.utcfromtimestamp(data_ctime))
if "now" in data.keys():
data_now = float(data['now']) / 1000.0
print("Data now time:",datetime.utcfromtimestamp(data_now))
print("Current UTC:", datetime.utcnow())
else:
data = None
return data
return data
except Exception as e:
print('Error calling RapidAPI', e)
return None

Loading…
Cancel
Save