2015-11-30 14:03:38 +00:00
import logging
2015-11-19 22:17:12 +00:00
2016-03-18 21:54:22 +00:00
from ogn . client import AprsClient
2016-10-31 07:58:19 +00:00
from ogn . gateway . process import process_beacon , message_to_beacon
2016-09-23 18:36:48 +00:00
from datetime import datetime
2015-11-15 08:10:46 +00:00
from manager import Manager
2017-05-26 20:56:38 +00:00
from ogn . model import AircraftBeacon , ReceiverBeacon
2016-01-28 19:29:08 +00:00
2015-11-15 08:10:46 +00:00
manager = Manager ( )
2015-11-30 14:03:38 +00:00
logging_formatstr = ' %(asctime)s - %(levelname).4s - %(name)s - %(message)s '
log_levels = [ ' CRITICAL ' , ' ERROR ' , ' WARNING ' , ' INFO ' , ' DEBUG ' ]
2015-11-29 20:51:23 +00:00
2015-11-15 11:10:20 +00:00
2015-11-15 08:10:46 +00:00
@manager.command
2015-11-30 14:03:38 +00:00
def run ( aprs_user = ' anon-dev ' , logfile = ' main.log ' , loglevel = ' INFO ' ) :
2015-11-15 08:10:46 +00:00
""" Run the aprs client. """
2015-11-29 20:51:23 +00:00
2015-11-30 14:03:38 +00:00
# User input validation
2015-11-22 20:11:55 +00:00
if len ( aprs_user ) < 3 or len ( aprs_user ) > 9 :
2015-11-30 14:03:38 +00:00
print ( ' aprs_user must be a string of 3-9 characters. ' )
2015-11-22 20:11:55 +00:00
return
2015-11-30 14:03:38 +00:00
if loglevel not in log_levels :
print ( ' loglevel must be an element of {} . ' . format ( log_levels ) )
return
# Enable logging
log_handlers = [ logging . StreamHandler ( ) ]
if logfile :
log_handlers . append ( logging . FileHandler ( logfile ) )
logging . basicConfig ( format = logging_formatstr , level = loglevel , handlers = log_handlers )
2015-11-30 14:11:10 +00:00
print ( ' Start ogn gateway ' )
2016-03-18 21:54:22 +00:00
client = AprsClient ( aprs_user )
client . connect ( )
2015-11-30 14:11:10 +00:00
try :
2016-03-18 21:54:22 +00:00
client . run ( callback = process_beacon , autoreconnect = True )
2015-11-30 14:11:10 +00:00
except KeyboardInterrupt :
print ( ' \n Stop ogn gateway ' )
2016-03-18 21:54:22 +00:00
client . disconnect ( )
2015-11-30 14:11:10 +00:00
logging . shutdown ( )
2016-09-22 08:40:02 +00:00
2017-05-26 20:56:38 +00:00
# get total lines of the input file
def file_len ( fname ) :
with open ( fname ) as f :
for i , l in enumerate ( f ) :
pass
return i + 1
@manager.command
def convert_logfile ( ogn_logfile , logfile = ' main.log ' , loglevel = ' INFO ' ) :
""" Convert ogn logfiles to csv logfiles (one for aircraft beacons and one for receiver beacons) <arg: ogn-logfile>. Logfile name: blablabla.txt_YYYY-MM-DD. """
2016-09-22 08:40:02 +00:00
# Enable logging
log_handlers = [ logging . StreamHandler ( ) ]
if logfile :
log_handlers . append ( logging . FileHandler ( logfile ) )
logging . basicConfig ( format = logging_formatstr , level = loglevel , handlers = log_handlers )
2017-05-26 20:56:38 +00:00
logger = logging . getLogger ( __name__ )
import re
match = re . search ( ' ^.+ \ .txt \ _( \ d {4} \ - \ d {2} \ - \ d {2} ) ' , ogn_logfile )
if match :
reference_date = match . group ( 1 )
else :
print ( " filename does not match pattern " )
return
fin = open ( ogn_logfile , ' r ' )
fout_ab = open ( ' aircraft_beacons.csv_ ' + reference_date , ' w ' )
fout_rb = open ( ' receiver_beacons.csv_ ' + reference_date , ' w ' )
try :
reference_date = datetime . strptime ( reference_date , " % Y- % m- %d " )
except :
print ( ' \n Error in reference_date argument ' , reference_date )
return
aircraft_beacons = list ( )
receiver_beacons = list ( )
total = file_len ( ogn_logfile )
progress = - 1
num_lines = 0
import csv
wr_ab = csv . writer ( fout_ab , delimiter = ' , ' )
wr_ab . writerow ( AircraftBeacon . get_csv_columns ( ) )
wr_rb = csv . writer ( fout_rb , delimiter = ' , ' )
wr_rb . writerow ( ReceiverBeacon . get_csv_columns ( ) )
2016-10-31 07:58:19 +00:00
2016-09-22 08:40:02 +00:00
print ( ' Start importing ogn-logfile ' )
2017-05-26 20:56:38 +00:00
for line in fin :
num_lines + = 1
if int ( 100 * num_lines / total ) != progress :
progress = round ( 100 * num_lines / total )
logger . info ( " Reading line {} ( {} % ) " . format ( num_lines , progress ) )
if len ( aircraft_beacons ) > 0 :
for beacon in aircraft_beacons :
wr_ab . writerow ( beacon . get_csv_values ( ) )
aircraft_beacons = list ( )
if len ( receiver_beacons ) > 0 :
for beacon in receiver_beacons :
wr_rb . writerow ( beacon . get_csv_values ( ) )
receiver_beacons = list ( )
2016-10-31 07:58:19 +00:00
beacon = message_to_beacon ( line , reference_date = reference_date )
if beacon is not None :
2017-05-26 20:56:38 +00:00
if isinstance ( beacon , AircraftBeacon ) :
aircraft_beacons . append ( beacon )
elif isinstance ( beacon , ReceiverBeacon ) :
receiver_beacons . append ( beacon )
if len ( aircraft_beacons ) > 0 :
for beacon in aircraft_beacons :
wr_ab . writerow ( [ beacon . get_csv_values ( ) ] )
if len ( receiver_beacons ) > 0 :
for beacon in receiver_beacons :
wr_rb . writerow ( beacon . get_csv_values ( ) )
fin . close ( )
fout_ab . close ( )
fout_rb . close ( )
2016-09-22 08:40:02 +00:00
logging . shutdown ( )