@iotize/tap
Version:
IoTize Device client for Javascript
16 lines (14 loc) • 34.5 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("rxjs"),require("rxjs/operators"),require("@iotize/common/error"),require("@iotize/tap"),require("@iotize/tap/client/impl"),require("@iotize/tap/service/impl/variable"),require("@iotize/tap/client/api"),require("@iotize/tap/service/impl/target"),require("@iotize/tap/service/impl/interface")):"function"==typeof define&&define.amd?define("@iotize/tap/data",["exports","rxjs","rxjs/operators","@iotize/common/error","@iotize/tap","@iotize/tap/client/impl","@iotize/tap/service/impl/variable","@iotize/tap/client/api","@iotize/tap/service/impl/target","@iotize/tap/service/impl/interface"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).iotize=e.iotize||{},e.iotize.tap=e.iotize.tap||{},e.iotize.tap.data={}),e.rxjs,e.rxjs.operators,e.error,e.iotize.tap,e.iotize.tap.client.impl,e.iotize.tap.service.impl.variable,e.iotize.tap.client.api)}(this,(function(e,t,r,n,i,a,o,u){"use strict";
/*! *****************************************************************************
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 c=function(e,t){return c=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])},c(e,t)};function s(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}c(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}function l(e,t){var r,n,i,a,o={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return a={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function u(a){return function(u){return function(a){if(r)throw new TypeError("Generator is already executing.");for(;o;)try{if(r=1,n&&(i=2&a[0]?n.return:a[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,a[1])).done)return i;switch(n=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,n=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!(i=o.trys,(i=i.length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){o=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]<i[3])){o.label=a[1];break}if(6===a[0]&&o.label<i[1]){o.label=i[1],i=a;break}if(i&&o.label<i[2]){o.label=i[2],o.ops.push(a);break}i[2]&&o.ops.pop(),o.trys.pop();continue}a=t.call(e,o)}catch(e){a=[6,e],n=0}finally{r=i=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,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,i,a=r.call(e),o=[];try{for(;(void 0===t||t-- >0)&&!(n=a.next()).done;)o.push(n.value)}catch(e){i={error:e}}finally{try{n&&!n.done&&(r=a.return)&&r.call(a)}finally{if(i)throw i.error}}return o}var p,h;Object.create;e.MonitorEngine=void 0,h=e.MonitorEngine||(e.MonitorEngine={}),(p=h.State||(h.State={}))[p.START=0]="START",p[p.PAUSE=1]="PAUSE",p[p.STOP=2]="STOP";this&&this.__awaiter;var b=function(){function r(e){void 0===e&&(e=w),this.initalState=e,this.subject=new t.BehaviorSubject(this.initalState)}return Object.defineProperty(r.prototype,"state",{get:function(){return this.subject.value.state},enumerable:!1,configurable:!0}),r.prototype.start=function(t){return this.subject.next(Object.assign({state:e.MonitorEngine.State.START},t)),this},r.prototype.stop=function(){return this.subject.next({state:e.MonitorEngine.State.STOP}),this},r.prototype.pause=function(){return this.subject.next({state:e.MonitorEngine.State.PAUSE}),this},r.prototype.setPeriod=function(e){this.subject.next({period:e})},r.prototype.asSubject=function(){return this.subject},r}();var v,y,w={dueTime:0,period:1e3,refresh:!1,state:e.MonitorEngine.State.STOP};function g(n){var i=n instanceof b?n.asSubject():n,a=Object.assign(Object.assign({},w),i.value);return i.pipe(r.switchMap((function(n){var i=Object.assign(Object.assign({},a),n);switch(a=i,i.state){case e.MonitorEngine.State.STOP:return t.empty();case e.MonitorEngine.State.START:return t.timer(i.dueTime,i.period).pipe(r.map((function(e){return{time:e,state:i}})));case e.MonitorEngine.State.PAUSE:return t.never();default:throw new Error("InternalErorr: Not implemented monitoring state: "+i.state)}})))}e.TapDataError=function(e){function t(t,r,n){var i=e.call(this,t,r)||this;return i.cause=n,i}return s(t,e),t.unexpectedEncodedValueError=function(e,r,n){return new t("An error occured while encoding value for variable "+e.id+". "+n.message,t.Code.UnexpectedEncodedValueError)},t.unexpectedVariableDataRead=function(e,r,n){return new t("An error occured while decoding value for variable "+e.id+". "+n.message,t.Code.UnexpectedVariableData)},t.bundleNotFound=function(e){return new t('Bundle with identifier "'+e.toString()+'" is not registered',t.Code.BundleNotFound)},t.variableNotFound=function(e){return new t('Variable with identifier "'+e.toString()+'" is not registered',t.Code.VariableNotFound)},t.bundleNotConfigured=function(e,r){return new t('Bundle with id "'+e.id+'" does not exist on this Tap. You should reconfigure your Tap to add this bundle.',t.Code.BundleNotConfigured,r)},t.variableNotConfigured=function(e,r){return new t('Variable with id "'+e.id+'" does not exist on this Tap. You should reconfigure your Tap to add this variable.',t.Code.VariableNotConfigured,r)},t}(n.CodeError),y=e.TapDataError||(e.TapDataError={}),(v=y.Code||(y.Code={})).UnexpectedVariableData="TapDataErrorUnexpectedVariableData",v.UnexpectedEncodedValueError="TapDataErrorUnexpectedEncodedValueError",v.BundleNotFound="TapDataErrorBundleNotFound",v.VariableNotFound="TapDataErrorVariableNotFound",v.BundleNotConfigured="TapDataErrorBundleNotConfigured",v.VariableNotConfigured="TapDataErrorVariableNotConfigured";var m,x={encode:function(e){return e},decode:function(e){return e}},V=new a.StringConverter("ascii",{addEndOfStringCharacter:!0,replaceEmptyStringWithEndOfLineChar:!0,stripEndOfStringCharacter:!0});function T(e){return e.length*E(e.format)}function S(e){return Math.ceil(E(e)/8)}function E(e){switch(e){case i.VariableFormat._1_BIT:return 1;case i.VariableFormat._8_BITS:return 8;case i.VariableFormat._16_BITS:return 16;case i.VariableFormat._32_BITS:return 32;default:throw new Error('InternalError: missing bit length for format value value "'+e+'" (name: '+i.VariableFormat[e]+")")}}function O(e){return D[e].size}var D=((m={})[o.VariableType.Data.BOOLEAN]={size:1},m[o.VariableType.Data.UINT8]={size:1},m[o.VariableType.Data.UINT16]={size:2},m[o.VariableType.Data.UINT32]={size:4},m[o.VariableType.Data.INT8]={size:1},m[o.VariableType.Data.INT16]={size:2},m[o.VariableType.Data.INT32]={size:4},m[o.VariableType.Data.FLOAT32]={size:4},m[o.VariableType.Data.ASCII]={size:1},m);function C(e,t){void 0===t&&(t=V);var r=e.dataType;if(!(r in o.VariableType.Data))throw new Error('Unknown variable type key "'+r+'"');return j(o.VariableType.Data[r],e.length||1,t)}function j(e,t,r){if(void 0===t&&(t=1),void 0===r&&(r=V),e===o.VariableType.Data.ASCII)return r;var n=function(e){switch(e){case o.VariableType.Data.BOOLEAN:return a.BooleanConverter.instanceBit0();case o.VariableType.Data.UINT8:return a.NumberConverter.uint8();case o.VariableType.Data.UINT16:return a.NumberConverter.uint16();case o.VariableType.Data.UINT32:return a.NumberConverter.uint32();case o.VariableType.Data.INT8:return a.NumberConverter.int8();case o.VariableType.Data.INT16:return a.NumberConverter.int16();case o.VariableType.Data.INT32:return a.NumberConverter.int32();case o.VariableType.Data.FLOAT32:return a.FloatConverter.instance32();case o.VariableType.Data.ASCII:return a.StringConverter.ascii();default:return}}(e);return n&&t>1?new a.ArrayConverter(n,{sizeOfItem:O(e)}):n}var _=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(i,a){function o(e){try{c(n.next(e))}catch(e){a(e)}}function u(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,u)}c((n=n.apply(e,t||[])).next())}))},R=function(){function e(e){this._variables=e}return Object.defineProperty(e.prototype,"variables",{get:function(){return Object.values(this._variables)},enumerable:!1,configurable:!0}),e.prototype.readRaw=function(){return _(this,void 0,void 0,(function(){var e;return l(this,(function(t){switch(t.label){case 0:return[4,this.readRawInner()];case 1:return e=t.sent(),this.notifyRawValue(e),[2,e]}}))}))},e.prototype.read=function(){return _(this,void 0,void 0,(function(){return l(this,(function(e){switch(e.label){case 0:return[4,this.readRaw()];case 1:return e.sent(),[2,this.valueSnapshot]}}))}))},e.prototype.write=function(e){return _(this,void 0,void 0,(function(){var t,r,n,i;return l(this,(function(a){switch(a.label){case 0:for(r in t=[],e)t.push(r);n=0,a.label=1;case 1:return n<t.length?(i=t[n],[4,this.variable(i).writeRaw(e[i])]):[3,4];case 2:a.sent(),a.label=3;case 3:return n++,[3,1];case 4:return[2]}}))}))},e.prototype.variable=function(e){if(!(e in this._variables))throw new Error('Variable "'+e+'" not found in bundle "'+this.id.toString()+'"');return this._variables[e]},Object.defineProperty(e.prototype,"rawValues",{get:function(){var e=Object.values(this._variables);return t.combineLatest(e.map((function(e){return e.rawValues.pipe(r.startWith(void 0))}))).pipe(r.map((function(t){return t.reduce((function(t,r,n){return t[e[n].id]=r,t}),{})})))},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"rawValueSnaphot",{get:function(){var e=Object.values(this._variables);return e.reduce((function(t,r,n){return t[e[n].id]=r.rawValueSnaphot,t}),{})},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"valueSnapshot",{get:function(){var e=Object.values(this._variables);return e.reduce((function(t,r,n){return t[e[n].id]=r.valueSnapshot,t}),{})},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"values",{get:function(){var e=Object.values(this._variables);return t.combineLatest(e.map((function(e){return e.values.pipe(r.startWith(void 0))}))).pipe(r.map((function(t){return t.reduce((function(t,r,n){return t[e[n].id]=r,t}),{})})))},enumerable:!1,configurable:!0}),e.prototype.notifyRawValue=function(e){var t;return _(this,void 0,void 0,(function(){var r,n;return l(this,(function(i){for(r in e)void 0!==(n=e[r])&&(null===(t=this._variables[r])||void 0===t||t.notifyRawValue(n));return[2]}))}))},e.prototype.findVariable=function(e){return e in this._variables?this._variables[e]:void 0},e.prototype.hasVariable=function(e){return!!this.findVariable(e)},e.prototype.writeRaw=function(e){return _(this,void 0,void 0,(function(){var t,r,n,i,a;return l(this,(function(o){switch(o.label){case 0:for(r in t=[],e)t.push(r);n=0,o.label=1;case 1:return n<t.length?(i=t[n],void 0===(a=e[i])?[3,3]:[4,this.variable(i).writeRaw(a)]):[3,4];case 2:o.sent(),o.label=3;case 3:return n++,[3,1];case 4:return[2]}}))}))},e}(),N=function(){function n(){var e=this;this.rawDataStream=new t.Subject,this.values=this.rawDataStream.pipe(r.map((function(t){return e.decodeValue(t)})))}return Object.defineProperty(n.prototype,"lastRawValue",{get:function(){return this._lastRawValue},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"rawValueSnaphot",{get:function(){return this.lastRawValue},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"valueSnapshot",{get:function(){if(void 0!==this.lastRawValue)return this.decodeValue(this.lastRawValue)},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"rawValues",{get:function(){return this.rawDataStream},enumerable:!1,configurable:!0}),n.prototype.notifyRawValue=function(e){return _(this,void 0,void 0,(function(){return l(this,(function(t){return this._lastRawValue=e,this.rawDataStream.next(e),[2]}))}))},n.prototype.readRaw=function(){return _(this,void 0,void 0,(function(){var e;return l(this,(function(t){switch(t.label){case 0:return[4,this.readRawInner()];case 1:return e=t.sent(),this.notifyRawValue(e),[2,e]}}))}))},n.prototype.read=function(){return _(this,void 0,void 0,(function(){var e;return l(this,(function(t){switch(t.label){case 0:return e=this.values.pipe(r.first()).toPromise(),[4,this.readRaw()];case 1:return t.sent(),[2,e]}}))}))},n.prototype.write=function(e){return _(this,void 0,void 0,(function(){var t;return l(this,(function(r){return t=this.encodeValue(e),[2,this.writeRaw(t)]}))}))},n.prototype.encodeValue=function(t){try{return this.converter.encode(t)}catch(r){throw e.TapDataError.unexpectedEncodedValueError(this,t,r)}},n.prototype.decodeValue=function(t){try{return this.converter.decode(t)}catch(r){throw e.TapDataError.unexpectedVariableDataRead(this,t,r)}},n}(),P=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(i,a){function o(e){try{c(n.next(e))}catch(e){a(e)}}function u(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,u)}c((n=n.apply(e,t||[])).next())}))},I=function(){function t(e,t){this.id=e,this.context=t}return Object.defineProperty(t.prototype,"converter",{get:function(){return this.context.converter},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"indexes",{get:function(){return this.context.indexes},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"rawValues",{get:function(){var e=this;return this.context.variable.rawValues.pipe(r.map((function(t){return e.subDataView(t)})))},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"valueSnapshot",{get:function(){var e=this.rawValueSnaphot;return void 0!==e?this.decodeValue(this.subDataView(e)):void 0},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"values",{get:function(){var e=this;return this.rawValues.pipe(r.map((function(t){return e.decodeValue(t)})))},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"rawValueSnaphot",{get:function(){var e=this.context.variable.rawValueSnaphot;return void 0!==e?this.subDataView(e):e},enumerable:!1,configurable:!0}),t.prototype.notifyRawValue=function(e){},t.prototype.read=function(){return P(this,void 0,void 0,(function(){var e;return l(this,(function(t){switch(t.label){case 0:return e=this.decodeValue,[4,this.readRaw()];case 1:return[2,e.apply(this,[t.sent()])]}}))}))},t.prototype.write=function(e){return this.writeRaw(this.encodeValue(e))},t.prototype.readRaw=function(){return P(this,void 0,void 0,(function(){var e;return l(this,(function(t){switch(t.label){case 0:return[4,this.context.variable.readRaw()];case 1:return e=t.sent(),[2,this.subDataView(e)]}}))}))},t.prototype.writeRaw=function(e){return P(this,void 0,void 0,(function(){var t,r;return l(this,(function(n){switch(n.label){case 0:return this.context.useLastKnownValueBeforeWrite?(r=this.context.variable.rawValueSnaphot)?[3,2]:[4,this.context.variable.readRaw()]:[3,3];case 1:r=n.sent(),n.label=2;case 2:return t=r,[3,5];case 3:return[4,this.context.variable.readRaw()];case 4:t=n.sent(),n.label=5;case 5:return this.indexes.forEach((function(r,n){t[r]=e[n]})),[4,this.context.variable.writeRaw(t)];case 6:return n.sent(),[2]}}))}))},t.prototype.subDataView=function(e){return this.indexes.reduce((function(t,r,n){return t[n]=e[r],t}),new Uint8Array(this.indexes.length))},t.prototype.encodeValue=function(t){try{return this.converter.encode(t)}catch(r){throw e.TapDataError.unexpectedEncodedValueError(this,t,r)}},t.prototype.decodeValue=function(t){try{return this.converter.decode(t)}catch(r){throw e.TapDataError.unexpectedVariableDataRead(this,t,r)}},t}(),B=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return s(t,e),t.prototype.variableView=function(e,t,r){return new I(e,{converter:(null==r?void 0:r.converter)||x,indexes:t,variable:this,useLastKnownValueBeforeWrite:null==r?void 0:r.useLastKnownValueBeforeWrite})},t}(N),z=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(i,a){function o(e){try{c(n.next(e))}catch(e){a(e)}}function u(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,u)}c((n=n.apply(e,t||[])).next())}))},M=function(e){function t(t,r){var n=e.call(this)||this;return n.id=t,n.context=r,n}return s(t,e),Object.defineProperty(t.prototype,"converter",{get:function(){return this.context.converter||x},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"config",{get:function(){return this.context.config},enumerable:!1,configurable:!0}),t.prototype.readRawInner=function(){return z(this,void 0,void 0,(function(){return l(this,(function(e){switch(e.label){case 0:return[4,this.context.targetService.modbusRead(this.context.config)];case 1:return[2,e.sent().body()]}}))}))},t.prototype.writeRaw=function(e){return z(this,void 0,void 0,(function(){var t;return l(this,(function(r){switch(r.label){case 0:if(!(e&&e instanceof Uint8Array))throw new Error("Bad parameter for ModbusAccessVariable.writeRaw(). Expecting an Uint8Array.");if(t=this.config.length*S(this.config.format),e.length!==t)throw new Error("Encoded value does not have the expected byte size. Expecting "+t+" byte(s) but given "+e.length+" byte(s)");return[4,this.context.targetService.modbusWrite({data:e,options:this.context.config})];case 1:return[2,r.sent().body()]}}))}))},t}(B),A=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(i,a){function o(e){try{c(n.next(e))}catch(e){a(e)}}function u(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,u)}c((n=n.apply(e,t||[])).next())}))};function F(e,t,r){return new U(t,{variableService:e,config:{id:r.id,dataType:r.dataType,length:r.length},converter:r.converter||(void 0!==r.dataType?j(r.dataType,r.length||1):x)})}var U=function(t){function r(e,r){var n=t.call(this)||this;return n.id=e,n.context=r,n}return s(r,t),Object.defineProperty(r.prototype,"variableId",{get:function(){return this.context.config.id},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"converter",{get:function(){return this.context.converter||x},set:function(e){this.context.converter=e},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"config",{get:function(){return this.context.config},enumerable:!1,configurable:!0}),r.prototype.readRawInner=function(){return A(this,void 0,void 0,(function(){var t,r;return l(this,(function(a){switch(a.label){case 0:return a.trys.push([0,2,,3]),[4,this.context.variableService.getValue(this.context.config.id)];case 1:return t=a.sent().body(),this.context.subIndex&&(t=this.context.subIndex.reduce((function(e,r,n){return e[n]=t[r],e}),new Uint8Array(this.context.subIndex.length))),[2,t];case 2:if(r=a.sent(),n.isCodeError(i.TapError.Code.ResponseStatusError,r)&&r.response.status===u.ResultCode.NOT_FOUND)throw e.TapDataError.variableNotConfigured(this.context.config,r);throw r;case 3:return[2]}}))}))},r.prototype.writeRaw=function(t){return A(this,void 0,void 0,(function(){var r,a;return l(this,(function(o){switch(o.label){case 0:return o.trys.push([0,4,,5]),this.context.subIndex?[4,this.context.variableService.getValue(this.context.config.id)]:[3,2];case 1:r=o.sent().body(),this.context.subIndex.forEach((function(e,n){r[e]=t[n]})),t=r,o.label=2;case 2:return[4,this.context.variableService.setValue(this.context.config.id,t)];case 3:return o.sent().body(),[3,5];case 4:if(a=o.sent(),n.isCodeError(i.TapError.Code.ResponseStatusError,a)&&a.response.status===u.ResultCode.NOT_FOUND)throw e.TapDataError.variableNotConfigured(this.context.config,a);throw a;case 5:return[2]}}))}))},r}(B),L=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(i,a){function o(e){try{c(n.next(e))}catch(e){a(e)}}function u(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,u)}c((n=n.apply(e,t||[])).next())}))},W=new a.TlvBundleConverter;function k(e,t,r){var n=Object.keys(r.variables).reduce((function(t,n){var i=r.variables[n];return t[n]=F(e.service.variable,n,i),t}),{});return new q(t,{tap:e,config:{id:r.id}},n)}var q=function(t){function r(e,r,n){var i=t.call(this,n)||this;return i._id=e,i.context=r,i}return s(r,t),Object.defineProperty(r.prototype,"id",{get:function(){return this._id},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"bundleId",{get:function(){return this.context.config.id},enumerable:!1,configurable:!0}),r.prototype.readRawInner=function(){return L(this,void 0,void 0,(function(){var t,r,a,o,c,s,d,p;return l(this,(function(l){switch(l.label){case 0:return l.trys.push([0,2,,3]),[4,this.context.tap.service.bundle.getValues(this.bundleId)];case 1:t=l.sent().body(W),r={};try{for(a=f(this.variables),o=a.next();!o.done;o=a.next())(c=o.value)instanceof U&&(c.variableId in t?r[c.id]=t[c.variableId]:r[c.id]=void 0)}catch(e){d={error:e}}finally{try{o&&!o.done&&(p=a.return)&&p.call(a)}finally{if(d)throw d.error}}return[2,r];case 2:if(s=l.sent(),n.isCodeError(i.TapError.Code.ResponseStatusError,s)&&s.response.status===u.ResultCode.NOT_FOUND)throw e.TapDataError.bundleNotConfigured(this.context.config,s);throw s;case 3:return[2]}}))}))},r}(R),K=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(i,a){function o(e){try{c(n.next(e))}catch(e){a(e)}}function u(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,u)}c((n=n.apply(e,t||[])).next())}))},Y=function(e){function t(t,r){var n=e.call(this)||this;return n.id=t,n.context=r,n}return s(t,e),Object.defineProperty(t.prototype,"converter",{get:function(){return this.context.converter||x},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"config",{get:function(){return this.context.config},enumerable:!1,configurable:!0}),t.prototype.readRawInner=function(){return K(this,void 0,void 0,(function(){return l(this,(function(e){switch(e.label){case 0:return[4,this.context.targetService.readAddress(this.context.config)];case 1:return[2,e.sent().body()]}}))}))},t.prototype.writeRaw=function(e){return K(this,void 0,void 0,(function(){var t;return l(this,(function(r){switch(r.label){case 0:if(!(e&&e instanceof Uint8Array))throw new Error("Bad parameter for TargetMemoryVariable.writeRaw(). Expecting an Uint8Array.");if(t=this.config.length*S(this.config.format),e.length!==t)throw new Error("Encoded value does not have the expected byte size. Expecting "+t+" byte(s) but given "+e.length+" byte(s)");return[4,this.context.targetService.writeAddress({data:e,info:this.context.config})];case 1:return[2,r.sent().body()]}}))}))},t}(B);function G(e){var t;return{bundles:null===(t=null==e?void 0:e.bundles)||void 0===t?void 0:t.reduce((function(e,t){var r,n=t.name||t.id,i=null===(r=t.variables)||void 0===r?void 0:r.reduce((function(e,t){return e[("object"==typeof t.meta?t.meta.name:t.id)||t.id]={id:t.id,dataType:o.VariableType.Data[t.dataType],length:t.length||1,converter:C(t)},e}),{}),a={id:t.id,variables:i||{}};return e[n]=a,e}),{})}}var H=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(i,a){function o(e){try{c(n.next(e))}catch(e){a(e)}}function u(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,u)}c((n=n.apply(e,t||[])).next())}))};function J(e,t){var r={};for(var n in t)r[n]=k(e,n,t[n]);return r}function Q(e,t){return void 0===t&&(t={bundleIdNameMap:{},variableIdNameMap:{}}),H(this,void 0,void 0,(function(){var r,n,i,a,o,u,c,s,f,d,p,h,b,v,y,w,g,m,x,V,T;return l(this,(function(l){switch(l.label){case 0:return r=t.bundleIdNameMap,n=t.variableIdNameMap,i=t.observer,[4,e.service.target.connect()];case 1:return l.sent().successful(),[4,e.service.variable.readProfile()];case 2:for(p in(a=l.sent()).successful(),o=a.rawBody(),u=W.decode(o),c={},s=Object.keys(u).length,f=0,null==i||i.next({step:"start-read-variable-config",profileData:u,progress:{loaded:f,total:s}}),d=[],u)d.push(p);h=0,l.label=3;case 3:if(!(h<d.length))return[3,9];if(b=d[h],v=parseInt(b,10),isNaN(v))throw new Error("Invalid variable integer identifier: "+b);return null==i||i.next({step:"read-variable",variableId:v,progress:{loaded:f+1,total:s}}),[4,X(e,v,n?n[v]:void 0)];case 4:return y=l.sent(),w=y.bundleId,g=y.variableName,m=y.config,x=void 0,r&&w in r?(x=r[w],[3,7]):[3,5];case 5:return[4,e.service.bundle.getName(w)];case 6:V=l.sent().body(),(T=V.indexOf("\0"))>=0&&(V=V.substr(0,T)),x=V||w,l.label=7;case 7:x in c||(c[x]={id:w,variables:{}}),c[x].variables[g]=m,f++,l.label=8;case 8:return h++,[3,3];case 9:return null==i||i.next({step:"end-read-variable-config",progress:{loaded:s,total:s}}),[2,c]}}))}))}function X(e,t,r){return H(this,void 0,void 0,(function(){var n,i,a,o,u,c,s,f,p,h;return l(this,(function(l){switch(l.label){case 0:return[4,e.service.interface.executeMultipleCalls([e.service.variable.getTypeCall(t),e.service.variable.getBundleIdCall(t),e.service.variable.getNumberOfElementsCall(t),e.service.variable.getMetaCall(t)])];case 1:return n=d.apply(void 0,[l.sent(),4]),i=n[0],a=n[1],o=n[2],u=n[3],c=a.body(),s=i.body(),f=o.body(),p=s.data,r||(h=u.body(),r=h.name||t.toString()),[2,{bundleId:c,variableName:r,config:{id:t,dataType:p,length:f}}]}}))}))}var Z,$=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(i,a){function o(e){try{c(n.next(e))}catch(e){a(e)}}function u(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,u)}c((n=n.apply(e,t||[])).next())}))},ee=function(){function n(e,r,n){var i=this;void 0===n&&(n=new b),this.tap=e,this.bundles=r,this.monitoring=n,this._events=new t.Subject,g(this.monitoring).subscribe((function(e){return $(i,void 0,void 0,(function(){return l(this,(function(e){switch(e.label){case 0:return[4,this.refreshValues()];case 1:return e.sent(),[2]}}))}))}))}return Object.defineProperty(n.prototype,"values",{get:function(){var e=this.listBundles();return t.combineLatest(e.map((function(e){return e.values.pipe(r.startWith(void 0))}))).pipe(r.map((function(t){return t.reduce((function(t,r,n){return t[e[n].id]=r,t}),{})})))},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"events",{get:function(){return this._events.asObservable()},enumerable:!1,configurable:!0}),n.prototype.listVariables=function(){return this.listBundles().reduce((function(e,t){return e.push.apply(e,function(e,t,r){if(r||2===arguments.length)for(var n,i=0,a=t.length;i<a;i++)!n&&i in t||(n||(n=Array.prototype.slice.call(t,0,i)),n[i]=t[i]);return e.concat(n||Array.prototype.slice.call(t))}([],d(t.variables))),e}),[])},n.prototype.bundle=function(t){if(!(t in this.bundles))throw e.TapDataError.bundleNotFound(t);return this.bundles[t]},n.prototype.variable=function(t){var r=this.listVariables().find((function(e){return e.id===t}));if(!r)throw e.TapDataError.variableNotFound(t);return r},n.prototype.destroy=function(){this.monitoring.stop()},n.prototype.refreshValues=function(){return $(this,void 0,void 0,(function(){var t,r,n,i,a,o,u,c,s,d=this;return l(this,(function(l){switch(l.label){case 0:t={},l.label=1;case 1:l.trys.push([1,6,7,8]),r=f(this.listBundles()),n=r.next(),l.label=2;case 2:return n.done?[3,5]:(i=n.value,a=t,o=i.id,[4,i.read().catch((function(t){return d._events.next({error:t,type:e.DataManager.EventType.error}),t}))]);case 3:a[o]=l.sent(),l.label=4;case 4:return n=r.next(),[3,2];case 5:return[3,8];case 6:return u=l.sent(),c={error:u},[3,8];case 7:try{n&&!n.done&&(s=r.return)&&s.call(r)}finally{if(c)throw c.error}return[7];case 8:return[2,t]}}))}))},n.prototype.listBundles=function(){return Object.values(this.bundles)},n.prototype.hasBundle=function(e){return e in this.bundles},n.prototype.createConfiguredBundle=function(e,t){return k(this.tap,e,t)},n.prototype.createConfiguredVariable=function(e,t){return F(this.tap.service.variable,e,t)},n.prototype.createModbusAccessVariable=function(e,t){return new M(e,{config:t.config,targetService:this.tap.service.target,converter:t.converter})},n.prototype.createTargetMemoryVariable=function(e,t){return new Y(e,{config:t.config,targetService:this.tap.service.target,converter:t.converter})},n}();e.DataManager=function(e){function r(t,r,n){return void 0===r&&(r={}),void 0===n&&(n=new b),e.call(this,t,r,n)||this}return s(r,e),r.prototype.setBundles=function(e){this.bundles=e},r.prototype.addBundle=function(e){this.bundles[e.id]=e},r.prototype.removeBundle=function(e){delete this.bundles[e]},r.prototype.addVariable=function(e,t){this.bundle(e).variables.push(t)},r.prototype.registerBundles=function(e){var t=J(this.tap,e);return this.setBundles(t),t},r.prototype.registerBundle=function(e,t){var r=k(this.tap,e,t);this.addBundle(r)},r.prototype.clear=function(){this.bundles={}},r.prototype.synchronizeTapConfig=function(){var e=this;return new t.Observable((function(t){try{Q(e.tap,{observer:t}).then((function(r){var n=e.registerBundles(r);t.next({step:"done",bundles:n}),t.complete()})).catch((function(e){t.error(e)}))}catch(e){t.error(e)}}))},r.prototype.configureWithDataConfig=function(e){this.clear(),this.registerBundles(G(e).bundles)},r}(ee),Z=e.DataManager||(e.DataManager={}),(Z.EventType||(Z.EventType={})).error="error";var te=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(i,a){function o(e){try{c(n.next(e))}catch(e){a(e)}}function u(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,u)}c((n=n.apply(e,t||[])).next())}))},re=function(e){function t(t,r){var n=e.call(this,r)||this;return n.id=t,n}return s(t,e),t.prototype.readRawInner=function(){return te(this,void 0,void 0,(function(){var e,t,r,n,i,a,o,u,c;return l(this,(function(s){switch(s.label){case 0:e={},s.label=1;case 1:s.trys.push([1,6,7,8]),t=f(this.variables),r=t.next(),s.label=2;case 2:return r.done?[3,5]:(n=r.value,i=e,a=n.id,[4,n.readRaw()]);case 3:i[a]=s.sent(),s.label=4;case 4:return r=t.next(),[3,2];case 5:return[3,8];case 6:return o=s.sent(),u={error:o},[3,8];case 7:try{r&&!r.done&&(c=t.return)&&c.call(t)}finally{if(u)throw u.error}return[7];case 8:return[2,e]}}))}))},t}(R),ne=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(i,a){function o(e){try{c(n.next(e))}catch(e){a(e)}}function u(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,u)}c((n=n.apply(e,t||[])).next())}))},ie=function(){function t(e,t){this.id=e,this.context=t}return Object.defineProperty(t.prototype,"converter",{get:function(){return this.context.converter},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"valueSnapshot",{get:function(){var e=this.context.variable.rawValueSnaphot;return void 0!==e?this.decodeValue(e):void 0},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"values",{get:function(){var e=this;return this.context.variable.rawValues.pipe(r.map((function(t){return e.decodeValue(t)})))},enumerable:!1,configurable:!0}),t.prototype.read=function(){return ne(this,void 0,void 0,(function(){var e;return l(this,(function(t){switch(t.label){case 0:return[4,this.context.variable.readRaw()];case 1:return e=t.sent(),[2,this.decodeValue(e)]}}))}))},t.prototype.write=function(e){return ne(this,void 0,void 0,(function(){var t;return l(this,(function(r){switch(r.label){case 0:return[4,this.getCurrentValueStream()];case 1:return t=r.sent(),this.encodeValue(e,t),t.pos=t.getStreamSize(),[2,this.context.variable.writeRaw(t.toBytes)]}}))}))},t.prototype.getCurrentValueStream=function(){return ne(this,void 0,void 0,(function(){var e;return l(this,(function(t){switch(t.label){case 0:return this.context.useLastKnownValueBeforeWrite?(e=this.context.variable.rawValueSnaphot,[3,3]):[3,1];case 1:return[4,this.context.variable.readRaw()];case 2:e=t.sent(),t.label=3;case 3:return e||(e=new Uint8Array),[2,new a.TapStreamWriter(e)]}}))}))},t.prototype.encodeValue=function(t,r){try{this.converter.encode(t,r)}catch(r){throw e.TapDataError.unexpectedEncodedValueError(this,t,r)}},t.prototype.decodeValue=function(t){try{var r=a.TapStreamReader.create(t);return this.converter.decode(r)}catch(r){throw e.TapDataError.unexpectedVariableDataRead(this,t,r)}},t}();e.AbstractEditableDataStream=N,e.AbstractVariable=B,e.ConverterVariableView=ie,e.ModbusAccessVariable=M,e.MonitoringController=b,e.TapBundle=q,e.TapVariable=U,e.TargetMemoryVariable=Y,e.TypedDataManager=ee,e.VariableView=I,e.VirtualBundle=re,e.computeBitLengthFromFormat=T,e.computeByteLengthFromFormat=function(e){return Math.ceil(T(e)/8)},e.createDataManagerConfigFromTapDataConfig=G,e.createDataManagerFromTapConfig=function(e,t){var r=t.bundles;return new ee(e,J(e,r))},e.createMonitoringTicker=g,e.createTapBundleFromConfig=k,e.createTapVariableFromConfig=F,e.getConverterFromVariableConfig=C,e.getConverterFromVariableType=j,e.readDataManagerTapConfigFromTap=Q,e.readTapVariableConfigFromTap=X,e.variableDataTypeToByteSize=O,e["ɵa"]=E,e["ɵc"]=R,e["ɵd"]=x,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=iotize-tap-data.umd.min.js.map