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.
This commit is contained in:
parent
e44761db8f
commit
be390de996
|
|
@ -3,6 +3,8 @@ from datetime import datetime
|
||||||
import pint
|
import pint
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from pyproj import Geod
|
from pyproj import Geod
|
||||||
|
import timezonefinder, pytz
|
||||||
|
from datetime import timezone as dttz
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
|
@ -15,10 +17,16 @@ with sqlite3.connect('location.db', check_same_thread=False) as conn:
|
||||||
lat REAL,
|
lat REAL,
|
||||||
long REAL,
|
long REAL,
|
||||||
gpsDateTime INTEGER,
|
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()
|
ureg = pint.UnitRegistry()
|
||||||
geodesic = Geod(ellps='WGS84')
|
geodesic = Geod(ellps='WGS84')
|
||||||
|
|
||||||
|
|
@ -30,8 +38,11 @@ def hello():
|
||||||
def getGpsDistance():
|
def getGpsDistance():
|
||||||
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 = dm(request.args.get('lat')) if request.args.get('lat') != '0' else 0.0
|
lat = request.args.get('lat').translate(str.maketrans('', '', ':'))
|
||||||
long = dm(request.args.get('long')) if request.args.get('long') != '0' else 0.0
|
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:
|
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')
|
||||||
|
|
@ -42,33 +53,60 @@ def getGpsDistance():
|
||||||
print(request.args)
|
print(request.args)
|
||||||
|
|
||||||
|
|
||||||
if (lat != 0.0 and long != 0.0):
|
with sqlite3.connect('location.db', check_same_thread=False) as conn:
|
||||||
with sqlite3.connect('location.db', check_same_thread=False) as conn:
|
timezone = None
|
||||||
cur = conn.cursor()
|
time = None
|
||||||
cur.execute(f'INSERT INTO location_data(sender_id, lat, long, gpsDateTime) VALUES({own_id}, {lat}, {long}, "{gpsDateTime}");')
|
cur = conn.cursor()
|
||||||
conn.commit()
|
try:
|
||||||
|
|
||||||
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()
|
||||||
|
|
||||||
res = cur.execute(f'SELECT lat, long, gpsDateTime, insertDateTime FROM location_data WHERE sender_id = {req_id} ORDER BY insertDateTime DESC')
|
if row:
|
||||||
|
req_lat, req_long, req_dateTime, insertDateTime = row
|
||||||
req_lat, req_long, req_dateTime, insertDateTime = res.fetchone()
|
time = datetime.fromisoformat(req_dateTime)
|
||||||
|
else:
|
||||||
except sqlite3.Error as error:
|
|
||||||
print("Failed to read data from table, using default values", error)
|
|
||||||
req_lat = 0.0
|
req_lat = 0.0
|
||||||
req_long = 0.0
|
req_long = 0.0
|
||||||
req_dateTime = datetime.now()
|
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
|
return jsonify({'distance': prettyDistance, 'azimuth': fwd_azimuth, 'lastUpdateTZ': time.strftime("%H:%M:%S")})
|
||||||
prettyDistance = f"{distance:.1f~#P}"
|
|
||||||
print(f'fwd: {fwd_azimuth}, bwd: {back_azimuth}, dist: {prettyDistance}')
|
|
||||||
return jsonify({'distance': prettyDistance, 'azimuth': fwd_azimuth})
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user