refactoring

This commit is contained in:
2015-06-01 17:03:23 +02:00
parent 69e8361ede
commit 777e225d0b
2 changed files with 119 additions and 96 deletions

View File

@ -7,62 +7,53 @@
<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';
var switchMapping = {
'kitchen': { 'index': 0, 'label': 'Kueche' },
'oven': { 'index': 1, 'label': 'Herd' },
'laundry': { 'index': 2, 'label': 'Waschkueche' },
};
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('href', '#dialog?switch=' + switchMapping[key]['index'])
var span = newListItem.find("#statusEmpty0");
span.attr('id', 'state' + key);
span = newListItem.find("#labelEmpty0");
span.attr('id', 'label' + key);
span.text(switchMapping[key]['label']);
newListItem.appendTo("#switchlist");
}
firstListItem.hide();
$("#target").hide();
$("#button").click(function() {
var $v = "nix";
if ($("#flip-checkbox-1").is(":checked")) {
$v = "on";
} else {
$v = "off";
}
var $w = $("#target").text();
var v = ($("#flip-checkbox-1").is(":checked")) ? 1 : 0;
var w = switchMapping[$("#target").text()]['index'];
$.ajax({
url: 'http://172.16.2.15:8080/switch?target=' + $w + '&state=' + $v
url: 'http://' + serverAddress + '/switchCommand?target=' + w + '&state=' + v
});
// $("#bla").text($v + $w);
});
$(".switchlink").click(function() {
var $v = $(this).attr("id");
var $w = $("span:first", this).text();
$("#dialogTitle").text($w);
$("#target").text($v);
var v = $(this).attr("id");
var w = $("span:first", this).text();
$("#dialogTitle").text(w);
$("#target").text(v);
});
(function worker1() {
$.ajax({
url: 'http://172.16.2.15:8080/kitchen',
success: function(data) {
$('#stateKueche').text(data);
},
complete: function() {
// Schedule the next request when the current one's complete
setTimeout(worker1, 1000);
}
});
})();
(function worker2() {
$.ajax({
url: 'http://172.16.2.15:8080/oven',
success: function(data) {
$('#stateHerd').text(data);
},
complete: function() {
// Schedule the next request when the current one's complete
setTimeout(worker2, 1000);
}
});
})();
(function worker3() {
$.ajax({
url: 'http://172.16.2.15:8080/laundry',
success: function(data) {
$('#stateWaschkueche').text(data);
},
complete: function() {
// Schedule the next request when the current one's complete
setTimeout(worker3, 1000);
}
});
})();
function updateState() {
for (var key in switchMapping) {
$.ajax({
async:false,
url: 'http://' + serverAddress + '/switchStatus?target=' + switchMapping[key]['index'],
success: function(data) {
$('#state' + key).text(data);
}
});
}
setTimeout(updateState, 1000);
}
updateState();
});
</script>
<style>
@ -77,10 +68,12 @@
<h1>RelayBox</h1>
</div>
<div data-role="main" class="ui-content">
<ul data-role="listview" data-inset="true" data-count-theme="b">
<li><a class="switchlink" id="kitchen" href="#dialog?switch=kitchen"><span>K&uuml;che Rest</span> <span id="stateKueche" class="ui-li-count">on</span></a></li>
<li><a class="switchlink" id="oven" href="#dialog?switch=oven"><span>Herd</span> <span id="stateHerd" class="ui-li-count">on</span></a></li>
<li><a class="switchlink" id="laundry" href="#dialog?switch=laundry"><span>Waschk&uuml;che</span> <span id="stateWaschkueche" class="ui-li-count">off</span></a></li>
<ul id="switchlist" data-role="listview" data-inset="true" data-count-theme="b">
<li><a class="switchlink" id="itemEmpty0" href="#dialog?switch=0"><span id="labelEmpty0">Vorlage</span> <span id="statusEmpty0" class="ui-li-count">on</span></a></li>
<!--
<li><a class="switchlink" id="oven" href="#dialog?switch=1"><span>Herd</span> <span id="stateHerd" class="ui-li-count">on</span></a></li>
<li><a class="switchlink" id="laundry" href="#dialog?switch=2"><span>Waschk&uuml;che</span> <span id="stateWaschkueche" class="ui-li-count">off</span></a></li>
-->
</ul>
</div>
</div>