UNPKG

@iotize/tap

Version:

IoTize Device client for Javascript

17 lines (15 loc) 18.9 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@iotize/tap/client/api"),require("@iotize/tap/client/impl"),require("rxjs"),require("@iotize/common/debug"),require("@iotize/common/array"),require("@iotize/common/byte-converter"),require("rxjs/operators"),require("@iotize/common/error")):"function"==typeof define&&define.amd?define("@iotize/tap",["exports","@iotize/tap/client/api","@iotize/tap/client/impl","rxjs","@iotize/common/debug","@iotize/common/array","@iotize/common/byte-converter","rxjs/operators","@iotize/common/error"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).iotize=e.iotize||{},e.iotize.tap={}),e.iotize.tap.client.api,e.iotize.tap.client.impl,e.rxjs,e.debug$1,e.array,e.byteConverter,e.rxjs.operators,e.error)}(this,(function(e,t,r,n,o,i,a,u,c){"use strict";e.PathParameter=function(){function e(){}return e.fillParam=function(t,r,n){var o={};return o[r]=n,e.fillParams(t,o)},e.fillAllParams=function(t,r){t=e.fillParams(t,r);var n=e.extractParams(t);if(n.length>0)throw new Error("Missing path parameter "+n.join(", ")+" (path: "+t+")");return t},e.fillParams=function(e,t){for(var r in t){var n="{"+r+"}";if(!(e.indexOf(n)>=0))throw new Error("Parameter "+r+" does not exist in path: "+e);e=e.replace(n,t[r].toString())}return e},e.extractParams=function(t){for(var r,n=[],o=e.PARAMETER_REGEX;r=o.exec(t);)n.push(r[1]);return n},e.hasParams=function(t){return e.extractParams(t).length>0},e}(),(e.PathParameter||(e.PathParameter={})).PARAMETER_REGEX=/{([^}]+)}/g; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ var s=function(e,t){return s=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])},s(e,t)};function p(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function r(){this.constructor=e}s(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}function l(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,u])}}}Object.create;function f(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function d(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,o,i=r.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(n=i.next()).done;)a.push(n.value)}catch(e){o={error:e}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(o)throw o.error}}return a}Object.create;var h,y,S="@iotize/tap",m=(o.createLazyDebugger(S),o.createDebugger(S));e.TapError=function(e){function r(t,r,n){var o=e.call(this,r,t)||this;return o.cause=n,o}return p(r,e),r.cannotStartScram=function(e){return new r(r.Code.CannotStartScram,"Cannot start encrypted session, initialization failed. Cause: "+e.message,e)},r.notImplementedYet=function(e){throw new r(r.Code.NotImplemented,e)},r.initializationVectorNotSupported=function(e){return new r(r.Code.InitializationVectorNotSupported,"Initialization vector for SCRAM is not implemented with your current firmware version. You cannot use this feature.",e)},r.executeRequestError=function(e,n){return new r(r.Code.ExecuteRequestError,"Tap request "+t.TapRequestFrame.MethodType[e.method]+" "+(e.pathAlias||e.path)+" failed. "+n.message,n)},r.reponseStatusError=function(e,t){return new E(e,t)},r.invalidServiceCallConfiguration=function(e){return new r(r.Code.InvalidServiceCallConfiguration,e)},r.appendChunkError=function(e,t,r){return new v(e,t,r)},r.nfcPairingError=function(e,t){return new r(r.Code.NfcPairingError,"NFC pairing failed. "+e,t)},r}(c.CodeError),y=e.TapError||(e.TapError={}),(h=y.Code||(y.Code={})).CannotStartScram="TapErrorCannotStartScram",h.NotImplemented="TapErrorNotImplemented",h.InitializationVectorNotSupported="TapErrorInitializationVectorNotSupported",h.AppendChunkError="AppendChunkError",h.ResponseStatusError="TapErrorTapResponseStatusError",h.InvalidServiceCallConfiguration="TapErrorInvalidServiceCallConfiguration",h.ExecuteRequestError="TapErrorTapExecuteRequestError",h.NfcPairingError="TapErrorNfcPairingError",h.ScramNotStartedYet="TapErrorScramNotStartedYet",h.InvalidScramKey="TapErrorInvalidScramKey";var E=function(t){function n(n,o){var i=t.call(this,e.TapError.Code.ResponseStatusError,"Tap request "+(o?b(o)+" ":n.request?r.TapRequestHelper.toString(n.request)+" ":"")+"failed. "+r.tapResponseStatusToString(n.status)+" (status code 0x"+n.status.toString(16)+")")||this;return i.response=n,i}return p(n,t),n}(e.TapError),v=function(t){function r(r,n,o){var i=t.call(this,e.TapError.Code.AppendChunkError,"Appending data chunk n°"+o.loaded+"/"+o.total+" failed with error: "+r.message,r)||this;return i.chunk=n,i.progress=o,i}return p(r,t),r}(e.TapError),T=function(){function n(e,t,r){this._tapResponse=e,this.request=t,this.bodyDecoder=r}return n.SUCCESS=function(e){return n.create(t.ResultCode.OK,e)},n.ERROR=function(e){return e=e||t.ResultCode.BAD_REQUEST,n.create(e)},n.create=function(e,t){return new n(r.TapResponseFrameBuilder.create(e,t))},Object.defineProperty(n.prototype,"data",{get:function(){return this._tapResponse.data},enumerable:!1,configurable:!0}),n.prototype.body=function(e){return this.successful(),e?e.decode(this.rawBody()):(void 0===this._bodyCache&&(this.bodyDecoder?this._bodyCache=this.bodyDecoder.decode(this.rawBody()):this._bodyCache=this.rawBody()),this._bodyCache)},n.prototype.rawBody=function(){return this.data?this.data:new Uint8Array(0)},n.prototype.setBodyDecoder=function(e){return this.bodyDecoder=e,this},n.prototype.hasBody=function(){return null!=this.rawBody()},n.prototype.codeRet=function(){return this.status},Object.defineProperty(n.prototype,"status",{get:function(){return this._tapResponse.status},enumerable:!1,configurable:!0}),n.prototype.toString=function(){return'ResponseMessage[codeRet="'+this.status+'";data="'+(this.hasBody()?a.bufferToHexString(this.rawBody()):"NO DATA")+'";decoder='+(this.bodyDecoder?this.bodyDecoder.constructor.name:"NONE")+"]"},n.prototype.toBytes=function(){return this.data},n.prototype.setBody=function(e){this._bodyCache=e},n.prototype.isSuccessful=function(){return 0==(128&this.status)},n.prototype.setError=function(e){return this.error=e},n.prototype.successful=function(){if(!this.isSuccessful())throw this.error?this.error:e.TapError.reponseStatusError(this)},n.prototype.setRequest=function(e){this.request=e},n}(),C=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{c(n.next(e))}catch(e){i(e)}}function u(e){try{c(n.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,u)}c((n=n.apply(e,t||[])).next())}))};function b(r,n){var o=_.resolvePathParameters(r),i=r.pathAlias?e.PathParameter.fillAllParams(r.pathAlias,r.pathParameters||{}):void 0,u=t.TapRequestFrame.MethodType[r.method]+" "+(i||o);return r.pathAlias&&(u+=" ("+o+")"),(null==n?void 0:n.skipBody)||void 0===r.body||(u+=" "+function e(t){return t instanceof Uint8Array?"0x"+a.bufferToHexString(t):"object"==typeof t?"{"+Object.keys(t).map((function(r){return'"'+r+'":'+e(t[r])})).join(",")+"}":JSON.stringify(t)}(r.body)),u}var _=function(){function o(e,t){void 0===t&&(t={chunkSize:220}),this.client=e,this.options=t}return o.prototype.get=function(e,r){return this.execute({method:t.TapRequestFrame.MethodType.GET,path:e,body:r})},o.prototype.put=function(e,r){return this.execute({method:t.TapRequestFrame.MethodType.PUT,path:e,body:r})},o.prototype.post=function(e,r){return this.execute({method:t.TapRequestFrame.MethodType.POST,path:e,body:r})},o.prototype.prepare=function(t){var r=this;return n.defer((function(){return C(r,void 0,void 0,(function(){var r,n,i,a,u;return l(this,(function(c){switch(c.label){case 0:r=o.toTapRequest(t),c.label=1;case 1:return c.trys.push([1,3,,4]),[4,this.client.request(r).toPromise()];case 2:if(!(n=c.sent()))throw new Error("No Tap response");return i=new T(n,r),(a=o.resolveResponseBodyDecoder(t))&&i.setBodyDecoder({decode:a}),i.isSuccessful()||i.setError(e.TapError.reponseStatusError(i,t)),[2,i];case 3:if(!((u=c.sent())instanceof e.TapError))throw e.TapError.executeRequestError(t,u);throw u;case 4:return[2]}}))}))}))},o.prototype.execute=function(e){return this.prepare(e).pipe(u.first()).toPromise()},o.prototype.createAppendCalls=function(e,t){return void 0===t&&(t=this.options.chunkSize),i.chunkArray(Array.from(e.body||new Uint8Array),t).map((function(t,r){return Object.assign(Object.assign({},e),{body:Uint8Array.from(t)})}))},o.prototype.prepareAppendCall=function(r,o){var i=this;return void 0===o&&(o=this.options.chunkSize),new n.Observable((function(n){var a=!1;return C(i,void 0,void 0,(function(){var i,u,c,s,p,d,h,y,S,m,E,v,T;return l(this,(function(l){switch(l.label){case 0:l.trys.push([0,9,,10]),i=this.createAppendCalls(r,o),u=0,c=0,l.label=1;case 1:l.trys.push([1,6,7,8]),s=f(i),p=s.next(),l.label=2;case 2:return p.done?[3,5]:(d=p.value,a?[2]:(h=d.body,y={total:h.length,loaded:u+1,byteOffset:c},n.next(y),[4,this.execute({method:d.method||t.TapRequestFrame.MethodType.PUT,path:d.path,body:h})]));case 3:S=l.sent();try{S.successful()}catch(t){throw e.TapError.appendChunkError(t,h,y)}c+=h.length,u++,l.label=4;case 4:return p=s.next(),[3,2];case 5:return[3,8];case 6:return m=l.sent(),v={error:m},[3,8];case 7:try{p&&!p.done&&(T=s.return)&&T.call(s)}finally{if(v)throw v.error}return[7];case 8:return n.complete(),[3,10];case 9:return E=l.sent(),n.error(E),[2];case 10:return[2]}}))})),function(){a=!0}}))},o.toTapRequest=function(e){var t=o.resolvePathParameters(e),n=o.encodeBody(e);return r.TapRequestFrameBuilder.create(e.method,t,n)},o.resolveResponseBodyDecoder=function(e){if(e.responseBodyDecoder)return"function"==typeof(t=e.responseBodyDecoder)?t:t.decode.bind(t);var t},o.resolvePathParameters=function(t){return e.PathParameter.fillAllParams(t.path,t.pathParameters||{})},o.encodeBody=function(t){if(void 0!==t.body){var r,n=t.bodyEncoder?("function"==typeof(r=t.bodyEncoder)?r:r.encode.bind(r))(t.body):t.body;if(void 0!==n&&!(n instanceof Uint8Array))throw e.TapError.invalidServiceCallConfiguration("Request body encoder is required for call to "+b(t));return n}},o}();var P=function(e){this.serviceCallRunner=e},g=function(){function e(e){this.serviceCallRunner=e}return e.prototype.services=function(){return Object.values(this.servicesMap())},e.prototype.servicesMap=function(){var t={};for(var r in this){var n=Object.getOwnPropertyDescriptor(e.prototype,r),o=this[r];(null==n?void 0:n.enumerable)&&o instanceof P&&(t[r]=o)}return t},e.prototype.resources=function(){return this.services().reduce((function(e,t){return e.push.apply(e,function(e,t,r){if(r||2===arguments.length)for(var n,o=0,i=t.length;o<i;o++)!n&&o in t||(n||(n=Array.prototype.slice.call(t,0,o)),n[o]=t[o]);return e.concat(n||Array.prototype.slice.call(t))}([],d(Object.values(t.resources)))),e}),[])},e}();var A=Uint8Array.from([0,164,4,0,7,240,2,76,119,77,50,77,144,0]),R=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{c(n.next(e))}catch(e){i(e)}}function u(e){try{c(n.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,u)}c((n=n.apply(e,t||[])).next())}))},w="Device",O=function(){function o(e){this._events=new n.Subject,this._client=e,this._lwm2m=new _(this.client),this.service=new g(this._lwm2m)}return Object.defineProperty(o.prototype,"events",{get:function(){return this._events.asObservable()},enumerable:!1,configurable:!0}),Object.defineProperty(o.prototype,"client",{get:function(){return this._client},set:function(e){this._client=e,this._lwm2m.client=e},enumerable:!1,configurable:!0}),Object.defineProperty(o.prototype,"lwm2m",{get:function(){return this._lwm2m},enumerable:!1,configurable:!0}),o.prototype.notifyEvent=function(e){this._events.next(e)},o.create=function(){return new o(r.TapClient.create())},o.fromProtocol=function(e){var t=r.TapClient.create();return t.addComProtocol(e),new o(t)},Object.defineProperty(o.prototype,"protocol",{get:function(){return this.client.getCurrentProtocol()},enumerable:!1,configurable:!0}),o.prototype.connect=function(e){return m(w,"connecting..."),e&&this.client.addComProtocol(e,"default"),this._client.connect().toPromise()},o.prototype.disconnect=function(){return m(w,"disconnecting..."),this._client.disconnect().toPromise()},o.prototype.isConnected=function(){return!!this._client&&this._client.isConnected()},o.prototype.configure=function(e){return e.configure(this).toPromise()},o.prototype.useComProtocol=function(e){return this.client.useComProtocol(e),this},o.prototype.nfcPairing=function(){return R(this,void 0,void 0,(function(){var n,o;return l(this,(function(i){switch(i.label){case 0:return[4,this.client.getCurrentProtocol()];case 1:return[4,i.sent().send(A).toPromise()];case 2:if(!(n=i.sent())||2!==n.length)throw e.TapError.nfcPairingError("Tap responded with a frame size of "+((null==n?void 0:n.length)||0)+" byte(s) but expecting 2 bytes.");if((o=r.NumberConverter.uint16().decode(n))!==t.ApduResponse.Status.OK)throw e.TapError.nfcPairingError("Tap responded with an invalid APDU response status 0x"+o.toString(16)+".");return[2,n]}}))}))},o}();O.TAG="Tap";var I,B,D,x,N,M=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{c(n.next(e))}catch(e){i(e)}}function u(e){try{c(n.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,u)}c((n=n.apply(e,t||[])).next())}))};e.HostProtocol=void 0,(I=e.HostProtocol||(e.HostProtocol={}))[I.NFC=1]="NFC",I[I.BLUETOOTH=2]="BLUETOOTH",I[I.GSM=4]="GSM",I[I.USB_RLINK=8]="USB_RLINK",I[I.USB_CMISIS_DAP=16]="USB_CMISIS_DAP",I[I.WIFI=32]="WIFI",I[I.BLE=64]="BLE",I[I.UART=256]="UART",I[I.MQTT=512]="MQTT",I[I.WEBSOCKET=1024]="WEBSOCKET",I[I.SLAVE_SERVER=2048]="SLAVE_SERVER",e.TargetProtocol=void 0,(B=e.TargetProtocol||(e.TargetProtocol={}))[B.SWD=0]="SWD",B[B.S3P=1]="S3P",B[B.JTAG=2]="JTAG",B[B.MODBUS=3]="MODBUS",B[B.DIRECT_IO=4]="DIRECT_IO",B[B.SERIAL=6]="SERIAL",B[B.CUSTOM=7]="CUSTOM",B[B.CAN=8]="CAN",B[B.MODBUS_TCP=9]="MODBUS_TCP",B[B.NONE=255]="NONE",e.VariableFormat=void 0,(D=e.VariableFormat||(e.VariableFormat={}))[D._1_BIT=0]="_1_BIT",D[D._8_BITS=1]="_8_BITS",D[D._16_BITS=2]="_16_BITS",D[D._32_BITS=3]="_32_BITS",e.AppPathType=void 0,(x=e.AppPathType||(e.AppPathType={})).PRIMER_CLOUD_WEB_APP_URL="$1/",x.URL="$2/",x.ANDROID_APP_ID="$4/",x.PRIMER_CLOUD_CONFIG_URL="$5/",x.AAR_2="$6/",e.SpecialFeature=void 0,(N=e.SpecialFeature||(e.SpecialFeature={}))[N.SWD_DIRECT_ACCESS=100]="SWD_DIRECT_ACCESS",N[N.SERIAL_ACCESS=101]="SERIAL_ACCESS",N[N.MODBUS_DIRECT_ACCESS=102]="MODBUS_DIRECT_ACCESS",N[N.TAPNPASS_CONTROL_ACCESS=103]="TAPNPASS_CONTROL_ACCESS",N[N.SP_CODEEXEC_ACCESS=104]="SP_CODEEXEC_ACCESS",N[N.MQTT_IDS_ACCESS=105]="MQTT_IDS_ACCESS",N[N.VARIABLE_CONFIG_ACCESS=106]="VARIABLE_CONFIG_ACCESS",N[N.FILE_SYSTEM_ACCESS=107]="FILE_SYSTEM_ACCESS",r.TapStreamReader.prototype.readTapVersion=function(){var e={};return e.major=this.readUnsigned(1),e.minor=this.readUnsigned(1),e.patch=this.readUnsigned(2),e},r.TapStreamWriter.prototype.writeTapVersion=function(e){return this.writeUnsigned(void 0!==e.major?e.major:0,1),this.writeUnsigned(void 0!==e.minor?e.minor:0,1),this.writeUnsigned(void 0!==e.patch?e.patch:0,2),this};var U=function(){function e(){}return e.prototype.encode=function(e,t){return void 0===t&&(t=new r.TapStreamWriter),t.writeTapVersion(e),t.toBytes},e.prototype.decode=function(e){return(e instanceof r.TapStreamReader?e:r.TapStreamReader.create(e)).readTapVersion()},e}();e.AbstractService=P,e.DEFAULT_TAP_MAX_FRAME_SIZE=250,e.NFC_PAIRING_COMMAND=A,e.ServiceCallRunner=_,e.ServiceContainer=g,e.Tap=O,e.TapResponse=T,e.TapResponseStatusError=E,e.TapVersionConverter=U,e.defaultValueIfTapRequetNotImplemented=function(e,r){return M(this,void 0,void 0,(function(){var n;return l(this,(function(o){switch(o.label){case 0:return[4,e];case 1:return(n=o.sent()).isSuccessful()||n.status!==t.ResultCode.NOT_IMPLEMENTED&&n.status!==t.ResultCode.NOT_FOUND?[2,n.body()]:[2,r]}}))}))},e.defineTapPropertyExtension=function(e,t){Object.defineProperty(O.prototype,e,{get:function(){var r=this,n="__"+e+"__";return r[n]||(r[n]=t({tap:this})),r[n]},enumerable:!1,configurable:!0})},e.extendServiceContainer=function(e,t){var r="__"+e+"__";return Object.defineProperty(g.prototype,r,{enumerable:!1,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(g.prototype,e,{get:function(){var e=this;return e[r]||(e[r]=new t(this.serviceCallRunner)),e[r]},enumerable:!0,configurable:!0})},e.serviceCallToString=b,e["ɵa"]=v,Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=iotize-tap.umd.min.js.map