2019-09-25 21:17:30 +00:00
|
|
|
import os
|
|
|
|
import datetime
|
|
|
|
|
2019-09-12 20:53:42 +00:00
|
|
|
from flask import current_app
|
2019-02-25 19:00:51 +00:00
|
|
|
from flask.cli import AppGroup
|
2019-09-25 21:17:30 +00:00
|
|
|
import click
|
2019-02-25 19:00:51 +00:00
|
|
|
|
|
|
|
from ogn.client import AprsClient
|
2019-09-25 21:17:30 +00:00
|
|
|
|
|
|
|
from app.gateway.bulkimport import convert, DbFeeder
|
2019-02-25 19:00:51 +00:00
|
|
|
|
2019-08-31 08:14:41 +00:00
|
|
|
user_cli = AppGroup("gateway")
|
2019-02-25 19:00:51 +00:00
|
|
|
user_cli.help = "Connection to APRS servers."
|
|
|
|
|
|
|
|
|
2019-08-31 08:14:41 +00:00
|
|
|
@user_cli.command("run")
|
|
|
|
def run(aprs_user="anon-dev"):
|
2019-09-25 21:17:30 +00:00
|
|
|
"""Run the aprs client and feed the DB with incoming data."""
|
2019-02-25 19:00:51 +00:00
|
|
|
|
|
|
|
# User input validation
|
|
|
|
if len(aprs_user) < 3 or len(aprs_user) > 9:
|
2019-08-31 08:14:41 +00:00
|
|
|
print("aprs_user must be a string of 3-9 characters.")
|
2019-02-25 19:00:51 +00:00
|
|
|
return
|
|
|
|
|
2019-09-12 20:53:42 +00:00
|
|
|
current_app.logger.warning("Start ogn gateway")
|
2019-02-25 19:00:51 +00:00
|
|
|
client = AprsClient(aprs_user)
|
|
|
|
client.connect()
|
|
|
|
|
2019-09-25 21:17:30 +00:00
|
|
|
with DbFeeder(prefix='continuous_import', reference_timestamp=datetime.utcnow, reference_timestamp_autoupdate=True) as feeder:
|
|
|
|
try:
|
|
|
|
client.run(callback=lambda x: feeder.add(x), autoreconnect=True)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
current_app.logger.warning("\nStop ogn gateway")
|
2019-02-25 19:00:51 +00:00
|
|
|
|
|
|
|
client.disconnect()
|
2019-09-25 21:17:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
@user_cli.command("convert")
|
|
|
|
@click.argument("path")
|
|
|
|
def file_import(path):
|
|
|
|
"""Convert APRS logfiles into csv files for fast bulk import."""
|
|
|
|
|
|
|
|
logfiles = []
|
|
|
|
for (root, dirs, files) in os.walk(path):
|
|
|
|
for file in sorted(files):
|
|
|
|
logfiles.append(os.path.join(root, file))
|
|
|
|
|
|
|
|
for logfile in logfiles:
|
|
|
|
convert(logfile)
|