commit 041b2c13d689b23cd302a93c0af2bafa77851268 Author: Wolfgang Hottgenroth Date: Mon Jun 1 00:51:45 2015 -0700 initial diff --git a/.hgignore b/.hgignore new file mode 100644 index 0000000..da61a25 --- /dev/null +++ b/.hgignore @@ -0,0 +1,3 @@ +syntax: glob +*.pyc + diff --git a/LaundryServer.py b/LaundryServer.py new file mode 100644 index 0000000..36f2e4a --- /dev/null +++ b/LaundryServer.py @@ -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() + diff --git a/index.html b/index.html new file mode 100644 index 0000000..7c79a9a --- /dev/null +++ b/index.html @@ -0,0 +1,104 @@ + + + + + + + + + + + + +
+
+

RelayBox

+
+ +
+ +
+
+

Switching

+
+
+
+ + x +
+ Ok + +
+
+ + + +