109 lines
3.0 KiB
Python
109 lines
3.0 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'
|
|
|
|
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:
|
|
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))
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
#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())
|
|
|
|
factory = Site(root, logPath='laundryServer.log')
|
|
|
|
reactor.listenTCP(8080, factory)
|
|
reactor.run()
|
|
|