-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhomegear-ws-1.0.13.min.js
1 lines (1 loc) · 7.53 KB
/
homegear-ws-1.0.13.min.js
1
function HomegearWS(e,t,i,o,n,s,r){this.host="string"!=typeof e?"localhost":e,this.port=void 0!==t?t:"2001",this.id=void 0!==i?i:"HomegearWS",this.id.length<10&&(this.id+="-"+this.getGuid()),this.ssl=void 0!==o&&("true"===o||!0===o),this.auth=!!n,this.user=void 0!==n?n:void 0,this.password=void 0!==s&&""!==s?s:void 0,this.client=null,this.authenticated=!this.auth,this.onEvent=Array(),this.onConnected=Array(),this.onDisconnected=Array(),this.onReconnected=Array(),this.onReady=Array(),this.onError=Array(),this.peers=Array(),this.enabled=!1,this.wasConnected=!1,this.messageCounter=1,this.requests={},this.connectTimer=null,this.reconnectAttempts=0,this.pingTimer=null,this.log=void 0!==r&&r}homegearWsSetTimeout=function(e,t){var i=this,o=Array.prototype.slice.call(arguments,2);return setTimeout(e instanceof Function?function(){e.apply(i,o)}:e,t)},homegearWsSetInterval=function(e,t){var i=this,o=Array.prototype.slice.call(arguments,2);return setInterval(e instanceof Function?function(){e.apply(i,o)}:e,t)},HomegearWS.prototype.getCounter=function(){return this.messageCounter++},HomegearWS.prototype.connect=function(){console.log("Connecting (My ID: "+this.id+")..."),this.disconnect(),this.enabled=!0,this.connectClient()},HomegearWS.prototype.disconnect=function(){this.enabled=!1,this.client&&(this.client.close(),this.client=null)},HomegearWS.prototype.connected=function(e){"function"==typeof e&&this.onConnected.push(e)},HomegearWS.prototype.invokeConnected=function(){for(i in console.log("Connected."),this.onConnected)"function"==typeof this.onConnected[i]&&this.onConnected[i]()},HomegearWS.prototype.disconnected=function(e){"function"==typeof e&&this.onDisconnected.push(e)},HomegearWS.prototype.invokeDisconnected=function(){for(i in console.log("Disconnected."),this.onDisconnected)"function"==typeof this.onDisconnected[i]&&this.onDisconnected[i]()},HomegearWS.prototype.reconnected=function(e){"function"==typeof e&&this.onReconnected.push(e)},HomegearWS.prototype.invokeReconnected=function(){for(i in console.log("Reconnected."),this.onReconnected)"function"==typeof this.onReconnected[i]&&this.onReconnected[i]()},HomegearWS.prototype.error=function(e){"function"==typeof e&&this.onError.push(e)},HomegearWS.prototype.invokeError=function(e){if(void 0!==e)for(i in console.log("Error: "+e),this.onError)"function"==typeof this.onError[i]&&this.onError[i](e)},HomegearWS.prototype.ready=function(e){"function"==typeof e&&this.onReady.push(e)},HomegearWS.prototype.invokeReady=function(){for(i in console.log("Ready."),this.onReady)"function"==typeof this.onReady[i]&&this.onReady[i]()},HomegearWS.prototype.event=function(e){"function"==typeof e&&this.onEvent.push(e)},HomegearWS.prototype.invokeEvent=function(e){for(i in this.log&&console.log("Event:",e),this.onEvent)"function"==typeof this.onEvent[i]&&this.onEvent[i](e)},HomegearWS.prototype.connectClient=function(){this.reconnectAttempts++,this.client=new WebSocket((this.ssl?"wss://":"ws://")+this.host+(this.port?":"+this.port:"")+"/"+this.id,"server2"),this.client.onmessage=function(e){packet=JSON.parse(e.data),"auth"in packet?"success"==packet.auth?(console.log("Authenticated."),this.authenticated=!0,this.subscribePeers()):this.invokeError("Authentication failed."):void 0!==packet.method?(this.log&&console.log("RPC call from Homegear: ",packet),response={},this.client.send(JSON.stringify(response)),this.invokeEvent(packet)):void 0!==packet.id?(this.log&&console.log("Response to id "+packet.id+" received: ",packet),"function"==typeof this.requests["c"+packet.id]&&(this.requests["c"+packet.id](packet),delete this.requests["c"+packet.id])):console.log("Unknown packet received: ",packet)}.bind(this),this.client.onopen=function(e){this.auth?(request={user:this.user,password:this.password},this.send(JSON.stringify(request))):this.subscribePeers(),this.invokeConnected(),this.wasConnected&&this.invokeReconnected(),this.reconnectAttempts=0,this.wasConnected=!0,this.pingTimer=homegearWsSetInterval.call(this,this.pingClient,15e3)}.bind(this),this.client.onclose=function(e){this.auth&&(this.authenticated=!1),this.enabled&&(this.client=null,clearInterval(this.pingTimer),clearTimeout(this.connectTimer),this.reconnectAttempts<4?this.connectTimer=homegearWsSetTimeout.call(this,this.connectClient,5e3):this.connectTimer=homegearWsSetTimeout.call(this,this.connectClient,6e4),this.invokeDisconnected())}.bind(this),this.client.onerror=function(e){this.client=null,this.auth&&(this.authenticated=!1),clearInterval(this.pingTimer),clearTimeout(this.connectTimer),this.connectTimer=homegearWsSetTimeout.call(this,this.connectClient,5e3),this.invokeError(e.data)}.bind(this)},HomegearWS.prototype.getTwoRandomHeyBytes=function(){return Math.floor(65536*(1+Math.random())).toString(16).substring(1)},HomegearWS.prototype.getGuid=function(){return this.getTwoRandomHeyBytes()+this.getTwoRandomHeyBytes()+"-"+this.getTwoRandomHeyBytes()+"-"+this.getTwoRandomHeyBytes()+"-"+this.getTwoRandomHeyBytes()+"-"+this.getTwoRandomHeyBytes()+this.getTwoRandomHeyBytes()+this.getTwoRandomHeyBytes()},HomegearWS.prototype.addPeers=function(e){if(!e.constructor!==Array){var t=Array();for(i in e)-1==this.peers.indexOf(e[i])&&(t.push(e[i]),this.peers.push(e[i]));this.isReady()&&0!=t.length&&(this.log&&console.log("Subscribing to peers:",t),this.send(JSON.stringify({id:this.messageCounter++,method:"subscribePeers",params:[this.id,t]})))}},HomegearWS.prototype.removePeers=function(e){if(!e.constructor!==Array){var t=Array();for(i in e){var o=this.peers.indexOf(e[i]);-1!=o&&(t.push(e[i]),this.peers.splice(o,1))}this.isReady()&&0!=t.length&&this.send(JSON.stringify({id:this.messageCounter++,method:"unsubscribePeers",params:[this.id,t]}))}},HomegearWS.prototype.addPeer=function(e){-1<this.peers.indexOf(e)||(this.peers.push(e),this.isReady()&&(this.log&&console.log("Subscribing to peer "+e),this.send(JSON.stringify({id:this.messageCounter++,method:"subscribePeers",params:[this.id,[e]]}))))},HomegearWS.prototype.removePeer=function(e){var t=this.peers.indexOf(e);-1<t&&this.peers.splice(t,1),this.isReady()&&this.send(JSON.stringify({id:this.messageCounter++,method:"unsubscribePeers",params:[this.id,[e]]}))},HomegearWS.prototype.isReady=function(){return this.client&&this.client.readyState===WebSocket.OPEN&&this.authenticated},HomegearWS.prototype.subscribePeers=function(){this.isReady()&&(this.log&&console.log("Subscribing to peers (2):",this.peers),this.send(JSON.stringify({id:this.messageCounter++,method:"subscribePeers",params:[this.id,this.peers]})),this.invokeReady())},HomegearWS.prototype.pingClient=function(){this.isReady()&&(this.log&&console.log("Pinging client..."),this.send(JSON.stringify({id:this.messageCounter++,method:"logLevel",params:[]})))},HomegearWS.prototype.send=function(e){this.client&&this.client.send(e)},HomegearWS.prototype.invoke=function(e){var t,i;this.isReady()&&(t=this.messageCounter++,"object"==typeof e&&void 0!==e.method?("function"==typeof arguments[1]&&(this.requests["c"+t]=arguments[1]),e.id=t,i=e,this.log&&console.log("Invoking RPC method (1): ",i),i=JSON.stringify(i),this.send(i)):"string"==typeof e&&(i={jsonrpc:"2.0",method:e,id:t},1<arguments.length&&"function"==typeof arguments[1]&&(this.requests["c"+t]=arguments[1]),2<arguments.length&&(i.params=Array.prototype.slice.call(arguments,2)),this.log&&console.log("Invoking RPC method (2): ",i),i=JSON.stringify(i),this.send(i)))},HomegearWS.prototype.invokeRaw=function(e,t){var i;this.isReady()&&(t=this.messageCounter++,"string"==typeof e&&"number"==typeof arguments[1]&&("function"==typeof arguments[2]&&(this.requests["c"+t]=arguments[2]),i=e,this.log&&console.log("Invoking RPC method (1): ",i),this.send(i)))};