diff --git a/ogn/collect/celery.py b/ogn/collect/celery.py index bc89364..dd8f289 100644 --- a/ogn/collect/celery.py +++ b/ogn/collect/celery.py @@ -9,7 +9,7 @@ from celery.signals import worker_init, worker_shutdown app = Celery('ogn.collect', broker='redis://localhost:6379/0', backend='redis://localhost:6379/0', - include=["ogn.collect.fetchddb"]) + include=["ogn.collect.fetchddb", "ogn.collect.logbook"]) DB_URI = 'sqlite:///beacons.db' diff --git a/ogn/collect/fetchddb.py b/ogn/collect/fetchddb.py index f228ed2..6b6fc57 100644 --- a/ogn/collect/fetchddb.py +++ b/ogn/collect/fetchddb.py @@ -36,6 +36,7 @@ def update_ddb_from_file(): .delete() devices = get_ddb('ogn/custom_ddb.txt') + logger.debug("New Devices: %s" % str(devices)) app.session.bulk_save_objects(devices) app.session.commit() diff --git a/ogn/collect/logbook.py b/ogn/collect/logbook.py index 59e835e..a0d51f1 100644 --- a/ogn/collect/logbook.py +++ b/ogn/collect/logbook.py @@ -1,6 +1,6 @@ from __future__ import absolute_import -from datetime import datetime +from datetime import datetime, timedelta from celery.utils.log import get_task_logger from ogn.collect.celery import app @@ -16,14 +16,24 @@ logger = get_task_logger(__name__) @app.task def compute_takeoff_and_landing(): + logger.info("Compute takeoffs and landings.") + takeoff_speed = 30 landing_speed = 30 - # get last takeoff_landing time as starting point for the following search + # calculate the time where the computation starts last_takeoff_landing_query = app.session.query(func.max(TakeoffLanding.timestamp)) last_takeoff_landing = last_takeoff_landing_query.one()[0] if last_takeoff_landing is None: + # if the table is empty last_takeoff_landing = datetime(2015, 1, 1, 0, 0, 0) + else: + # we get the beacons async. to be safe we delete takeoffs/landings from last 5 minutes and recalculate from then + # alternative: takeoff/landing has a primary key (timestamp,address) + last_takeoff_landing = last_takeoff_landing - timedelta(minutes=5) + app.session.query(TakeoffLanding) \ + .filter(TakeoffLanding.timestamp > last_takeoff_landing) \ + .delete() # make a query with current, previous and next position, so we can detect takeoffs and landings sq = app.session.query( @@ -75,5 +85,9 @@ def compute_takeoff_and_landing(): # ... and save them ins = insert(TakeoffLanding).from_select((TakeoffLanding.address, TakeoffLanding.timestamp, TakeoffLanding.latitude, TakeoffLanding.longitude, TakeoffLanding.track, TakeoffLanding.ground_speed, TakeoffLanding.altitude, TakeoffLanding.is_takeoff), takeoff_landing_query) - app.session.execute(ins) + result = app.session.execute(ins) + counter = result.rowcount app.session.commit() + logger.debug("New/recalculated takeoffs and landings: %s" % counter) + + return counter diff --git a/ogn/commands/logbook.py b/ogn/commands/logbook.py index 7b2484a..35f079c 100644 --- a/ogn/commands/logbook.py +++ b/ogn/commands/logbook.py @@ -9,11 +9,21 @@ from sqlalchemy.sql.expression import true, false, label from ogn.model import Device, TakeoffLanding from ogn.commands.dbutils import session +from ogn.collect.logbook import compute_takeoff_and_landing from manager import Manager manager = Manager() +@manager.command +def compute(): + """Compute takeoffs and landings.""" + print("Compute takeoffs and landings...") + result = compute_takeoff_and_landing.delay() + counter = result.get() + print("New/recalculated takeoffs/landings: %s" % counter) + + @manager.command def show(airport_name, latitude, longitude, altitude): """Show a logbook for located at given position.""" diff --git a/ogn/gateway/__init__.py b/ogn/gateway/__init__.py index 2a12b37..5a6e4ec 100644 --- a/ogn/gateway/__init__.py +++ b/ogn/gateway/__init__.py @@ -58,10 +58,10 @@ class ognGateway: try: beacon = parse_aprs(line) except AprsParseError: - logger.error('AprsParseError', exc_info=True) + logger.error('AprsParseError while parsing line: %s' % line, exc_info=True) return except OgnParseError: - logger.error('OgnParseError', exc_info=True) + logger.error('OgnParseError while parsing line: ' % line, exc_info=True) return if beacon is not None: diff --git a/ogn/gateway/settings.py b/ogn/gateway/settings.py index d311da2..0273c48 100644 --- a/ogn/gateway/settings.py +++ b/ogn/gateway/settings.py @@ -1,7 +1,7 @@ APRS_SERVER_HOST = 'aprs.glidernet.org' -APRS_SERVER_PORT = 14580 +APRS_SERVER_PORT = 10152 APRS_APP_NAME = 'ogn-gateway-python' APRS_APP_VER = '0.2' -APRS_FILTER = 'r/50.0000/10.0000/5000' +APRS_FILTER = '' APRS_KEEPALIVE_TIME = 240