Added endpoints for retrieving device data

This commit is contained in:
Benjamin Ramhorst 2022-02-07 13:54:40 +00:00
parent e0f2f18bd2
commit 2fa0e252f8

View file

@ -1,7 +1,9 @@
import datetime
import time
import json
from flask import Response, Blueprint, request
from firebase_admin import firestore
data = Blueprint('data', __name__)
@data.route('/readings/save', methods=['POST'])
@ -13,7 +15,7 @@ def uploadReadings():
body = request.json
if body is None:
return Response("{'error':'Invalid request - please provide a body'}", status=400, mimetype='application/json')
body['timestamp'] = datetime.datetime.now()
body['timestamp'] = time.time()
doc = firestore.client().collection(u'readings').document(deviceId).get()
if doc.exists:
@ -26,3 +28,18 @@ def uploadReadings():
upload = {'data': data}
firestore.client().collection(u'readings').document(deviceId).set(upload)
return Response("{'success':'Data saved'}", status=200, mimetype='application/json')
@data.route('/readings/getall', methods=['GET'])
def getAllReadings():
deviceId = request.headers.get('deviceid')
if deviceId is None:
return Response("{'error':'Device not specified'}", status=400, mimetype='application/json')
doc = firestore.client().collection(u'readings').document(deviceId).get()
if doc.exists:
data = doc.to_dict()['data']
else:
data = []
results = {'data': data}
return Response(json.dumps(results), status=200, mimetype='application/json')