From 63963c0ade8e0e4d2bb27bfacd80632c45d39bad Mon Sep 17 00:00:00 2001 From: W1CDN Date: Fri, 14 Apr 2023 22:06:48 -0500 Subject: [PATCH] Store frames in database, remove frames older than chosen age. --- config.ini | 3 ++ db_test.py | 14 -------- schema.sql | 38 +++++++++++++++++++- tcp_kiss_send_recv.py | 83 +++++++++++++++++++++++++++++++++++++++---- 4 files changed, 116 insertions(+), 22 deletions(-) delete mode 100644 db_test.py diff --git a/config.ini b/config.ini index 61ac29d..b4f65fe 100644 --- a/config.ini +++ b/config.ini @@ -7,3 +7,6 @@ log_folder = logs/ station_call = W1CDN-1 station_lat = 47.941500 station_lon = -97.027000 + +# How long to keep packets (frames) e.g., "2 days", "5 minutes" +keep_time = "2 days" diff --git a/db_test.py b/db_test.py deleted file mode 100644 index e098f01..0000000 --- a/db_test.py +++ /dev/null @@ -1,14 +0,0 @@ -import sqlite3 - -def get_db_connection(): - conn = sqlite3.connect('database.db') - conn.row_factory = sqlite3.Row - return conn - -frame1 = "W1CDN-1>APDW16,K0UND-2*:;147.390GF*111111z4755.45N/09700.58Wr147.390MHz +060 https://www.wa0jxt.org/" - -conn = get_db_connection() -conn.execute('INSERT INTO frames (frame) VALUES (?)', - (frame1,)) -conn.commit() -conn.close() diff --git a/schema.sql b/schema.sql index e658834..f9ee929 100644 --- a/schema.sql +++ b/schema.sql @@ -2,6 +2,42 @@ DROP TABLE IF EXISTS frames; CREATE TABLE frames ( id INTEGER PRIMARY KEY AUTOINCREMENT, + addresse TEXT, + alive INT, + altitude REAL, + comment TEXT, + course REAL, created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - frame TEXT NOT NULL + format TEXT, + frame TEXT, + "from" TEXT, + gpsfixstatus TEXT, + latitude REAL, + longitude REAL, + mbits INT, + messagecapable INT, + message_text TEXT, + mtype TEXT, + object_format TEXT, + object_name TEXT, + path TEXT, + posambiguity INT, + raw TEXT, + raw_timestamp TEXT, + speed REAL, + station_call TEXT, + station_lat REAL, + station_lon REAL, + status TEXT, + symbol TEXT, + symbol_table TEXT, + telemetry TEXT, + timestamp INT, + "to" TEXT, + tEQNS TEXT, + tPARM TEXT, + tUNIT TEXT, + via TEXT, + weather TEXT, + wx_raw_timestamp TIMESTAMP ); diff --git a/tcp_kiss_send_recv.py b/tcp_kiss_send_recv.py index 88522db..4e6fe5a 100644 --- a/tcp_kiss_send_recv.py +++ b/tcp_kiss_send_recv.py @@ -4,37 +4,106 @@ import sqlite3 import aprs import json import aprslib - +import configparser MYCALL = os.environ.get("MYCALL", "W1CDN") KISS_HOST = os.environ.get("KISS_HOST", "192.168.0.30") KISS_PORT = os.environ.get("KISS_PORT", "8001") +db_fields = ("id", +"addresse", +"alive", +"altitude", +"comment", +"course", +"created", +"format", +"frame", +"from", +"gpsfixstatus", +"latitude", +"longitude", +"mbits", +"messagecapable", +"message_text", +"mtype", +"object_format", +"object_name", +"path", +"posambiguity", +"raw", +"raw_timestamp", +"speed", +"station_call", +"station_lat", +"station_lon", +"status", +"symbol", +"symbol_table", +"telemetry", +"timestamp", +"to", +"tEQNS", +"tPARM", +"tUNIT", +"via", +"weather", +"wx_raw_timestamp") + +def read_config(): + config = configparser.ConfigParser() + config.read('config.ini') + return config + def get_db_connection(): conn = sqlite3.connect('database.db') conn.row_factory = sqlite3.Row return conn def main(): + + # Add the call and location of this station to the packet info + config = read_config() + ki = aprs.TCPKISS(host=KISS_HOST, port=int(KISS_PORT)) ki.start() + + # Make a simple frame and send it frame = aprs.APRSFrame.ui( destination="APZ001", source=MYCALL, path=["WIDE1-1"], info=b">Hello World!", ) - ki.write(frame) + #ki.write(frame) + + # Watch for new packets to come in while True: + conn = get_db_connection() for frame in ki.read(min_frames=1): - #print(repr(frame)) a = aprslib.parse(str(frame)) + a['station_call'] = config['Settings']['station_call'] + a['station_lat'] = config['Settings']['station_lat'] + a['station_lon'] = config['Settings']['station_lon'] print(a) - conn = get_db_connection() - # conn.execute('INSERT INTO frames (frame) VALUES (?)', - # (a,)) + # Make this a string and deal with it later (probably a mistake) + a['path'] = str(a['path']) + # Build an INSERT statement based on the fields we have from the frame + attrib_names = ', '.join(f'"{w}"' for w in a.keys()) + attrib_values = ", ".join("?" * len(a.keys())) + sql = f"INSERT INTO frames ({attrib_names}) VALUES ({attrib_values})" + # Insert data + conn.execute(sql, list(a.values())) conn.commit() - conn.close() + + # TODO remove packets that are older ('created') than a limit set in config.ini + # "5 minutes" also works + conn.execute("DELETE FROM frames WHERE created < DATETIME('now', '"+config['Settings']['keep_time']+"')") + conn.commit() + + conn.close() + + if __name__ == "__main__": main()