forked from anujsrc/devicehive-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevicehive.device.min.js
More file actions
1 lines (1 loc) · 23.7 KB
/
devicehive.device.min.js
File metadata and controls
1 lines (1 loc) · 23.7 KB
1
!function(e,t){"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?module.exports=t():e.DHDevice=t()}(this,function(){var e=function(){"use strict";var e={guid:function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=16*Math.random()|0,n="x"==e?t:3&t|8;return n.toString(16)})},isFunction:function(e){return e&&"[object Function]"===Object.prototype.toString.call(e)},isArray:Array.isArray||function(e){return"[object Array]"===Object.prototype.toString.call(e)},isArrayLike:function(e){return e&&e.length>=0&&e.length===Math.floor(e.length)},isString:function(e){return"[object String]"===Object.prototype.toString.call(e)},isAccessKey:function(e){return 44===e.length&&new RegExp("[A-Za-z0-9+/=]").test(e)},inArray:function(t,n,i){if(n){if(!e.isArrayLike(n))throw new TypeError("utils.inArray second argument must be an array");if(Array.prototype.indexOf&&e.isArray(n))return n.indexOf(t,i);var r=n.length,s=+i||0;if(!r||s>=r)return-1;for(s=0>s?Math.max(0,r+s):s;r>s;s++)if(s in n&&n[s]===t)return s;return-1}},map:function(t,n){if(!t)return t;if(!e.isFunction(n))throw new TypeError(callback+" is not a function");var i=[];return e.forEach(t,function(e){i.push(n.call(this,e,t))}),i},reduce:function(t,n){if(!t)return t;if(!e.isFunction(n))throw new TypeError(n+" is not a function");var i,r=t,s=r.length>>>0,a=0;if(3==arguments.length)i=arguments[2];else{for(;s>a&&!a in r;)a++;if(a>=s)throw new TypeError("Reduce of empty array with no initial value");i=r[a++]}for(;s>a;a++)a in r&&(i=n(i,r[a],a,r));return i},forEach:function(t,n){if(!t)return t;if(!e.isFunction(n))throw new TypeError(n+" is not a function");var i;if(e.isArrayLike(t)){var r=t.length;for(i=0;r>i&&n.call(t[i],i,t)!==!1;i++);}else for(i in t)if(t.hasOwnProperty(i)&&n.call(t[i],i,t)===!1)break;return t},filter:function(t,n){if(!t)return t;if(!e.isFunction(n))throw new TypeError(n+" is not a function");var i=[];return e.forEach(t,function(e){n.call(this,e,t)&&i.push(this)}),i},toArray:function(t){return e.filter(t,function(){return!0})},find:function(t,n){var i=e.filter(t,n);return i&&i.length>0?i[0]:null},parseDate:function(e){return new Date(e.substring(0,4),parseInt(e.substring(5,7),10)-1,e.substring(8,10),e.substring(11,13),e.substring(14,16),e.substring(17,19),e.substring(20,23))},formatDate:function(t){if(e.isString(t))return t;if("[object Date]"!==Object.prototype.toString.call(t))throw new Error("Invalid object type");var n=function(e,t){for(e=String(e),t=t||2;e.length<t;)e="0"+e;return e};return t.getUTCFullYear()+"-"+n(t.getUTCMonth()+1)+"-"+n(t.getUTCDate())+"T"+n(t.getUTCHours())+":"+n(t.getUTCMinutes())+":"+n(t.getUTCSeconds())+"."+n(t.getUTCMilliseconds(),3)},encodeBase64:function(e){var t,n,i,r,s,a,o,c,u="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",h=0,l=0,d="",f=[];if(!e)return e;do t=e.charCodeAt(h++),n=e.charCodeAt(h++),i=e.charCodeAt(h++),c=t<<16|n<<8|i,r=c>>18&63,s=c>>12&63,a=c>>6&63,o=63&c,f[l++]=u.charAt(r)+u.charAt(s)+u.charAt(a)+u.charAt(o);while(h<e.length);d=f.join("");var p=e.length%3;return(p?d.slice(0,p-3):d)+"===".slice(p||3)},noop:function(){},createCallback:function(t){return e.isFunction(t)?t:e.noop},serializeQuery:function(e){var t,n,i="";for(t in e)e.hasOwnProperty(t)&&(""!=i&&(i+="&"),n=e[t],n=null==n?"":n,i+=encodeURIComponent(t)+"="+encodeURIComponent(n));return i},makeUrl:function(t){var n=t.method,i=t.url,r=t.data;return"GET"===n&&r&&(r=e.serializeQuery(r),r&&(i+=(-1!=i.indexOf("?")?"&":"?")+r)),i},serverErrorMessage:function(e){var t="DeviceHive server error";if(e.responseText)try{t+=" - "+JSON.parse(e.responseText).message}catch(n){t+=" - "+e.responseText}return{error:t,request:e}},errorMessage:function(e){return{error:"DeviceHive error: "+e}},setTimeout:function(e,t){return setTimeout(e,t)},clearTimeout:function(e){clearTimeout(e)}};return e}(),t=function(){"use strict";var t=function(){};return t.prototype={bind:function(e,t,n){this._handlers||(this._handlers={});var i=this._handlers[e]||(this._handlers[e]=[]);i.push({callback:t,context:n||this});var r=this;return{unbind:function(){r.unbind(e,t)}}},unbind:function(t,n){if(!t&&!n)return this._handlers=null,this;var i=this._handlers[t];if(!i)return this;if(!n)return delete this._handlers[t],this;var r=[];return e.forEach(i,function(){var e=this;n&&n!==e.callback&&r.push(e)}),r.length?this._handlers[t]=r:delete this._handlers[t],this},trigger:function(t){if(!this._handlers)return this;var n=e.toArray(arguments).slice(1),i=this._handlers[t];return i&&this._triggerEvents(i,n),this},_triggerEvents:function(t,n){e.forEach(t,function(){var e=this;e.callback.apply(e.context,n)})}},t}(),n=function(){"use strict";var t=e.noop();if(t="undefined"!=typeof XMLHttpRequest?function(){return new XMLHttpRequest}:function(){try{return new ActiveXObject("Microsoft.XMLHTTP")}catch(e){return null}},!t())throw new Error("DeviceHive: XMLHttpRequest is not available");return{send:function(n,i){n.method=n.method||"GET",i=e.createCallback(i);var r=t(),s=n.headers,a=e.makeUrl(n),o=n.method;return r.open(o,a,!0),("POST"==o||"PUT"==o)&&(r.setRequestHeader("Content-Type","application/json"),n.data=JSON.stringify(n.data)),r.onreadystatechange=function(){var t,n;if(4===r.readyState){t=r.status&&r.status>=200&&r.status<300||304===r.status,t||(n=e.serverErrorMessage(r));var s=r.responseText?JSON.parse(r.responseText):null;return i(n,s)}},s&&e.forEach(s,function(e){r.setRequestHeader(e,this)}),r.send(n.data||void 0),{abort:function(){r.abort()}}}}}(),i=function(){"use strict";var t={USER:1,KEY:2,DEVICE:4},i=function(e,t){return(e&t)==t},r=function(n,r){var s=r.authTypes,a=r.auth;if(n.headers=r.headers||{},s){if(!a)throw new Error("Authentication parameters must be specified for this endpoint. Endpoint auth code: "+s);if(i(s,t.KEY)&&a.accessKey)n.headers.Authorization="Bearer "+a.accessKey;else if(i(s,t.DEVICE)&&a.deviceId&&a.deviceKey)n.headers["Auth-DeviceID"]=a.deviceId,n.headers["Auth-DeviceKey"]=a.deviceKey;else{if(!i(s,t.USER))throw new Error("Invalid authentication parameters. Endpoint auth code: "+s);n.headers.Authorization="Basic "+e.encodeBase64(a.login+":"+a.password)}}},s=function(e,t){var i={method:e.method,url:e.base+e.relative,data:e.data};return r(i,e),n.send(i,t)};return{info:function(e,t){return s({base:e,relative:"/info",method:"GET"},t)},getAccessKeys:function(e,n,i,r){return s({base:e,relative:"/user/"+i+"/accesskey",method:"GET",authTypes:t.USER,auth:n},r)},getAccessKey:function(e,n,i,r,a){return s({base:e,relative:"/user/"+i+"/accesskey/"+i,method:"GET",authTypes:t.USER,auth:n},a)},insertAccessKey:function(e,n,i,r,a){return s({base:e,relative:"/user/"+i+"/accesskey",data:r,method:"POST",authTypes:t.USER,auth:n},a)},updateAccessKey:function(e,n,i,r,a,o){return s({base:e,relative:"/user/"+i+"/accesskey/"+r,data:a,method:"PUT",authTypes:t.USER,auth:n},o)},deleteAccessKey:function(e,n,i,r,a){return s({base:e,relative:"/user/"+i+"/accesskey/"+r,method:"DELETE",authTypes:t.USER,auth:n},a)},getDevices:function(e,n,i,r){return s({base:e,relative:"/device",method:"GET",data:i,authTypes:t.USER|t.KEY,auth:n},r)},getDevice:function(e,n,i,r){return s({base:e,relative:"/device/"+i,method:"GET",authTypes:t.USER|t.KEY|t.DEVICE,auth:n},r)},getEquipmentState:function(e,n,i,r){return s({base:e,relative:"/device/"+i+"/equipment",method:"GET",authTypes:t.USER|t.KEY,auth:n},r)},registerDevice:function(e,n,i,r,a){return s({base:e,relative:"/device/"+i,method:"PUT",data:r,authTypes:t.USER|t.KEY|t.DEVICE,auth:n},a)},getDeviceClass:function(e,n,i,r){return s({base:e,relative:"/device/class/"+i,method:"GET",authTypes:t.USER,auth:n},r)},getCommands:function(n,i,r,a,o){return a&&a.start&&(a.start=e.formatDate(a.start)),a&&a.end&&(a.end=e.formatDate(a.end)),s({base:n,relative:"/device/"+r+"/command",method:"GET",data:a,authTypes:t.USER|t.KEY|t.DEVICE,auth:i},o)},getCommand:function(e,n,i,r,a){return s({base:e,relative:"/device/"+i+"/command/"+r,method:"GET",authTypes:t.USER|t.KEY|t.DEVICE,auth:n},a)},insertCommand:function(e,n,i,r,a){return s({base:e,relative:"/device/"+i+"/command",method:"POST",data:r,authTypes:t.USER|t.KEY,auth:n},a)},updateCommand:function(e,n,i,r,a,o){return s({base:e,relative:"/device/"+i+"/command/"+r,method:"PUT",data:a,authTypes:t.USER|t.KEY|t.DEVICE,auth:n},o)},pollCommands:function(e,n,i,r,a){return s({base:e,relative:"/device/"+i+"/command/poll",method:"GET",data:r,authTypes:t.USER|t.KEY|t.DEVICE,auth:n},a)},pollManyCommands:function(e,n,i,r){return s({base:e,relative:"/device/command/poll",method:"GET",data:i,authTypes:t.USER|t.KEY,auth:n},r)},waitCommandResult:function(e,n,i,r,a,o){return s({base:e,relative:"/device/"+i+"/command/"+r+"/poll",method:"GET",data:a,authTypes:t.USER|t.KEY,auth:n},o)},getNotifications:function(n,i,r,a,o){return a&&a.start&&(a.start=e.formatDate(a.start)),a&&a.end&&(a.end=e.formatDate(a.end)),s({base:n,relative:"/device/"+r+"/notification",method:"GET",data:a,authTypes:t.USER|t.KEY,auth:i},o)},getNotification:function(e,n,i,r,a){return s({base:e,relative:"/device/"+i+"/notification/"+r,method:"GET",authTypes:t.USER|t.KEY,auth:n},a)},insertNotification:function(e,n,i,r,a){return s({base:e,relative:"/device/"+i+"/notification",method:"POST",data:r,authTypes:t.USER|t.KEY|t.DEVICE,auth:n},a)},pollNotifications:function(e,n,i,r,a){return s({base:e,relative:"/device/"+i+"/notification/poll",method:"GET",data:r,authTypes:t.USER|t.KEY,auth:n},a)},pollManyNotifications:function(e,n,i,r){return s({base:e,relative:"/device/notification/poll",method:"GET",data:i,authTypes:t.USER|t.KEY,auth:n},r)},getNetworks:function(e,n,i,r){return s({base:e,relative:"/network",method:"GET",data:i,authTypes:t.USER|t.KEY,auth:n},r)},getNetwork:function(e,n,i,r){return s({base:e,relative:"/network/"+i,method:"GET",authTypes:t.USER|t.KEY,auth:n},r)},insertNetwork:function(e,n,i,r){return s({base:e,relative:"/network",method:"POST",data:i,authTypes:t.USER,auth:n},r)},updateNetwork:function(e,n,i,r,a){return s({base:e,relative:"/network/"+i,method:"PUT",data:r,authTypes:t.USER,auth:n},a)},deleteNetwork:function(e,n,i,r){return s({base:e,relative:"/network/"+i,method:"DELETE",authTypes:t.USER,auth:n},r)},getCurrentUser:function(e,n,i){return s({base:e,relative:"/user/current",method:"GET",authTypes:t.USER,auth:n},i)},updateCurrentUser:function(e,n,i,r){return s({base:e,relative:"/user/current",method:"PUT",data:i,authTypes:t.USER,auth:n},r)}}}(),r=function(){"use strict";var n=function(e,n,i){return i=i||e.channelState,i===e.channelState?(e.channelState=n,e._events=e._events||new t,e._events.trigger("onChannelStateChanged",{oldState:i,newState:n}),!0):!1},a=function(t,n){return e.find(t.subscriptions,function(){return this.id===n})},o=function(e,t){var n=e.subscriptions.indexOf(t);e.subscriptions.splice(n,1),t._changeState(s.states.unsubscribed)},c={disconnected:0,connecting:1,connected:2};return r={channelStates:c,channelState:c.disconnected,openChannel:function(t,r){function s(i){a.serverInfo=i,r?e.isArray(r)||(r=[r]):(r=[],e.forEach(a._channels,function(e){r.push(e)}));var s=!0;!function o(i){e.forEach(i,function(r){var c=this;return a._channels[c]?(a.channel=new a._channels[c](a),a.channel.open(function(s){if(s){var u=i.slice(++r);if(!u.length)return t(e.errorMessage("Cannot open any of the specified channels"));o(u)}else n(a,a.channelStates.connected),t(null,c)}),s=!1):void 0})}(r),s&&t(e.errorMessage("None of the specified channels are supported"))}if(t=e.createCallback(t),!n(this,this.channelStates.connecting,this.channelStates.disconnected))return void t(null);var a=this;this.serverInfo?s(this.serverInfo):i.info(this.serviceUrl,function(e,i){e?(n(a,a.channelStates.disconnected),t(e,i)):s(i)})},closeChannel:function(t){if(t=e.createCallback(t),this.channelState===this.channelStates.disconnected)return t(null);var i=this;this.channel&&this.channel.close(function(r,s){return r?t(r,s):(e.forEach(e.toArray(i.channel.subscriptions),function(){o(i.channel,this)}),i.channel=null,n(i,i.channelStates.disconnected),t(null))})},channelStateChanged:function(n){n=e.createCallback(n);var i=this;return this._events=this._events||new t,this._events.bind("onChannelStateChanged",function(e){n.call(i,e)})},subscribe:function(t,n){this._ensureConnectedState(),t=e.createCallback(t),n=n||{};var i=this.channel,r=new s(n.deviceIds,n.names,n.onMessage);return i.subscriptions.push(r),r._changeState(s.states.subscribing),i.subscribe(r,function(e,n){return e?(o(i,r),t(e)):(r._setId(n),r._changeState(s.states.subscribed),t(e,r))}),r},unsubscribe:function(t,n){this._ensureConnectedState(),n=e.createCallback(n);var i=this.channel;if(!t)throw new Error("Subscription must be defined. To unsubscribe from all subscriptions just close the channel");var r=t;return t instanceof s||(r=a(i,t))?r.state===s.states.unsubscribed?n(null):i.unsubscribe(r,function(e){return e?n(e):(o(i,r),n(e,r))}):n(e.errorMessage("Subscription with id "+t+" was not found"))},_ensureConnectedState:function(){if(this.channelState===this.channelStates.disconnected)throw new Error("DeviceHive: Channel is not opened, call the .openChannel() method first");if(this.channelState===this.channelStates.connecting)throw new Error("DeviceHive: Channel has not been initialized, use .openChannel().done() to run logic after the channel is initialized")}}}(),s=function(){"use strict";var n=function(i,r,s){i&&!e.isArray(i)&&(i=[i]),r&&!e.isArray(r)&&(r=[r]),this.deviceIds=i||null,this.names=r||null,this.state=n.states.unsubscribed,this._events=new t,this.message(s)};return n.prototype.stateChanged=function(t){t=e.createCallback(t);var n=this;return this._events.bind("onStateChanged",function(e){t.call(n,e)})},n.prototype.message=function(t){return t=e.createCallback(t),this._events.bind("onMessage",t)},n.prototype._handleMessage=function(){this.state===n.states.subscribed&&this._events.trigger.apply(this._events,["onMessage"].concat(e.toArray(arguments)))},n.prototype._changeState=function(e){if(this.state===e)return!1;var t=this.state;this.state=e,this._events.trigger("onStateChanged",{oldState:t,newState:e})},n.prototype._setId=function(t){this.id=t||e.guid()},n.prototype.toJSON=function(){return{deviceIds:this.deviceIds,names:this.names,state:this.state}},n.states={unsubscribed:0,subscribing:1,subscribed:2},n}(),a=function(){"use strict";var t=function(t,n,i){e.forEach(n.deviceIds,function(){t.deviceIds[this]=i}),e.forEach(n.names,function(){t.names[this]=i})},n=function(e,n){!n.deviceIds&&++e.allDeviceIds,!n.names&&++e.allNames,t(e,n,!0)},i=function(e,n){!n.deviceIds&&--e.allDeviceIds,!n.names&&--e.allNames,t(e,n,!1)},r=function(t){var n=[];return e.forEach(t,function(e){t[e]&&n.push(e)}),n},s=function(e){return!e.allDeviceIds&&!e.allNames&&0===r(e.deviceIds).length&&0===r(e.names).length},a=function(t){return e.map(t,function(){return this.toLowerCase()})},c={open:function(t){t=e.createCallback(t),this._sub={deviceIds:{},allDeviceIds:0,names:{},allNames:0};var n=this._pollParams,i=this;return this._lp=new o(this._hive.serviceUrl,{executePoll:function(e,t){return e.deviceGuids=i._sub.allDeviceIds>0?null:r(i._sub.deviceIds),e.names=i._sub.allNames>0?null:r(i._sub.names),n.executePoll(e,t)},resolveTimestamp:n.resolveTimestamp,onData:function(t){var r=i.subscriptions,s=n.resolveName(t).toLowerCase(),o=n.resolveDeviceId(t).toLowerCase(),c=e.filter(r,function(){return(null===this.names||e.inArray(s,a(this.names))>-1)&&(null===this.deviceIds||e.inArray(o,a(this.deviceIds))>-1)});e.forEach(c,function(){var i=this;e.setTimeout(function(){i._handleMessage.apply(i,n.resolveDataArgs(t))},0)})}}),t(null)},close:function(t){return t=e.createCallback(t),this._lp.stopPolling(),t(null)},subscribe:function(t,i){return i=e.createCallback(i),this._lp.stopPolling(),n(this._sub,t),this._lp.startPolling(i)},unsubscribe:function(t,n){return n=e.createCallback(n),i(this._sub,t),this._lp.stopPolling(),s(this._sub)?n(null):this._lp.startPolling(n)}};return c}(),o=function(){"use strict";var t=function(n,i){var r={timestamp:i},s=function(r,s){if(r)n._polling&&e.setTimeout(function(){t(n,i)},1e3);else{var a=null;s&&e.forEach(s,function(){var e=n._poller.resolveTimestamp(this);(!a||e>a)&&(a=e),n._poller.onData(this)}),t(n,a||i)}};n._request=n._polling&&n._poller.executePoll(r,s)},n=function(e,t){this.serviceUrl=e,this._poller=t};return n.prototype={startPolling:function(n){n=e.createCallback(n),this._polling=!0;var r=this;return this._request=i.info(this.serviceUrl,function(e,i){if(e){var s=r._polling;return r._polling=!1,n(s?e:null)}return t(r,i.serverTimestamp),n(null)})},stopPolling:function(){this._polling=!1,this._request&&this._request.abort()}},n}(),c=function(){"use strict";var t=e.noop;return t.requestTimeout=1e4,t.prototype={_handler:e.noop,open:function(t,n){n=e.createCallback(n);var i=e.errorMessage("WebSockets are not supported");try{if(!WebSocket)return n(i)}catch(r){return n(i)}var s=this,a=!1;this._native=new WebSocket(t),this._native.onopen=function(e){a=!0,n(null,e)},this._native.onmessage=function(t){var n=JSON.parse(t.data);if(s._requests&&n.requestId){var i=s._requests[n.requestId];i&&(e.clearTimeout(i.timeout),n.status&&"success"==n.status?i.cb(null,n):i.cb({error:n.error}),delete s._requests[n.requestId])}else s._handler(n)},this._native.onclose=function(t){if(!a){var i=e.errorMessage("WebSocket connection has failed to open");return i.data=t,n(i)}}},close:function(t){t=e.createCallback(t),this._native.onclose=function(e){return t(null,e)},this._native.close()},message:function(e){this._handler=e},send:function(n,i,r){r=e.createCallback(r);var s=this,a={};return this._requestId=this._requestId||0,a.id=++this._requestId,a.cb=r,a.timeout=e.setTimeout(function(){a.cb(e.errorMessage("Operation timeout")),delete s._requests[a.id]},t.requestTimeout),this._requests=this._requests||{},this._requests[a.id]=a,i=i||{},i.requestId=a.id,i.action=n,this._native.send(JSON.stringify(i)),a}},t}(),u=function(){"use strict";var e=function(){var e=new t;this._events=e,this._transport=new c,this._transport.message(function(t){"command/insert"==t.action&&t.command&&t.command.id&&e.trigger("onCommandInsert",t),"command/update"==t.action&&e.trigger("onCommandUpdate",t),"notification/insert"==t.action&&t.deviceGuid&&t.notification&&e.trigger("onNotificationInsert",t)})};return e.prototype={open:function(e,t){this._transport.open(e+"/client",t)},close:function(e){this._transport.close(e)},getInfo:function(e){this._transport.send("server/info",null,e)},authenticate:function(e,t,n,i){this._transport.send("authenticate",{login:e,password:t,accessKey:n},i)},sendCommand:function(e,t){this._transport.send("command/insert",e,t)},updateCommand:function(e,t){this._transport.send("command/update",e,t)},commandSubscribe:function(e,t){this._transport.send("command/subscribe",e,t)},commandUnSubscribe:function(e,t){this._transport.send("command/unsubscribe",e,t)},sendNotification:function(e,t){this._transport.send("notification/insert",e,t)},notificationSubscribe:function(e,t){this._transport.send("notification/subscribe",e,t)},notificationUnSubscribe:function(e,t){this._transport.send("notification/unsubscribe",e,t)}},e}(),h=function(){"use strict";var e=function(){var e=new t;this._events=e,this._transport=new c,this._transport.message(function(t){"command/insert"==t.action&&t.command&&t.command.id&&e.trigger("onCommandInsert",t)})};return e.prototype={open:function(e,t){return this._transport.open(e+"/device",t)},close:function(e){return this._transport.close(e)},getInfo:function(e){this._transport.send("server/info",null,e)},authenticate:function(e,t,n){this._transport.send("authenticate",{deviceId:e,deviceKey:t},n)},updateCommand:function(e,t){this._transport.send("command/update",e,t)},commandSubscribe:function(e,t){this._transport.send("command/subscribe",e,t)},commandUnSubscribe:function(e,t){this._transport.send("command/unsubscribe",e,t)},sendNotification:function(e,t){this._transport.send("notification/insert",e,t)}},e}(),l=function(){"use strict";var t=function(e){this.subscriptions=[],this._hive=e,this._pollParams={executePoll:function(t,n){return e._executeApi(i.pollCommands,[t,n])},resolveTimestamp:function(e){return e.timestamp},resolveDataArgs:function(t){return[e.deviceId,t]},resolveName:function(e){return e.command},resolveDeviceId:function(){return e.deviceId}}};return t.prototype=a,t.constructor=t,t.prototype.sendNotification=function(t,n){return n=e.createCallback(n),this._hive._executeApi(i.insertNotification,[t.notification,n])},t.prototype.updateCommand=function(t,n){return n=e.createCallback(n),this._hive._executeApi(i.updateCommand,[t.commandId,t.command,n])},t}(),d=function(){"use strict";var t=function(e){this.subscriptions=[],this.compatibilityMode=!e.auth.accessKey,this._hive=e};return t.prototype={open:function(t){t=e.createCallback(t);var n=this._hive.serverInfo.webSocketServerUrl;if(!n)return void t(e.errorMessage("Open channel failed. Cannot get web socket server url"));var i=this;this._wsApi=this.compatibilityMode?new h:new u,this._wsApi._events.bind("onCommandInsert",function(t){var n=i.subscriptions;i.compatibilityMode||(n=e.find(i.subscriptions,function(){return this.id===t.subscriptionId}),n=[n]),e.forEach(n,function(){this._handleMessage(i._hive.deviceId,t.command)})}),this._wsApi.open(n,function(e){return e?t(e):void(i._hive.auth.accessKey?i._wsApi.authenticate(null,null,i._hive.auth.accessKey,t):i._wsApi.authenticate(i._hive.auth.deviceId,i._hive.auth.deviceKey,t))})},close:function(t){t=e.createCallback(t),this._wsApi.close(t)},subscribe:function(t,n){if(n=e.createCallback(n),t.names&&this.compatibilityMode)throw new Error("Command name filtering is not supported for deviceKey authentication and websocket channel. Note: device key auth is deprecated, use access key auth instead.");return t?this.compatibilityMode&&this.subscriptions.length>1?n(null):void this._wsApi.commandSubscribe(this.compatibilityMode?null:{deviceGuids:t.deviceIds,names:t.names},function(e,t){return n(e,t&&t.subscriptionId)}):n(null)},unsubscribe:function(t,n){return n=e.createCallback(n),this.compatibilityMode&&this.subscriptions.length>1?n(null):void this._wsApi.commandUnSubscribe({subscriptionId:t.id},n)},sendNotification:function(t,n){n=e.createCallback(n),this._wsApi.sendNotification(t,n)},updateCommand:function(t,n){n=e.createCallback(n),this._wsApi.updateCommand(t,n)}},t}(),f=function(){"use strict";var t=function(t,n,i,r){this.serviceUrl=t,this.deviceId=n,this.auth={},r||!e.isAccessKey(i)?(this.auth.deviceId=n,this.auth.deviceKey=i):this.auth.accessKey=i};t.prototype=r,t.constructor=t,t.prototype.getDevice=function(t){return t=e.createCallback(t),this._executeApi(i.getDevice,[t])},t.prototype.registerDevice=function(t,n){if(n=e.createCallback(n),t.key&&this.auth.deviceKey&&t.key!==this.auth.deviceKey)throw new Error("Conflicting device keys on device registration");if(t.key=t.key||this.auth.deviceKey,!t.key)throw new Error("Device key was not provided during the DHDevice object creation and therefore must be specified in the parameters");return this._executeApi(i.registerDevice,[t,n])},t.prototype.updateDevice=function(t,n){return n=e.createCallback(n),this._executeApi(i.registerDevice,[t,n])},t.prototype.sendNotification=function(t,n,i){return i=e.createCallback(i),this._ensureConnectedState(),this.channel.sendNotification({notification:{notification:t,parameters:n},deviceGuid:this.deviceId},i)};var n=r.subscribe;return t.prototype.subscribe=function(e,t){t=t||{},t.deviceIds=[this.deviceId];var i=n.call(this,e,t);i._handleMessageOld=i._handleMessage;var r=this;return i._handleMessage=function(e,t){r._populateCmd(t),i._handleMessageOld(t)},i},t.prototype._populateCmd=function(t){var n=this.channel,i=this.deviceId;t.update=function(r,s){if(s=e.createCallback(s),!r||!r.status)throw new Error("Command status must be specified");var a={};return a.commandId=t.id,a.command=r||{},a.deviceGuid=i,n.updateCommand(a,s)}},t.prototype._executeApi=function(e,t){var n=[this.serviceUrl,this.auth,this.deviceId].concat(t);return e.apply(null,n)},t.prototype._channels={},t.prototype._channels.websocket=d,t.prototype._channels.longpolling=l,t.channelStates=r.channelStates,t.subscriptionStates=s.states,t}();return f});