initial
This commit is contained in:
commit
041b2c13d6
78
LaundryServer.py
Normal file
78
LaundryServer.py
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
from twisted.internet import reactor
|
||||||
|
from twisted.web.server import Site
|
||||||
|
from twisted.web.resource import Resource
|
||||||
|
from twisted.web.static import File
|
||||||
|
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
state = ['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x']
|
||||||
|
|
||||||
|
|
||||||
|
def on_message(client, userdata, msg):
|
||||||
|
j = json.loads(msg.payload)
|
||||||
|
ss = j['data']['switchStates']
|
||||||
|
for s in ss:
|
||||||
|
if s['feedbackState'] == 0:
|
||||||
|
state[s['index']] = 'aus'
|
||||||
|
elif s['state'] == 1:
|
||||||
|
state[s['index']] = 'an'
|
||||||
|
else:
|
||||||
|
state[s['index']] = 'unbekannt'
|
||||||
|
|
||||||
|
|
||||||
|
def send_message(target, switchState):
|
||||||
|
t = {'kitchen':0, 'oven':1, 'laundry':2}
|
||||||
|
# command = "switch %s %s" % [target, switchState]
|
||||||
|
command = "switch " + str(t[target]) + " " + str(switchState)
|
||||||
|
mqttClient.publish('IoT/Command/RelayBox', command)
|
||||||
|
|
||||||
|
class HelloWorld(Resource):
|
||||||
|
isLeaf = True
|
||||||
|
|
||||||
|
def render_GET(self, request):
|
||||||
|
return "Hello world!"
|
||||||
|
|
||||||
|
|
||||||
|
class Switch(Resource):
|
||||||
|
isLeaf = True
|
||||||
|
|
||||||
|
def render_GET(self, request):
|
||||||
|
target = request.args['target'][0]
|
||||||
|
switchState = request.args['state'][0]
|
||||||
|
send_message(target, switchState)
|
||||||
|
return "OK"
|
||||||
|
|
||||||
|
|
||||||
|
class Status(Resource):
|
||||||
|
isLeaf = False
|
||||||
|
|
||||||
|
def __init__(self, target):
|
||||||
|
self.target = target
|
||||||
|
|
||||||
|
def render_GET(self, request):
|
||||||
|
t = {'kitchen':0, 'oven':1, 'laundry':2}
|
||||||
|
return state[t[self.target]]
|
||||||
|
|
||||||
|
|
||||||
|
mqttClient = mqtt.Client()
|
||||||
|
mqttClient.on_message = on_message
|
||||||
|
mqttClient.connect("mqttbroker", 1883, 60)
|
||||||
|
mqttClient.subscribe("IoT/Status/RelayBox")
|
||||||
|
mqttClient.loop_start()
|
||||||
|
|
||||||
|
root = Resource()
|
||||||
|
root.putChild("", File("index.html"))
|
||||||
|
root.putChild("Hello", HelloWorld())
|
||||||
|
root.putChild("switch", Switch())
|
||||||
|
root.putChild("oven", Status("oven"))
|
||||||
|
root.putChild("kitchen", Status("kitchen"))
|
||||||
|
root.putChild("laundry", Status("laundry"))
|
||||||
|
|
||||||
|
factory = Site(root, logPath='/tmp/laundryServer.log')
|
||||||
|
|
||||||
|
reactor.listenTCP(8080, factory)
|
||||||
|
reactor.run()
|
||||||
|
|
104
index.html
Normal file
104
index.html
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
|
||||||
|
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
|
||||||
|
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
|
||||||
|
<script>
|
||||||
|
$("document").ready(function() {
|
||||||
|
$("#target").hide();
|
||||||
|
$("#button").click(function() {
|
||||||
|
var $v = "nix";
|
||||||
|
if ($("#flip-checkbox-1").is(":checked")) {
|
||||||
|
$v = "on";
|
||||||
|
} else {
|
||||||
|
$v = "off";
|
||||||
|
}
|
||||||
|
var $w = $("#target").text();
|
||||||
|
$.ajax({
|
||||||
|
url: 'http://172.16.2.15:8080/switch?target=' + $w + '&state=' + $v
|
||||||
|
});
|
||||||
|
// $("#bla").text($v + $w);
|
||||||
|
});
|
||||||
|
$(".switchlink").click(function() {
|
||||||
|
var $v = $(this).attr("id");
|
||||||
|
var $w = $("span:first", this).text();
|
||||||
|
$("#dialogTitle").text($w);
|
||||||
|
$("#target").text($v);
|
||||||
|
});
|
||||||
|
(function worker1() {
|
||||||
|
$.ajax({
|
||||||
|
url: 'http://172.16.2.15:8080/kitchen',
|
||||||
|
success: function(data) {
|
||||||
|
$('#stateKueche').text(data);
|
||||||
|
},
|
||||||
|
complete: function() {
|
||||||
|
// Schedule the next request when the current one's complete
|
||||||
|
setTimeout(worker1, 1000);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
(function worker2() {
|
||||||
|
$.ajax({
|
||||||
|
url: 'http://172.16.2.15:8080/oven',
|
||||||
|
success: function(data) {
|
||||||
|
$('#stateHerd').text(data);
|
||||||
|
},
|
||||||
|
complete: function() {
|
||||||
|
// Schedule the next request when the current one's complete
|
||||||
|
setTimeout(worker2, 1000);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
(function worker3() {
|
||||||
|
$.ajax({
|
||||||
|
url: 'http://172.16.2.15:8080/laundry',
|
||||||
|
success: function(data) {
|
||||||
|
$('#stateWaschkueche').text(data);
|
||||||
|
},
|
||||||
|
complete: function() {
|
||||||
|
// Schedule the next request when the current one's complete
|
||||||
|
setTimeout(worker3, 1000);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
<style>
|
||||||
|
.red { background-color: red; }
|
||||||
|
.green { background-color: green; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div data-role="page" id="mainpage">
|
||||||
|
<div data-role="header">
|
||||||
|
<h1>RelayBox</h1>
|
||||||
|
</div>
|
||||||
|
<div data-role="main" class="ui-content">
|
||||||
|
<ul data-role="listview" data-inset="true" data-count-theme="b">
|
||||||
|
<li><a class="switchlink" id="kitchen" href="#dialog?switch=kitchen"><span>Küche Rest</span> <span id="stateKueche" class="ui-li-count">on</span></a></li>
|
||||||
|
<li><a class="switchlink" id="oven" href="#dialog?switch=oven"><span>Herd</span> <span id="stateHerd" class="ui-li-count">on</span></a></li>
|
||||||
|
<li><a class="switchlink" id="laundry" href="#dialog?switch=laundry"><span>Waschküche</span> <span id="stateWaschkueche" class="ui-li-count">off</span></a></li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div data-role="dialog" id="dialog">
|
||||||
|
<div data-role="header">
|
||||||
|
<h1 id="dialogTitle">Switching</h1>
|
||||||
|
</div>
|
||||||
|
<div data-role="main" class="ui-content">
|
||||||
|
<center>
|
||||||
|
<input name="flip-checkbox-1" id="flip-checkbox-1" type="checkbox" data-role="flipswitch">
|
||||||
|
<span id="target">x</span>
|
||||||
|
</center>
|
||||||
|
<a href="#mainpage" id="button" data-role="button" data-rel="back">Ok</a>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user