From a7af51865385bc1e6baeb9a2afddd62ffc4ed0aa Mon Sep 17 00:00:00 2001 From: Nick Foster Date: Tue, 18 Jun 2013 21:49:07 -0700 Subject: [PATCH] Move parser factory decorator into parse.py. Fix multiple bugs in parse. --- apps/modes_rx | 22 ++-------------------- python/__init__.py | 1 + python/msprint.py | 6 ++++-- python/parse.py | 27 ++++++++++++++++++++++----- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/apps/modes_rx b/apps/modes_rx index b47a9c0..047e1e9 100755 --- a/apps/modes_rx +++ b/apps/modes_rx @@ -60,32 +60,14 @@ def main(): if options.remote is not None: servers += options.remote.split(",") 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() - def make_report(pub, message): - [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) + relay.subscribe("dl_data", air_modes.make_parser(publisher)) if options.location is not None: 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_dec = air_modes.cpr.cpr_decoder(my_position) + cpr_dec = air_modes.cpr_decoder(my_position) if options.kml is not None: dbname = 'adsb.db' diff --git a/python/__init__.py b/python/__init__.py index c42c61d..093cdf7 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -64,6 +64,7 @@ from exceptions import * from az_map import * from types import * from altitude import * +from cpr import cpr_decoder #this is try/excepted in case the user doesn't have numpy installed try: from flightgear import output_flightgear diff --git a/python/msprint.py b/python/msprint.py index 5a4914f..0e4b39e 100644 --- a/python/msprint.py +++ b/python/msprint.py @@ -45,8 +45,10 @@ class output_print: if msg.data.get_type() not in self._fns: retstr = output_print.prefix(msg) retstr += "No handler for message type %i" % msg.data.get_type() - if "ap" in msg.data.fields: - retstr += " from %.6x" % msg.data["ap"] + if "aa" not in msg.data.fields: + retstr += " from %.6x" % msg.ecc + else: + retstr += " from %.6x" % msg.data["aa"] print retstr def handle0(self, msg): diff --git a/python/parse.py b/python/parse.py index 15cf210..1a80c4e 100644 --- a/python/parse.py +++ b/python/parse.py @@ -23,8 +23,8 @@ import time, os, sys from string import split, join from altitude import decode_alt import math +import air_modes from air_modes.exceptions import * -from air_modes import cpr #this implements a packet class which can retrieve its own fields. class data_field: @@ -376,7 +376,7 @@ def parseBDS62(data): def parseMB_id(data): #bds1 == 2, bds2 == 0 msg = "" 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) 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 #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)} - (resolutions, complements) = self.parseMB_TCAS_resolutions(data) + (resolutions, complements) = parseMB_TCAS_resolutions(data) return (resolutions, complements, data["rat"], data["mte"], data["tid"]) 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) return (resolutions, complements, data["rat"], data["mte"], threat_alt, data["tidr"], data["tidb"]) #type 16 Coordination Reply Message 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"]) + +#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