From 26b40817202c30633d883ea7b84ad8ba87c1b2a5 Mon Sep 17 00:00:00 2001 From: W1CDN Date: Wed, 13 Dec 2023 10:13:25 -0600 Subject: [PATCH] Working snapshot. --- kiss_and_db.py | 109 +++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 54 deletions(-) diff --git a/kiss_and_db.py b/kiss_and_db.py index 453fa47..e9d7af2 100644 --- a/kiss_and_db.py +++ b/kiss_and_db.py @@ -67,63 +67,64 @@ def main(): try: a = aprslib.parse(str(frame)) except Exception as error: - logging.error("Error with aprslib:", error) - 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()) + 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']) - if 'subpacket' in a: - a['subpacket'] = str(a['subpacket']) - #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()) + # Make this a string and deal with it later (probably a mistake) + a['path'] = str(a['path']) + if 'subpacket' in a: + a['subpacket'] = str(a['subpacket']) + #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()) - logging.debug("Inserting into database") - try: - # Insert data - sql = "INSERT INTO frames ("+attrib_names+") VALUES ("+attrib_values+")" - conn.execute(sql, list(a.values())) - 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) - logging.debug("Station table updated") - conn.commit() - #except: - # print("Stations table couldn't be updated.") + logging.debug("Inserting into database") + try: + # Insert data + sql = "INSERT INTO frames ("+attrib_names+") VALUES ("+attrib_values+")" + conn.execute(sql, list(a.values())) + 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) + 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() - except: - #print("Error with SQLite!") - logging.error("Error with SQLite!") + # 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() + except: + #print("Error with SQLite!") + logging.error("Error with SQLite!") except Exception as error: #print("Frame could not be parsed.") logging.error("Frame could not be parsed:", error)