-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpy-webrelais.py
56 lines (40 loc) · 1.44 KB
/
py-webrelais.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
__author__ = 'schinken'
from helpers import check_permission, auth_required, relay_result
from flask import Flask, render_template, jsonify
from relays import SainSmart, Relay, RelaysProxy
from settings import relay_cards
app = Flask(__name__)
relays_proxy = RelaysProxy()
for entry in relay_cards:
sainsmart = SainSmart(entry['serial'])
wrapper = Relay(sainsmart)
relays_proxy.add_relay(entry['start'], entry['relays'], wrapper)
@app.route("/")
def page_main():
return render_template('index.html')
@app.route("/relais", methods=["GET"])
@app.route("/relais/<int:relay>", methods=["GET"])
def get_relais(relay=None):
if relay is None:
response = []
for relay, status in enumerate(relays_proxy.get_relays()):
response.append(relay_result(relay, status))
else:
response = relay_result(relay, relais_proxy.get_relay(relay))
return jsonify(payload=response)
@app.route("/relais/<int:relay>", methods=["POST"])
def set_relais(relay):
if check_permission(relay):
relays_proxy.set_relay(relay, True)
else:
return auth_required()
return jsonify(payload=relay_result(relay, True))
@app.route("/relais/<int:relay>", methods=["DELETE"])
def reset_relais(relay):
if check_permission(relay):
relays_proxy.set_relay(relay, False)
else:
return auth_required()
return jsonify(payload=relay_result(relay, False))
if __name__ == '__main__':
app.run()