aprs_tool/kiss_and_db.py

166 lines
7.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import os
2023-04-15 13:27:00 -05:00
import sqlite3
import aprs
import json
import aprslib
import configparser
import time
2023-07-12 12:43:24 -05:00
import logging
from apscheduler.schedulers.asyncio import AsyncIOScheduler
import time
2023-04-15 13:27:00 -05:00
def read_config():
config = configparser.ConfigParser()
config.read('config.ini')
return config
2023-04-15 13:27:00 -05:00
def get_db_connection():
conn = sqlite3.connect('database.db')
conn.row_factory = sqlite3.Row
return conn
def refresh_kiss_connection(kiss_conn):
logging.debug("Restarting KISS connection on schedule")
logging.debug("Stopping current connection")
kiss_conn.stop()
2023-12-11 21:41:57 -06:00
#logging.debug("Waiting 5 seconds")
#time.sleep(5)
logging.debug("Starting new connection")
kiss_conn.start()
def main():
2023-04-15 13:27:00 -05:00
# Add the call and location of this station to the packet info
config = read_config()
2023-04-16 16:45:22 -05:00
# 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")
2023-08-25 21:38:17 -05:00
logging.basicConfig(filename=config['Settings']['log_path'], level=logging.DEBUG, \
format='%(asctime)s - %(message)s')
2023-12-11 21:41:57 -06:00
logging.debug('============= kiss_and_db.py running =============')
2023-04-15 13:27:00 -05:00
2023-04-16 16:45:22 -05:00
ki = aprs.TCPKISS(host=config['Settings']['kiss_host'], port=int(config['Settings']['kiss_port']))
ki.start()
2023-04-15 13:27:00 -05:00
2023-12-11 21:41:57 -06:00
#scheduler = AsyncIOScheduler()
#scheduler.add_job(refresh_kiss_connection, 'interval', hours = 1, args = [ki])
#scheduler.start()
2023-07-09 11:22:23 -05:00
2023-04-15 13:27:00 -05:00
# Make a simple frame and send it
2023-12-11 21:41:57 -06:00
# frame = aprs.APRSFrame.ui(
# destination="APZ001",
# source=config['Settings']['mycall'],
# path=["WIDE1-1"],
# info=b">Hello World!",
# )
#ki.write(frame)
2023-04-15 13:27:00 -05:00
# Watch for new packets to come in
while True:
conn = get_db_connection()
for frame in ki.read(min_frames=1):
logging.debug("New packet, trying to parse")
2023-12-11 21:41:57 -06:00
logging.debug(str(frame))
2023-04-23 21:13:27 -05:00
try:
2023-12-11 21:41:57 -06:00
try:
a = aprslib.parse(str(frame))
2023-12-12 20:25:00 -06:00
except Exception as error:
2023-12-13 10:13:25 -06:00
logging.error("Error with aprslib:", exc_info = error)
else:
a['station_call'] = config['Settings']['station_call']
a['station_lat'] = config['Settings']['station_lat']
a['station_lon'] = config['Settings']['station_lon']
a['created_unix'] = int(time.time())
# Make this a string and deal with it later (probably a mistake)
a['path'] = str(a['path'])
2023-12-13 20:44:11 -06:00
# Process 3rd-party data
2023-12-13 10:13:25 -06:00
if 'subpacket' in a:
a['subpacket'] = str(a['subpacket'])
2023-12-13 20:44:11 -06:00
b = aprslib.parse(str(a['subpacket']))
b['party'] = 3
b['station_call'] = config['Settings']['station_call']
b['station_lat'] = config['Settings']['station_lat']
b['station_lon'] = config['Settings']['station_lon']
b['created_unix'] = int(time.time())
else:
a['party'] = 1
2023-12-13 10:13:25 -06:00
#logging.debug(a['path'])
# Store true/false as 1/0
if 'alive' in a:
if a['alive'] == True:
a['alive'] = 1
else:
a['alive'] = 0
# Build an INSERT statement based on the fields we have from the frame
attrib_names = ', '.join('"%s"' % w for w in a.keys())
attrib_values = ", ".join("?" * len(a.keys()))
logging.debug(attrib_names)
logging.debug(a.values())
2023-12-13 20:44:11 -06:00
if 'subpacket' in a:
# 3rd-party
b_attrib_names = ', '.join('"%s"' % w for w in b.keys())
b_attrib_values = ", ".join("?" * len(b.keys()))
logging.debug(b_attrib_names)
logging.debug(b.values())
2023-12-13 10:13:25 -06:00
try:
2023-12-13 20:44:11 -06:00
logging.debug("Inserting into database")
2023-12-13 10:13:25 -06:00
# Insert data
sql = "INSERT INTO frames ("+attrib_names+") VALUES ("+attrib_values+")"
conn.execute(sql, list(a.values()))
2023-12-13 20:44:11 -06:00
if 'subpacket' in a:
b_sql = "INSERT INTO frames ("+b_attrib_names+") VALUES ("+b_attrib_values+")"
conn.execute(b_sql, list(b.values()))
2023-12-13 10:13:25 -06:00
logging.debug("Frames table updated")
# TODO update stations table here
# Original intent was to include the id from the frames table,
# but that would mean making another query.
# It's not immediately needed, so I'm skipping it.
# Build query
# "from" is wrappedin [] because it is a reserved word and using '' doesn't work.
# https://www.sqlite.org/lang_keywords.html
#try:
station_update = "'"+a['from'] +"', '"+ str(a['created_unix']) +"', '1'"
query3 = "INSERT INTO stations ([from], last_heard_unix, count) \
VALUES("+station_update+") \
ON CONFLICT([from]) \
DO UPDATE SET count = count + 1,\
last_heard_unix = excluded.last_heard_unix;"
# Insert/update data
conn.execute(query3)
2023-12-13 20:44:11 -06:00
if 'subpacket' in a:
b_station_update = "'"+b['from'] +"', '"+ str(b['created_unix']) +"', '1'"
b_query3 = "INSERT INTO stations ([from], last_heard_unix, count) \
VALUES("+b_station_update+") \
ON CONFLICT([from]) \
DO UPDATE SET count = count + 1,\
last_heard_unix = excluded.last_heard_unix;"
# Insert/update data
conn.execute(b_query3)
2023-12-13 10:13:25 -06:00
logging.debug("Station table updated")
conn.commit()
#except:
# print("Stations table couldn't be updated.")
# 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()
2023-12-13 10:40:32 -06:00
except Exception as error:
2023-12-13 10:13:25 -06:00
#print("Error with SQLite!")
2023-12-13 10:40:32 -06:00
logging.error("Error with SQLite!", exc_info = error)
2023-12-12 20:25:00 -06:00
except Exception as error:
2023-07-12 12:43:24 -05:00
#print("Frame could not be parsed.")
2023-12-13 10:40:32 -06:00
logging.error("Frame could not be parsed:", exc_info = error)
2023-04-15 13:27:00 -05:00
conn.close()
if __name__ == "__main__":
main()