"use strict";var servConn={_socket:null,_onConnChange:null,_onUpdate:null,_isConnected:!1,_disconnectedSince:null,_connCallbacks:{onConnChange:null,onUpdate:null,onRefresh:null,onAuth:null,onCommand:null,onError:null},_authInfo:null,_isAuthDone:!1,_isAuthRequired:!1,_authRunning:!1,_cmdQueue:[],_connTimer:null,_type:"socket.io",_timeout:0,_reconnectInterval:1e4,_reloadInterval:30,_cmdData:null,_cmdInstance:null,_isSecure:!1,_defaultMode:1604,_useStorage:!1,_objects:null,_enums:null,_autoSubscribe:!0,namespace:"vis.0",getType:function(){return this._type},getIsConnected:function(){return this._isConnected},getIsLoginRequired:function(){return this._isSecure},getUser:function(){return this._user},setReloadTimeout:function(e){this._reloadInterval=parseInt(e,10)},setReconnectInterval:function(e){this._reconnectInterval=parseInt(e,10)},_checkConnection:function(e,t){return this._isConnected?!this._queueCmdIfRequired(e,t)&&(null!==this._socket||(console.log("socket.io not initialized"),!1)):(console.log("No connection!"),!1)},_monitor:function(){if(!this._timer){var e=(new Date).getTime();this._reloadInterval&&e-this._lastTimer>1e3*this._reloadInterval?this.reload():this._lastTimer=e;var t=this;this._timer=setTimeout(function(){t._timer=null,t._monitor()},1e4)}},_onAuth:function(e,t){var n=this;this._isSecure=t,this._isSecure&&(n._lastTimer=(new Date).getTime(),this._monitor()),this._autoSubscribe&&this._socket.emit("subscribe","*"),e&&this._socket.emit("subscribeObjects","*"),!0!==this._isConnected&&(this._isConnected=!0,this._connCallbacks.onConnChange&&setTimeout(function(){n._socket.emit("authEnabled",function(e,t){n._user=t,n._connCallbacks.onConnChange(n._isConnected),"undefined"!=typeof app&&app.onConnChange(n._isConnected)})},0))},reconnect:function(e){var t=this;e.mayReconnect&&!e.mayReconnect()||this._connectInterval||(this._connectInterval=setInterval(function(){console.log("Trying connect..."),t._socket.connect(),t._countDown=Math.floor(t._reconnectInterval/1e3),"undefined"!=typeof $&&$(".splash-screen-text").html(t._countDown+"...").css("color","red")},this._reconnectInterval),this._countDown=Math.floor(this._reconnectInterval/1e3),"undefined"!=typeof $&&$(".splash-screen-text").html(this._countDown+"..."),this._countInterval=setInterval(function(){t._countDown--,"undefined"!=typeof $&&$(".splash-screen-text").html(t._countDown+"...")},1e3))},reload:function(){"yunkong2.net"===window.location.host||"yunkong2.biz"===window.location.host?window.location="/":window.location.reload()},init:function(n,t,i,e){var r=this;if("undefined"!=typeof socketNamespace&&(this.namespace=socketNamespace),(n=n||{}).name||(n.name=this.namespace),void 0!==e&&(this._autoSubscribe=e),(document.URL.split("/local/")[1]||"undefined"==typeof socketUrl&&!n.connLink||"undefined"!=typeof socketUrl&&"local"===socketUrl)&&(this._type="local"),"undefined"!=typeof session){var o=session.get("user");o&&(r._authInfo={user:o,hash:session.get("hash"),salt:session.get("salt")})}this._connCallbacks=t;var a=n.connLink||window.localStorage.getItem("connLink");if(a||"undefined"==typeof socketUrl||(a=socketUrl),n.socketSession||"undefined"==typeof socketSession||(n.socketSession=socketSession),void 0===n.socketForceWebSockets&&"undefined"!=typeof socketForceWebSockets&&(n.socketForceWebSockets=socketForceWebSockets),"local"===this._type)this._isConnected=!0,this._connCallbacks.onConnChange&&this._connCallbacks.onConnChange(this._isConnected),"undefined"!=typeof app&&app.onConnChange(this._isConnected);else if("undefined"!=typeof io){var s;if(n.socketSession=n.socketSession||"nokey",a?void 0!==(s=a)&&":"===a[0]&&(a=location.protocol+"//"+location.hostname+a):s=location.protocol+"//"+location.host,this._socket)try{this._socket.close(),this._socket=null}catch(e){console.log("Cannot close connection: "+e)}this._socket=io.connect(s,{query:"key="+n.socketSession,"reconnection limit":1e4,"max reconnection attempts":1/0,reconnection:!1,upgrade:!n.socketForceWebSockets,rememberUpgrade:n.socketForceWebSockets,transports:n.socketForceWebSockets?["websocket"]:void 0}),this._socket.on("connect",function(){if(r._disconnectedSince){var e=(new Date).getTime()-r._disconnectedSince;console.log("was offline for "+e/1e3+"s"),r._reloadInterval&&e>1e3*r._reloadInterval&&!r.authError&&r.reload(),r._disconnectedSince=null}r._connectInterval&&(clearInterval(r._connectInterval),r._connectInterval=null),r._countInterval&&(clearInterval(r._countInterval),r._countInterval=null);var t=document.getElementById("server-disconnect");t&&(t.style.display="none"),r._socket.emit("name",n.name),console.log((new Date).toISOString()+" Connected => authenticate"),setTimeout(function(){r.waitConnect=setTimeout(function(){console.error("No answer from server"),r.authError||r.reload()},6e3),r._socket.emit("authenticate",function(e,t){r.waitConnect&&(clearTimeout(r.waitConnect),r.waitConnect=null),console.log((new Date).toISOString()+" Authenticated: "+e),e?r._onAuth(i,t):console.log("permissionError")})},50)}),this._socket.on("reauthenticate",function(e){r._connCallbacks.onConnChange&&(r._connCallbacks.onConnChange(!1),"undefined"==typeof app||r.authError||app.onConnChange(!1)),console.warn("reauthenticate"),r.waitConnect&&(clearTimeout(r.waitConnect),r.waitConnect=null),t.onAuthError?r.authError||(r.authError=!0,t.onAuthError(e)):r.reload()}),this._socket.on("connect_error",function(){"undefined"!=typeof $&&$(".splash-screen-text").css("color","#002951"),r.reconnect(n)}),this._socket.on("disconnect",function(){if(r._disconnectedSince=(new Date).getTime(),r._isConnected=!1,r._connCallbacks.onConnChange)setTimeout(function(){var e=document.getElementById("server-disconnect");e&&(e.style.display=""),r._connCallbacks.onConnChange(r._isConnected),"undefined"!=typeof app&&app.onConnChange(r._isConnected)},5e3);else{var e=document.getElementById("server-disconnect");e&&(e.style.display="")}r.reconnect(n)}),this._socket.on("reconnect",function(){var e=(new Date).getTime()-r._disconnectedSince;console.log("was offline for "+e/1e3+"s"),r._reloadInterval&&e>1e3*r._reloadInterval&&r.reload()}),this._socket.on("objectChange",function(e,t){if(r._useStorage&&"undefined"!=typeof storage){var n=r._objects||storage.get("objects");n&&(t?n[e]=t:n[e]&&delete n[e],storage.set("objects",n))}r._connCallbacks.onObjectChange&&r._connCallbacks.onObjectChange(e,t)}),this._socket.on("stateChange",function(e,t){if(e&&null!==t&&"object"==typeof t)if(r._connCallbacks.onCommand&&e===r.namespace+".control.command"){if(t.ack)return;if(t.val&&"string"==typeof t.val&&"{"===t.val[0]&&"}"===t.val[t.val.length-1])try{t.val=JSON.parse(t.val)}catch(e){console.log("Command seems to be an object, but cannot parse it: "+t.val)}t.val&&t.val.instance?r._connCallbacks.onCommand(t.val.instance,t.val.command,t.val.data)&&r.setState(e,{val:"",ack:!0}):r._connCallbacks.onCommand(r._cmdInstance,t.val,r._cmdData)&&r.setState(e,{val:"",ack:!0})}else e===r.namespace+".control.data"?r._cmdData=t.val:e===r.namespace+".control.instance"?r._cmdInstance=t.val:r._connCallbacks.onUpdate&&r._connCallbacks.onUpdate(e,t)}),this._socket.on("permissionError",function(e){r._connCallbacks.onError?r._connCallbacks.onError(e):console.log("permissionError")}),this._socket.on("error",function(e){"Invalid password or user name"===e?(console.warn("reauthenticate"),r.waitConnect&&(clearTimeout(r.waitConnect),r.waitConnect=null),t.onAuthError?r.authError||(r.authError=!0,t.onAuthError(e)):r.reload()):(console.error("Socket error: "+e),"undefined"!=typeof $&&$(".splash-screen-text").css("color","#002951"),r.reconnect(n))})}},logout:function(e){this._isConnected?this._socket.emit("logout",e):console.log("No connection!")},getVersion:function(t){this._checkConnection("getVersion",arguments)&&this._socket.emit("getVersion",function(e){t&&t(e)})},subscribe:function(e,t){this._checkConnection("subscribe",arguments)&&this._socket.emit("subscribe",e,t)},unsubscribe:function(e,t){this._checkConnection("unsubscribe",arguments)&&this._socket.emit("unsubscribe",e,t)},_checkAuth:function(t){this._isConnected?null!==this._socket?this._socket.emit("getVersion",function(e){t&&t(e)}):console.log("socket.io not initialized"):console.log("No connection!")},readFile:function(i,r,e){if(!r)throw"No callback set";if("local"===this._type)try{var t=storage.get(i);r(null,t?JSON.parse(storage.get(i)):null)}catch(e){r(e,null)}else{if(!this._checkConnection("readFile",arguments))return;if(e||"undefined"==typeof app){var n=this.namespace;if("/"===i[0]){var o=i.split("/");n=o[1],o.splice(0,2),i=o.join("/")}this._socket.emit("readFile",n,i,function(e,t,n){setTimeout(function(){r(e,t,i,n)},0)})}else app.readLocalFile(i.replace(/^\/vis\.0\//,""),r)}},getMimeType:function(e){return-1!==e.indexOf(".")&&(e=e.toLowerCase().match(/\.[^.]+$/)),".css"===e?"text/css":".bmp"===e?"image/bmp":".png"===e?"image/png":".jpg"===e?"image/jpeg":".jpeg"===e?"image/jpeg":".gif"===e?"image/gif":".tif"===e?"image/tiff":".js"===e?"application/javascript":".html"===e?"text/html":".htm"===e?"text/html":".json"===e?"application/json":".xml"===e?"text/xml":".svg"===e?"image/svg+xml":".eot"===e?"application/vnd.ms-fontobject":".ttf"===e?"application/font-sfnt":".woff"===e?"application/font-woff":".wav"===e?"audio/wav":".mp3"===e?"audio/mpeg3":"text/javascript"},readFile64:function(i,r,e){var o=this;if(!r)throw"No callback set";if(this._checkConnection("readFile",arguments))if(e||"undefined"==typeof app){var t=this.namespace;if("/"===i[0]){var n=i.split("/");t=n[1],n.splice(0,2),i=n.join("/")}this._socket.emit("readFile64",t,i,function(e,t,n){setTimeout(function(){r(e,t?{mime:n||o.getMimeType(i),data:t}:{mime:n||o.getMimeType(i)},i)},0)})}else app.readLocalFile(i.replace(/^\/vis\.0\//,""),function(e,t,n){setTimeout(function(){t?r(e,{mime:n||o.getMimeType(i),data:btoa(t)},i):r(e,i)},0)})},writeFile:function(e,t,n,i){if("function"==typeof n&&(i=n,n=null),"local"===this._type)storage.set(e,JSON.stringify(t)),i&&i();else{if(!this._checkConnection("writeFile",arguments))return;"object"==typeof t&&(t=JSON.stringify(t,null,2));var r=e.split("/"),o=r[1];r.splice(0,2),"vis"===o?this._socket.emit("writeFile",o,r.join("/"),t,n?{mode:this._defaultMode}:{},i):this._socket.emit("writeFile",this.namespace,e,t,n?{mode:this._defaultMode}:{},i)}},writeFile64:function(e,t,n){if(this._checkConnection("writeFile64",arguments)){var i=e.split("/"),r=i[1];i.splice(0,2),this._socket.emit("writeFile",r,i.join("/"),atob(t),{mode:this._defaultMode},n)}},readDir:function(e,n){if(null!==this._socket){e||(e="/");var t=e.split("/"),i=t[1];t.splice(0,2),this._socket.emit("readDir",i,t.join("/"),{filter:!0},function(e,t){n&&n(e,t)})}else console.log("socket.io not initialized")},mkdir:function(e,t){var n=e.split("/"),i=n[1];n.splice(0,2),this._socket.emit("mkdir",i,n.join("/"),function(e){t&&t(e)})},unlink:function(e,t){var n=e.split("/"),i=n[1];n.splice(0,2),this._socket.emit("unlink",i,n.join("/"),function(e){t&&t(e)})},renameFile:function(e,t,n){var i=e.split("/"),r=i[1];i.splice(0,2);var o=t.split("/");o.splice(0,2),this._socket.emit("rename",r,i.join("/"),o.join("/"),function(e){n&&n(e)})},setState:function(e,t,n){null!==this._socket&&this._socket.emit("setState",e,t,n)},getStates:function(e,n){if("function"==typeof e&&(n=e,e=null),"local"===this._type)return n(null,[]);if(this._checkConnection("getStates",arguments)){this.gettingStates=this.gettingStates||0,this.gettingStates++,1