Compare commits
80 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
eb10695873 | ||
|
a5978c2289 | ||
a1b9fc7ac4 | |||
|
c8f8f28a4a | ||
|
ab595ed3cb | ||
|
3758ac21cb | ||
|
260e946ab6 | ||
|
5fb589507e | ||
|
bcb0624786 | ||
|
4642f45adc | ||
|
31fb381063 | ||
|
8f2425290c | ||
|
9797f29bf2 | ||
|
bfa7b91446 | ||
|
0592f45af8 | ||
|
c1f58966a6 | ||
|
4439ae680e | ||
|
25bffd9b61 | ||
|
1894de5d86 | ||
|
79da9de6f7 | ||
|
26b4081720 | ||
|
50085de7db | ||
|
e7002c712c | ||
|
307c1fcd86 | ||
|
dc7d4ed8a3 | ||
|
e1211fe108 | ||
|
6686cba26d | ||
|
1b0494c45a | ||
|
f694e65c2a | ||
|
78641d0eef | ||
|
50e8324786 | ||
|
acdee84d3e | ||
|
fcd776174c | ||
|
ef539e2aa9 | ||
|
9fb3d28cdc | ||
|
f447a807b2 | ||
|
875546040f | ||
|
1a5df46eca | ||
|
ebd237d9d3 | ||
9c11d8d494 | |||
e3cb68551b | |||
|
d2cdaa820a | ||
|
93156311e2 | ||
|
d382a2b8f7 | ||
|
40513cc488 | ||
|
c0ff61063f | ||
|
b0f0a4f8dc | ||
|
8d94794c90 | ||
|
ee75cccc68 | ||
|
1c057a5555 | ||
|
c25a10ae77 | ||
|
e1cd7ccaae | ||
|
00ede8860f | ||
|
8f3b2ae707 | ||
|
e05a3790d6 | ||
|
fb4d89cd9b | ||
|
8972c8d447 | ||
|
1362558deb | ||
|
f6a71e4851 | ||
|
1d8699df94 | ||
|
1ad8c848c4 | ||
|
9deb160989 | ||
|
e2c3adf5c0 | ||
|
4203af5b81 | ||
|
494f53bd62 | ||
|
e19a8c777c | ||
|
cb9af0f5b8 | ||
|
863efdd84c | ||
|
a99de3a859 | ||
|
efe61ae4c5 | ||
|
cc89ab1a4c | ||
|
f396fe87af | ||
|
ab850a76a3 | ||
|
2121119365 | ||
|
6957219468 | ||
|
cd5d24b641 | ||
|
1cffde2903 | ||
|
5793e57aa9 | ||
|
19b3a54d98 | ||
|
467ec11522 |
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -1 +1,6 @@
|
|||
/logs/*
|
||||
config.ini
|
||||
*.db
|
||||
*.log
|
||||
/temp/*
|
||||
*.pyc
|
||||
|
|
80
README.md
80
README.md
|
@ -7,44 +7,72 @@ direwolf logs into a REST API in JSON format.
|
|||
## Setup
|
||||
1. Run direwolf with logging to CSV on by using `-l`. (`-L` not yet implemented).
|
||||
1. Install requirements using `pip install -r requirements.txt`.
|
||||
1. Set up database file with `python init_db.py`.
|
||||
2. Run `app.py` with either a Python call or a real WSGI server.
|
||||
You can use screen to detach the session.
|
||||
- Default URL is http://127.0.0.1:5000
|
||||
- Default URL is http://127.0.0.1:5001
|
||||
- Example `waitress` and `screen` scripts are included, see
|
||||
- `api_waitress.py` and
|
||||
- `start-aprs_api.sh`
|
||||
3. Access the API from whatever other system you want.
|
||||
|
||||
## Endpoints:
|
||||
-`/packets` - gives the most recent packets, with the fields from the Dire Wolf
|
||||
User Guide.
|
||||
-`/packets` - gives the most recent packets, sorted descending by time received.
|
||||
- argument `n` will return a specific number of packets, default 10. E.g.,
|
||||
`https://digi.w1cdn.net/aprs_api/packets?n=1` returns one packet.
|
||||
- argument `from` will return packets from the named station-SSID (no wildcards).
|
||||
E.g., `https://digi.w1cdn.net/aprs_api/packets?n=1&from=W1CDN-1` returns
|
||||
one packet from W1CDN-1.
|
||||
|
||||
Example of an object packet sent by W1CDN-1 and digipeated by K0UND-2:
|
||||
```
|
||||
{
|
||||
"chan": 0,
|
||||
"utime": 1680566406,
|
||||
"isotime": "2023-04-04T00:00:06Z",
|
||||
"source": "W1CDN-1",
|
||||
"heard": "K0UND-2",
|
||||
"level": "113(71/42)",
|
||||
"error": 0,
|
||||
"dti": ";",
|
||||
"name": "147.390GF",
|
||||
"symbol": "/r",
|
||||
"latitude": 47.924167,
|
||||
"longitude": -97.009667,
|
||||
"speed": 0.0,
|
||||
"course": 0.0,
|
||||
"altitude": 0.0,
|
||||
"frequency": 147.39,
|
||||
"offset": 600.0,
|
||||
"tone": 0.0,
|
||||
"system": "DireWolf, WB2OSZ",
|
||||
"status": 0,
|
||||
"telemetry": 0.0,
|
||||
"comment": " https://www.wa0jxt.org/"
|
||||
},
|
||||
"id": 1,
|
||||
"addresse": null,
|
||||
"alive": null,
|
||||
"altitude": null,
|
||||
"comment": "Leave a message to say hi!",
|
||||
"course": null,
|
||||
"created": "2023-04-16 15:04:03",
|
||||
"format": "uncompressed",
|
||||
"frame": null,
|
||||
"from": "W1CDN-2",
|
||||
"gpsfixstatus": null,
|
||||
"latitude": 47.94133333333333,
|
||||
"longitude": -97.02683333333333,
|
||||
"mbits": null,
|
||||
"messagecapable": 1,
|
||||
"message_text": null,
|
||||
"mtype": null,
|
||||
"object_format": null,
|
||||
"object_name": null,
|
||||
"path": "['K0UND-2', 'WIDE2-2']",
|
||||
"phg": null,
|
||||
"phg_dir": null,
|
||||
"phg_gain": null,
|
||||
"phg_height": null,
|
||||
"phg_power": null,
|
||||
"phg_range": null,
|
||||
"posambiguity": 0,
|
||||
"raw": "W1CDN-2>APQTH1,K0UND-2,WIDE2-2:@150321h4756.48N/09701.61W-Leave a message to say hi!",
|
||||
"raw_timestamp": "150321h",
|
||||
"speed": null,
|
||||
"station_call": "W1CDN-1",
|
||||
"station_lat": 47.9415,
|
||||
"station_lon": -97.027,
|
||||
"status": null,
|
||||
"symbol": "-",
|
||||
"symbol_table": "/",
|
||||
"telemetry": null,
|
||||
"timestamp": 1681657401,
|
||||
"to": "APQTH1",
|
||||
"tEQNS": null,
|
||||
"tPARM": null,
|
||||
"tUNIT": null,
|
||||
"via": "",
|
||||
"weather": null,
|
||||
"wx_raw_timestamp": null
|
||||
}
|
||||
```
|
||||
|
||||
# Contributing
|
||||
|
|
244
api_app.py
244
api_app.py
|
@ -1,48 +1,26 @@
|
|||
from flask import Flask
|
||||
from flask_restful import Resource, Api, reqparse
|
||||
from flask import Flask, request, render_template
|
||||
from flask_restful import Resource, Api, reqparse, url_for
|
||||
from datetime import date, timedelta
|
||||
import configparser
|
||||
import csv
|
||||
import datetime
|
||||
import timeago
|
||||
import ast
|
||||
import glob
|
||||
import json
|
||||
import json, operator
|
||||
import requests
|
||||
import sqlite3
|
||||
api_app = Flask(__name__)
|
||||
api = Api(api_app)
|
||||
|
||||
# TODO this is duplicated from kiss_and_db.py, can I avoid that?
|
||||
import constants
|
||||
|
||||
def read_config():
|
||||
config = configparser.ConfigParser()
|
||||
config.read('config.ini')
|
||||
return config
|
||||
|
||||
def read_logs(log_folder):
|
||||
# Read some log files
|
||||
# UTC time, so let's look at tomorrow, today, and yesterday.
|
||||
today = date.today()
|
||||
yesterday = today - timedelta(days = 1)
|
||||
tomorrow = today + timedelta(days = 1)
|
||||
file_list = glob.glob(log_folder+str(yesterday)+"*") + \
|
||||
glob.glob(log_folder+str(today)+"*") + \
|
||||
glob.glob(log_folder+str(tomorrow)+"*")
|
||||
|
||||
# https://stackoverflow.com/a/66071962
|
||||
json_array = []
|
||||
for file in file_list:
|
||||
with open(file, encoding='utf-8') as csvf:
|
||||
csvReader = csv.DictReader(csvf)
|
||||
for row in csvReader:
|
||||
#add this python dict to json array
|
||||
json_array.append(row)
|
||||
|
||||
# Add the call and location of this station to the packet info
|
||||
config = read_config()
|
||||
for item in json_array:
|
||||
item['station_name'] = config['Settings']['station_call']
|
||||
item['station_lat'] = config['Settings']['station_lat']
|
||||
item['station_lon'] = config['Settings']['station_lon']
|
||||
|
||||
return(json_array)
|
||||
|
||||
def dict_factory(cursor, row):
|
||||
d = {}
|
||||
for idx, col in enumerate(cursor.description):
|
||||
|
@ -51,7 +29,6 @@ def dict_factory(cursor, row):
|
|||
|
||||
def get_db_connection():
|
||||
conn = sqlite3.connect('database.db')
|
||||
#conn.row_factory = sqlite3.Row
|
||||
conn.row_factory = dict_factory
|
||||
return conn
|
||||
|
||||
|
@ -66,24 +43,213 @@ def select_all_frames(conn):
|
|||
rows = cur.fetchall()
|
||||
return rows
|
||||
|
||||
def select_all_stations(conn):
|
||||
"""
|
||||
Query all rows in the stations table
|
||||
:param conn: the Connection object
|
||||
:return:
|
||||
"""
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT * FROM stations ORDER BY last_heard_unix DESC")
|
||||
rows = cur.fetchall()
|
||||
return rows
|
||||
|
||||
def unique_stations(conn):
|
||||
"""
|
||||
Query all rows in the frames table
|
||||
:param conn: the Connection object
|
||||
:return:
|
||||
"""
|
||||
cur = conn.cursor()
|
||||
cur.execute('SELECT *, MAX(id), COUNT(id) FROM frames GROUP BY "from" ORDER BY MAX(id) DESC')
|
||||
rows = cur.fetchall()
|
||||
return rows
|
||||
|
||||
def select_frames(conn, n, url_params):
|
||||
|
||||
# Should pass this a dict of fields and values (request.args)
|
||||
# TODO clean data before sending to DB
|
||||
# Filter out any keys that don't match db fields
|
||||
# From https://stackoverflow.com/a/20256491
|
||||
dictfilt = lambda x, y: dict([ (i,x[i]) for i in x if i in set(y) ])
|
||||
field_where = dictfilt(url_params, constants.db_frames_fields)
|
||||
# Then loop through fields to create query parts
|
||||
# From https://stackoverflow.com/a/73512269/2152245
|
||||
field_where_str = ' AND '.join([f'"{k}" LIKE \'{v}\'' for k,v in field_where.items()])
|
||||
|
||||
cur = conn.cursor()
|
||||
# Workaround to deal with missing value in WHERE
|
||||
field_where_query = "" if field_where_str == "" else "WHERE "+field_where_str
|
||||
sql = 'SELECT * FROM frames {field_where_query} ORDER BY id DESC LIMIT {n}'.format(field_where_query=field_where_query, n=n)
|
||||
print(sql)
|
||||
cur.execute(sql)
|
||||
rows = cur.fetchall()
|
||||
return rows
|
||||
|
||||
def select_stations(conn, n):
|
||||
"""
|
||||
Query rows in the stations table
|
||||
:param conn: the Connection object
|
||||
:return:
|
||||
"""
|
||||
cur = conn.cursor()
|
||||
sql = 'SELECT * FROM stations ORDER BY last_heard_unix DESC LIMIT {n}'.format(n=n)
|
||||
print(sql)
|
||||
cur.execute(sql)
|
||||
rows = cur.fetchall()
|
||||
return rows
|
||||
|
||||
@api_app.route('/')
|
||||
def index():
|
||||
|
||||
path = config['Settings']['base_url']
|
||||
|
||||
# Get list of recent packets using API
|
||||
# TODO use relative path
|
||||
#frames = json.loads(requests.get(url_for("packets", _external=True)).text)['data']
|
||||
#frames = json.loads(requests.get("https://digi.w1cdn.net/aprs_api/packets").text)['data']
|
||||
frames = json.loads(requests.get(config['Settings']['base_url']+"/packets").text)['data']
|
||||
for frame in frames:
|
||||
if frame['created'] != None:
|
||||
frame['time_ago'] = timeago.format(frame['created_unix'], datetime.datetime.now())
|
||||
|
||||
|
||||
# Play with function to create station list
|
||||
#stations = select_all_stations(get_db_connection())
|
||||
#print(url_for("static", filename="test.txt", _external=True))
|
||||
# this should work: stations = json.loads(requests.get(url_for("stations", _external=True)).text)['data']
|
||||
#stations = json.loads(requests.get(url_for("stations", _external=True)).text)['data']
|
||||
#stations = json.loads(requests.get("https://digi.w1cdn.net/aprs_api/stations").text)['data']
|
||||
stations = json.loads(requests.get(config['Settings']['base_url']+"/stations").text)['data']
|
||||
# Convert unix time to datetime on the fly because I'm lazy right now
|
||||
for station in stations:
|
||||
if station['last_heard_unix'] != None:
|
||||
station['last_heard'] = datetime.datetime.utcfromtimestamp(station['last_heard_unix'])
|
||||
station['time_ago'] = timeago.format(station['last_heard_unix'], datetime.datetime.now())
|
||||
|
||||
|
||||
# Map stuff
|
||||
frames_locs = list(filter(lambda x: x['latitude'] != None, frames))
|
||||
# Make a GeoJSON
|
||||
geojs = json.dumps({
|
||||
"type": "FeatureCollection",
|
||||
"features":[
|
||||
{
|
||||
"type":"Feature",
|
||||
"geometry": {
|
||||
"type":"Point",
|
||||
"coordinates":[frame['longitude'], frame['latitude']],
|
||||
},
|
||||
"properties":frame,
|
||||
|
||||
} for frame in frames_locs
|
||||
]
|
||||
})
|
||||
|
||||
return render_template('index.html',
|
||||
station_call = config['Settings']['station_call'],
|
||||
station_lat = config['Settings']['station_lat'],
|
||||
station_lon = config['Settings']['station_lon'],
|
||||
frames = frames,
|
||||
stations = stations,
|
||||
geojs = geojs,
|
||||
path = path)
|
||||
|
||||
@api_app.route('/map')
|
||||
def map():
|
||||
|
||||
# Get the default list of frames from the API
|
||||
frames = json.loads(requests.get(config['Settings']['base_url']+"/packets").text)['data']
|
||||
|
||||
frames_locs = list(filter(lambda x: x['latitude'] != None, frames))
|
||||
|
||||
# Make a GeoJSON
|
||||
geojs = json.dumps({
|
||||
"type": "FeatureCollection",
|
||||
"features":[
|
||||
{
|
||||
"type":"Feature",
|
||||
"geometry": {
|
||||
"type":"Point",
|
||||
"coordinates":[frame['longitude'], frame['latitude']],
|
||||
},
|
||||
"properties":frame,
|
||||
|
||||
} for frame in frames_locs
|
||||
]
|
||||
})
|
||||
|
||||
# Make markers for all the frames
|
||||
# id_counter = 0
|
||||
# markers = ''
|
||||
# marker_ids = []
|
||||
# for frame in frames:
|
||||
# if frame['latitude'] != None:
|
||||
# # Create unique ID for each marker
|
||||
# idd = 'frame' + str(id_counter)
|
||||
# id_counter += 1
|
||||
|
||||
# # Create each marker
|
||||
# markers += "var {idd} = L.marker([{latitude}, {longitude}]);\
|
||||
# {idd}.addTo(map).bindTooltip('{from_ssid}', permanent=true).openTooltip();".format(idd=idd, latitude=frame['latitude'],\
|
||||
# longitude=frame['longitude'],
|
||||
# from_ssid=frame['from'],
|
||||
# created=frame['created'])
|
||||
# # Try to make a list of markers for Leaflet, but not working
|
||||
# marker_ids.append(idd)
|
||||
|
||||
|
||||
return render_template('map.html',
|
||||
station_lat = config['Settings']['station_lat'],
|
||||
station_lon = config['Settings']['station_lon'],
|
||||
station_call = config['Settings']['station_call'],
|
||||
#markers = markers,
|
||||
geojs = geojs)
|
||||
|
||||
class Packets(Resource):
|
||||
def get(self):
|
||||
#data = read_logs(log_folder)
|
||||
# Handle arguments that may or may not exist
|
||||
try:
|
||||
n = int(request.args.get('n'))
|
||||
except:
|
||||
n = 20
|
||||
|
||||
conn = get_db_connection()
|
||||
data = select_all_frames(conn)
|
||||
# Limit to number of records requested
|
||||
data = select_frames(conn, n = n, url_params = request.args.to_dict())
|
||||
# Sort by created date, descending (https://stackoverflow.com/a/45266808)
|
||||
#data.sort(key=operator.itemgetter('created'), reverse=True)
|
||||
return {'data':data}, 200 # return data and 200 OK code
|
||||
|
||||
class Stations(Resource):
|
||||
def get(self):
|
||||
# Handle arguments that may or may not exist
|
||||
try:
|
||||
n = int(request.args.get('n'))
|
||||
except:
|
||||
n = 20
|
||||
|
||||
conn = get_db_connection()
|
||||
# Limit to number of records requested
|
||||
data = select_stations(conn, n = n)
|
||||
# Sort by created date, descending (https://stackoverflow.com/a/45266808)
|
||||
#data.sort(key=operator.itemgetter('created'), reverse=True)
|
||||
return {'data':data}, 200 # return data and 200 OK code
|
||||
|
||||
# Read config
|
||||
config = read_config()
|
||||
log_folder = config['Settings']['log_folder']
|
||||
# Load logs first (just to check for errors before page loads)
|
||||
#data = read_logs(log_folder)
|
||||
|
||||
# Start subprocess to watch KISS connection
|
||||
import subprocess
|
||||
subprocess.Popen(["python3","kiss_and_db.py"])
|
||||
#proc = subprocess.Popen(["python3","kiss_and_db.py"])
|
||||
# Combine under one process https://stackoverflow.com/a/13143013/2152245
|
||||
proc = subprocess.Popen("exec " + "python3 kiss_and_db.py", stdout=subprocess.PIPE, shell=True)
|
||||
print("kiss_and_db.py as subprocess pid "+str(proc.pid))
|
||||
|
||||
api.add_resource(Packets, '/packets') # and '/locations' is our entry point for Locations
|
||||
# The packets endpoint
|
||||
api.add_resource(Packets, '/packets')
|
||||
# The stations endpoint
|
||||
api.add_resource(Stations, '/stations')
|
||||
|
||||
if __name__ == '__main__':
|
||||
api_app.run(debug=True, host='0.0.0.0', port=5001) # run our Flask app
|
||||
|
|
7
aprs_tool.code-workspace
Normal file
7
aprs_tool.code-workspace
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"folders": [
|
||||
{
|
||||
"path": "."
|
||||
}
|
||||
]
|
||||
}
|
|
@ -1,19 +1,19 @@
|
|||
[Settings]
|
||||
# Path to direwolf log folder, include trailing slash
|
||||
log_folder = logs/
|
||||
#log_folder = /home/pi/logs/direwolf/
|
||||
|
||||
# Name and location of this station, for inclusion in the API
|
||||
station_call = W1CDN-1
|
||||
station_lat = 47.941500
|
||||
station_lon = -97.027000
|
||||
|
||||
# Base URL for application (no trailing slash)
|
||||
base_url = https://digi.w1cdn.net/aprs_api
|
||||
|
||||
# How long to keep packets (frames) e.g., "2 days", "5 minutes"
|
||||
keep_time = "2 days"
|
||||
|
||||
# KISS settings
|
||||
kiss_host = 192.168.0.30
|
||||
kiss_host = 192.168.0.100
|
||||
kiss_port = 8001
|
||||
|
||||
# Development settings (not operational yet)
|
||||
mycall = W1CDN-15
|
||||
log_path = aprs_api.log
|
54
constants.py
Normal file
54
constants.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
# Tuple of frames table fields
|
||||
db_frames_fields = ("id",
|
||||
"addresse",
|
||||
"alive",
|
||||
"altitude",
|
||||
"body",
|
||||
"comment",
|
||||
"course",
|
||||
"created",
|
||||
"created_unix",
|
||||
"format",
|
||||
"frame",
|
||||
"from",
|
||||
"gpsfixstatus",
|
||||
"header_raw",
|
||||
"latitude",
|
||||
"longitude",
|
||||
"mbits",
|
||||
"messagecapable",
|
||||
"message_text",
|
||||
"msgNo",
|
||||
"mtype",
|
||||
"object_format",
|
||||
"object_name",
|
||||
"party",
|
||||
"path",
|
||||
"phg",
|
||||
"phg_dir",
|
||||
"phg_gain",
|
||||
"phg_height",
|
||||
"phg_power",
|
||||
"phg_range",
|
||||
"posambiguity",
|
||||
"raw",
|
||||
"raw_timestamp",
|
||||
"rng",
|
||||
"speed",
|
||||
"station_call",
|
||||
"station_lat",
|
||||
"station_lon",
|
||||
"status",
|
||||
"subpacket",
|
||||
"symbol",
|
||||
"symbol_table",
|
||||
"telemetry",
|
||||
"timestamp",
|
||||
"to",
|
||||
"tEQNS",
|
||||
"tPARM",
|
||||
"tUNIT",
|
||||
"type",
|
||||
"via",
|
||||
"weather",
|
||||
"wx_raw_timestamp")
|
161
kiss_and_db.py
161
kiss_and_db.py
|
@ -5,53 +5,10 @@ import aprs
|
|||
import json
|
||||
import aprslib
|
||||
import configparser
|
||||
|
||||
db_fields = ("id",
|
||||
"addresse",
|
||||
"alive",
|
||||
"altitude",
|
||||
"comment",
|
||||
"course",
|
||||
"created",
|
||||
"format",
|
||||
"frame",
|
||||
"from",
|
||||
"gpsfixstatus",
|
||||
"latitude",
|
||||
"longitude",
|
||||
"mbits",
|
||||
"messagecapable",
|
||||
"message_text",
|
||||
"mtype",
|
||||
"object_format",
|
||||
"object_name",
|
||||
"path",
|
||||
"phg",
|
||||
"phg_dir",
|
||||
"phg_gain",
|
||||
"phg_height",
|
||||
"phg_power",
|
||||
"phg_range",
|
||||
"posambiguity",
|
||||
"raw",
|
||||
"raw_timestamp",
|
||||
"speed",
|
||||
"station_call",
|
||||
"station_lat",
|
||||
"station_lon",
|
||||
"status",
|
||||
"subpacket",
|
||||
"symbol",
|
||||
"symbol_table",
|
||||
"telemetry",
|
||||
"timestamp",
|
||||
"to",
|
||||
"tEQNS",
|
||||
"tPARM",
|
||||
"tUNIT",
|
||||
"via",
|
||||
"weather",
|
||||
"wx_raw_timestamp")
|
||||
import time
|
||||
import logging
|
||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||
import time
|
||||
|
||||
def read_config():
|
||||
config = configparser.ConfigParser()
|
||||
|
@ -63,6 +20,15 @@ def get_db_connection():
|
|||
conn.row_factory = sqlite3.Row
|
||||
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():
|
||||
|
||||
# Add the call and location of this station to the packet info
|
||||
|
@ -71,30 +37,58 @@ def main():
|
|||
# KISS_HOST = os.environ.get("KISS_HOST", "192.168.0.30")
|
||||
# KISS_PORT = os.environ.get("KISS_PORT", "8001")
|
||||
|
||||
logging.basicConfig(filename=config['Settings']['log_path'], level=logging.DEBUG, \
|
||||
format='%(asctime)s - %(message)s')
|
||||
logging.debug('============= kiss_and_db.py running =============')
|
||||
|
||||
ki = aprs.TCPKISS(host=config['Settings']['kiss_host'], port=int(config['Settings']['kiss_port']))
|
||||
ki.start()
|
||||
|
||||
#scheduler = AsyncIOScheduler()
|
||||
#scheduler.add_job(refresh_kiss_connection, 'interval', hours = 1, args = [ki])
|
||||
#scheduler.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)
|
||||
# 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):
|
||||
logging.debug("New packet, trying to parse")
|
||||
logging.debug(str(frame))
|
||||
try:
|
||||
try:
|
||||
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_lat'] = config['Settings']['station_lat']
|
||||
a['station_lon'] = config['Settings']['station_lon']
|
||||
print(a)
|
||||
a['created_unix'] = int(time.time())
|
||||
a['party'] = 1
|
||||
|
||||
# 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:
|
||||
b = a['subpacket'] # make a copy
|
||||
a['subpacket'] = str(a['subpacket']) # turn the original to a string
|
||||
b['party'] = 3
|
||||
b['path'] = str(b['path'])
|
||||
b['header_raw'] = a['raw'].split("}", 1)[0] # just the first part
|
||||
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())
|
||||
#logging.debug(a['path'])
|
||||
# Store true/false as 1/0
|
||||
if 'alive' in a:
|
||||
if a['alive'] == True:
|
||||
|
@ -104,18 +98,65 @@ def main():
|
|||
# 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+")"
|
||||
logging.debug(attrib_names)
|
||||
logging.debug(a.values())
|
||||
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,
|
||||
# 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)
|
||||
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:
|
||||
# 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!")
|
||||
#conn.execute("DELETE FROM frames WHERE created < DATETIME('now', '"+config['Settings']['keep_time']+"')")
|
||||
#conn.commit()
|
||||
except Exception as error:
|
||||
#print("Error with SQLite!")
|
||||
logging.error("Error with SQLite!", exc_info = error)
|
||||
except Exception as error:
|
||||
#print("Frame could not be parsed.")
|
||||
logging.error("Frame could not be parsed:", exc_info = error)
|
||||
|
||||
|
||||
conn.close()
|
||||
|
||||
|
|
|
@ -1,4 +1,9 @@
|
|||
flask
|
||||
flask_restful
|
||||
aprs
|
||||
aprs3
|
||||
kiss3
|
||||
kiss
|
||||
aprslib
|
||||
sqlite3
|
||||
json
|
||||
timeago
|
16
schema.sql
16
schema.sql
|
@ -5,21 +5,26 @@ CREATE TABLE frames (
|
|||
addresse TEXT,
|
||||
alive INT,
|
||||
altitude REAL,
|
||||
body TEXT,
|
||||
comment TEXT,
|
||||
course REAL,
|
||||
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
created_unix INT,
|
||||
format TEXT,
|
||||
frame TEXT,
|
||||
"from" TEXT,
|
||||
gpsfixstatus TEXT,
|
||||
header_raw TEXT,
|
||||
latitude REAL,
|
||||
longitude REAL,
|
||||
mbits INT,
|
||||
messagecapable INT,
|
||||
message_text TEXT,
|
||||
msgNo INT,
|
||||
mtype TEXT,
|
||||
object_format TEXT,
|
||||
object_name TEXT,
|
||||
party INT,
|
||||
path TEXT,
|
||||
phg REAL,
|
||||
phg_dir TEXT,
|
||||
|
@ -30,6 +35,7 @@ CREATE TABLE frames (
|
|||
posambiguity INT,
|
||||
raw TEXT,
|
||||
raw_timestamp TEXT,
|
||||
rng REAL,
|
||||
speed REAL,
|
||||
station_call TEXT,
|
||||
station_lat REAL,
|
||||
|
@ -44,7 +50,17 @@ CREATE TABLE frames (
|
|||
tEQNS TEXT,
|
||||
tPARM TEXT,
|
||||
tUNIT TEXT,
|
||||
type TEXT,
|
||||
via TEXT,
|
||||
weather TEXT,
|
||||
wx_raw_timestamp TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE "stations" (
|
||||
"id" INTEGER NOT NULL UNIQUE,
|
||||
"from" TEXT UNIQUE,
|
||||
"frames_id" INTEGER,
|
||||
"last_heard_unix" INTEGER,
|
||||
"count" INTEGER,
|
||||
PRIMARY KEY("id" AUTOINCREMENT)
|
||||
);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#!/bin/bash
|
||||
# Run `chmod +x start-aprs_api.sh` so this can be run
|
||||
|
||||
screen -dmS aprs_api python3 /home/pi/aprs_tools/api_waitress.py
|
||||
screen -dmS aprs_api python3 /home/pi/aprs_tool/api_waitress.py
|
||||
|
|
|
@ -1,109 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
import os
|
||||
import sqlite3
|
||||
import aprs
|
||||
import json
|
||||
import aprslib
|
||||
import configparser
|
||||
|
||||
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")
|
||||
|
||||
db_fields = ("id",
|
||||
"addresse",
|
||||
"alive",
|
||||
"altitude",
|
||||
"comment",
|
||||
"course",
|
||||
"created",
|
||||
"format",
|
||||
"frame",
|
||||
"from",
|
||||
"gpsfixstatus",
|
||||
"latitude",
|
||||
"longitude",
|
||||
"mbits",
|
||||
"messagecapable",
|
||||
"message_text",
|
||||
"mtype",
|
||||
"object_format",
|
||||
"object_name",
|
||||
"path",
|
||||
"posambiguity",
|
||||
"raw",
|
||||
"raw_timestamp",
|
||||
"speed",
|
||||
"station_call",
|
||||
"station_lat",
|
||||
"station_lon",
|
||||
"status",
|
||||
"symbol",
|
||||
"symbol_table",
|
||||
"telemetry",
|
||||
"timestamp",
|
||||
"to",
|
||||
"tEQNS",
|
||||
"tPARM",
|
||||
"tUNIT",
|
||||
"via",
|
||||
"weather",
|
||||
"wx_raw_timestamp")
|
||||
|
||||
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()
|
||||
|
||||
ki = aprs.TCPKISS(host=KISS_HOST, port=int(KISS_PORT))
|
||||
ki.start()
|
||||
|
||||
# Make a simple frame and send it
|
||||
frame = aprs.APRSFrame.ui(
|
||||
destination="APZ001",
|
||||
source=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):
|
||||
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']
|
||||
print(a)
|
||||
# Make this a string and deal with it later (probably a mistake)
|
||||
a['path'] = str(a['path'])
|
||||
# Build an INSERT statement based on the fields we have from the frame
|
||||
attrib_names = ', '.join(f'"{w}"' for w in a.keys())
|
||||
attrib_values = ", ".join("?" * len(a.keys()))
|
||||
sql = f"INSERT INTO frames ({attrib_names}) VALUES ({attrib_values})"
|
||||
# Insert data
|
||||
conn.execute(sql, list(a.values()))
|
||||
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()
|
||||
|
||||
conn.close()
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
129
templates/index.html
Normal file
129
templates/index.html
Normal file
|
@ -0,0 +1,129 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>{{station_call}} Status</title>
|
||||
|
||||
<!-- Leaflet's CSS -->
|
||||
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
|
||||
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
|
||||
crossorigin=""/>
|
||||
<!-- Make sure you put this AFTER Leaflet's CSS -->
|
||||
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
|
||||
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
|
||||
crossorigin=""></script>
|
||||
|
||||
<style>
|
||||
table, th, td {
|
||||
border: 1px solid black;
|
||||
}
|
||||
#map { height: 250px; }
|
||||
.leaflet-tooltip.my-labels {
|
||||
background-color: transparent;
|
||||
border: transparent;
|
||||
box-shadow: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div style="width: 100%; overflow: hidden;">
|
||||
<div style="width: 50%; float: left;">
|
||||
|
||||
<h1>{{station_call}} Status</h1>
|
||||
Station location: {{station_lat}}, {{station_lon}}
|
||||
|
||||
<h2> About </h2>
|
||||
This is a work in progress. See <a href="https://amiok.net/gitea/W1CDN/aprs_tool">https://amiok.net/gitea/W1CDN/aprs_tool</a> for usage.
|
||||
</div>
|
||||
<div style="margin-left: 50%;">
|
||||
<div id="map"></div>
|
||||
<script>
|
||||
var map = L.map('map').setView([{{station_lat}}, {{station_lon}}], 10);
|
||||
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {attribution: '© <a href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> contributors'}).addTo(map);
|
||||
|
||||
//{{markers|safe}}
|
||||
|
||||
// Show station location
|
||||
var station = L.marker([{{station_lat}}, {{station_lon}}]).addTo(map).bindTooltip('{{station_call}}', {permanent: true}).openTooltip();
|
||||
|
||||
// Show GeoJSON of markers
|
||||
var group = L.geoJSON({{geojs|safe}},
|
||||
{
|
||||
style: function (feature) {
|
||||
return {color: feature.properties.color};
|
||||
}
|
||||
});
|
||||
|
||||
// group.bindTooltip(function (layer) {
|
||||
// return 'Object '+layer.feature.properties.object_name+' from '+layer.feature.properties.from;
|
||||
// }, {permanent: false}).openTooltip().addTo(map);
|
||||
// Hacked together from https://gis.stackexchange.com/a/246919
|
||||
var pointLayer = L.geoJSON(null, {
|
||||
pointToLayer: function(feature,latlng){
|
||||
//(true condition) ? "true" : "false"
|
||||
label = (feature.properties.object_name === null) ? String(feature.properties.from) : String(feature.properties.object_name)
|
||||
//label = String('Object '+feature.properties.object_name+' from '+feature.properties.from) // Must convert to string, .bindTooltip can't use straight 'feature.properties.attribute'
|
||||
return new L.CircleMarker(latlng, {
|
||||
radius: 1,
|
||||
}).bindTooltip(label, {permanent: true, opacity: 0.7, className: "my-labels"}).openTooltip();
|
||||
}
|
||||
});
|
||||
pointLayer.addData({{geojs|safe}});
|
||||
map.addLayer(pointLayer);
|
||||
|
||||
|
||||
// Zoom to show all
|
||||
map.fitBounds(group.getBounds().pad(0.3));
|
||||
</script>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2> Recent RF Packets </h2>
|
||||
Query <a href="{{path}}/packets?n=10">10</a> | <a href="{{path}}/packets?n=50">50</a> | <a href="{{path}}/packets?n=100">100</a>
|
||||
<table>
|
||||
<tr>
|
||||
<th> from </th>
|
||||
<th> object_name </th>
|
||||
<th> raw </th>
|
||||
<th> created (utc) </th>
|
||||
<th> relative </th>
|
||||
<th> more </th>
|
||||
</tr>
|
||||
{% for i in frames %}
|
||||
<tr>
|
||||
<td> <a href="https://digi.w1cdn.net/aprs_api/packets?from={{ i['from'] }}">{{ i['from'] }}</a> </td>
|
||||
<td> {{ i['object_name'] }} </td>
|
||||
<td> {{ i['raw'] }} </td>
|
||||
<td> {{ i['created'] }} </td>
|
||||
<td> {{ i['time_ago'] }} </td>
|
||||
<td> <a href="https://digi.w1cdn.net/aprs_api/packets?id={{ i['id'] }}">query</a>,
|
||||
<a href="https://aprs.fi/#!mt=roadmap&z=12&call=a%2F{{ i['from'] }}">aprs.fi</a></td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
||||
|
||||
|
||||
<h2> Recent Stations </h2>
|
||||
Query <a href="{{path}}/stations?n=10">10</a> | <a href="{{path}}/stations?n=50">50</a> | <a href="{{path}}/stations?n=100">100</a>
|
||||
<table>
|
||||
<tr>
|
||||
<th> from </th>
|
||||
<th> last heard (utc) </th>
|
||||
<th> relative </th>
|
||||
<th> count </th>
|
||||
<th> more </th>
|
||||
</tr>
|
||||
{% for i in stations %}
|
||||
<tr>
|
||||
<td> <a href="https://digi.w1cdn.net/aprs_api/packets?from={{ i['from'] }}">{{ i['from'] }}</a> </td>
|
||||
<td> {{ i['last_heard'] }} </td>
|
||||
<td> {{ i['time_ago'] }} </td>
|
||||
<td> {{ i['count']}} </td>
|
||||
<td> <a href="https://aprs.fi/#!mt=roadmap&z=12&call=a%2F{{ i['from'] }}">aprs.fi</a></td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
||||
</body>
|
||||
</html>
|
42
templates/map.html
Normal file
42
templates/map.html
Normal file
|
@ -0,0 +1,42 @@
|
|||
<html>
|
||||
<head>
|
||||
<!-- Leaflet's CSS -->
|
||||
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
|
||||
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
|
||||
crossorigin=""/>
|
||||
<!-- Make sure you put this AFTER Leaflet's CSS -->
|
||||
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
|
||||
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
|
||||
crossorigin=""></script>
|
||||
<style>
|
||||
#map { height: 100%; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="map"></div>
|
||||
|
||||
<script>
|
||||
var map = L.map('map').setView([{{station_lat}}, {{station_lon}}], 10);
|
||||
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {attribution: '© <a href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> contributors'}).addTo(map);
|
||||
|
||||
//{{markers|safe}}
|
||||
|
||||
// Show station location
|
||||
var station = L.marker([{{station_lat}}, {{station_lon}}]).addTo(map).bindTooltip('{{station_call}}', permanent=true).openTooltip();
|
||||
|
||||
// Show GeoJSON of markers
|
||||
var group = L.geoJSON({{geojs|safe}},
|
||||
{
|
||||
style: function (feature) {
|
||||
return {color: feature.properties.color};
|
||||
}
|
||||
}).bindTooltip(function (layer) {
|
||||
return 'Object '+layer.feature.properties.object_name+' from '+layer.feature.properties.from;
|
||||
}, permanent=true).addTo(map);
|
||||
|
||||
// Zoom to show all
|
||||
map.fitBounds(group.getBounds().pad(0.2));
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
11
test_async.py
Normal file
11
test_async.py
Normal 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())
|
Loading…
Reference in New Issue
Block a user