initial version flask with active status polling
This commit is contained in:
commit
69a443c8a8
2 changed files with 163 additions and 0 deletions
62
app.py
Normal file
62
app.py
Normal file
|
@ -0,0 +1,62 @@
|
|||
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.render_template('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)
|
||||
|
||||
@application.route('/light/<int:lid>/on/set/true', methods=['POST'])
|
||||
def set_light(lid):
|
||||
mqtt.publish.single("devices/light_904649/light_" + str(lid) + "/on/set", "true", qos=1, hostname="10.8.1.16", auth = {'username':"light", 'password':"MG4Dhp6vCZjgbTzJ"})
|
||||
return "Succes"
|
||||
|
||||
@application.route('/light/<int:lid>/on/toggle', methods=['POST'])
|
||||
def toggle_light(lid):
|
||||
mqtt.publish.single("devices/light_904649/light_" + str(lid) + "/on/toggle", "true", qos=1, hostname="10.8.1.16", auth = {'username':"light", 'password':"MG4Dhp6vCZjgbTzJ"})
|
||||
return "Succes"
|
||||
|
||||
if __name__ == "__main__":
|
||||
application.run(host='127.0.0.1',port=8000,debug=True)
|
||||
|
101
templates/index.html
Normal file
101
templates/index.html
Normal file
|
@ -0,0 +1,101 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>title</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<script src="script.js"></script>
|
||||
</head>
|
||||
<h1 style='color:blue'>Warsaw Hackespace lights switching panel</h1>
|
||||
<svg height="240" width="240">
|
||||
<rect
|
||||
y="20"
|
||||
x="19.999992"
|
||||
height="25"
|
||||
width="50"
|
||||
id="rect4145"
|
||||
style="opacity:1;fill:none;fill-opacity:1;stroke:#000000;stroke-width:1;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
|
||||
<rect
|
||||
y="115"
|
||||
x="114.99999"
|
||||
height="10"
|
||||
width="10"
|
||||
id="rect4149"
|
||||
style="opacity:1;fill:none;fill-opacity:1;stroke:#000000;stroke-width:1;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
|
||||
<path
|
||||
id="path4151"
|
||||
d="m 124.99999,120 95,0"
|
||||
style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" />
|
||||
<path
|
||||
id="path4153"
|
||||
d="m 119.99999,115 0,-40"
|
||||
style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" />
|
||||
<path
|
||||
id="path4155"
|
||||
d="m 119.99999,60 0,-40"
|
||||
style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" />
|
||||
<path
|
||||
id="path4157"
|
||||
d="m 99.999992,20 -80,0 0,200 199.999998,0 0,-200 -105,0"
|
||||
style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" />
|
||||
<path
|
||||
d="m 133.14724,67.221494 a 15,15 0 0 1 -13.17405,7.778482"
|
||||
id="path4161"
|
||||
style="opacity:1;fill:none;fill-opacity:1;stroke:#000000;stroke-width:1;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
|
||||
<path
|
||||
id="path4163"
|
||||
d="M 133.25892,67.767857 119.99999,60"
|
||||
style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" />
|
||||
<path
|
||||
transform="matrix(0,-1,-1,0,0,0)"
|
||||
style="opacity:1;fill:none;fill-opacity:1;stroke:#000000;stroke-width:1;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
|
||||
id="path4165"
|
||||
d="m -7.0534165,-107.93734 a 15,15 0 0 1 -13.1740505,7.77848" />
|
||||
<path
|
||||
style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
|
||||
d="m 107.39099,6.941734 7.76786,13.258928"
|
||||
id="path4167" />
|
||||
<rect
|
||||
ry="5"
|
||||
y="30"
|
||||
x="29.999992"
|
||||
height="80"
|
||||
width="180"
|
||||
id="rec_light_1"
|
||||
style="opacity:0.5;fill:#ffff00;fill-opacity:0.50196078;stroke:#000000;stroke-width:1;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
|
||||
<rect
|
||||
style="opacity:0.5;fill:#000000;fill-opacity:0.50196078;stroke:#000000;stroke-width:1;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
|
||||
id="rec_light_2"
|
||||
width="180"
|
||||
height="80"
|
||||
x="29.999992"
|
||||
y="130"
|
||||
ry="5" />
|
||||
</svg>
|
||||
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
//document.getElementById("btn_l1").onclick=function(){ $.post("http://localhost/light/1/on/toggle"); };
|
||||
//document.getElementById("btn_l2").onclick=function(){ $.post("http://localhost/light/2/on/toggle"); };
|
||||
//document.getElementById("btn_l3").onclick=function(){ $.post("http://localhost/light/3/on/toggle"); };
|
||||
//document.getElementById("btn_l4").onclick=function(){ $.post("http://localhost/light/4/on/toggle"); };
|
||||
document.getElementById("rec_light_1").onclick=function(){ $.post("http://localhost/light/3/on/toggle"); };
|
||||
document.getElementById("rec_light_2").onclick=function(){ $.post("http://localhost/light/4/on/toggle"); };
|
||||
var updateStatus = function() {
|
||||
$.getJSON("/status", function(data) {
|
||||
if (data.lights[2] == true) {
|
||||
document.getElementById("rec_light_1").style.fill = "#ffff00";
|
||||
} else {
|
||||
document.getElementById("rec_light_1").style.fill = "#000000";
|
||||
}
|
||||
if (data.lights[3] == true) {
|
||||
document.getElementById("rec_light_2").style.fill = "#ffff00";
|
||||
} else {
|
||||
document.getElementById("rec_light_2").style.fill = "#000000";
|
||||
}
|
||||
});
|
||||
};
|
||||
updateStatus();
|
||||
setInterval(updateStatus, 100);});
|
||||
</script>
|
||||
</body>
|
Loading…
Reference in a new issue