Files
RelayBoxWebApp/LaundryServer.py
Wolfgang Hottgenroth eed7bc8d26 add new switches
2015-06-10 17:21:59 +02:00

129 lines
3.6 KiB
Python

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
logfile = 'laundry.log'
switchMapping = {
'oven': { 'index': 1, 'label': 'Herd' },
'laundry': { 'index': 2, 'label': 'Waschkueche' },
'kitchen': { 'index': 0, 'label': 'Kueche' },
'lightbasem': { 'index': 3, 'label': 'Licht Keller' },
'light1flr': { 'index': 4, 'label': 'Licht EG' },
'light2flr': { 'index': 5, 'label': 'Licht OG' },
}
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'
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:
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 %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))
class HelloWorld(MyResource):
isLeaf = True
def render_GET(self, request):
return "Hello world!"
class SwitchCommand(MyResource):
isLeaf = True
def render_GET(self, request):
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 SwitchStatus(MyResource):
isLeaf = False
def render_GET(self, request):
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
class SwitchMapping(MyResource):
isLeaf = False
def render_GET(self, request):
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()
root = Resource()
root.putChild("", File("index.html"))
root.putChild("Hello", HelloWorld())
root.putChild("switchCommand", SwitchCommand())
root.putChild("switchStatus", SwitchStatus())
root.putChild("switchMapping", SwitchMapping())
factory = Site(root, logPath='laundryServer.log')
reactor.listenTCP(8080, factory)
reactor.run()