Fix timezone / utc issue, ignore gps invalid date
As the gps time reading is quite unreliable, instead use current server system time when the gps time is farer away than 24h (timezone adjusted).
This commit is contained in:
parent
a4286d6e9b
commit
85f216b869
|
|
@ -27,7 +27,7 @@ with sqlite3.connect('location.db', check_same_thread=False) as conn:
|
|||
ureg = pint.UnitRegistry()
|
||||
geodesic = Geod(ellps='WGS84')
|
||||
|
||||
current_datetime = datetime.utcnow()
|
||||
current_datetime = datetime.now(dttz.utc)
|
||||
date_yesterday = current_datetime - timedelta(days=1)
|
||||
date_tomorrow = current_datetime + timedelta(days=1)
|
||||
|
||||
|
|
@ -53,12 +53,12 @@ def getGpsDistance():
|
|||
try:
|
||||
gpsDateTime = datetime.strptime(request.args.get('gpsDateTime')[:11], '%d%m%y%H%M%S')
|
||||
if gpsDateTime > date_tomorrow or current_datetime < date_yesterday:
|
||||
valid_date = False
|
||||
gpsDateTime = datetime.now(dttz.utc)
|
||||
print(gpsDateTime)
|
||||
except ValueError:
|
||||
print("No gpsDateTime")
|
||||
gpsDateTime = datetime.now()
|
||||
valid_date = False
|
||||
gpsDateTime = datetime.now(dttz.utc)
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -78,16 +78,16 @@ def getGpsDistance():
|
|||
else:
|
||||
req_lat = 0.0
|
||||
req_long = 0.0
|
||||
req_dateTime = datetime.now()
|
||||
rTime = datetime.now()
|
||||
req_dateTime = datetime.now(dttz.utc)
|
||||
rTime = datetime.now(dttz.utc)
|
||||
|
||||
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()
|
||||
req_dateTime = datetime.now(dttz.utc)
|
||||
|
||||
if (lat != 0.0 and long != 0.0 and valid_date):
|
||||
if (((lat > 0.005 or lat < -0.005) and (long > 0.005 or long < - 0.005)) and valid_date):
|
||||
cur.execute(f'INSERT INTO location_data(sender_id, lat, long, gpsDateTime) VALUES({own_id}, {lat}, {long}, "{gpsDateTime.isoformat()}");')
|
||||
conn.commit()
|
||||
|
||||
|
|
@ -109,9 +109,40 @@ def getGpsDistance():
|
|||
|
||||
print(rTime)
|
||||
else:
|
||||
|
||||
try:
|
||||
|
||||
res = cur.execute(f'SELECT lat, long, gpsDateTime, insertDateTime FROM location_data WHERE sender_id = {own_id} ORDER BY ROWID DESC LIMIT 1;')
|
||||
row = res.fetchone()
|
||||
|
||||
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}')
|
||||
|
||||
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")
|
||||
else:
|
||||
print(rTime)
|
||||
|
||||
|
||||
|
||||
else:
|
||||
|
||||
print(timezone_str)
|
||||
print(pytz.timezone(timezone_str))
|
||||
rTime = rTime.replace(tzinfo=dttz.utc).astimezone(pytz.timezone(timezone_str))
|
||||
except sqlite3.Error as error:
|
||||
print("Failed to read data for last own entry from table -- ignore", error)
|
||||
prettyDistance = "Invalid GPS Signal"
|
||||
fwd_azimuth = 0.0
|
||||
rTime = datetime.utcnow()
|
||||
rTime = datetime.now(dttz.utc)
|
||||
|
||||
return jsonify({'distance': prettyDistance, 'azimuth': fwd_azimuth, 'lastUpdateTZ': rTime.strftime("%m/%d %H:%M:%S"), 'bear_correction': bear_correction})
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user