diff --git a/LaundryServer.py b/LaundryServer.py
index 36f2e4a..cb0c5ba 100644
--- a/LaundryServer.py
+++ b/LaundryServer.py
@@ -1,77 +1,107 @@
+import sys
+from time import strftime, gmtime
+
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
+#import paho.mqtt.client as mqtt
+#import json
+
+logfile = 'laundry.log'
+
+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'
-state = ['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x']
+class MyResource(Resource):
+ def log(self, m):
+ t = strftime("%d %b %Y %H:%M:%S", gmtime())
+ with open(logfile, 'a') as f:
+ f.write("%s %s\n" % (t, m))
+ def send_message(self, target, switchState):
+ try:
+ 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)
+ self.log(command)
+ except ValueError, e:
+ self.log("ValueError in send_message: " + str(e))
+
+
-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):
+class HelloWorld(MyResource):
isLeaf = True
def render_GET(self, request):
return "Hello world!"
-class Switch(Resource):
+class SwitchCommand(MyResource):
isLeaf = True
def render_GET(self, request):
- target = request.args['target'][0]
- switchState = request.args['state'][0]
- send_message(target, switchState)
- return "OK"
+ res = "ERR"
+ try:
+ target = request.args['target'][0]
+ switchState = request.args['state'][0]
+ self.send_message(target, switchState)
+ res = "OK"
+ except KeyError, e:
+ self.log("KeyError in SwitchCommand, render_GET: " + str(e))
+ return res
-class Status(Resource):
+class SwitchStatus(MyResource):
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]]
+ state = "ERR"
+ try:
+ target = request.args['target'][0]
+ t = int(target)
+ state = loadedSwitchStates[t]
+ except ValueError, e:
+ self.log("ValueError in SwitchStatus, render_GET: " + str(e))
+ except KeyError, e:
+ self.log("KeyError in SwitchStatus, render_GET: " + str(e))
+ except IndexError, e:
+ self.log("KeyError in SwitchStatus, render_GET: " + str(e))
+ return state
-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"))
root.putChild("Hello", HelloWorld())
-root.putChild("switch", Switch())
-root.putChild("oven", Status("oven"))
-root.putChild("kitchen", Status("kitchen"))
-root.putChild("laundry", Status("laundry"))
+root.putChild("switchCommand", SwitchCommand())
+root.putChild("switchStatus", SwitchStatus())
-factory = Site(root, logPath='/tmp/laundryServer.log')
+factory = Site(root, logPath='laundryServer.log')
reactor.listenTCP(8080, factory)
reactor.run()
diff --git a/index.html b/index.html
index 546c66b..7d3d124 100644
--- a/index.html
+++ b/index.html
@@ -7,62 +7,53 @@