This commit is contained in:
hg
2015-06-01 22:26:00 +02:00
parent b127096c98
commit 19628552ee
2 changed files with 27 additions and 27 deletions

View File

@ -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"))