diff --git a/app.py b/app.py new file mode 100644 index 0000000..65fb71b --- /dev/null +++ b/app.py @@ -0,0 +1,117 @@ +import flask +import threading +import paho.mqtt as mqtt +import paho.mqtt.client +import paho.mqtt.publish +import re +import json + +c = threading.Condition() +application = flask.Flask(__name__) +application.msg="ab" +application.lights = [False, False, False, False] + +#def on_message(client,userdata,message): +# c.acquire() +# application.msg = str(message.payload) +# print(message.payload) +# light_status = re.compile('devices/light_[^/]+/light_([0-9]+)/on') +# m = light_status.search(message.topic) +# if m: +# n = int(m.group(1)) +# if n > 0 and n < 5: +# if message.payload == "true": +# application.lights[n-1] = True +# else: +# application.lights[n-1] = False +# +# +# c.release() +# print("MQTT Thread: " + str(threading.current_thread())) +# +#mc = mqtt.client.Client() +#mc.username_pw_set("light","MG4Dhp6vCZjgbTzJ") +#mc.connect("10.8.1.16") +#mc.on_message = on_message +#mc.subscribe("devices/#") +#mc.loop_start() + + +@application.route("/") +def main(): + return flask.send_file('static/index.html') + +@application.route("/status") +def status(): + print("Main Thread: " + str(threading.current_thread())) + c.acquire() + st = {"lights" : application.lights} + c.release() + return json.dumps(st) + +config = { + "mqtt_server": "sound.waw.hackerspace.pl", + "mqtt_port": 1881, + "mqtt_username": "light", + "mqtt_password": "MG4Dhp6vCZjgbTzJ", + "lights": { + "l1" : { + "enabled": True, + "mqtt_path": "devices/light_904649/light_2/on", + }, + "l2": { + "enabled": True, + "mqtt_path": "devices/light_904649/light_3/on", + }, + "l3": { + "enabled": True, + "mqtt_path": "devices/light_904649/light_1/on", + }, + "l4": { + "enabled": True, + "mqtt_path": "iot/077521/relay/on", + }, + "l7": { + "enabled": True, + "mqtt_path": "devices/light_904649/light_4/on", + }, + } +} + +@application.route("/config") +def get_config(): + return flask.jsonify(config) + +@application.route('/light//on/set', methods=['POST']) +def set_light(lid): + if lid in config["lights"]: + mqtt.publish.single( + config["lights"][lid]["mqtt_path"] + "/set", + "true", + qos=1, + hostname=config["mqtt_server"], + auth = { + 'username':config["mqtt_username"], + 'password':config["mqtt_password"] + } + ) + return flask.jsonify({"ok": True}) + +@application.route('/light//on/toggle', methods=['POST']) +def toggle_light(lid): + if lid in config["lights"]: + mqtt.publish.single( + config["lights"][lid]["mqtt_path"] + "/toggle", + "true", + qos=1, + hostname=config["mqtt_server"], + auth = { + 'username':config["mqtt_username"], + 'password':config["mqtt_password"] + } + ) + return flask.jsonify({"ok": True}) + +if __name__ == "__main__": + application.run(host='127.0.0.1',port=8000,debug=True) + diff --git a/static/index.html b/static/index.html index 218e2f2..903f3e3 100644 --- a/static/index.html +++ b/static/index.html @@ -108,14 +108,14 @@ id="l6" width="2.625" height="2.25" - x="7.4374995" + x="7.4375" y="6.375" ry="0.375" /> - +