From be390de9962586df7656b7fa01ad637524fa8289 Mon Sep 17 00:00:00 2001 From: Tim Staat Date: Thu, 28 Aug 2025 16:59:29 +0200 Subject: [PATCH] Update Server for returning time 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. --- server/python_server.py | 82 ++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 22 deletions(-) diff --git a/server/python_server.py b/server/python_server.py index 91234bf..af5d7c8 100644 --- a/server/python_server.py +++ b/server/python_server.py @@ -3,6 +3,8 @@ from datetime import datetime import pint import sqlite3 from pyproj import Geod +import timezonefinder, pytz +from datetime import timezone as dttz app = Flask(__name__) @@ -15,10 +17,16 @@ with sqlite3.connect('location.db', check_same_thread=False) as conn: lat REAL, long REAL, gpsDateTime INTEGER, - insertDateTime TEXT DEFUALT CURRENT_TIMESTAMP + 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') @@ -30,8 +38,11 @@ def hello(): def getGpsDistance(): own_id = request.args.get('id') req_id = request.args.get('req_id') - lat = dm(request.args.get('lat')) if request.args.get('lat') != '0' else 0.0 - long = dm(request.args.get('long')) if request.args.get('long') != '0' else 0.0 + 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') @@ -41,34 +52,61 @@ def getGpsDistance(): 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: - if (lat != 0.0 and long != 0.0): - 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() + res = cur.execute(f'SELECT lat, long, gpsDateTime, insertDateTime FROM location_data WHERE sender_id = {req_id} ORDER BY insertDateTime DESC;') + row = res.fetchone() - 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) + 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 - 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}) + return jsonify({'distance': prettyDistance, 'azimuth': fwd_azimuth, 'lastUpdateTZ': time.strftime("%H:%M:%S")})