-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.py
111 lines (82 loc) · 2.83 KB
/
server.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# todo: Implement Signals to notify the app whenever the relay status changes
# todo: use web sockets to update the status display when the relay status changes
from __future__ import print_function
from flask import Flask
from flask import make_response
from flask import render_template
from flask_bootstrap import Bootstrap
from relay_lib_seeed import *
error_msg = '{msg:"error"}'
success_msg = '{msg:"success"}'
NUM_RELAY_PORTS = 4
app = Flask(__name__)
bootstrap = Bootstrap(app)
# turn all of the relays off, so we're starting with a clean slate. Since the library can determine
# relay status by querying the board, there's no real, burning need for this, but it seemed
# like the right thing to do.
relay_all_off()
@app.route('/')
def index():
print("Loading app Main page")
# return success_resp
return render_template('index.html')
@app.route('/status/<int:relay>')
def api_get_status(relay):
res = relay_get_port_status(relay)
if res:
print("Relay is ON")
return make_response("1", 200)
else:
print("Relay is OFF")
return make_response("0", 200)
@app.route('/toggle/<int:relay>')
def api_toggle_relay(relay):
print("Executing api_relay_toggle:", relay)
relay_toggle_port(relay)
return make_response(success_msg, 200)
@app.route('/on/<int:relay>')
def api_relay_on(relay):
print("Executing api_relay_on:", relay)
if validate_relay(relay):
print("valid relay")
relay_on(relay)
return make_response(success_msg, 200)
else:
print("invalid relay")
return make_response(error_msg, 404)
@app.route('/off/<int:relay>')
def api_relay_off(relay):
print("Executing api_relay_off:", relay)
if validate_relay(relay):
print("valid relay")
relay_off(relay)
return make_response(success_msg, 200)
else:
print("invalid relay")
return make_response(error_msg, 404)
@app.route('/all_on/')
def api_relay_all_on():
print("Executing api_relay_all_on")
relay_all_on()
return make_response(success_msg, 200)
@app.route('/all_off/')
def api_all_relay_off():
print("Executing api_relay_all_off")
relay_all_off()
return make_response(success_msg, 200)
@app.errorhandler(404)
def page_not_found(e):
print("ERROR: 404")
return render_template('404.html', the_error=e), 404
@app.errorhandler(500)
def internal_server_error(e):
print("ERROR: 500")
return render_template('500.html', the_error=e), 500
def validate_relay(relay):
# Make sure the port falls between 1 and NUM_RELAY_PORTS
return (relay > 0) and (relay <= NUM_RELAY_PORTS)
if __name__ == "__main__":
# On the Pi, you need to run the app using this command to make sure it
# listens for requests outside of the device.
# app.run(host='0.0.0.0')
app.run(host='0.0.0.0', port=5000, debug=True)