Compare commits
16 Commits
4439ae680e
...
main
Author | SHA1 | Date | |
---|---|---|---|
eb10695873 | |||
a5978c2289 | |||
a1b9fc7ac4 | |||
c8f8f28a4a | |||
ab595ed3cb | |||
3758ac21cb | |||
260e946ab6 | |||
5fb589507e | |||
bcb0624786 | |||
4642f45adc | |||
31fb381063 | |||
8f2425290c | |||
9797f29bf2 | |||
bfa7b91446 | |||
0592f45af8 | |||
c1f58966a6 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,3 +2,5 @@
|
|||||||
config.ini
|
config.ini
|
||||||
*.db
|
*.db
|
||||||
*.log
|
*.log
|
||||||
|
/temp/*
|
||||||
|
*.pyc
|
||||||
|
96
api_app.py
96
api_app.py
@ -80,7 +80,20 @@ def select_frames(conn, n, url_params):
|
|||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
# Workaround to deal with missing value in WHERE
|
# Workaround to deal with missing value in WHERE
|
||||||
field_where_query = "" if field_where_str == "" else "WHERE "+field_where_str
|
field_where_query = "" if field_where_str == "" else "WHERE "+field_where_str
|
||||||
sql = 'SELECT * FROM frames {field_where_query} ORDER BY created DESC LIMIT {n}'.format(field_where_query=field_where_query, n=n)
|
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)
|
print(sql)
|
||||||
cur.execute(sql)
|
cur.execute(sql)
|
||||||
rows = cur.fetchall()
|
rows = cur.fetchall()
|
||||||
@ -89,6 +102,8 @@ def select_frames(conn, n, url_params):
|
|||||||
@api_app.route('/')
|
@api_app.route('/')
|
||||||
def index():
|
def index():
|
||||||
|
|
||||||
|
path = config['Settings']['base_url']
|
||||||
|
|
||||||
# Get list of recent packets using API
|
# Get list of recent packets using API
|
||||||
# TODO use relative path
|
# TODO use relative path
|
||||||
#frames = json.loads(requests.get(url_for("packets", _external=True)).text)['data']
|
#frames = json.loads(requests.get(url_for("packets", _external=True)).text)['data']
|
||||||
@ -113,12 +128,83 @@ def index():
|
|||||||
station['time_ago'] = timeago.format(station['last_heard_unix'], datetime.datetime.now())
|
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',
|
return render_template('index.html',
|
||||||
station_call = config['Settings']['station_call'],
|
station_call = config['Settings']['station_call'],
|
||||||
station_lat = config['Settings']['station_lat'],
|
station_lat = config['Settings']['station_lat'],
|
||||||
station_lon = config['Settings']['station_lon'],
|
station_lon = config['Settings']['station_lon'],
|
||||||
frames = frames,
|
frames = frames,
|
||||||
stations = stations)
|
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):
|
class Packets(Resource):
|
||||||
def get(self):
|
def get(self):
|
||||||
@ -126,7 +212,7 @@ class Packets(Resource):
|
|||||||
try:
|
try:
|
||||||
n = int(request.args.get('n'))
|
n = int(request.args.get('n'))
|
||||||
except:
|
except:
|
||||||
n = 10
|
n = 20
|
||||||
|
|
||||||
conn = get_db_connection()
|
conn = get_db_connection()
|
||||||
# Limit to number of records requested
|
# Limit to number of records requested
|
||||||
@ -141,11 +227,11 @@ class Stations(Resource):
|
|||||||
try:
|
try:
|
||||||
n = int(request.args.get('n'))
|
n = int(request.args.get('n'))
|
||||||
except:
|
except:
|
||||||
n = 10
|
n = 20
|
||||||
|
|
||||||
conn = get_db_connection()
|
conn = get_db_connection()
|
||||||
# Limit to number of records requested
|
# Limit to number of records requested
|
||||||
data = select_all_stations(conn)
|
data = select_stations(conn, n = n)
|
||||||
# Sort by created date, descending (https://stackoverflow.com/a/45266808)
|
# Sort by created date, descending (https://stackoverflow.com/a/45266808)
|
||||||
#data.sort(key=operator.itemgetter('created'), reverse=True)
|
#data.sort(key=operator.itemgetter('created'), reverse=True)
|
||||||
return {'data':data}, 200 # return data and 200 OK code
|
return {'data':data}, 200 # return data and 200 OK code
|
||||||
|
7
aprs_tool.code-workspace
Normal file
7
aprs_tool.code-workspace
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"folders": [
|
||||||
|
{
|
||||||
|
"path": "."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -11,7 +11,7 @@ base_url = https://digi.w1cdn.net/aprs_api
|
|||||||
keep_time = "2 days"
|
keep_time = "2 days"
|
||||||
|
|
||||||
# KISS settings
|
# KISS settings
|
||||||
kiss_host = 192.168.0.30
|
kiss_host = 192.168.0.100
|
||||||
kiss_port = 8001
|
kiss_port = 8001
|
||||||
|
|
||||||
# Development settings (not operational yet)
|
# Development settings (not operational yet)
|
||||||
|
@ -22,6 +22,7 @@ db_frames_fields = ("id",
|
|||||||
"mtype",
|
"mtype",
|
||||||
"object_format",
|
"object_format",
|
||||||
"object_name",
|
"object_name",
|
||||||
|
"party",
|
||||||
"path",
|
"path",
|
||||||
"phg",
|
"phg",
|
||||||
"phg_dir",
|
"phg_dir",
|
||||||
@ -32,6 +33,7 @@ db_frames_fields = ("id",
|
|||||||
"posambiguity",
|
"posambiguity",
|
||||||
"raw",
|
"raw",
|
||||||
"raw_timestamp",
|
"raw_timestamp",
|
||||||
|
"rng",
|
||||||
"speed",
|
"speed",
|
||||||
"station_call",
|
"station_call",
|
||||||
"station_lat",
|
"station_lat",
|
||||||
|
@ -73,11 +73,21 @@ def main():
|
|||||||
a['station_lat'] = config['Settings']['station_lat']
|
a['station_lat'] = config['Settings']['station_lat']
|
||||||
a['station_lon'] = config['Settings']['station_lon']
|
a['station_lon'] = config['Settings']['station_lon']
|
||||||
a['created_unix'] = int(time.time())
|
a['created_unix'] = int(time.time())
|
||||||
|
a['party'] = 1
|
||||||
|
|
||||||
# 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'])
|
||||||
|
# Process 3rd-party data
|
||||||
if 'subpacket' in a:
|
if 'subpacket' in a:
|
||||||
a['subpacket'] = str(a['subpacket'])
|
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'])
|
#logging.debug(a['path'])
|
||||||
# Store true/false as 1/0
|
# Store true/false as 1/0
|
||||||
if 'alive' in a:
|
if 'alive' in a:
|
||||||
@ -90,12 +100,21 @@ def main():
|
|||||||
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())
|
||||||
|
if 'subpacket' in a:
|
||||||
logging.debug("Inserting into database")
|
# 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:
|
try:
|
||||||
|
logging.debug("Inserting into database")
|
||||||
# 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()))
|
||||||
|
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")
|
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,
|
||||||
@ -113,6 +132,15 @@ def main():
|
|||||||
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)
|
||||||
|
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")
|
logging.debug("Station table updated")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
#except:
|
#except:
|
||||||
|
@ -24,6 +24,7 @@ CREATE TABLE frames (
|
|||||||
mtype TEXT,
|
mtype TEXT,
|
||||||
object_format TEXT,
|
object_format TEXT,
|
||||||
object_name TEXT,
|
object_name TEXT,
|
||||||
|
party INT,
|
||||||
path TEXT,
|
path TEXT,
|
||||||
phg REAL,
|
phg REAL,
|
||||||
phg_dir TEXT,
|
phg_dir TEXT,
|
||||||
@ -34,6 +35,7 @@ CREATE TABLE frames (
|
|||||||
posambiguity INT,
|
posambiguity INT,
|
||||||
raw TEXT,
|
raw TEXT,
|
||||||
raw_timestamp TEXT,
|
raw_timestamp TEXT,
|
||||||
|
rng REAL,
|
||||||
speed REAL,
|
speed REAL,
|
||||||
station_call TEXT,
|
station_call TEXT,
|
||||||
station_lat REAL,
|
station_lat REAL,
|
||||||
|
@ -4,20 +4,82 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>{{station_call}} Status</title>
|
<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>
|
<style>
|
||||||
table, th, td {
|
table, th, td {
|
||||||
border: 1px solid black;
|
border: 1px solid black;
|
||||||
}
|
}
|
||||||
|
#map { height: 250px; }
|
||||||
|
.leaflet-tooltip.my-labels {
|
||||||
|
background-color: transparent;
|
||||||
|
border: transparent;
|
||||||
|
box-shadow: none;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>{{station_call}} Status</h1>
|
<div style="width: 100%; overflow: hidden;">
|
||||||
Station location: {{station_lat}}, {{station_lon}}
|
<div style="width: 50%; float: left;">
|
||||||
|
|
||||||
<h2> About </h2>
|
<h1>{{station_call}} Status</h1>
|
||||||
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.
|
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>
|
<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>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th> from </th>
|
<th> from </th>
|
||||||
@ -40,7 +102,10 @@ This is a work in progress. See <a href="https://amiok.net/gitea/W1CDN/aprs_tool
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<h2> Recent Stations </h2>
|
<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>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th> from </th>
|
<th> from </th>
|
||||||
|
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>
|
36
test_db.py
36
test_db.py
@ -1,36 +0,0 @@
|
|||||||
|
|
||||||
# Learn how to update database
|
|
||||||
|
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
def get_db_connection():
|
|
||||||
conn = sqlite3.connect('database.db')
|
|
||||||
conn.row_factory = sqlite3.Row
|
|
||||||
return conn
|
|
||||||
|
|
||||||
conn = get_db_connection()
|
|
||||||
|
|
||||||
# Grab a random row from frames table and pretend it is new
|
|
||||||
cur = conn.cursor()
|
|
||||||
cur.execute("SELECT [from], id, created_unix FROM frames ORDER BY RANDOM() LIMIT 1;")
|
|
||||||
rows = cur.fetchall()
|
|
||||||
results = dict(rows[0])
|
|
||||||
values = ', '.join('"%s"' % w for w in results.values())
|
|
||||||
|
|
||||||
|
|
||||||
# Build query
|
|
||||||
# "from" is wrappedin [] because it is a reserved word and using '' doesn't work.
|
|
||||||
query3 = "INSERT INTO stations ([from], frames_id, last_heard_unix, count) \
|
|
||||||
VALUES("+values+", 1) \
|
|
||||||
ON CONFLICT([from]) \
|
|
||||||
DO UPDATE SET count = count + 1;"
|
|
||||||
|
|
||||||
# example https://stackoverflow.com/a/50718957/2152245
|
|
||||||
# query2 = "INSERT INTO stations ([from], frames_id, last_heard_unix, count) \
|
|
||||||
# VALUES('KC9TZN-8', 4068, 1687623864, 1) \
|
|
||||||
# ON CONFLICT([from]) \
|
|
||||||
# DO UPDATE SET count = count + 1;"
|
|
||||||
|
|
||||||
conn.execute(query3)
|
|
||||||
conn.commit()
|
|
||||||
conn.close()
|
|
Reference in New Issue
Block a user