change to config file for settings
parent
39cf8d3408
commit
2b5945ded1
|
|
@ -1 +1,2 @@
|
||||||
|
*kate-swp
|
||||||
/venv
|
/venv
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
[mqtt]
|
||||||
|
Server = 172.16.1.70
|
||||||
|
Port = 1883
|
||||||
|
Topic = devices/main-door/open/open/set
|
||||||
|
User = th
|
||||||
|
Password = h82b7782
|
||||||
|
|
||||||
|
[api]
|
||||||
|
Key = c5S8De2TTPkDhkXREzSP
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
import configparser
|
||||||
|
|
||||||
|
def load_settings():
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read_file(open('doorcontrol.ini'))
|
||||||
|
|
||||||
|
try:
|
||||||
|
mqtt_config = config['mqtt']
|
||||||
|
except KeyError as k_error:
|
||||||
|
print("Missing section: ", k_error, "in config file")
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
MQTT_SERVER = mqtt_config.get("Server")
|
||||||
|
MQTT_PORT = mqtt_config.getint("Port", "1883")
|
||||||
|
MQTT_TOPIC = mqtt_config.get("Topic")
|
||||||
|
MQTT_USER = mqtt_config.get("User")
|
||||||
|
MQTT_PASSWD = mqtt_config.get("Password")
|
||||||
|
|
||||||
|
try:
|
||||||
|
api_config = config['apip']
|
||||||
|
except KeyError as k_error:
|
||||||
|
print("Missing section: ", k_error, " in config file")
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
API_KEY = api_config.get("Key")
|
||||||
|
|
||||||
|
print("Hello")
|
||||||
|
|
||||||
|
|
||||||
28
src/app.py
28
src/app.py
|
|
@ -3,29 +3,39 @@ HTTP API to trigger unlock through MQTT
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
import configparser
|
||||||
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
|
||||||
|
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
|
||||||
|
try:
|
||||||
|
config.read_file(open('../doorcontrol.ini'))
|
||||||
|
except FileNotFoundError:
|
||||||
|
print("Can't find config file. \n\n")
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
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(config['mqtt']['User'], password=config['mqtt']['Password'])
|
||||||
|
|
||||||
|
mqttc.connect(config['mqtt']['Server'], int(config['mqtt']['Port']))
|
||||||
|
except:
|
||||||
|
print("Error connecting to mqtt broker, check configuration.\n\n")
|
||||||
|
raise
|
||||||
|
|
||||||
mqttc.username_pw_set("th", password="h82b7782")
|
|
||||||
mqttc.connect("172.16.1.70", port=1883)
|
|
||||||
mqttc.loop_start()
|
mqttc.loop_start()
|
||||||
|
|
||||||
app = application = Bottle()
|
app = application = Bottle()
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
def index():
|
|
||||||
return "<div>Hello world</div>"
|
|
||||||
|
|
||||||
@app.route('/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 == "c5S8De2TTPkDhkXREzSP":
|
if apiKey == config['api']['key']:
|
||||||
try:
|
try:
|
||||||
if int(delay) > 0:
|
if int(delay) > 0:
|
||||||
time.sleep(int(delay))
|
time.sleep(int(delay))
|
||||||
|
|
@ -33,8 +43,8 @@ def unlock():
|
||||||
pass
|
pass
|
||||||
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>" + str(delay) + " Unlocked!</div>"
|
return '{"message": "Unlocked"}'
|
||||||
return "<div>Hello World</div>"
|
return '{"message": "Unauthorized!"}'
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run(
|
run(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue