-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
72 lines (56 loc) · 1.81 KB
/
app.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
from flask import Flask, render_template, redirect, url_for, jsonify
from flask_basicauth import BasicAuth
import RPi.GPIO as GPIO
import time
class ButtonSwitcher(Flask):
def __init__(self, port, name):
super().__init__(name)
self.port = port
GPIO.setmode(GPIO.BCM)
GPIO.setup(port, GPIO.OUT)
# keep it high by default
GPIO.output(port, GPIO.HIGH)
def switch_output(self):
state = self.get_state()
if state == 0:
GPIO.output(self.port, GPIO.HIGH)
elif state == 1:
GPIO.output(self.port, GPIO.LOW)
def get_state(self):
state = GPIO.input(self.port)
return state
app = ButtonSwitcher(15, __name__)
# @app.route("/")
# @app.route('/home')
# def home():
# return render_template("button.html", state='Gate button')
@app.route('/')
@app.route('/home')
def home():
state = app.get_state()
print(state)
if state == 0:
button_caption = 'Start MOKE'
button_class = 'start_block'
elif state == 1:
button_caption = 'Stop MOKE'
button_class = 'stop_block'
print(button_caption)
return render_template("button.html", state=button_caption, button_class=button_class)
@app.route("/echo")
def echo():
print('echo received')
app.switch_output()
return redirect(url_for('home'))
@app.route("/trigger")
def trigger():
app.switch_output()
msg = {"msg": "Successfully triggered."}
return jsonify(msg)
if __name__ == '__main__':
# set up authentication
app.config['BASIC_AUTH_USERNAME'] = 'pi'
app.config['BASIC_AUTH_PASSWORD'] = 'pi@moke'
app.config['BASIC_AUTH_FORCE'] = True
basic_auth = BasicAuth(app)
app.run(debug=False, host='0.0.0.0', port=2525)