diff --git a/constants.py b/constants.py index 7ade087..c711b2f 100644 --- a/constants.py +++ b/constants.py @@ -22,6 +22,7 @@ db_frames_fields = ("id", "mtype", "object_format", "object_name", +"party", "path", "phg", "phg_dir", diff --git a/kiss_and_db.py b/kiss_and_db.py index 9d74bfa..1b1882e 100644 --- a/kiss_and_db.py +++ b/kiss_and_db.py @@ -76,8 +76,17 @@ def main(): # Make this a string and deal with it later (probably a mistake) a['path'] = str(a['path']) + # Process 3rd-party data if 'subpacket' in a: a['subpacket'] = str(a['subpacket']) + 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 #logging.debug(a['path']) # Store true/false as 1/0 if 'alive' in a: @@ -90,12 +99,21 @@ def main(): attrib_values = ", ".join("?" * len(a.keys())) logging.debug(attrib_names) logging.debug(a.values()) - - logging.debug("Inserting into database") + 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()) + try: + logging.debug("Inserting into database") # Insert data sql = "INSERT INTO frames ("+attrib_names+") VALUES ("+attrib_values+")" conn.execute(sql, list(a.values())) + if 'subpacket' in a: + b_sql = "INSERT INTO frames ("+b_attrib_names+") VALUES ("+b_attrib_values+")" + conn.execute(b_sql, list(b.values())) logging.debug("Frames table updated") # TODO update stations table here # Original intent was to include the id from the frames table, @@ -113,6 +131,15 @@ def main(): last_heard_unix = excluded.last_heard_unix;" # Insert/update data conn.execute(query3) + 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) logging.debug("Station table updated") conn.commit() #except: diff --git a/schema.sql b/schema.sql index c6fe23d..ea29c02 100644 --- a/schema.sql +++ b/schema.sql @@ -24,6 +24,7 @@ CREATE TABLE frames ( mtype TEXT, object_format TEXT, object_name TEXT, + party INT, path TEXT, phg REAL, phg_dir TEXT,