mirror of
https://github.com/supleed2/ELEC60013-ES-CW1.git
synced 2024-11-10 01:35:50 +00:00
Added endpoints for last location and total steps today
This commit is contained in:
parent
c661fe3595
commit
e3a612f207
38
api/data.py
38
api/data.py
|
@ -46,3 +46,41 @@ def getAllReadings():
|
||||||
|
|
||||||
results = {'data': data}
|
results = {'data': data}
|
||||||
return Response(json.dumps(results), status=200, mimetype='application/json')
|
return Response(json.dumps(results), status=200, mimetype='application/json')
|
||||||
|
|
||||||
|
@data.route('/readings/last/location', methods=['GET'])
|
||||||
|
def getLastLocation():
|
||||||
|
deviceId = request.headers.get('deviceid')
|
||||||
|
if deviceId is None:
|
||||||
|
resp = {'error': 'Device not specified'}
|
||||||
|
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
||||||
|
|
||||||
|
doc = firestore.client().collection(u'readings').document(deviceId).get()
|
||||||
|
if doc.exists:
|
||||||
|
data = doc.to_dict()['data']
|
||||||
|
lastEntry = data[-1]
|
||||||
|
lat = lastEntry['latitude']
|
||||||
|
lon = lastEntry['longitude']
|
||||||
|
else:
|
||||||
|
lat = -1.0
|
||||||
|
lon = -1.0
|
||||||
|
|
||||||
|
results = {'latitude': lat, 'longitude': lon}
|
||||||
|
return Response(json.dumps(results), status=200, mimetype='application/json')
|
||||||
|
|
||||||
|
@data.route('/readings/last/steps', methods=['GET'])
|
||||||
|
def getStepsToday():
|
||||||
|
deviceId = request.headers.get('deviceid')
|
||||||
|
if deviceId is None:
|
||||||
|
resp = {'error': 'Device not specified'}
|
||||||
|
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
||||||
|
|
||||||
|
doc = firestore.client().collection(u'readings').document(deviceId).get()
|
||||||
|
if doc.exists:
|
||||||
|
data = doc.to_dict()['data']
|
||||||
|
lastEntry = data[-1]
|
||||||
|
steps = lastEntry['cumulative_steps_today']
|
||||||
|
else:
|
||||||
|
steps = 0
|
||||||
|
|
||||||
|
results = {'cumulative_steps_today': steps}
|
||||||
|
return Response(json.dumps(results), status=200, mimetype='application/json')
|
||||||
|
|
Loading…
Reference in a new issue