2019-06-05 18:19:08 +02:00
// 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" ;
2019-07-08 15:04:38 +02:00
window . currentChannel = null ;
2019-06-05 18:19:08 +02:00
window . currentVersion = null ;
window . latestVersion = null ;
2019-06-20 21:02:37 +02:00
window . latestStableVersion = null ;
window . latestBetaVersion = null ;
2020-02-19 17:29:11 +01:00
window . latestAlphaVersion = null ;
window . latestRcVersion = null ;
2019-06-22 21:30:17 +02:00
window . gitHubVersionList = null ;
2019-06-05 18:19:08 +02:00
window . serverInfo = { } ;
window . serverSchema = { } ;
window . serverConfig = { } ;
window . schema = { } ;
window . sysInfo = { } ;
2019-07-18 22:32:06 +02:00
window . jsonPort = 8090 ;
2019-06-05 18:19:08 +02:00
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 = [ ] ;
2019-07-14 22:43:22 +02:00
window . currentHyperionInstance = 0 ;
window . currentHyperionInstanceName = "?" ;
2019-06-05 18:19:08 +02:00
window . comps = [ ] ;
2019-09-17 21:33:46 +02:00
window . defaultPasswordIsSet = null ;
2019-07-12 16:54:26 +02:00
tokenList = { } ;
2016-12-21 18:24:03 +01:00
function initRestart ( )
{
2019-06-05 18:19:08 +02:00
$ ( window . hyperion ) . off ( ) ;
2016-12-21 18:24:03 +01:00
requestServerConfigReload ( ) ;
2019-06-05 18:19:08 +02:00
window . watchdog = 10 ;
2017-01-17 21:53:35 +01:00
connectionLostDetection ( 'restart' ) ;
2016-12-21 18:24:03 +01:00
}
2017-01-17 21:53:35 +01:00
function connectionLostDetection ( type )
2017-01-15 10:32:09 +01:00
{
2019-06-05 18:19:08 +02:00
if ( window . watchdog > 2 )
2016-09-06 10:14:54 +02:00
{
2020-05-25 21:51:11 +02:00
var interval _id = window . setInterval ( function ( ) { clearInterval ( interval _id ) ; } , 9999 ) ; // Get a reference to the last
2016-09-06 10:14:54 +02:00
for ( var i = 1 ; i < interval _id ; i ++ )
window . clearInterval ( i ) ;
2017-01-17 21:53:35 +01:00
if ( type == 'restart' )
{
2017-09-04 23:12:59 +02:00
$ ( "body" ) . html ( $ ( "#container_restart" ) . html ( ) ) ;
// setTimeout delay for probably slower systems, some browser don't execute THIS action
setTimeout ( restartAction , 250 ) ;
2017-01-17 21:53:35 +01:00
}
else
{
$ ( "body" ) . html ( $ ( "#container_connection_lost" ) . html ( ) ) ;
connectionLostAction ( ) ;
}
2016-09-06 10:14:54 +02:00
}
2017-01-15 10:32:09 +01:00
else
{
2019-06-05 18:19:08 +02:00
$ . get ( "/cgi/cfg_jsonserver" , function ( ) { window . watchdog = 0 } ) . fail ( function ( ) { window . watchdog ++ ; } ) ;
2017-01-15 10:32:09 +01:00
}
2016-09-03 15:54:33 +02:00
}
2016-08-31 00:22:17 +02:00
2017-01-15 10:32:09 +01:00
setInterval ( connectionLostDetection , 3000 ) ;
2016-12-21 18:24:03 +01:00
2016-09-03 15:54:33 +02:00
// init websocket to hyperion and bind socket events to jquery events of $(hyperion) object
2018-12-27 23:11:32 +01:00
2016-09-03 15:54:33 +02:00
function initWebSocket ( )
{
if ( "WebSocket" in window )
{
2019-06-05 18:19:08 +02:00
if ( window . websocket == null )
2016-09-03 15:54:33 +02:00
{
2019-06-05 18:19:08 +02:00
window . jsonPort = ( document . location . port == '' ) ? '80' : document . location . port ;
2019-08-21 16:09:28 +02:00
window . websocket = ( document . location . protocol == "https:" ) ? new WebSocket ( 'wss://' + document . location . hostname + ":" + window . jsonPort ) : new WebSocket ( 'ws://' + document . location . hostname + ":" + window . jsonPort ) ;
2018-12-27 23:11:32 +01:00
2019-06-05 18:19:08 +02:00
window . websocket . onopen = function ( event ) {
$ ( window . hyperion ) . trigger ( { type : "open" } ) ;
2017-11-20 00:06:45 +01:00
2019-06-05 18:19:08 +02:00
$ ( window . hyperion ) . on ( "cmd-serverinfo" , function ( event ) {
window . watchdog = 0 ;
2017-11-20 00:06:45 +01:00
} ) ;
} ;
2019-06-05 18:19:08 +02:00
window . websocket . onclose = function ( event ) {
2017-11-20 00:06:45 +01:00
// 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" ;
}
2019-06-05 18:19:08 +02:00
$ ( window . hyperion ) . trigger ( { type : "close" , reason : reason } ) ;
window . watchdog = 10 ;
2017-11-20 00:06:45 +01:00
connectionLostDetection ( ) ;
} ;
2019-06-05 18:19:08 +02:00
window . websocket . onmessage = function ( event ) {
2017-11-20 00:06:45 +01:00
try
{
2019-06-05 18:19:08 +02:00
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" )
2016-09-03 15:54:33 +02:00
{
2019-06-05 18:19:08 +02:00
$ ( window . hyperion ) . trigger ( { type : "cmd-" + cmd , response : response } ) ;
2016-09-03 15:54:33 +02:00
}
2017-11-20 00:06:45 +01:00
else
2016-09-03 15:54:33 +02:00
{
2019-06-05 18:19:08 +02:00
var error = response . hasOwnProperty ( "error" ) ? response . error : "unknown" ;
$ ( window . hyperion ) . trigger ( { type : "error" , reason : error } ) ;
2019-08-21 16:10:35 +02:00
console . log ( "[window.websocket::onmessage] " , error )
2016-09-03 15:54:33 +02:00
}
2017-11-20 00:06:45 +01:00
}
catch ( exception _error )
{
2019-06-05 18:19:08 +02:00
$ ( window . hyperion ) . trigger ( { type : "error" , reason : exception _error } ) ;
2019-08-21 16:10:35 +02:00
console . log ( "[window.websocket::onmessage] " , exception _error )
2017-11-20 00:06:45 +01:00
}
} ;
2019-06-05 18:19:08 +02:00
window . websocket . onerror = function ( error ) {
$ ( window . hyperion ) . trigger ( { type : "error" , reason : error } ) ;
2019-08-21 16:10:35 +02:00
console . log ( "[window.websocket::onerror] " , error )
2017-11-20 00:06:45 +01:00
} ;
2016-09-03 15:54:33 +02:00
}
}
else
{
2019-06-05 18:19:08 +02:00
$ ( window . hyperion ) . trigger ( "error" ) ;
2016-09-03 15:54:33 +02:00
alert ( "Websocket is not supported by your browser" ) ;
return ;
}
}
2016-08-31 00:22:17 +02:00
2016-12-05 11:10:44 +01:00
function sendToHyperion ( command , subcommand , msg )
{
if ( typeof subcommand != 'undefined' && subcommand . length > 0 )
subcommand = ',"subcommand":"' + subcommand + '"' ;
else
subcommand = "" ;
2016-09-06 10:14:54 +02:00
2016-12-05 11:10:44 +01:00
if ( typeof msg != 'undefined' && msg . length > 0 )
msg = "," + msg ;
else
msg = "" ;
2019-08-13 23:22:06 +02:00
window . websocket . send ( '{"command":"' + command + '", "tan":' + window . wsTan + subcommand + msg + '}' ) ;
2016-12-05 11:10:44 +01:00
}
2016-09-06 10:14:54 +02:00
2016-09-03 15:54:33 +02:00
// -----------------------------------------------------------
// wrapped server commands
2016-08-31 00:22:17 +02:00
2019-09-17 21:33:46 +02:00
// Test if admin requires authentication
function requestRequiresAdminAuth ( )
2019-07-12 16:54:26 +02:00
{
2019-09-17 21:33:46 +02:00
sendToHyperion ( "authorize" , "adminRequired" ) ;
}
// Test if the default password needs to be changed
function requestRequiresDefaultPasswortChange ( )
{
sendToHyperion ( "authorize" , "newPasswordRequired" ) ;
}
// Change password
function requestChangePassword ( oldPw , newPw )
{
sendToHyperion ( "authorize" , "newPassword" , '"password": "' + oldPw + '", "newPassword":"' + newPw + '"' ) ;
}
function requestAuthorization ( password )
{
sendToHyperion ( "authorize" , "login" , '"password": "' + password + '"' ) ;
}
function requestTokenAuthorization ( token )
{
sendToHyperion ( "authorize" , "login" , '"token": "' + token + '"' ) ;
2019-07-12 16:54:26 +02:00
}
function requestToken ( comment )
{
sendToHyperion ( "authorize" , "createToken" , '"comment": "' + comment + '"' ) ;
}
function requestTokenInfo ( )
{
sendToHyperion ( "authorize" , "getTokenList" , "" ) ;
}
2020-03-26 17:59:41 +01:00
function requestGetPendingTokenRequests ( id , state ) {
sendToHyperion ( "authorize" , "getPendingTokenRequests" , "" ) ;
}
2019-07-12 16:54:26 +02:00
function requestHandleTokenRequest ( id , state )
{
sendToHyperion ( "authorize" , "answerRequest" , '"id":"' + id + '", "accept":' + state ) ;
}
function requestTokenDelete ( id )
{
sendToHyperion ( "authorize" , "deleteToken" , '"id":"' + id + '"' ) ;
}
2019-08-04 20:19:33 +02:00
function requestInstanceRename ( inst , name )
{
sendToHyperion ( "instance" , "saveName" , '"instance": ' + inst + ', "name": "' + name + '"' ) ;
}
2019-07-14 22:43:22 +02:00
function requestInstanceStartStop ( inst , start )
{
if ( start )
sendToHyperion ( "instance" , "startInstance" , '"instance": ' + inst ) ;
else
sendToHyperion ( "instance" , "stopInstance" , '"instance": ' + inst ) ;
}
function requestInstanceDelete ( inst )
{
sendToHyperion ( "instance" , "deleteInstance" , '"instance": ' + inst ) ;
}
function requestInstanceCreate ( name )
{
sendToHyperion ( "instance" , "createInstance" , '"name": "' + name + '"' ) ;
}
function requestInstanceSwitch ( inst )
{
sendToHyperion ( "instance" , "switchTo" , '"instance": ' + inst ) ;
}
2016-12-05 23:16:14 +01:00
function requestServerInfo ( )
{
2019-07-14 22:43:22 +02:00
sendToHyperion ( "serverinfo" , "" , '"subscribe":["components-update","sessions-update","priorities-update", "imageToLedMapping-update", "adjustment-update", "videomode-update", "effects-update", "settings-update", "instance-update"]' ) ;
2016-09-03 15:54:33 +02:00
}
2016-08-30 23:25:27 +02:00
2017-03-04 22:17:42 +01:00
function requestSysInfo ( )
{
sendToHyperion ( "sysinfo" ) ;
}
2016-12-05 23:16:14 +01:00
function requestServerConfigSchema ( )
{
2016-12-05 11:10:44 +01:00
sendToHyperion ( "config" , "getschema" ) ;
2016-09-03 15:54:33 +02:00
}
2016-12-05 23:16:14 +01:00
function requestServerConfig ( )
{
2016-12-05 11:10:44 +01:00
sendToHyperion ( "config" , "getconfig" ) ;
2016-09-05 17:26:29 +02:00
}
2016-12-05 23:16:14 +01:00
function requestServerConfigReload ( )
{
2016-12-05 11:10:44 +01:00
sendToHyperion ( "config" , "reload" ) ;
2016-09-15 20:42:58 +02:00
}
2016-12-05 23:16:14 +01:00
function requestLedColorsStart ( )
{
2019-06-05 18:19:08 +02:00
window . ledStreamActive = true ;
2016-12-05 11:10:44 +01:00
sendToHyperion ( "ledcolors" , "ledstream-start" ) ;
2016-09-05 17:26:29 +02:00
}
2016-12-05 23:16:14 +01:00
function requestLedColorsStop ( )
{
2019-06-05 18:19:08 +02:00
window . ledStreamActive = false ;
2016-12-05 11:10:44 +01:00
sendToHyperion ( "ledcolors" , "ledstream-stop" ) ;
2016-09-06 10:14:54 +02:00
}
2016-12-18 19:00:14 +01:00
function requestLedImageStart ( )
{
2019-06-05 18:19:08 +02:00
window . imageStreamActive = true ;
2016-12-18 19:00:14 +01:00
sendToHyperion ( "ledcolors" , "imagestream-start" ) ;
}
function requestLedImageStop ( )
{
2019-06-05 18:19:08 +02:00
window . imageStreamActive = false ;
2016-12-18 19:00:14 +01:00
sendToHyperion ( "ledcolors" , "imagestream-stop" ) ;
}
2017-01-17 21:53:35 +01:00
function requestPriorityClear ( prio )
2016-12-05 23:16:14 +01:00
{
2017-01-17 21:53:35 +01:00
if ( typeof prio !== 'number' )
2019-06-05 18:19:08 +02:00
prio = window . webPrio ;
2017-01-17 21:53:35 +01:00
sendToHyperion ( "clear" , "" , '"priority":' + prio + '' ) ;
2016-09-03 15:54:33 +02:00
}
2017-03-24 10:17:36 +01:00
function requestClearAll ( )
{
2018-12-27 23:11:32 +01:00
requestPriorityClear ( - 1 )
2017-03-24 10:17:36 +01:00
}
2017-02-28 17:53:41 +01:00
function requestPlayEffect ( effectName , duration )
2017-09-04 23:12:59 +02:00
{
2019-06-05 18:19:08 +02:00
sendToHyperion ( "effect" , "" , '"effect":{"name":"' + effectName + '"},"priority":' + window . webPrio + ',"duration":' + validateDuration ( duration ) + ',"origin":"' + window . webOrigin + '"' ) ;
2017-02-28 17:53:41 +01:00
}
function requestSetColor ( r , g , b , duration )
2017-09-04 23:12:59 +02:00
{
2019-06-05 18:19:08 +02:00
sendToHyperion ( "color" , "" , '"color":[' + r + ',' + g + ',' + b + '], "priority":' + window . webPrio + ',"duration":' + validateDuration ( duration ) + ',"origin":"' + window . webOrigin + '"' ) ;
2016-09-03 15:54:33 +02:00
}
2019-08-21 16:10:35 +02:00
function requestSetImage ( data , duration , name )
2017-09-04 23:12:59 +02:00
{
2019-08-21 16:10:35 +02:00
sendToHyperion ( "image" , "" , '"imagedata":"' + data + '", "priority":' + window . webPrio + ',"duration":' + validateDuration ( duration ) + ', "format":"auto", "origin":"' + window . webOrigin + '", "name":"' + name + '"' ) ;
2016-09-03 15:54:33 +02:00
}
2016-12-05 23:16:14 +01:00
function requestSetComponentState ( comp , state )
{
2019-06-05 18:19:08 +02:00
var state _str = state ? "true" : "false" ;
2016-12-05 11:10:44 +01:00
sendToHyperion ( "componentstate" , "" , '"componentstate":{"component":"' + comp + '","state":' + state _str + '}' ) ;
2016-09-03 15:54:33 +02:00
}
2016-09-09 10:15:35 +02:00
function requestSetSource ( prio )
2016-09-03 15:54:33 +02:00
{
if ( prio == "auto" )
2016-12-05 11:10:44 +01:00
sendToHyperion ( "sourceselect" , "" , '"auto":true' ) ;
2016-09-03 15:54:33 +02:00
else
2016-12-05 11:10:44 +01:00
sendToHyperion ( "sourceselect" , "" , '"priority":' + prio ) ;
2016-09-03 15:54:33 +02:00
}
2016-08-31 00:22:17 +02:00
2017-01-17 21:53:35 +01:00
function requestWriteConfig ( config , full )
2016-09-09 10:15:35 +02:00
{
2017-01-17 21:53:35 +01:00
if ( full === true )
2019-06-05 18:19:08 +02:00
window . serverConfig = config ;
2017-01-17 21:53:35 +01:00
else
{
jQuery . each ( config , function ( i , val ) {
2019-06-05 18:19:08 +02:00
window . serverConfig [ i ] = val ;
2017-01-17 21:53:35 +01:00
} ) ;
}
2016-12-05 11:10:44 +01:00
2019-06-05 18:19:08 +02:00
sendToHyperion ( "config" , "setconfig" , '"config":' + JSON . stringify ( window . serverConfig ) ) ;
2016-09-09 10:15:35 +02:00
}
2016-10-30 17:54:38 +01:00
2019-01-06 19:49:56 +01:00
function requestWriteEffect ( effectName , effectPy , effectArgs , data )
2016-10-30 17:54:38 +01:00
{
var cutArgs = effectArgs . slice ( 1 , - 1 ) ;
2019-01-06 19:49:56 +01:00
sendToHyperion ( "create-effect" , "" , '"name":"' + effectName + '", "script":"' + effectPy + '", ' + cutArgs + ',"imageData":"' + data + '"' ) ;
2016-10-30 22:59:45 +01:00
}
2019-01-06 19:49:56 +01:00
function requestTestEffect ( effectName , effectPy , effectArgs , data )
2016-12-05 23:16:14 +01:00
{
2019-06-05 18:19:08 +02:00
sendToHyperion ( "effect" , "" , '"effect":{"name":"' + effectName + '", "args":' + effectArgs + '}, "priority":' + window . webPrio + ', "origin":"' + window . webOrigin + '", "pythonScript":"' + effectPy + '", "imageData":"' + data + '"' ) ;
2016-10-30 22:59:45 +01:00
}
2016-11-18 18:39:27 +01:00
2016-12-05 11:10:44 +01:00
function requestDeleteEffect ( effectName )
{
sendToHyperion ( "delete-effect" , "" , '"name":"' + effectName + '"' ) ;
2016-11-18 18:39:27 +01:00
}
2016-11-26 22:46:16 +01:00
2016-12-05 11:10:44 +01:00
function requestLoggingStart ( )
{
2019-06-05 18:19:08 +02:00
window . loggingStreamActive = true ;
2016-12-05 11:10:44 +01:00
sendToHyperion ( "logging" , "start" ) ;
2016-11-26 22:46:16 +01:00
}
2016-12-05 11:10:44 +01:00
function requestLoggingStop ( )
{
2019-06-05 18:19:08 +02:00
window . loggingStreamActive = false ;
2016-12-05 11:10:44 +01:00
sendToHyperion ( "logging" , "stop" ) ;
2016-11-26 22:46:16 +01:00
}
2016-12-21 18:24:03 +01:00
function requestMappingType ( type )
{
sendToHyperion ( "processing" , "" , '"mappingType": "' + type + '"' ) ;
}
2017-09-04 23:12:59 +02:00
function requestVideoMode ( newMode )
{
sendToHyperion ( "videomode" , "" , '"videoMode": "' + newMode + '"' ) ;
}
2017-02-08 14:36:28 +01:00
function requestAdjustment ( type , value , complete )
{
if ( complete === true )
sendToHyperion ( "adjustment" , "" , '"adjustment": ' + type + '' ) ;
2017-09-04 23:12:59 +02:00
else
2017-02-08 14:36:28 +01:00
sendToHyperion ( "adjustment" , "" , '"adjustment": {"' + type + '": ' + value + '}' ) ;
}