hyperion.ng/assets/webconfig/js/hyperion.js

316 lines
9.9 KiB
JavaScript
Raw Normal View History

// global vars (read and write in window object)
window.webPrio = 1;
window.webOrigin = "Web Configuration";
window.showOptHelp = true;
2019-06-23 00:45:28 +02:00
window.gitHubReleaseApiUrl = "https://api.github.com/repos/hyperion-project/hyperion.ng/releases";
window.currentChannel = null;
window.currentVersion = null;
window.latestVersion = null;
window.latestStableVersion = null;
window.latestBetaVersion = null;
window.gitHubVersionList = null;
window.serverInfo = {};
window.serverSchema = {};
window.serverConfig = {};
window.schema = {};
window.sysInfo = {};
window.jsonPort = 19444;
window.websocket = null;
window.hyperion = {};
window.wsTan = 1;
window.ledStreamActive = false;
window.imageStreamActive = false;
window.loggingStreamActive = false;
window.loggingHandlerInstalled = false;
window.watchdog = 0;
window.debugMessagesActive = true;
window.wSess = [];
window.comps = [];
function initRestart()
{
$(window.hyperion).off();
requestServerConfigReload();
window.watchdog = 10;
connectionLostDetection('restart');
}
function connectionLostDetection(type)
{
if ( window.watchdog > 2 )
{
var interval_id = window.setInterval("", 9999); // Get a reference to the last
for (var i = 1; i < interval_id; i++)
window.clearInterval(i);
if(type == 'restart')
{
$("body").html($("#container_restart").html());
// setTimeout delay for probably slower systems, some browser don't execute THIS action
setTimeout(restartAction,250);
}
else
{
$("body").html($("#container_connection_lost").html());
connectionLostAction();
}
}
else
{
$.get( "/cgi/cfg_jsonserver", function() {window.watchdog=0}).fail(function() {window.watchdog++;});
}
}
2016-08-31 00:22:17 +02:00
setInterval(connectionLostDetection, 3000);
// init websocket to hyperion and bind socket events to jquery events of $(hyperion) object
2018-12-27 23:11:32 +01:00
function initWebSocket()
{
if ("WebSocket" in window)
{
if (window.websocket == null)
{
window.jsonPort = (document.location.port == '') ? '80' : document.location.port;
window.websocket = new WebSocket('ws://'+document.location.hostname+":"+window.jsonPort);
2018-12-27 23:11:32 +01:00
window.websocket.onopen = function (event) {
$(window.hyperion).trigger({type:"open"});
$(window.hyperion).on("cmd-serverinfo", function(event) {
window.watchdog = 0;
});
};
window.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";
}
$(window.hyperion).trigger({type:"close", reason:reason});
window.watchdog = 10;
connectionLostDetection();
};
window.websocket.onmessage = function (event) {
try
{
var response = JSON.parse(event.data);
var success = response.success;
var cmd = response.command;
2018-12-27 23:11:32 +01:00
if (success || typeof(success) == "undefined")
{
$(window.hyperion).trigger({type:"cmd-"+cmd, response:response});
}
else
{
var error = response.hasOwnProperty("error")? response.error : "unknown";
$(window.hyperion).trigger({type:"error",reason:error});
console.log("[window.websocket::onmessage] "+error)
}
}
catch(exception_error)
{
$(window.hyperion).trigger({type:"error",reason:exception_error});
console.log("[window.websocket::onmessage] "+exception_error)
}
};
window.websocket.onerror = function (error) {
$(window.hyperion).trigger({type:"error",reason:error});
console.log("[window.websocket::onerror] "+error)
};
}
}
else
{
$(window.hyperion).trigger("error");
alert("Websocket is not supported by your browser");
return;
}
}
2016-08-31 00:22:17 +02:00
function sendToHyperion(command, subcommand, msg)
{
if (typeof subcommand != 'undefined' && subcommand.length > 0)
subcommand = ',"subcommand":"'+subcommand+'"';
else
subcommand = "";
if (typeof msg != 'undefined' && msg.length > 0)
msg = ","+msg;
else
msg = "";
window.websocket.send(encode_utf8('{"command":"'+command+'", "tan":'+window.wsTan+subcommand+msg+'}'));
}
// -----------------------------------------------------------
// wrapped server commands
2016-08-31 00:22:17 +02:00
// also used for watchdog
2016-12-05 23:16:14 +01:00
function requestServerInfo()
{
sendToHyperion("serverinfo","",'"subscribe":["components-update","sessions-update","priorities-update", "imageToLedMapping-update", "adjustment-update", "videomode-update", "effects-update", "settings-update"]');
}
function requestSysInfo()
{
sendToHyperion("sysinfo");
}
2016-12-05 23:16:14 +01:00
function requestServerConfigSchema()
{
sendToHyperion("config","getschema");
}
2016-12-05 23:16:14 +01:00
function requestServerConfig()
{
sendToHyperion("config", "getconfig");
}
2016-12-05 23:16:14 +01:00
function requestServerConfigReload()
{
sendToHyperion("config", "reload");
}
2016-12-05 23:16:14 +01:00
function requestLedColorsStart()
{
window.ledStreamActive=true;
sendToHyperion("ledcolors", "ledstream-start");
}
2016-12-05 23:16:14 +01:00
function requestLedColorsStop()
{
window.ledStreamActive=false;
sendToHyperion("ledcolors", "ledstream-stop");
}
function requestLedImageStart()
{
window.imageStreamActive=true;
sendToHyperion("ledcolors", "imagestream-start");
}
function requestLedImageStop()
{
window.imageStreamActive=false;
sendToHyperion("ledcolors", "imagestream-stop");
}
function requestPriorityClear(prio)
2016-12-05 23:16:14 +01:00
{
if(typeof prio !== 'number')
prio = window.webPrio;
sendToHyperion("clear", "", '"priority":'+prio+'');
}
function requestClearAll()
{
2018-12-27 23:11:32 +01:00
requestPriorityClear(-1)
}
function requestPlayEffect(effectName, duration)
{
sendToHyperion("effect", "", '"effect":{"name":"'+effectName+'"},"priority":'+window.webPrio+',"duration":'+validateDuration(duration)+',"origin":"'+window.webOrigin+'"');
}
function requestSetColor(r,g,b,duration)
{
sendToHyperion("color", "", '"color":['+r+','+g+','+b+'], "priority":'+window.webPrio+',"duration":'+validateDuration(duration)+',"origin":"'+window.webOrigin+'"');
}
function requestSetImage(data,width,height,duration)
{
sendToHyperion("image", "", '"imagedata":"'+data+'", "imagewidth":'+width+',"imageheight":'+height+', "priority":'+window.webPrio+',"duration":'+validateDuration(duration)+'');
}
2016-12-05 23:16:14 +01:00
function requestSetComponentState(comp, state)
{
var state_str = state ? "true" : "false";
sendToHyperion("componentstate", "", '"componentstate":{"component":"'+comp+'","state":'+state_str+'}');
}
function requestSetSource(prio)
{
if ( prio == "auto" )
sendToHyperion("sourceselect", "", '"auto":true');
else
sendToHyperion("sourceselect", "", '"priority":'+prio);
}
2016-08-31 00:22:17 +02:00
function requestWriteConfig(config, full)
{
if(full === true)
window.serverConfig = config;
else
{
jQuery.each(config, function(i, val) {
window.serverConfig[i] = val;
});
}
sendToHyperion("config","setconfig", '"config":'+JSON.stringify(window.serverConfig));
}
function requestWriteEffect(effectName,effectPy,effectArgs,data)
{
var cutArgs = effectArgs.slice(1, -1);
sendToHyperion("create-effect", "", '"name":"'+effectName+'", "script":"'+effectPy+'", '+cutArgs+',"imageData":"'+data+'"');
}
function requestTestEffect(effectName,effectPy,effectArgs,data)
2016-12-05 23:16:14 +01:00
{
sendToHyperion("effect", "", '"effect":{"name":"'+effectName+'", "args":'+effectArgs+'}, "priority":'+window.webPrio+', "origin":"'+window.webOrigin+'", "pythonScript":"'+effectPy+'", "imageData":"'+data+'"');
}
function requestDeleteEffect(effectName)
{
sendToHyperion("delete-effect", "", '"name":"'+effectName+'"');
}
function requestLoggingStart()
{
window.loggingStreamActive=true;
sendToHyperion("logging", "start");
}
function requestLoggingStop()
{
window.loggingStreamActive=false;
sendToHyperion("logging", "stop");
}
function requestMappingType(type)
{
sendToHyperion("processing", "", '"mappingType": "'+type+'"');
}
function requestVideoMode(newMode)
{
sendToHyperion("videomode", "", '"videoMode": "'+newMode+'"');
}
function requestAdjustment(type, value, complete)
{
if(complete === true)
sendToHyperion("adjustment", "", '"adjustment": '+type+'');
else
sendToHyperion("adjustment", "", '"adjustment": {"'+type+'": '+value+'}');
}