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

View File

@ -7,18 +7,15 @@
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script>
$("document").ready(function() {
var serverAddress = '127.0.0.1:8080';
$.ajaxSetup({
async: false
});
$("#flip-checkbox-1").flipswitch({"defaults": true});
var switchMapping = $.getJSON('http://' + serverAddress + '/switchMapping').responseJSON;
var switchMapping = $.getJSON('/switchMapping').responseJSON;
var firstListItem = $("li:first", "#switchlist");
for (var key in switchMapping) {
var newListItem = firstListItem.clone()
var href = newListItem.children('a')
href.attr('id', 'switch' + key);
href.attr('id', key);
href.attr('href', '#dialog?switch=' + switchMapping[key]['index'])
var span = newListItem.find("#statusEmpty0");
span.attr('id', 'state' + key);
@ -32,8 +29,9 @@
$("#button").click(function() {
var v = ($("#flip-checkbox-1").is(":checked")) ? 1 : 0;
var w = switchMapping[$("#target").text()]['index'];
console.debug('y:' + v + ', ' + w);
$.ajax({
url: 'http://' + serverAddress + '/switchCommand?target=' + w + '&state=' + v
url: '/switchCommand?target=' + w + '&state=' + v
});
});
$(".switchlink").click(function() {
@ -43,7 +41,7 @@
$("#dialogTitle").text(w);
var x = $("span:last", this).text();
console.debug(x);
if (x == 'on') {
if (x == 'an') {
console.debug('x:on');
$("#flip-checkbox-1").prop('checked', true);
} else {
@ -55,7 +53,7 @@
function updateState() {
for (var key in switchMapping) {
$.ajax({
url: 'http://' + serverAddress + '/switchStatus?target=' + switchMapping[key]['index'],
url: '/switchStatus?target=' + switchMapping[key]['index'],
success: function(data) {
$('#state' + key).text(data);
}