index(model) ::= <<
<!doctype html>
<html>
<style>
</style>
<head>
<title>Bespiel</title>
<link rel="stylesheet" href="base.css" />
<link rel="stylesheet" href="status.css" />
<script src="jquery.js"></script>
<script>
var localRevision = -1;
var runningAjaxRequest;
// call method
function callDeviceMethod(deviceId, methodId) {
jQuery.get('rest/devices/' + deviceId + '/methods/' + methodId);
}
// set attribute
function setDeviceAttribute(deviceId, attributeId, attributeName, currentValue) {
var newValue = prompt("Attributwert \"" + attributeName + "\" ändern:", currentValue);
if (newValue != null) {
jQuery.get('rest/devices/' + deviceId + '/attributes/' + attributeId + '/valueText?set=' + newValue);
}
}
// update all gui elements
function updateGui(attributeUpdates) {
var lastDeviceId = -1;
jQuery.each( attributeUpdates, function( index, updateItem ){
// device
var deviceSelectorKey = '#device' + updateItem.D;
// detect first attribute of a device
var firstAttributeOfDevice = (lastDeviceId != updateItem.D);
lastDeviceId = updateItem.D;
if (firstAttributeOfDevice) {
// color knob
var deviceKnob = jQuery(deviceSelectorKey + ' a span');
var deviceColorType = deviceKnob.data('devicecolortype');
var knobColor = "white";
switch (deviceColorType) {
case "RedGreen":
switch (updateItem.V) {
case 0:
knobColor = "red";
break;
case 1:
knobColor = "green";
break;
}
break;
case "RedYellowGreen":
switch (updateItem.V) {
case 0:
knobColor = "red";
break;
case 1:
knobColor = "yellow";
break;
case 2:
knobColor = "green";
break;
}
break;
}
deviceKnob.attr("class", knobColor + "Knob");
// device value (value of first attribute, if this attribute is not represented by color)
var deviceText = jQuery(deviceSelectorKey + ' a div');
deviceText.text(updateItem.T);
}
// attribute
var attributeSelectorKey = deviceSelectorKey + 'attribute' + updateItem.A;
// update attribute edit href
var obj = jQuery(attributeSelectorKey + ' a');
var oldHref = obj.attr("href");
var newHref = oldHref.replace(/(setDeviceAttribute\('\w+', '\w+', '.*?', ').+?('\))/, '\$1' + updateItem.T + '\$2');
obj.attr("href", newHref);
// update attribute value div
jQuery(attributeSelectorKey + ' a div').text(updateItem.T);
});
}
// server push by long polling (Comet)
function longPollAttributes(urlToPoll) {
runningAjaxRequest = jQuery.ajax({
url: urlToPoll,
type: "GET",
dataType : "json",
// Code to run if the request succeeds;
// the response is passed to the function
success: function( attributeUpdateMessage ) {
localRevision = attributeUpdateMessage.revisionNumber;
updateGui(attributeUpdateMessage.attributeUpdates);
jQuery("#connectionLostBox").hide();
// current request completed, just start another call
longPollAttributes("events/attributes?clientRev=" + localRevision); // long poll (wait for updates in relation to local revision)
},
error: function(xhr, error, errorThrown){
if (error != "abort") {
jQuery("#connectionLostBox").show();
longPollAttributes("rest/attributes"); // full reload (no long poll) after connection broken
}
}
});
}
// initial call (get all attributes for first display, after that do long polling)
longPollAttributes("rest/attributes");
jQuery(window).bind("beforeunload", function() {runningAjaxRequest.abort();});
</script>
</head>
<body>
<div class="header">
<ul>
<li>
<a href="/index.html" class="header-actyvE">Admin</a>
</li>
</ul>
</div>
<table border="0" cellpadding="40">
<tr align="left" valign="top">
$model.infrastructure.Groups:{group |
<td>
<h1 class="group">$group.Name$</h1>
$index_devices(group.Devices)$
$group.Groups:{group |
<h2 class="group">$group.Name$</h2>
$index_devices(group.Devices)$
$group.Groups:{group |
<h3 class="group">$group.Name$</h3>
$index_devices(group.Devices)$
$group.Groups:{group |
<h4 class="group">$group.Name$</h4>
$index_devices(group.Devices)$
$group.Groups:{group |
<h5 class="group">$group.Name$</h5>
$index_devices(group.Devices)$
}$
}$
}$
}$
</td>
}$
</tr>
</table>
<div class="graybox-ontop" id="connectionLostBox">
<div class="error-box">
<div class="msg">Fehler – Keine Verbindung!</div>
</div>
</div>
</body>
</html>
>>