change to config file for settings

master
Bryan 2021-03-16 20:25:53 -06:00
parent 39cf8d3408
commit 2b5945ded1
4 changed files with 59 additions and 10 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*kate-swp
/venv

9
doorcontrol.ini Normal file
View File

@ -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

29
readconfig.py Normal file
View File

@ -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")

View File

@ -3,29 +3,39 @@ HTTP API to trigger unlock through MQTT
'''
import time
import configparser
from bottle import Bottle, get, request, run
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.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()
app = application = Bottle()
@app.route('/')
def index():
return "<div>Hello world</div>"
@app.route('/unlock')
def unlock():
deviceID = request.query.get('deviceID')
apiKey = request.query.get('apiKey')
delay = request.query.get('delay')
if apiKey == "c5S8De2TTPkDhkXREzSP":
if apiKey == config['api']['key']:
try:
if int(delay) > 0:
time.sleep(int(delay))
@ -33,8 +43,8 @@ def unlock():
pass
result = mqttc.publish("devices/main-door/unlock/unlock/set", "true")
result.wait_for_publish()
return "<div>" + str(delay) + " Unlocked!</div>"
return "<div>Hello World</div>"
return '{"message": "Unlocked"}'
return '{"message": "Unauthorized!"}'
if __name__ == '__main__':
run(