Handle breaking errors in kiss_and_db.py #38

Merged
W1CDN merged 9 commits from fix-kiss into main 2023-12-13 20:20:59 -06:00
4 changed files with 101 additions and 59 deletions

View File

@ -3,6 +3,7 @@ db_frames_fields = ("id",
"addresse", "addresse",
"alive", "alive",
"altitude", "altitude",
"body",
"comment", "comment",
"course", "course",
"created", "created",
@ -11,6 +12,7 @@ db_frames_fields = ("id",
"frame", "frame",
"from", "from",
"gpsfixstatus", "gpsfixstatus",
"header_raw",
"latitude", "latitude",
"longitude", "longitude",
"mbits", "mbits",
@ -44,6 +46,7 @@ db_frames_fields = ("id",
"tEQNS", "tEQNS",
"tPARM", "tPARM",
"tUNIT", "tUNIT",
"type",
"via", "via",
"weather", "weather",
"wx_raw_timestamp") "wx_raw_timestamp")

View File

@ -7,6 +7,8 @@ import aprslib
import configparser import configparser
import time import time
import logging import logging
from apscheduler.schedulers.asyncio import AsyncIOScheduler
import time
def read_config(): def read_config():
config = configparser.ConfigParser() config = configparser.ConfigParser()
@ -18,6 +20,15 @@ def get_db_connection():
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
return conn return conn
def refresh_kiss_connection(kiss_conn):
logging.debug("Restarting KISS connection on schedule")
logging.debug("Stopping current connection")
kiss_conn.stop()
#logging.debug("Waiting 5 seconds")
#time.sleep(5)
logging.debug("Starting new connection")
kiss_conn.start()
def main(): def main():
# Add the call and location of this station to the packet info # Add the call and location of this station to the packet info
@ -28,34 +39,46 @@ def main():
logging.basicConfig(filename=config['Settings']['log_path'], level=logging.DEBUG, \ logging.basicConfig(filename=config['Settings']['log_path'], level=logging.DEBUG, \
format='%(asctime)s - %(message)s') format='%(asctime)s - %(message)s')
logging.debug('kiss_and_db.py running') logging.debug('============= kiss_and_db.py running =============')
ki = aprs.TCPKISS(host=config['Settings']['kiss_host'], port=int(config['Settings']['kiss_port'])) ki = aprs.TCPKISS(host=config['Settings']['kiss_host'], port=int(config['Settings']['kiss_port']))
ki.start() ki.start()
#scheduler = AsyncIOScheduler()
#scheduler.add_job(refresh_kiss_connection, 'interval', hours = 1, args = [ki])
#scheduler.start()
# Make a simple frame and send it # Make a simple frame and send it
frame = aprs.APRSFrame.ui( # frame = aprs.APRSFrame.ui(
destination="APZ001", # destination="APZ001",
source=config['Settings']['mycall'], # source=config['Settings']['mycall'],
path=["WIDE1-1"], # path=["WIDE1-1"],
info=b">Hello World!", # info=b">Hello World!",
) # )
#ki.write(frame) #ki.write(frame)
# Watch for new packets to come in # Watch for new packets to come in
while True: while True:
conn = get_db_connection() conn = get_db_connection()
for frame in ki.read(min_frames=1): for frame in ki.read(min_frames=1):
logging.debug("New packet, trying to parse")
logging.debug(str(frame))
try:
try: try:
a = aprslib.parse(str(frame)) a = aprslib.parse(str(frame))
except Exception as error:
logging.error("Error with aprslib:", exc_info = error)
else:
a['station_call'] = config['Settings']['station_call'] a['station_call'] = config['Settings']['station_call']
a['station_lat'] = config['Settings']['station_lat'] a['station_lat'] = config['Settings']['station_lat']
a['station_lon'] = config['Settings']['station_lon'] a['station_lon'] = config['Settings']['station_lon']
a['created_unix'] = int(time.time()) a['created_unix'] = int(time.time())
print(a)
# Make this a string and deal with it later (probably a mistake) # Make this a string and deal with it later (probably a mistake)
a['path'] = str(a['path']) a['path'] = str(a['path'])
if 'subpacket' in a:
a['subpacket'] = str(a['subpacket'])
#logging.debug(a['path'])
# Store true/false as 1/0 # Store true/false as 1/0
if 'alive' in a: if 'alive' in a:
if a['alive'] == True: if a['alive'] == True:
@ -65,8 +88,10 @@ def main():
# Build an INSERT statement based on the fields we have from the frame # Build an INSERT statement based on the fields we have from the frame
attrib_names = ', '.join('"%s"' % w for w in a.keys()) attrib_names = ', '.join('"%s"' % w for w in a.keys())
attrib_values = ", ".join("?" * len(a.keys())) attrib_values = ", ".join("?" * len(a.keys()))
logging.debug(attrib_names)
logging.debug(a.values())
logging.debug("Inserting into database")
try: try:
# Insert data # Insert data
sql = "INSERT INTO frames ("+attrib_names+") VALUES ("+attrib_values+")" sql = "INSERT INTO frames ("+attrib_names+") VALUES ("+attrib_values+")"
@ -97,12 +122,12 @@ def main():
# "5 minutes" also works # "5 minutes" also works
#conn.execute("DELETE FROM frames WHERE created < DATETIME('now', '"+config['Settings']['keep_time']+"')") #conn.execute("DELETE FROM frames WHERE created < DATETIME('now', '"+config['Settings']['keep_time']+"')")
#conn.commit() #conn.commit()
except: except Exception as error:
#print("Error with SQLite!") #print("Error with SQLite!")
logging.error("Error with SQLite!") logging.error("Error with SQLite!", exc_info = error)
except: except Exception as error:
#print("Frame could not be parsed.") #print("Frame could not be parsed.")
logging.error("Frame could not be parsed.") logging.error("Frame could not be parsed:", exc_info = error)
conn.close() conn.close()

View File

@ -5,6 +5,7 @@ CREATE TABLE frames (
addresse TEXT, addresse TEXT,
alive INT, alive INT,
altitude REAL, altitude REAL,
body TEXT,
comment TEXT, comment TEXT,
course REAL, course REAL,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
@ -13,6 +14,7 @@ CREATE TABLE frames (
frame TEXT, frame TEXT,
"from" TEXT, "from" TEXT,
gpsfixstatus TEXT, gpsfixstatus TEXT,
header_raw TEXT,
latitude REAL, latitude REAL,
longitude REAL, longitude REAL,
mbits INT, mbits INT,
@ -46,6 +48,7 @@ CREATE TABLE frames (
tEQNS TEXT, tEQNS TEXT,
tPARM TEXT, tPARM TEXT,
tUNIT TEXT, tUNIT TEXT,
type TEXT,
via TEXT, via TEXT,
weather TEXT, weather TEXT,
wx_raw_timestamp TIMESTAMP wx_raw_timestamp TIMESTAMP

11
test_async.py Normal file
View File

@ -0,0 +1,11 @@
import asyncio
import aprs
async def main():
transport, protocol = await aprs.create_tcp_connection("192.168.0.30", 8001)
async for frame in protocol.read():
print(frame)
if __name__ == "__main__":
asyncio.run(main())