Resolving further gps time issues

This commit is contained in:
Tim Staat 2025-09-11 01:15:20 +02:00
parent 85f216b869
commit c976809c1e

View File

@ -27,9 +27,7 @@ with sqlite3.connect('location.db', check_same_thread=False) as conn:
ureg = pint.UnitRegistry() ureg = pint.UnitRegistry()
geodesic = Geod(ellps='WGS84') geodesic = Geod(ellps='WGS84')
current_datetime = datetime.now(dttz.utc)
date_yesterday = current_datetime - timedelta(days=1)
date_tomorrow = current_datetime + timedelta(days=1)
@app.route('/') @app.route('/')
@ -38,6 +36,10 @@ def hello():
@app.route('/updateGPS') @app.route('/updateGPS')
def getGpsDistance(): def getGpsDistance():
current_datetime = datetime.now(dttz.utc)
date_yesterday = current_datetime - timedelta(days=1)
date_tomorrow = current_datetime + timedelta(days=1)
own_id = request.args.get('id') own_id = request.args.get('id')
req_id = request.args.get('req_id') req_id = request.args.get('req_id')
lat = request.args.get('lat').translate(str.maketrans('', '', ':')) lat = request.args.get('lat').translate(str.maketrans('', '', ':'))
@ -51,10 +53,10 @@ def getGpsDistance():
print(request.args) print(request.args)
try: try:
gpsDateTime = datetime.strptime(request.args.get('gpsDateTime')[:11], '%d%m%y%H%M%S') gpsDateTime = datetime.strptime(request.args.get('gpsDateTime')[:11], '%d%m%y%H%M%S').replace(tzinfo=dttz.utc)
if gpsDateTime > date_tomorrow or current_datetime < date_yesterday: if gpsDateTime > date_tomorrow or current_datetime < date_yesterday:
gpsDateTime = datetime.now(dttz.utc) gpsDateTime = datetime.now(dttz.utc)
print(gpsDateTime) print(f'GPS datetime out of boundaries - replacing with system time: {gpsDateTime}')
except ValueError: except ValueError:
print("No gpsDateTime") print("No gpsDateTime")
gpsDateTime = datetime.now(dttz.utc) gpsDateTime = datetime.now(dttz.utc)
@ -103,11 +105,8 @@ def getGpsDistance():
if timezone_str is None: if timezone_str is None:
print("Could not determine the time zone") print("Could not determine the time zone")
else: else:
print(timezone_str)
print(pytz.timezone(timezone_str))
rTime = rTime.replace(tzinfo=dttz.utc).astimezone(pytz.timezone(timezone_str)) rTime = rTime.replace(tzinfo=dttz.utc).astimezone(pytz.timezone(timezone_str))
print(f'GPS datetime of requested database entry: {rTime}')
print(rTime)
else: else:
try: try:
@ -118,7 +117,8 @@ def getGpsDistance():
if row: if row:
lat, long, lastOwnDateTime, lastOwnInsertDateTime = row lat, long, lastOwnDateTime, lastOwnInsertDateTime = row
print(f'lat: {lat}, long: {long}, req_lat: {req_lat}, req_long: {req_long}, req_dateTime: {req_dateTime}') print(f'lat: {lat}, long: {long}, req_lat: {req_lat}, req_long: {req_long}, req_dateTime: {req_dateTime}')
rTime = datetime.fromisoformat(req_dateTime)
if rTime < date_tomorrow and current_datetime > date_yesterday:
fwd_azimuth, back_azimuth, distance = geodesic.inv(long, lat, req_long, req_lat) fwd_azimuth, back_azimuth, distance = geodesic.inv(long, lat, req_long, req_lat)
distance = distance * ureg.meter distance = distance * ureg.meter
@ -129,15 +129,17 @@ def getGpsDistance():
if timezone_str is None: if timezone_str is None:
print("Could not determine the time zone") print("Could not determine the time zone")
else: else:
print(rTime) rTime = rTime.replace(tzinfo=dttz.utc).astimezone(pytz.timezone(timezone_str))
else:
prettyDistance = "Invalid GPS Signal"
fwd_azimuth = 0.0
rTime = datetime.utcnow()
else: else:
print(timezone_str) prettyDistance = "Invalid GPS Signal"
print(pytz.timezone(timezone_str)) fwd_azimuth = 0.0
rTime = rTime.replace(tzinfo=dttz.utc).astimezone(pytz.timezone(timezone_str)) rTime = datetime.utcnow()
except sqlite3.Error as error: except sqlite3.Error as error:
print("Failed to read data for last own entry from table -- ignore", error) print("Failed to read data for last own entry from table -- ignore", error)
prettyDistance = "Invalid GPS Signal" prettyDistance = "Invalid GPS Signal"