fix after packaging
parent
175ae7a58b
commit
c70e2d7a73
|
@ -9,6 +9,12 @@ import yaml
|
|||
import re
|
||||
import json
|
||||
|
||||
from pkg_resources import resource_string, resource_filename, cleanup_resources
|
||||
import atexit
|
||||
|
||||
# remove temporary files created by resource_filename
|
||||
atexit.register(cleanup_resources, force=True)
|
||||
|
||||
secrets_path = Path(os.environ.get("LIGHTS_WEB_SECRETS", 'secrets.yaml'))
|
||||
config_path = Path(os.environ.get("LIGHTS_WEB_CONFIG", 'config.yaml'))
|
||||
config = yaml.safe_load(config_path.read_text())
|
||||
|
@ -17,37 +23,35 @@ config.update(yaml.safe_load(secrets_path.read_text()))
|
|||
c = threading.Condition()
|
||||
application = flask.Flask(__name__)
|
||||
application.msg="ab"
|
||||
application.lights = [False, False, False, False]
|
||||
application.lights = { lid: False for lid in config['lights'] }
|
||||
|
||||
#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(config['mqtt_rw_username'], config['mqtt_rw_password'])
|
||||
#mc.connect("10.8.1.16")
|
||||
#mc.on_message = on_message
|
||||
#mc.subscribe("devices/#")
|
||||
#mc.loop_start()
|
||||
def on_message(client,userdata,message):
|
||||
c.acquire()
|
||||
for lid, cfg in config['lights']:
|
||||
if cfg.mqtt_path == message.topic:
|
||||
if message.payload == "true":
|
||||
application.lights[lid] = True
|
||||
elif message.payload == "false":
|
||||
application.lights[lid] = False
|
||||
|
||||
c.release()
|
||||
|
||||
|
||||
def on_connect(client, userdata, rc):
|
||||
for lid, cfg in config['lights']:
|
||||
client.subscribe(cfg['mqtt_path'])
|
||||
|
||||
mc = mqtt.client.Client()
|
||||
mc.username_pw_set(config['mqtt_rw_username'], config['mqtt_rw_password'])
|
||||
mc.on_connect = on_connect
|
||||
mc.on_message = on_message
|
||||
mc.connect(config["mqtt_server"])
|
||||
mc.loop_start()
|
||||
|
||||
|
||||
@application.route("/")
|
||||
def main():
|
||||
return flask.send_file('static/index.html')
|
||||
return flask.send_file(resource_filename(__name__, 'static/index.html'))
|
||||
|
||||
@application.route("/status")
|
||||
def status():
|
||||
|
@ -93,6 +97,10 @@ def toggle_light(lid):
|
|||
publish(config["lights"][lid]["mqtt_path"] + "/toggle", "true")
|
||||
return flask.jsonify({"ok": True})
|
||||
|
||||
if __name__ == "__main__":
|
||||
def run_development():
|
||||
application.run(host='127.0.0.1',port=8000,debug=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_development()
|
||||
|
||||
|
|
Loading…
Reference in New Issue