add delay functionality, cleanup
parent
d479829672
commit
3de5bbd826
17
src/app.py
17
src/app.py
|
|
@ -1,33 +1,36 @@
|
||||||
#!/usr/bin/python3
|
|
||||||
'''
|
'''
|
||||||
A basic bottle app skeleton
|
HTTP API to trigger unlock through MQTT
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
from time import sleep
|
||||||
from bottle import Bottle, get, request, run
|
from bottle import Bottle, get, request, run
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
|
|
||||||
mqttc = mqtt.Client(client_id="", clean_session=True, userdata=None, transport="tcp")
|
mqttc = mqtt.Client(client_id="", clean_session=True, userdata=None, transport="tcp")
|
||||||
|
|
||||||
mqttc.username_pw_set("th", password="h82b7782")
|
mqttc.username_pw_set("th", password="h82b7782")
|
||||||
mqttc.connect("localhost", port=1883)
|
mqttc.connect("172.16.1.70", port=1883)
|
||||||
mqttc.loop_start()
|
mqttc.loop_start()
|
||||||
|
|
||||||
app = application = Bottle()
|
app = application = Bottle()
|
||||||
|
|
||||||
@get('/unlock')
|
@app.route('/unlock')
|
||||||
def unlock():
|
def unlock():
|
||||||
deviceID = request.query.get('deviceID')
|
deviceID = request.query.get('deviceID')
|
||||||
apiKey = request.query.get('apiKey')
|
apiKey = request.query.get('apiKey')
|
||||||
delay = request.query.get('delay')
|
delay = request.query.get('delay')
|
||||||
|
|
||||||
if apiKey == "123":
|
if apiKey == "c5S8De2TTPkDhkXREzSP":
|
||||||
|
if delay > 0:
|
||||||
|
sleep(delay)
|
||||||
result = mqttc.publish("devices/main-door/unlock/unlock/set", "true")
|
result = mqttc.publish("devices/main-door/unlock/unlock/set", "true")
|
||||||
result.wait_for_publish()
|
result.wait_for_publish()
|
||||||
return "<div>Unlocked!</div>"
|
return "<div>Unlocked!</div>"
|
||||||
#?deviceID=${dID}&apiKey=${aK}&delay=${d}
|
return "<div>Hello World</div>"
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run(
|
run(
|
||||||
host='localhost',
|
host='localhost',
|
||||||
port=8000)
|
port=8000)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue