diff --git a/server/python_server.py b/server/python_server.py index 9570131..ddc35ab 100644 --- a/server/python_server.py +++ b/server/python_server.py @@ -27,9 +27,7 @@ with sqlite3.connect('location.db', check_same_thread=False) as conn: ureg = pint.UnitRegistry() 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('/') @@ -38,6 +36,10 @@ def hello(): @app.route('/updateGPS') 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') req_id = request.args.get('req_id') lat = request.args.get('lat').translate(str.maketrans('', '', ':')) @@ -51,10 +53,10 @@ def getGpsDistance(): print(request.args) 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: gpsDateTime = datetime.now(dttz.utc) - print(gpsDateTime) + print(f'GPS datetime out of boundaries - replacing with system time: {gpsDateTime}') except ValueError: print("No gpsDateTime") gpsDateTime = datetime.now(dttz.utc) @@ -103,11 +105,8 @@ def getGpsDistance(): if timezone_str is None: print("Could not determine the time zone") else: - print(timezone_str) - print(pytz.timezone(timezone_str)) rTime = rTime.replace(tzinfo=dttz.utc).astimezone(pytz.timezone(timezone_str)) - - print(rTime) + print(f'GPS datetime of requested database entry: {rTime}') else: try: @@ -118,26 +117,29 @@ def getGpsDistance(): if row: lat, long, lastOwnDateTime, lastOwnInsertDateTime = row 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 - prettyDistance = f"{distance:.1f~#P}" + ' (N)' if not bear_correction else '' - 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") + distance = distance * ureg.meter + prettyDistance = f"{distance:.1f~#P}" + ' (N)' if not bear_correction else '' + 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: + rTime = rTime.replace(tzinfo=dttz.utc).astimezone(pytz.timezone(timezone_str)) else: - print(rTime) - - - + prettyDistance = "Invalid GPS Signal" + fwd_azimuth = 0.0 + rTime = datetime.utcnow() else: - print(timezone_str) - print(pytz.timezone(timezone_str)) - rTime = rTime.replace(tzinfo=dttz.utc).astimezone(pytz.timezone(timezone_str)) + prettyDistance = "Invalid GPS Signal" + fwd_azimuth = 0.0 + rTime = datetime.utcnow() + except sqlite3.Error as error: print("Failed to read data for last own entry from table -- ignore", error) prettyDistance = "Invalid GPS Signal"