From 3de5bbd82682447e2271359f9c5d257f370c8de0 Mon Sep 17 00:00:00 2001 From: Bryan Date: Mon, 15 Mar 2021 00:12:55 -0600 Subject: [PATCH] add delay functionality, cleanup --- src/app.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/app.py b/src/app.py index 9c2892f..db6c08e 100644 --- a/src/app.py +++ b/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 import paho.mqtt.client as mqtt + mqttc = mqtt.Client(client_id="", clean_session=True, userdata=None, transport="tcp") mqttc.username_pw_set("th", password="h82b7782") -mqttc.connect("localhost", port=1883) +mqttc.connect("172.16.1.70", port=1883) mqttc.loop_start() app = application = Bottle() -@get('/unlock') +@app.route('/unlock') def unlock(): deviceID = request.query.get('deviceID') apiKey = request.query.get('apiKey') 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.wait_for_publish() return "
Unlocked!
" -#?deviceID=${dID}&apiKey=${aK}&delay=${d} - + return "
Hello World
" if __name__ == '__main__': run( host='localhost', port=8000) +