Working snapshot.
This commit is contained in:
parent
50085de7db
commit
26b4081720
109
kiss_and_db.py
109
kiss_and_db.py
|
@ -67,63 +67,64 @@ def main():
|
||||||
try:
|
try:
|
||||||
a = aprslib.parse(str(frame))
|
a = aprslib.parse(str(frame))
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
logging.error("Error with aprslib:", error)
|
logging.error("Error with aprslib:", exc_info = error)
|
||||||
a['station_call'] = config['Settings']['station_call']
|
else:
|
||||||
a['station_lat'] = config['Settings']['station_lat']
|
a['station_call'] = config['Settings']['station_call']
|
||||||
a['station_lon'] = config['Settings']['station_lon']
|
a['station_lat'] = config['Settings']['station_lat']
|
||||||
a['created_unix'] = int(time.time())
|
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)
|
# 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:
|
if 'subpacket' in a:
|
||||||
a['subpacket'] = str(a['subpacket'])
|
a['subpacket'] = str(a['subpacket'])
|
||||||
#logging.debug(a['path'])
|
#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:
|
||||||
a['alive'] = 1
|
a['alive'] = 1
|
||||||
else:
|
else:
|
||||||
a['alive'] = 0
|
a['alive'] = 0
|
||||||
# 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(attrib_names)
|
||||||
logging.debug(a.values())
|
logging.debug(a.values())
|
||||||
|
|
||||||
logging.debug("Inserting into database")
|
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+")"
|
||||||
conn.execute(sql, list(a.values()))
|
conn.execute(sql, list(a.values()))
|
||||||
logging.debug("Frames table updated")
|
logging.debug("Frames table updated")
|
||||||
# TODO update stations table here
|
# TODO update stations table here
|
||||||
# Original intent was to include the id from the frames table,
|
# Original intent was to include the id from the frames table,
|
||||||
# but that would mean making another query.
|
# but that would mean making another query.
|
||||||
# It's not immediately needed, so I'm skipping it.
|
# It's not immediately needed, so I'm skipping it.
|
||||||
# Build query
|
# Build query
|
||||||
# "from" is wrappedin [] because it is a reserved word and using '' doesn't work.
|
# "from" is wrappedin [] because it is a reserved word and using '' doesn't work.
|
||||||
# https://www.sqlite.org/lang_keywords.html
|
# https://www.sqlite.org/lang_keywords.html
|
||||||
#try:
|
#try:
|
||||||
station_update = "'"+a['from'] +"', '"+ str(a['created_unix']) +"', '1'"
|
station_update = "'"+a['from'] +"', '"+ str(a['created_unix']) +"', '1'"
|
||||||
query3 = "INSERT INTO stations ([from], last_heard_unix, count) \
|
query3 = "INSERT INTO stations ([from], last_heard_unix, count) \
|
||||||
VALUES("+station_update+") \
|
VALUES("+station_update+") \
|
||||||
ON CONFLICT([from]) \
|
ON CONFLICT([from]) \
|
||||||
DO UPDATE SET count = count + 1,\
|
DO UPDATE SET count = count + 1,\
|
||||||
last_heard_unix = excluded.last_heard_unix;"
|
last_heard_unix = excluded.last_heard_unix;"
|
||||||
# Insert/update data
|
# Insert/update data
|
||||||
conn.execute(query3)
|
conn.execute(query3)
|
||||||
logging.debug("Station table updated")
|
logging.debug("Station table updated")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
#except:
|
#except:
|
||||||
# print("Stations table couldn't be updated.")
|
# print("Stations table couldn't be updated.")
|
||||||
|
|
||||||
# TODO remove packets that are older ('created') than a limit set in config.ini
|
# TODO remove packets that are older ('created') than a limit set in config.ini
|
||||||
# "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:
|
||||||
#print("Error with SQLite!")
|
#print("Error with SQLite!")
|
||||||
logging.error("Error with SQLite!")
|
logging.error("Error with SQLite!")
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
#print("Frame could not be parsed.")
|
#print("Frame could not be parsed.")
|
||||||
logging.error("Frame could not be parsed:", error)
|
logging.error("Frame could not be parsed:", error)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user