Server now return lastUpdateTZ, so that the requesting client receives the last update time in his respective local time (given GPS coordinates are provided). Lots of print statements still in place.
128 lines
3.9 KiB
Python
128 lines
3.9 KiB
Python
from flask import Flask, request, jsonify
|
|
from datetime import datetime
|
|
import pint
|
|
import sqlite3
|
|
from pyproj import Geod
|
|
import timezonefinder, pytz
|
|
from datetime import timezone as dttz
|
|
|
|
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 DEFAULT CURRENT_TIMESTAMP
|
|
);"""
|
|
)
|
|
|
|
with sqlite3.connect('location.db', check_same_thread=False) as conn:
|
|
cur = conn.cursor()
|
|
res = cur.execute("SELECT * FROM location_data;")
|
|
print(res.fetchall())
|
|
|
|
|
|
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 = request.args.get('lat').translate(str.maketrans('', '', ':'))
|
|
long = request.args.get('long').translate(str.maketrans('', '', ':'))
|
|
|
|
lat = dm(lat) if lat and len(lat) > 1 else 0.0
|
|
long = dm(long) if long and len(long) > 1 else 0.0
|
|
|
|
try:
|
|
gpsDateTime = datetime.strptime(request.args.get('gpsDateTime')[:11], '%d%m%y%H%M%S')
|
|
print(gpsDateTime)
|
|
except ValueError:
|
|
print("No gpsDateTime")
|
|
gpsDateTime = datetime.now()
|
|
print(request.args)
|
|
|
|
|
|
with sqlite3.connect('location.db', check_same_thread=False) as conn:
|
|
timezone = None
|
|
time = None
|
|
cur = conn.cursor()
|
|
try:
|
|
|
|
res = cur.execute(f'SELECT lat, long, gpsDateTime, insertDateTime FROM location_data WHERE sender_id = {req_id} ORDER BY insertDateTime DESC;')
|
|
row = res.fetchone()
|
|
|
|
if row:
|
|
req_lat, req_long, req_dateTime, insertDateTime = row
|
|
time = datetime.fromisoformat(req_dateTime)
|
|
else:
|
|
req_lat = 0.0
|
|
req_long = 0.0
|
|
req_dateTime = datetime.now()
|
|
time = datetime.now()
|
|
|
|
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()
|
|
|
|
if (lat != 0.0 and long != 0.0):
|
|
cur.execute(f'INSERT INTO location_data(sender_id, lat, long, gpsDateTime) VALUES({own_id}, {lat}, {long}, "{gpsDateTime.isoformat()}");')
|
|
conn.commit()
|
|
|
|
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}')
|
|
tf = timezonefinder.TimezoneFinder()
|
|
timezone_str = tf.certain_timezone_at(lat=lat, lng=long)
|
|
if timezone_str is None:
|
|
print("Could not determine the time zone")
|
|
else:
|
|
print(timezone_str)
|
|
print(pytz.timezone(timezone_str))
|
|
time = time.replace(tzinfo=dttz.utc).astimezone(pytz.timezone(timezone_str))
|
|
|
|
print(time)
|
|
else:
|
|
prettyDistance = "No GPS Signal"
|
|
fwd_azimuth = 0.0
|
|
time = datetime.utcnow
|
|
|
|
|
|
|
|
|
|
return jsonify({'distance': prettyDistance, 'azimuth': fwd_azimuth, 'lastUpdateTZ': time.strftime("%H:%M:%S")})
|
|
|
|
|
|
|
|
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
|