mirror of
https://github.com/supleed2/ELEC60013-ES-CW1.git
synced 2024-12-23 05:55:49 +00:00
Added functionality for reading last 5 step counts£
This commit is contained in:
parent
e3a612f207
commit
78ac68cb2d
41
api/data.py
41
api/data.py
|
@ -1,5 +1,6 @@
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
|
from datetime import datetime, time, timedelta
|
||||||
from flask import Response, Blueprint, request
|
from flask import Response, Blueprint, request
|
||||||
from firebase_admin import firestore
|
from firebase_admin import firestore
|
||||||
|
|
||||||
|
@ -12,12 +13,14 @@ def uploadReadings():
|
||||||
resp = {'error': 'Device not specified'}
|
resp = {'error': 'Device not specified'}
|
||||||
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
||||||
|
|
||||||
|
# Check that measurements are provided
|
||||||
body = request.json
|
body = request.json
|
||||||
if body is None:
|
if body is None:
|
||||||
resp = {'error': 'Invalid request - please provide a body'}
|
resp = {'error': 'Invalid request - please provide a body'}
|
||||||
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
||||||
body['timestamp'] = time.time()
|
body['timestamp'] = time.time()
|
||||||
|
|
||||||
|
# Save all the measurements
|
||||||
doc = firestore.client().collection(u'readings').document(deviceId).get()
|
doc = firestore.client().collection(u'readings').document(deviceId).get()
|
||||||
if doc.exists:
|
if doc.exists:
|
||||||
list = doc.to_dict()['data']
|
list = doc.to_dict()['data']
|
||||||
|
@ -25,9 +28,9 @@ def uploadReadings():
|
||||||
data = list
|
data = list
|
||||||
else:
|
else:
|
||||||
data = [body]
|
data = [body]
|
||||||
|
|
||||||
upload = {'data': data}
|
upload = {'data': data}
|
||||||
firestore.client().collection(u'readings').document(deviceId).set(upload)
|
firestore.client().collection(u'readings').document(deviceId).set(upload)
|
||||||
|
|
||||||
resp = {'success': 'Data saved'}
|
resp = {'success': 'Data saved'}
|
||||||
return Response(json.dumps(resp), status=200, mimetype='application/json')
|
return Response(json.dumps(resp), status=200, mimetype='application/json')
|
||||||
|
|
||||||
|
@ -47,7 +50,7 @@ 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'])
|
@data.route('/readings/location/last', methods=['GET'])
|
||||||
def getLastLocation():
|
def getLastLocation():
|
||||||
deviceId = request.headers.get('deviceid')
|
deviceId = request.headers.get('deviceid')
|
||||||
if deviceId is None:
|
if deviceId is None:
|
||||||
|
@ -67,7 +70,7 @@ def getLastLocation():
|
||||||
results = {'latitude': lat, 'longitude': lon}
|
results = {'latitude': lat, 'longitude': lon}
|
||||||
return Response(json.dumps(results), status=200, mimetype='application/json')
|
return Response(json.dumps(results), status=200, mimetype='application/json')
|
||||||
|
|
||||||
@data.route('/readings/last/steps', methods=['GET'])
|
@data.route('/readings/steps/today', methods=['GET'])
|
||||||
def getStepsToday():
|
def getStepsToday():
|
||||||
deviceId = request.headers.get('deviceid')
|
deviceId = request.headers.get('deviceid')
|
||||||
if deviceId is None:
|
if deviceId is None:
|
||||||
|
@ -84,3 +87,35 @@ def getStepsToday():
|
||||||
|
|
||||||
results = {'cumulative_steps_today': steps}
|
results = {'cumulative_steps_today': steps}
|
||||||
return Response(json.dumps(results), status=200, mimetype='application/json')
|
return Response(json.dumps(results), status=200, mimetype='application/json')
|
||||||
|
|
||||||
|
@data.route('/readings/steps/last-five-days', methods=['GET'])
|
||||||
|
def getStepsLastFiveDays():
|
||||||
|
deviceId = request.headers.get('deviceid')
|
||||||
|
if deviceId is None:
|
||||||
|
resp = {'error': 'Device not specified'}
|
||||||
|
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
||||||
|
|
||||||
|
upcomingMidnight = datetime.combine(datetime.today(), time.min) + timedelta(days=1)
|
||||||
|
doc = firestore.client().collection(u'readings').document(deviceId).get()
|
||||||
|
|
||||||
|
if doc.exists:
|
||||||
|
data = doc.to_dict()['data']
|
||||||
|
listOfDailySteps = []
|
||||||
|
|
||||||
|
for i in range(0, 5):
|
||||||
|
found = False
|
||||||
|
previousMidnight = upcomingMidnight - timedelta(days=1)
|
||||||
|
print(previousMidnight.timestamp())
|
||||||
|
steps = 0
|
||||||
|
for reading in reversed(data):
|
||||||
|
if reading['timestamp'] <= upcomingMidnight.timestamp() and reading['timestamp'] >= previousMidnight.timestamp() and not found:
|
||||||
|
steps = reading['cumulative_steps_today']
|
||||||
|
found = True
|
||||||
|
listOfDailySteps.append(steps)
|
||||||
|
upcomingMidnight = previousMidnight
|
||||||
|
else:
|
||||||
|
listOfDailySteps = [0] * 5
|
||||||
|
|
||||||
|
results = {'daily_steps': listOfDailySteps}
|
||||||
|
return Response(json.dumps(results), status=200, mimetype='application/json')
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue