This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
73 lines (59 loc) · 1.99 KB
/
service.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/python
import settings
from hardware.hardware import PortalHW
from flask import Flask
from flask import jsonify
import json
from flask import Response
from flask import render_template
app = Flask(__name__)
version = settings.constants['API_VERSION']
door_ids = settings.constants['DOOR_IDS']
api_endpoint = "/api/" + version + "/"
@app.route(api_endpoint + "doors", methods = ['GET'])
def getIds():
print door_ids
print type(door_ids)
# You can't actually use flask's jsonify here for security reasons
# Turns out, showing that it's a list under-the-hood is bad.
# TODO find a better way to do this. Dict?
return json.dumps(door_ids)
@app.route(api_endpoint + "doors/<door_id>/toggle", methods = ['POST'])
def toggle(door_id):
if (door_id not in door_ids):
return json.dumps("invalid door id")
portal = PortalHW()
portal.toggle_door(door_id)
return json.dumps("toggling door state on: " + str(door_id))
@app.route(api_endpoint + "doors/<door_id>/toggle", methods = ['GET'])
def getDoorState(door_id):
return notYetImplemented()
@app.route(api_endpoint + "temperature", methods = ['GET'])
def getTemperature():
return notYetImplemented()
@app.route(api_endpoint + "lights", methods = ['POST'])
def getLights():
return notYetImplemented()
@app.route(api_endpoint + "brewer", methods = ['POST'])
def startBrewingCoffee():
return buildErrorFeedback(418, "Error code 418. For help, visit: http://tools.ietf.org/html/rfc2324")
@app.route("/")
def hello():
return render_template('gui.html')
@app.errorhandler(404)
def pageNotFound(error=None):
return buildErrorFeedback(404, "Page not found")
def notYetImplemented():
errorCode = 404
message = 'Endpoint has not yet been implemented'
return buildErrorFeedback(errorCode, message)
def buildErrorFeedback(errorCode, errorMessage):
message = {
'status': errorCode,
'message': errorMessage
}
response = jsonify(message)
response.status_code = errorCode
return response
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=False)