refactoring of webui and event based websocket layer (#219)

* make hyperion websocket api event based

* implement new websocket handling for generalconf

* migrate all webui stuff to new event based websocket api
some cleanup ... now all html templates are in content
refactoring of web stuff

* add hyperionport to global
start impl. removing advanced key

* separate dashboard
serverinfo is updated every 3 seconds automatily
add input selection
cleanup and remove not needed stuff

* prepare infrastructure for server sided file execution

* webui minor fixes

* fix compile
This commit is contained in:
redPanther
2016-09-03 15:54:33 +02:00
committed by GitHub
parent c17c3bd273
commit 4a27f3d43e
35 changed files with 842 additions and 865 deletions

View File

@@ -30,50 +30,130 @@ var parsedServerInfoJSON;
var parsedUpdateJSON;
var parsedConfSchemaJSON;
var hyperionport = 19444;
var websocket = null;
var hyperion = {};
var wsTan = 1;
var cronId = 0;
function button_reloaddata(){
hyperionport = $("#json_port").val();
loaddata();
};
//
function cron()
{
requestServerInfo();
}
function loaddata() {
// init websocket to hyperion and bind socket events to jquery events of $(hyperion) object
function initWebSocket()
{
if ("WebSocket" in window)
{
if (websocket == null)
{
$.ajax({ url: "/cgi/cfg_jsonserver" }).done(function(data) {
hyperionport = data.substr(1);
websocket = new WebSocket('ws://'+document.location.hostname+data);
webSocket = new WebSocket('ws://'+document.location.hostname+':'+hyperionport);
websocket.onopen = function (event) {
$(hyperion).trigger({type:"open"});
cronId = window.setInterval(cron,3000);
};
webSocket.onerror = function(event) {
$('#con_error_modal').modal('show');
};
webSocket.onopen = function(event) {
webSocket.send('{"command":"serverinfo"}');
};
webSocket.onmessage = function(response){
parsedServerInfoJSON = JSON.parse(response.data );
currentVersion = parsedServerInfoJSON.info.hyperion[0].version;
cleanCurrentVersion = currentVersion.replace(/\./g, '');
// get active led device
var leddevice = parsedServerInfoJSON.info.ledDevices.active;
$('#dash_leddevice').html(leddevice);
// get host
var hostname = parsedServerInfoJSON.info.hostname;
$('#dash_systeminfo').html(hostname+':'+hyperionport);
$.get( "https://raw.githubusercontent.com/hyperion-project/hyperion.ng/master/version.json", function( data ) {
parsedUpdateJSON = JSON.parse(data);
latestVersion = parsedUpdateJSON[0].versionnr;
cleanLatestVersion = latestVersion.replace(/\./g, '');
$('#currentversion').html(' V'+currentVersion);
$('#latestversion').html(' V'+latestVersion);
if ( cleanCurrentVersion < cleanLatestVersion ) {
$('#versioninforesult').html('<div lang="en" data-lang-token="dashboard_message_infobox_updatewarning" style="margin:0px;" class="alert alert-warning">A newer version of Hyperion is available!</div>');
websocket.onclose = function (event) {
// See http://tools.ietf.org/html/rfc6455#section-7.4.1
var reason;
switch(event.code)
{
case 1000: reason = "Normal closure, meaning that the purpose for which the connection was established has been fulfilled."; break;
case 1001: reason = "An endpoint is \"going away\", such as a server going down or a browser having navigated away from a page."; break;
case 1002: reason = "An endpoint is terminating the connection due to a protocol error"; break;
case 1003: reason = "An endpoint is terminating the connection because it has received a type of data it cannot accept (e.g., an endpoint that understands only text data MAY send this if it receives a binary message)."; break;
case 1004: reason = "Reserved. The specific meaning might be defined in the future."; break;
case 1005: reason = "No status code was actually present."; break;
case 1006: reason = "The connection was closed abnormally, e.g., without sending or receiving a Close control frame"; break;
case 1007: reason = "An endpoint is terminating the connection because it has received data within a message that was not consistent with the type of the message (e.g., non-UTF-8 [http://tools.ietf.org/html/rfc3629] data within a text message)."; break;
case 1008: reason = "An endpoint is terminating the connection because it has received a message that \"violates its policy\". This reason is given either if there is no other sutible reason, or if there is a need to hide specific details about the policy."; break;
case 1009: reason = "An endpoint is terminating the connection because it has received a message that is too big for it to process."; break;
case 1010: reason = "An endpoint (client) is terminating the connection because it has expected the server to negotiate one or more extension, but the server didn't return them in the response message of the WebSocket handshake. <br /> Specifically, the extensions that are needed are: " + event.reason; break;
case 1011: reason = "A server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request."; break;
case 1015: reason = "The connection was closed due to a failure to perform a TLS handshake (e.g., the server certificate can't be verified)."; break;
default: reason = "Unknown reason";
}
else{
$('#versioninforesult').html('<div lang="en" data-lang-token="dashboard_message_infobox_updatesuccess" style="margin:0px;" class="alert alert-success">You run the latest version of Hyperion.</div>');
$(hyperion).trigger({type:"close", reason:reason});
};
websocket.onmessage = function (event) {
try
{
response = JSON.parse(event.data);
success = response.success;
cmd = response.command;
if (success)
{
$(hyperion).trigger({type:"cmd-"+cmd, response:response});
}
else
{
error = response.hasOwnProperty("error")? response.error : "unknown";
$(hyperion).trigger({type:"error",reason:error});
console.log("[websocket::onmessage] "+error)
}
}
catch(exception_error)
{
$(hyperion).trigger({type:"error",reason:exception_error});
console.log("[websocket::onmessage] "+exception_error)
}
};
websocket.onerror = function (error) {
$(hyperion).trigger({type:"error",reason:error});
console.log("[websocket::onerror] "+error)
};
});
}
}
else
{
$(hyperion).trigger("error");
alert("Websocket is not supported by your browser");
return;
}
}
// -----------------------------------------------------------
// wrapped server commands
function requestServerInfo() {
websocket.send('{"command":"serverinfo", "tan":'+wsTan+'}');
}
function requestServerConfigSchema() {
websocket.send('{"command":"config", "tan":'+wsTan+',"subcommand":"getschema"}');
}
function requestPriorityClear() {
websocket.send('{"command":"clear", "tan":'+wsTan+', "priority":1}');
}
function requestPlayEffect(effectName) {
websocket.send('{"command":"effect", "tan":'+wsTan+',"effect":{"name":"'+effectName+'"},"priority":1}');
}
function requestSetColor(r,g,b) {
websocket.send('{"command":"color", "tan":'+wsTan+', "color":['+r+','+g+','+b+'], "priority":1}');
}
function requestSetComponentState(comp, state){
state_str = state?"true":"false";
websocket.send('{"command":"componentstate", "tan":'+wsTan+',"componentstate":{"component":"'+comp+'","state":'+state_str+'}}');
console.log(comp+' state: '+state_str);
}
function requestSetSource( prio )
{
if ( prio == "auto" )
websocket.send('{"command":"sourceselect", "tan":'+wsTan+', "auto" : true}');
else
websocket.send('{"command":"sourceselect", "tan":'+wsTan+', "priority" : '+prio+'}');
}
};
};

View File

@@ -0,0 +1,12 @@
function bindNavToContent(containerId, fileName, loadNow=false)
{
$("#page-wrapper").off();
$(containerId).on("click", function() {
$("#page-wrapper").load("/content/"+fileName+".html");
});
if (loadNow)
{
$("#page-wrapper").load("/content/"+fileName+".html");
}
}