Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- import time
- from collections import namedtuple
- from datetime import datetime
- from threading import Thread
- import pyModeS as pym
- from pprint import pprint
- def callback(data):
- pprint(data)
- print()
- class Sender:
- def __init__(self, p, v, i, callback):
- self.p = p
- self.v = v
- self.i = i
- self.callback = callback
- def get(self):
- ret = {}
- for icao, lat, lon, raw_even, raw_odd in self.p.positions:
- ret[icao] = {'lat': lat,
- 'lon': lon,
- 'raw_even': raw_even,
- 'raw_odd': raw_odd}
- for icao, v, raw in self.v.velocity:
- data = {'velocity': v, 'velocity_raw': raw}
- try:
- ret[icao].update(data)
- except:
- continue
- for icao, callsign, callsign_raw in self.i.identifications:
- data = {'callsign': callsign, 'callsign_raw': callsign_raw}
- try:
- ret[icao].update(data)
- except:
- continue
- return ret
- def run(self):
- while True:
- time.sleep(1)
- self.callback(self.get())
- class Data:
- EVEN = 0
- ODD = 1
- MAX_AGE = 60
- def __init__(self):
- self._data = {}
- def __bool__(self):
- if len(self._data):
- return True
- else:
- return False
- def __len__(self):
- return len(self._data)
- def update(self, msg):
- frame = self.create_entry(msg)
- self._data.update(frame)
- self.wipe()
- def create_entry(self, msg):
- return {datetime.now(): msg}
- def is_old(self, dt):
- if self.tdelta(dt) > self.MAX_AGE:
- return True
- else:
- return False
- def tdelta(self, dt):
- return (datetime.now() - dt).seconds
- def wipe(self):
- to_delete = []
- for key in self._data.keys():
- if self.is_old(key):
- to_delete.append(key)
- for i in to_delete:
- del self._data[i]
- class AirbonePositions(Data):
- def __len__(self):
- return len(self.positions)
- def get_sorted_items(self):
- sorted_items = sorted(self._data.items(),
- key=lambda i: (datetime.now() - i[0]).microseconds)
- return sorted_items
- def get_position_dict(self):
- pos_dict = {}
- for key, value in self.get_sorted_items():
- icao = pym.adsb.icao(value)
- oeflag = pym.adsb.oe_flag(value)
- data = {oeflag: (key, value)}
- if icao not in pos_dict:
- pos_dict[icao] = data
- elif oeflag not in pos_dict[icao]:
- pos_dict[icao].update(data)
- elif oeflag in pos_dict[icao]:
- t = pos_dict[icao][oeflag][0]
- td = (t - data[oeflag][0]).microseconds
- if td < 0:
- pos_dict[icao].update(data)
- return pos_dict
- @property
- def positions(self):
- ret = []
- positions = self.get_position_dict()
- for icao, item in positions.items():
- if self.EVEN in item and self.ODD in item:
- lat, lon = pym.adsb.position(
- item[0][1],
- item[1][1],
- item[0][0],
- item[1][0],
- )
- ret.append((icao, lat, lon, item[0][1], item[1][1]))
- return ret
- class Identifications(Data):
- def __len__(self):
- return len(self.identifications)
- @property
- def identifications(self):
- return {(pym.adsb.icao(d), pym.adsb.callsign(d).replace('_','').replace('*', ''), d) for
- d in identifications._data.values()}
- def callsign(self, icao):
- for data in self._data.values():
- if icao == pym.adsb.icao(data):
- return pym.adsb.callsign(data).replace('_','').replace('*', '')
- class Velocity(Data):
- def __len(self):
- return len(velocity)
- @property
- def velocity(self):
- return {(pym.adsb.icao(d), pym.adsb.velocity(d), d)
- for d in self._data.values()}
- def v(self, icao):
- for data in self._data.values():
- if icao == pym.adsb.icao(data):
- return pym.adsb.velocity(data)
- def stripper(data):
- return data.lstrip('*').rstrip(';\n\r')
- def get_df_tc(data):
- #print(pym.adsb.df(data), pym.adsb.typecode(data))
- return (pym.adsb.df(data), pym.adsb.typecode(data))
- def selector(data):
- for mode in msgtypes:
- df, tc = get_df_tc(data)
- if mode['df'] == df and mode['tc_min'] <= tc <= mode['tc_max']:
- return mode['msgtype']
- def aircraft_identification(data):
- identifications.update(data)
- def airbone_positions(data):
- positions.update(data)
- def airbone_velocity(data):
- velocity.update(data)
- msgtypes = [{'df': 17, 'tc_min': 1, 'tc_max': 4,
- 'msgtype': aircraft_identification},
- {'df': 17, 'tc_min': 9, 'tc_max': 18,
- 'msgtype': airbone_positions},
- {'df': 17, 'tc_min': 19, 'tc_max': 19,
- 'msgtype': airbone_velocity},
- ]
- positions = AirbonePositions()
- identifications = Identifications()
- velocity = Velocity()
- sender = Sender(positions, velocity, identifications, callback)
- sender_thread = Thread(target=sender.run)
- sender_thread.start()
- with open('adsb.record') as fd:
- for line in fd:
- msg = stripper(line)
- func = selector(msg)
- if func:
- func(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement