diff --git a/LaundryServer.py b/LaundryServer.py index c0a2740..ff61e4b 100644 --- a/LaundryServer.py +++ b/LaundryServer.py @@ -6,7 +6,7 @@ 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 paho.mqtt.client as mqtt import json logfile = 'laundry.log' @@ -18,19 +18,19 @@ switchMapping = { } -loadedSwitchStates = ['on', 'on', 'off', 'x3', 'x4', 'x5', 'x6', 'x7'] +loadedSwitchStates = ['x0', 'x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7'] -#def on_message(client, userdata, msg): -# j = json.loads(msg.payload) -# ss = j['data']['switchStates'] -# for s in ss: -# if s['feedbackState'] == 0: -# loadedSwitchStates[s['index']] = 'aus' -# elif s['state'] == 1: -# loadedSwitchStates[s['index']] = 'an' -# else: -# loadedSwitchStates[s['index']] = 'unbekannt' +def on_message(client, userdata, msg): + j = json.loads(msg.payload) + ss = j['data']['switchStates'] + for s in ss: + if s['feedbackState'] == 0: + loadedSwitchStates[s['index']] = 'aus' + elif s['state'] == 1: + loadedSwitchStates[s['index']] = 'an' + else: + loadedSwitchStates[s['index']] = 'unbekannt' @@ -42,14 +42,16 @@ class MyResource(Resource): def send_message(self, target, switchState): try: + cs = ['off', 'on'] t = int(target) s = int(switchState) if s not in [0, 1]: raise ValueError('illegal switchState ' + switchState) if t < 0 or t >= len(loadedSwitchStates): raise ValueError('illegal target ' + target) - command = "switch %d %d" % (t, s) - #mqttClient.publish('IoT/Command/RelayBox', command) + command = "switch %d %s" % (t, cs[s]) + mqttClient.publish('IoT/Command/RelayBox', command) + #mqttClient.publish('IoT/x', command) self.log(command) except ValueError, e: self.log("ValueError in send_message: " + str(e)) @@ -103,11 +105,11 @@ class SwitchMapping(MyResource): return json.dumps(switchMapping,indent=2, separators=(',', ': ')) -#mqttClient = mqtt.Client() -#mqttClient.on_message = on_message -#mqttClient.connect("mqttbroker", 1883, 60) -#mqttClient.subscribe("IoT/Status/RelayBox") -#mqttClient.loop_start() +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")) diff --git a/index.html b/index.html index 6909c26..bbdc5d5 100644 --- a/index.html +++ b/index.html @@ -7,18 +7,15 @@