89 lines
2.4 KiB
Python
89 lines
2.4 KiB
Python
from flask import Flask, request, jsonify
|
|
from datetime import datetime
|
|
import pint
|
|
import sqlite3
|
|
from pyproj import Geod
|
|
|
|
app = Flask(__name__)
|
|
|
|
with sqlite3.connect('location.db', check_same_thread=False) as conn:
|
|
cur = conn.cursor()
|
|
|
|
cur.execute("""CREATE TABLE IF NOT EXISTS location_data (
|
|
row_id INTEGER PRIMARY KEY,
|
|
sender_id INTEGER,
|
|
lat REAL,
|
|
long REAL,
|
|
gpsDateTime INTEGER,
|
|
insertDateTime TEXT DEFUALT CURRENT_TIMESTAMP
|
|
);"""
|
|
)
|
|
|
|
ureg = pint.UnitRegistry()
|
|
geodesic = Geod(ellps='WGS84')
|
|
|
|
@app.route('/')
|
|
def hello():
|
|
return 'Hello!'
|
|
|
|
@app.route('/updateGPS')
|
|
def getGpsDistance():
|
|
own_id = request.args.get('id')
|
|
req_id = request.args.get('req_id')
|
|
lat = dm(request.args.get('lat'))
|
|
long = dm(request.args.get('long'))
|
|
try:
|
|
gpsDateTime = datetime.strptime(request.args.get('gpsDateTime')[:11], '%d%m%y%H%M%S')
|
|
except ValueError:
|
|
print("No gpsDateTime")
|
|
gpsDateTime = datetime.now()
|
|
print(request.args)
|
|
|
|
with sqlite3.connect('location.db', check_same_thread=False) as conn:
|
|
cur = conn.cursor()
|
|
cur.execute(f'INSERT INTO location_data(sender_id, lat, long, gpsDateTime) VALUES({own_id}, {lat}, {long}, "{gpsDateTime}");')
|
|
conn.commit()
|
|
|
|
try:
|
|
|
|
res = cur.execute(f'SELECT lat, long, gpsDateTime, insertDateTime FROM location_data WHERE sender_id = {req_id} ORDER BY insertDateTime DESC')
|
|
|
|
req_lat, req_long, req_dateTime, insertDateTime = res.fetchone()
|
|
|
|
except sqlite3.Error as error:
|
|
print("Failed to read data from table, using default values", error)
|
|
req_lat = 0.0
|
|
req_long = 0.0
|
|
req_dateTime = datetime.now()
|
|
|
|
|
|
print(f'lat: {lat}, long: {long}, req_lat: {req_lat}, req_long: {req_long}, req_dateTime: {req_dateTime}')
|
|
|
|
fwd_azimuth, back_azimuth, distance = geodesic.inv(long, lat, req_long, req_lat)
|
|
|
|
|
|
|
|
|
|
distance = distance * ureg.meter
|
|
prettyDistance = f"{distance:.1f~#P}"
|
|
print(f'fwd: {fwd_azimuth}, bwd: {back_azimuth}, dist: {prettyDistance}')
|
|
return jsonify({'distance': prettyDistance, 'azimuth': fwd_azimuth})
|
|
|
|
|
|
|
|
def dm(ddmm: str):
|
|
south = False
|
|
|
|
south = 'S' in str(ddmm) or 'W' in str(ddmm)
|
|
|
|
ddmm = float(str(ddmm)[:-1])
|
|
|
|
|
|
degrees = int(ddmm) // 100
|
|
minutes = ddmm - 100*degrees
|
|
|
|
x = degrees + minutes/60
|
|
if south:
|
|
x = -x
|
|
return x
|