-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
45 lines (35 loc) · 1.29 KB
/
app.py
File metadata and controls
45 lines (35 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from flask import Flask, jsonify
from flask import request
from flask.ext.mongoengine import MongoEngine
app = Flask(__name__)
app.config['MONGODB_SETTINGS'] = {'DB': 'thermostats' }
db = MongoEngine(app)
class Temperature(db.Document):
temp = db.FloatField()
timestamp = db.IntField()
@app.route('/temperature', methods = ['POST'])
def post_temperature():
# POST A TEMP FROM REQUEST ARGS
temperature = request.args.get('temperature', None)
timestamp = request.args.get('timestamp', None)
if temperature and timestamp:
try:
temperature = float( temperature )
except ValueError:
return jsonify( { 'return_code': 'Error! Temperature invalid.' } )
try:
timestamp = int( timestamp )
except ValueError:
return jsonify( { 'return_code': 'Error! Timestamp invalid.' } )
t = { 'temp':temperature,
'timestamp':timestamp }
try:
Temperature(**t).save()
return jsonify( t )
except:
return jsonify( { 'return_code': 'Error! Database error.' } )
else:
return jsonify( { 'return_code': 'Error! Must supply a temperature.' } )
return jsonify( { 'return_code': 'Success!' } )
if __name__ == '__main__':
app.run(debug = True)