Browse Source

Implement OpenSky

pull/2/head
Jxck-S 4 years ago
parent
commit
01022e5e7b
  1. 32
      NotifyBotMulti.py
  2. 32
      defOpenSky.py
  3. 41
      planeClass.py

32
NotifyBotMulti.py

@ -3,6 +3,7 @@ import configparser
import json
import time
from defADSBX import pullADSBX
from defOpenSky import pullOpenSky
from colorama import init, Fore, Back, Style
init(convert=True)
from planeClass import Plane
@ -12,16 +13,13 @@ main_config = configparser.ConfigParser()
main_config.read('mainconf.ini')
import os
#Setup Plane Objects off of Plane configs
if main_config.get('DATA', 'SOURCE') == "ADSBX":
planes = {}
for filename in os.listdir(os. getcwd()):
if filename.endswith(".ini") and filename != "mainconf.ini":
plane_config = configparser.ConfigParser()
plane_config.read(filename)
planes[plane_config.get('DATA', 'ICAO').upper()] = Plane(plane_config.get('DATA', 'ICAO'), filename)
planes = {}
for filename in os.listdir(os. getcwd()):
if filename.endswith(".ini") and filename != "mainconf.ini":
plane_config = configparser.ConfigParser()
plane_config.read(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
try:
tz = pytz.timezone(main_config.get('DATA', 'TZ'))
@ -51,6 +49,22 @@ while True:
else:
for obj in planes.values():
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
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)

32
defOpenSky.py

@ -1,35 +1,17 @@
def pullOpenSky(TRACK_PLANE):
def pullOpenSky(planes):
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
config.read('mainconf.ini')
from opensky_api import OpenSkyApi
planeData = None
opens_api = OpenSkyApi(config.get('OPENSKY', 'USERNAME'), config.get('OPENSKY', 'PASSWORD'))
failed = False
icao_array = []
for key, obj in planes.items():
icao_array.append(key.lower())
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:
print ("OpenSky Error")
failed = True
if failed is False and planeData != None:
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
return planeData, failed

41
planeClass.py

@ -70,14 +70,29 @@ class Plane:
self.latitude = None
self.on_ground = None
self.has_location = None
#Get API States for Plane
#Get States from ADSBX or OPENS Vector
self.plane_Dict = None
if main_config.get('DATA', 'SOURCE') == "OPENS":
raise NotImplementedError
#plane_Dict, failed = pullOpenSky(icao)
#print (Fore.YELLOW)
#print ("OpenSky Sourced Data: ", plane_Dict)
#print(Style.RESET_ALL)
self.val_error = False
if ac_dict != None:
try:
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)}
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":
self.val_error = False
if ac_dict != None:
@ -93,14 +108,14 @@ class Plane:
else:
self.plane_Dict = None
print (Fore.YELLOW)
print ("ADSBX Sourced Data: ", self.plane_Dict)
print(Style.RESET_ALL)
print (Fore.CYAN)
print ("ICAO:", self.icao)
print(Style.RESET_ALL)
print (Fore.YELLOW)
print ("ADSBX Sourced Data: ", self.plane_Dict)
print(Style.RESET_ALL)
print (Fore.CYAN)
print ("ICAO:", self.icao)
print(Style.RESET_ALL)
#Pull Variables from plane_Dict
if self.val_error is False:
if self.plane_Dict == None:
self.feeding = False

Loading…
Cancel
Save