Implement OpenSky

This commit is contained in:
Jxck-S 2020-10-05 08:11:09 -04:00
parent 38fadf09d0
commit 01022e5e7b
3 changed files with 58 additions and 47 deletions

View File

@ -3,6 +3,7 @@ import configparser
import json import json
import time import time
from defADSBX import pullADSBX from defADSBX import pullADSBX
from defOpenSky import pullOpenSky
from colorama import init, Fore, Back, Style from colorama import init, Fore, Back, Style
init(convert=True) init(convert=True)
from planeClass import Plane from planeClass import Plane
@ -12,16 +13,13 @@ main_config = configparser.ConfigParser()
main_config.read('mainconf.ini') main_config.read('mainconf.ini')
import os import os
#Setup Plane Objects off of Plane configs #Setup Plane Objects off of Plane configs
if main_config.get('DATA', 'SOURCE') == "ADSBX": planes = {}
planes = {} for filename in os.listdir(os. getcwd()):
for filename in os.listdir(os. getcwd()): if filename.endswith(".ini") and filename != "mainconf.ini":
if filename.endswith(".ini") and filename != "mainconf.ini": plane_config = configparser.ConfigParser()
plane_config = configparser.ConfigParser() plane_config.read(filename)
plane_config.read(filename) planes[plane_config.get('DATA', 'ICAO').upper()] = Plane(plane_config.get('DATA', 'ICAO'), filename)
planes[plane_config.get('DATA', 'ICAO').upper()] = Plane(plane_config.get('DATA', 'ICAO'), filename)
elif main_config.get('DATA', 'SOURCE') == "OPENS":
raise NotImplementedError
running_Count = 0 running_Count = 0
try: try:
tz = pytz.timezone(main_config.get('DATA', 'TZ')) tz = pytz.timezone(main_config.get('DATA', 'TZ'))
@ -51,6 +49,22 @@ while True:
else: else:
for obj in planes.values(): for obj in planes.values():
obj.run(None) obj.run(None)
elif main_config.get('DATA', 'SOURCE') == "OPENS":
planeData, failed = pullOpenSky(planes)
if failed == False:
if planeData.states != []:
for key, obj in planes.items():
has_data = False
for dataState in planeData.states:
if (dataState.icao24).upper() == key:
obj.run(dataState)
has_data = True
break
if has_data is False:
obj.run(None)
else:
for obj in planes.values():
obj.run(None)
elapsed_calc_time = time.time() - start_time elapsed_calc_time = time.time() - start_time
datetime_tz = datetime.now(tz) datetime_tz = datetime.now(tz)
print (Back.GREEN, Fore.BLACK, "--------", running_Count, "--------", datetime_tz.strftime("%I:%M:%S %p"), "------------------------Elapsed Time-", elapsed_calc_time, " -------------------------------------", Style.RESET_ALL) print (Back.GREEN, Fore.BLACK, "--------", running_Count, "--------", datetime_tz.strftime("%I:%M:%S %p"), "------------------------Elapsed Time-", elapsed_calc_time, " -------------------------------------", Style.RESET_ALL)

View File

@ -1,35 +1,17 @@
def pullOpenSky(TRACK_PLANE): def pullOpenSky(planes):
import configparser import configparser
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read('config.ini') config.read('mainconf.ini')
from opensky_api import OpenSkyApi from opensky_api import OpenSkyApi
planeData = None planeData = None
opens_api = OpenSkyApi(config.get('OPENSKY', 'USERNAME'), config.get('OPENSKY', 'PASSWORD')) opens_api = OpenSkyApi(config.get('OPENSKY', 'USERNAME'), config.get('OPENSKY', 'PASSWORD'))
failed = False failed = False
icao_array = []
for key, obj in planes.items():
icao_array.append(key.lower())
try: try:
planeData = opens_api.get_states(time_secs=0, icao24=TRACK_PLANE.lower()) planeData = opens_api.get_states(time_secs=0, icao24=icao_array)
except: except:
print ("OpenSky Error") print ("OpenSky Error")
failed = True failed = True
if failed is False and planeData != None: return planeData, failed
plane_Dict = {}
geo_alt_m = None
for dataStates in planeData.states:
plane_Dict['icao'] = (dataStates.icao24).upper()
plane_Dict['callsign'] = (dataStates.callsign)
plane_Dict['longitude'] = (dataStates.longitude)
plane_Dict['latitude'] = (dataStates.latitude)
plane_Dict['on_ground'] = (dataStates.on_ground)
geo_alt_m = (dataStates.geo_altitude)
try:
if geo_alt_m != None:
plane_Dict['geo_alt_ft'] = geo_alt_m * 3.281
elif plane_Dict['on_ground']:
plane_Dict['geo_alt_ft'] = 0
except KeyError:
pass
if plane_Dict == {}:
plane_Dict = None
else:
plane_Dict = None
return plane_Dict, failed

View File

@ -70,14 +70,29 @@ class Plane:
self.latitude = None self.latitude = None
self.on_ground = None self.on_ground = None
self.has_location = None self.has_location = None
#Get API States for Plane #Get States from ADSBX or OPENS Vector
self.plane_Dict = None self.plane_Dict = None
if main_config.get('DATA', 'SOURCE') == "OPENS": if main_config.get('DATA', 'SOURCE') == "OPENS":
raise NotImplementedError self.val_error = False
#plane_Dict, failed = pullOpenSky(icao) if ac_dict != None:
#print (Fore.YELLOW) try:
#print ("OpenSky Sourced Data: ", plane_Dict) self.plane_Dict ={'icao' : ac_dict.icao24, 'callsign' : ac_dict.callsign, 'latitude' : ac_dict.latitude, 'longitude' : ac_dict.longitude, 'on_ground' : bool(ac_dict.on_ground)}
#print(Style.RESET_ALL) if ac_dict.geo_altitude != None:
self.plane_Dict['geo_alt_ft'] = float(ac_dict.geo_altitude) * 3.281
elif self.plane_Dict['on_ground']:
self.plane_Dict['geo_alt_ft'] = 0
except ValueError as e:
self.plane_Dict = None
self.val_error = True
print("Got data but some data is invalid!")
print(e)
else:
self.plane_Dict = None
print (Fore.YELLOW)
print ("OpenSky Sourced Data: ", self.plane_Dict)
print(Style.RESET_ALL)
elif main_config.get('DATA', 'SOURCE') == "ADSBX": elif main_config.get('DATA', 'SOURCE') == "ADSBX":
self.val_error = False self.val_error = False
if ac_dict != None: if ac_dict != None:
@ -93,14 +108,14 @@ class Plane:
else: else:
self.plane_Dict = None self.plane_Dict = None
print (Fore.YELLOW) print (Fore.YELLOW)
print ("ADSBX Sourced Data: ", self.plane_Dict) print ("ADSBX Sourced Data: ", self.plane_Dict)
print(Style.RESET_ALL) print(Style.RESET_ALL)
print (Fore.CYAN) print (Fore.CYAN)
print ("ICAO:", self.icao) print ("ICAO:", self.icao)
print(Style.RESET_ALL) print(Style.RESET_ALL)
#Pull Variables from plane_Dict
if self.val_error is False: if self.val_error is False:
if self.plane_Dict == None: if self.plane_Dict == None:
self.feeding = False self.feeding = False