mirror of
https://github.com/supleed2/ELEC60013-ES-CW1.git
synced 2024-12-22 21:45:48 +00:00
Added endpoints for saving measurements
This commit is contained in:
parent
c470ebf42d
commit
e0f2f18bd2
28
api/data.py
28
api/data.py
|
@ -0,0 +1,28 @@
|
||||||
|
import datetime
|
||||||
|
from flask import Response, Blueprint, request
|
||||||
|
from firebase_admin import firestore
|
||||||
|
|
||||||
|
data = Blueprint('data', __name__)
|
||||||
|
|
||||||
|
@data.route('/readings/save', methods=['POST'])
|
||||||
|
def uploadReadings():
|
||||||
|
deviceId = request.headers.get('deviceid')
|
||||||
|
if deviceId is None:
|
||||||
|
return Response("{'error':'Device not specified'}", status=400, mimetype='application/json')
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
doc = firestore.client().collection(u'readings').document(deviceId).get()
|
||||||
|
if doc.exists:
|
||||||
|
list = doc.to_dict()['data']
|
||||||
|
list.append(body)
|
||||||
|
data = list
|
||||||
|
else:
|
||||||
|
data = [body]
|
||||||
|
|
||||||
|
upload = {'data': data}
|
||||||
|
firestore.client().collection(u'readings').document(deviceId).set(upload)
|
||||||
|
return Response("{'success':'Data saved'}", status=200, mimetype='application/json')
|
3
main.py
3
main.py
|
@ -2,10 +2,13 @@ from config.variables import MAIL_SERVER, MAIL_PORT, MAIL_USERNAME, MAIL_PASSWOR
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from firebase_admin import credentials, initialize_app
|
from firebase_admin import credentials, initialize_app
|
||||||
from api.authentication import authentication
|
from api.authentication import authentication
|
||||||
|
from api.data import data
|
||||||
|
|
||||||
# Initialize Flask app and register all the endpoints
|
# Initialize Flask app and register all the endpoints
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.register_blueprint(authentication)
|
app.register_blueprint(authentication)
|
||||||
|
app.register_blueprint(data)
|
||||||
|
|
||||||
|
|
||||||
# Initialize Mail instance
|
# Initialize Mail instance
|
||||||
app.config['MAIL_SERVER'] = MAIL_SERVER
|
app.config['MAIL_SERVER'] = MAIL_SERVER
|
||||||
|
|
Loading…
Reference in a new issue