89 lines
2.8 KiB
Python
89 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
import os
|
|
import sqlite3
|
|
import aprs
|
|
import json
|
|
import aprslib
|
|
import configparser
|
|
import time
|
|
|
|
def read_config():
|
|
config = configparser.ConfigParser()
|
|
config.read('config.ini')
|
|
return config
|
|
|
|
def get_db_connection():
|
|
conn = sqlite3.connect('database.db')
|
|
conn.row_factory = sqlite3.Row
|
|
return conn
|
|
|
|
def main():
|
|
|
|
# Add the call and location of this station to the packet info
|
|
config = read_config()
|
|
# 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")
|
|
|
|
|
|
ki = aprs.TCPKISS(host=config['Settings']['kiss_host'], port=int(config['Settings']['kiss_port']))
|
|
ki.start()
|
|
|
|
# Make a simple frame and send it
|
|
frame = aprs.APRSFrame.ui(
|
|
destination="APZ001",
|
|
source=config['Settings']['mycall'],
|
|
path=["WIDE1-1"],
|
|
info=b">Hello World!",
|
|
)
|
|
#ki.write(frame)
|
|
|
|
# Watch for new packets to come in
|
|
while True:
|
|
conn = get_db_connection()
|
|
for frame in ki.read(min_frames=1):
|
|
try:
|
|
a = aprslib.parse(str(frame))
|
|
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())
|
|
print(a)
|
|
# Make this a string and deal with it later (probably a mistake)
|
|
a['path'] = str(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()))
|
|
sql = "INSERT INTO frames ("+attrib_names+") VALUES ("+attrib_values+")"
|
|
try:
|
|
# Insert data
|
|
conn.execute(sql, list(a.values()))
|
|
|
|
# TODO update stations table here
|
|
|
|
|
|
conn.commit()
|
|
|
|
# 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!")
|
|
except:
|
|
print("Frame could not be parsed.")
|
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|