Move parser factory decorator into parse.py. Fix multiple bugs in parse.

This commit is contained in:
Nick Foster 2013-06-18 21:49:07 -07:00
parent 302fa7203d
commit a7af518653
4 changed files with 29 additions and 27 deletions

View File

@ -60,32 +60,14 @@ def main():
if options.remote is not None: if options.remote is not None:
servers += options.remote.split(",") servers += options.remote.split(",")
relay = air_modes.zmq_pubsub_iface(context, subaddr=servers, pubaddr=None) relay = air_modes.zmq_pubsub_iface(context, subaddr=servers, pubaddr=None)
#ok now relay is gonna get all those tasty strings
#internally we want to distribute parsed data instead, lighten the load
#TODO obviously this should go somewhere besides the main app. But where?
publisher = pubsub() publisher = pubsub()
def make_report(pub, message): relay.subscribe("dl_data", air_modes.make_parser(publisher))
[data, ecc, reference, timestamp] = message.split()
try:
ret = air_modes.modes_report(air_modes.modes_reply(int(data, 16)),
int(ecc, 16),
20.0*math.log10(float(reference)),
air_modes.stamp(0, float(timestamp)))
pub["modes_dl"] = ret
pub["type%i_dl" % ret.data.get_type()] = ret
except ADSBError:
pass
send = lambda msg: make_report(publisher, msg)
relay.subscribe("dl_data", send)
if options.location is not None: if options.location is not None:
my_position = [float(n) for n in options.location.split(",")] my_position = [float(n) for n in options.location.split(",")]
#CPR decoder obj to handle getting position from BDS0,5 and BDS0,6 pkts #CPR decoder obj to handle getting position from BDS0,5 and BDS0,6 pkts
cpr_dec = air_modes.cpr.cpr_decoder(my_position) cpr_dec = air_modes.cpr_decoder(my_position)
if options.kml is not None: if options.kml is not None:
dbname = 'adsb.db' dbname = 'adsb.db'

View File

@ -64,6 +64,7 @@ from exceptions import *
from az_map import * from az_map import *
from types import * from types import *
from altitude import * from altitude import *
from cpr import cpr_decoder
#this is try/excepted in case the user doesn't have numpy installed #this is try/excepted in case the user doesn't have numpy installed
try: try:
from flightgear import output_flightgear from flightgear import output_flightgear

View File

@ -45,8 +45,10 @@ class output_print:
if msg.data.get_type() not in self._fns: if msg.data.get_type() not in self._fns:
retstr = output_print.prefix(msg) retstr = output_print.prefix(msg)
retstr += "No handler for message type %i" % msg.data.get_type() retstr += "No handler for message type %i" % msg.data.get_type()
if "ap" in msg.data.fields: if "aa" not in msg.data.fields:
retstr += " from %.6x" % msg.data["ap"] retstr += " from %.6x" % msg.ecc
else:
retstr += " from %.6x" % msg.data["aa"]
print retstr print retstr
def handle0(self, msg): def handle0(self, msg):

View File

@ -23,8 +23,8 @@ import time, os, sys
from string import split, join from string import split, join
from altitude import decode_alt from altitude import decode_alt
import math import math
import air_modes
from air_modes.exceptions import * from air_modes.exceptions import *
from air_modes import cpr
#this implements a packet class which can retrieve its own fields. #this implements a packet class which can retrieve its own fields.
class data_field: class data_field:
@ -376,7 +376,7 @@ def parseBDS62(data):
def parseMB_id(data): #bds1 == 2, bds2 == 0 def parseMB_id(data): #bds1 == 2, bds2 == 0
msg = "" msg = ""
for i in range(0, 8): for i in range(0, 8):
msg += self.charmap( data["ais"] >> (42-6*i) & 0x3F) msg += charmap( data["ais"] >> (42-6*i) & 0x3F)
return (msg) return (msg)
def parseMB_TCAS_resolutions(data): def parseMB_TCAS_resolutions(data):
@ -406,15 +406,32 @@ def parseMB_TCAS_resolutions(data):
def parseMB_TCAS_threatid(data): #bds1==3, bds2==0, TTI==1 def parseMB_TCAS_threatid(data): #bds1==3, bds2==0, TTI==1
#3: {"bds1": (33,4), "bds2": (37,4), "ara": (41,14), "rac": (55,4), "rat": (59,1), #3: {"bds1": (33,4), "bds2": (37,4), "ara": (41,14), "rac": (55,4), "rat": (59,1),
# "mte": (60,1), "tti": (61,2), "tida": (63,13), "tidr": (76,7), "tidb": (83,6)} # "mte": (60,1), "tti": (61,2), "tida": (63,13), "tidr": (76,7), "tidb": (83,6)}
(resolutions, complements) = self.parseMB_TCAS_resolutions(data) (resolutions, complements) = parseMB_TCAS_resolutions(data)
return (resolutions, complements, data["rat"], data["mte"], data["tid"]) return (resolutions, complements, data["rat"], data["mte"], data["tid"])
def parseMB_TCAS_threatloc(data): #bds1==3, bds2==0, TTI==2 def parseMB_TCAS_threatloc(data): #bds1==3, bds2==0, TTI==2
(resolutions, complements) = self.parseMB_TCAS_resolutions(data) (resolutions, complements) = parseMB_TCAS_resolutions(data)
threat_alt = decode_alt(data["tida"], True) threat_alt = decode_alt(data["tida"], True)
return (resolutions, complements, data["rat"], data["mte"], threat_alt, data["tidr"], data["tidb"]) return (resolutions, complements, data["rat"], data["mte"], threat_alt, data["tidr"], data["tidb"])
#type 16 Coordination Reply Message #type 16 Coordination Reply Message
def parse_TCAS_CRM(data): def parse_TCAS_CRM(data):
(resolutions, complements) = self.parseMB_TCAS_resolutions(data) (resolutions, complements) = parseMB_TCAS_resolutions(data)
return (resolutions, complements, data["rat"], data["mte"]) return (resolutions, complements, data["rat"], data["mte"])
#this decorator takes a pubsub and returns a function which parses and publishes messages
def make_parser(pub):
publisher = pub
def publish(message):
[data, ecc, reference, timestamp] = message.split()
try:
ret = air_modes.modes_report(modes_reply(int(data, 16)),
int(ecc, 16),
20.0*math.log10(float(reference)),
air_modes.stamp(0, float(timestamp)))
pub["modes_dl"] = ret
pub["type%i_dl" % ret.data.get_type()] = ret
except ADSBError:
pass
return publish