Created
November 17, 2023 16:16
-
-
Save mpentler/6dcf8ce2121676a69a68bd22e8a68ffa to your computer and use it in GitHub Desktop.
Adsb stats parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import json | |
import time | |
import os | |
import json | |
from contextlib import closing | |
from urllib.request import Request, urlopen | |
#--------- URLS for receiver, accepts multiple | |
urls= ["http://127.0.0.1/tar1090/data/aircraft.json"] | |
#--------- Basic aircraft class | |
class Aircraft: | |
hex = "" | |
flt = "" | |
reg = "" | |
swk = "" | |
lat = 0 | |
lng = 0 | |
spd = -999 | |
trk = -999 | |
alt = -999 | |
time = 0 | |
dis = 0 | |
dir = 0 | |
#--------- Code that is polling each receiver | |
def fetchADSBData(url): | |
tgts = [] | |
#aircraft_file = open(file_path, "r") | |
#aircraft_data = json.load(aircraft_file) | |
request_site = Request(url, headers={"User-Agent": "Mozilla/5.0"}) | |
with closing(urlopen(request_site)) as aircraft_file: | |
aircraft_data = json.load(aircraft_file) | |
for a in aircraft_data["aircraft"]: | |
tgt = Aircraft() | |
tgt.hex = a.get("hex") | |
tgt.flt = a.get("flight") | |
tgt.lat = a.get("lat") | |
tgt.lng = a.get("lon") | |
tgt.spd = a.get("gs") | |
tgt.trk = a.get("track") | |
tgt.alt = a.get("alt_geom") | |
tgt.swk = a.get("squawk") | |
tgt.dis = a.get("r_dst") | |
tgt.dir = a.get ("r_dir") | |
#Some cleanup to prevent invalid variables being passed on: | |
if tgt.reg is None or len(tgt.reg) < 1: | |
tgt.reg = tgt.hex | |
if tgt.flt is None: | |
tgt.flt = tgt.reg | |
if tgt.swk is None: | |
tgt.swk = 9999 | |
if tgt.alt is None: | |
tgt.alt = -999 | |
if tgt.spd is None: | |
tgt.spd = -999 | |
if tgt.trk is None: | |
tgt.trk = -999 | |
if tgt.lat is None: | |
tgt.lat = -999 | |
if tgt.lng is None: | |
tgt.lng = -999 | |
if tgt.dis is None: | |
tgt.dis = -999 | |
if tgt.dir is None: | |
tgt.dir = -999 | |
tgts.append(tgt) | |
return(tgts) | |
#--------- Main Code Loop that is called by the scheduler | |
def mainProcess(): | |
aircrafts = [] | |
# Grab aircraft.json | |
for i in range(0,len(urls),1): | |
aircrafts += fetchADSBData(urls[i]) | |
# Buffers for minimums and maximums: | |
dis_lo = 9999 | |
dis_lo_ac = Aircraft() | |
dis_hi = 0 | |
dis_hi_ac = Aircraft() | |
spd_lo = 999 | |
spd_lo_ac = Aircraft() | |
spd_hi = 0 | |
spd_hi_ac = Aircraft() | |
alt_lo = 99999 | |
alt_lo_ac = Aircraft() | |
alt_hi = 0 | |
alt_hi_ac = Aircraft() | |
for acft in aircrafts: | |
# Listing all aircraft: | |
#print( | |
# "Flight: {flt} Icao24 ID: {hex} Distance: {dis:8.2f} Altitude: {alt:5} - Latitude: {lat:7.4f} Longitude: {lng:7.4f} ".format( | |
# flt=acft.flt.strip().ljust(7), | |
# dis=acft.dis, | |
# hex=acft.hex, | |
# lat=acft.lat, | |
# lng=acft.lng, | |
# alt=acft.alt, | |
# ) | |
# ) | |
# Getting minimas and maximas: | |
if(acft.dis < dis_lo and acft.dis > 0): | |
dis_lo = acft.dis | |
dis_lo_ac = acft | |
if(acft.dis > dis_hi): | |
dis_hi = acft.dis | |
dis_hi_ac = acft | |
if(acft.spd < spd_lo and acft.spd > 0): | |
spd_lo = acft.spd | |
spd_lo_ac = acft | |
if(acft.spd > spd_hi): | |
spd_hi = acft.spd | |
spd_hi_ac = acft | |
if(acft.alt < alt_lo and acft.alt > 0): | |
alt_lo = acft.alt | |
alt_lo_ac = acft | |
if(acft.alt > alt_hi): | |
alt_hi = acft.alt | |
alt_hi_ac = acft | |
#------------------------------------------------------------------ | |
#Output the aircraft count | |
print() | |
print("Received Aircraft Count: {cnt}".format(cnt = len(aircrafts))) | |
print() | |
#Printing slowest and fastest aircraft: | |
print("Results:") | |
print("Closest: {flt} - {val} nm".format(flt = dis_lo_ac.flt.ljust(9), val = dis_lo)) | |
print("Furthest: {flt} - {val} nm".format(flt = dis_hi_ac.flt.ljust(9), val = dis_hi)) | |
print("Slowest: {flt} - {val} kts".format(flt = spd_lo_ac.flt.ljust(9), val = spd_lo)) | |
print("Fastest: {flt} - {val} kts".format(flt = spd_hi_ac.flt.ljust(9), val = spd_hi)) | |
print("Lowest: {flt} - {val} ft".format(flt = alt_lo_ac.flt.ljust(9), val = alt_lo)) | |
print("Highest: {flt} - {val} ft".format(flt = alt_hi_ac.flt.ljust(9), val = alt_hi)) | |
print("------------------------------------------------") | |
#--------- Main Code Loop | |
#while True: | |
mainProcess() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment