qcloud-iotexplorer-appdev-sdk
Advanced tools
Comparing version 2.3.12 to 2.3.13
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.AppDevSdk=exports.AppDevPlugin=void 0;var tslib_1=require("tslib"),event_emitter_for_miniprogram_1=tslib_1.__importDefault(require("event-emitter-for-miniprogram")),modules_1=require("./modules"),utils_1=require("./utils"),utils=tslib_1.__importStar(require("./utils")),constants=tslib_1.__importStar(require("./constants")),ReportLevel={INFO:0,WARN:1,ERROR:2},AppDevPlugin=function(){function e(){}return e.install=function(e){},e}();exports.AppDevPlugin=AppDevPlugin;var AppDevSdk=function(e){function t(t){var n=t.getAccessToken,i=t.appKey,r=void 0===i?"":i,s=t.apiPlatform,o=void 0===s?"":s,a=t.apiRegionId,c=void 0===a?1:a,u=t.debug,l=void 0!==u&&u,d=t.apiUrl,p=t.wsConfig,_=t.reporter,v=t.plugins,h=void 0===v?[]:v,f=t.defaultUin,g=void 0===f?"unknown":f,b=t.enableAccessTokenCache,y=void 0===b||b,m=e.call(this)||this;m.isManuallyClose=!1,m._defaultFamilyIdPromise=null,m._connectWsWhenInit=!0,m.constants=constants,m.utils=utils,m.plugins={},m._defaultUin=g,m._apiUrl=d,m._debug=l;var w=_||utils_1.noop,P=function(e){modules_1.appDevSdkLogger.warn("reporter","report fail",{error:e})};m.reporter={info:function(e,t){void 0===t&&(t={});try{w(e,tslib_1.__assign({level:ReportLevel.INFO},t))}catch(e){P(e)}},warn:function(e,t){void 0===t&&(t={});try{w(e,tslib_1.__assign({level:ReportLevel.WARN},t))}catch(e){P(e)}},error:function(e,t){void 0===t&&(t={});try{w(e,tslib_1.__assign({level:ReportLevel.ERROR},t))}catch(e){P(e)}}};var D=p||{},k=D.autoReconnect,I=void 0===k||k,W=D.disconnectWhenAppHide,L=void 0===W||W,A=D.connectWhenAppShow,E=void 0===A||A,R=D.connectWhenInit,S=void 0===R||R,T=tslib_1.__rest(D,["autoReconnect","disconnectWhenAppHide","connectWhenAppShow","connectWhenInit"]);return m.ws=new modules_1.IotWebsocket(m,tslib_1.__assign(tslib_1.__assign({},T),{apiPlatform:o})),m.loginManager=new modules_1.LoginManager(m,{getAccessToken:n,appKey:r,enableAccessTokenCache:y}),m._apiPlatform=o,m._apiRegionId=c,m.loginManager.on(constants.EventTypes.LoginStateChange,(function(e){return m.emit(constants.EventTypes.LoginStateChange,{isLogin:e})})),m.ws.on("error",(function(e){m.emit(constants.EventTypes.WsError,e),I&&m._reconnectWs()})),m.ws.on("close",(function(e){var t=void 0===e?{}:e,n=t.code,i=t.reason;m.emit(constants.EventTypes.WsClose,{code:n,reason:i}),I&&m._onWebsocketClose()})),m.ws.on("push",(function(e){return m._handlePushEvent(e)})),utils_1.envDetect.isMiniProgram&&(wx.onAppHide((function(){L&&(m.isManuallyClose=!0,m.ws.disconnect())})),wx.onAppShow((function(){E&&m.isLogin&&m.ws.connect()}))),(null==h?void 0:h.length)&&h.forEach((function(e){return m.usePlugin(e)})),m._connectWsWhenInit=S,m}return tslib_1.__extends(t,e),t.prototype.usePlugin=function(e){e.install(this)},Object.defineProperty(t.prototype,"userInfo",{get:function(){return this.loginManager.userInfo},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isLogin",{get:function(){return this.loginManager.isLogin},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"userId",{get:function(){return this.loginManager.userId},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"uin",{get:function(){return this.loginManager.userId},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"nickName",{get:function(){return this.loginManager.nickName},enumerable:!1,configurable:!0}),t.prototype.init=function(e){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t=this;return tslib_1.__generator(this,(function(n){return e||(e={}),e.reload&&(this._initPromise=null),this.isLogin?[2,Promise.resolve()]:[2,this._initPromise||(this._initPromise=new Promise((function(n,i){return tslib_1.__awaiter(t,void 0,void 0,(function(){var t;return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return r.trys.push([0,4,5,6]),[4,this.loginManager.login({reload:e.reload})];case 1:return r.sent(),this._connectWsWhenInit?[4,this.ws.connect()]:[3,3];case 2:r.sent(),r.label=3;case 3:return n(),[3,6];case 4:return t=r.sent(),i(utils_1.normalizeError(t)),[3,6];case 5:return this._initPromise=null,[7];case 6:return[2]}}))}))})))]}))}))},t.prototype.getDefaultFamilyId=function(){var e=this;return this._defaultFamilyIdPromise||(this._defaultFamilyIdPromise=new Promise((function(t,n){return tslib_1.__awaiter(e,void 0,void 0,(function(){var e,i,r,s;return tslib_1.__generator(this,(function(o){switch(o.label){case 0:return o.trys.push([0,4,,5]),[4,this.requestApi("AppGetFamilyList",{Offset:0,Limit:100})];case 1:return e=o.sent(),i=e.FamilyList,e.Total?[3,3]:[4,this.requestApi("AppCreateFamily",{Name:this.loginManager.nickName})];case 2:return r=o.sent().Data.FamilyId,[2,t(r)];case 3:return t(i[0].FamilyId),[3,5];case 4:return s=o.sent(),n(s),this._defaultFamilyIdPromise=null,[3,5];case 5:return[2]}}))}))})))},t.prototype.sendWebsocketMessage=function(e,t){return void 0===t&&(t={}),tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(n){switch(n.label){case 0:return[4,this.init()];case 1:return n.sent(),[2,this.ws.send(e,t)]}}))}))},t.prototype.connectWebsocket=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return[4,this.init()];case 1:return e.sent(),[4,this.ws.connect()];case 2:return e.sent(),[2]}}))}))},t.prototype.disconnectWebsocket=function(){this.ws.disconnect()},t.prototype.subscribeDevices=function(e){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t;return tslib_1.__generator(this,(function(n){switch(n.label){case 0:modules_1.appDevSdkLogger.info("subscribeDevices","start"),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,this.ws.connect((e||[]).map((function(e){return"string"==typeof e?e:(null==e?void 0:e.DeviceId)?e.DeviceId:null})).filter(Boolean))];case 2:return n.sent(),[3,4];case 3:return t=n.sent(),modules_1.appDevSdkLogger.error("subscribeDevices","fail",{error:t}),[3,4];case 4:return[2]}}))}))},t.prototype.requestApi=function(e,t,n){void 0===t&&(t={}),void 0===n&&(n={});var i=n.doNotRetry,r=void 0!==i&&i,s=n.needLogin,o=void 0===s||s,a=n.doNotReport,c=void 0!==a&&a,u=tslib_1.__rest(n,["doNotRetry","needLogin","doNotReport"]);return tslib_1.__awaiter(this,void 0,void 0,(function(){var n,i,s,a,l,d,p;return tslib_1.__generator(this,(function(_){switch(_.label){case 0:return _.trys.push([0,6,,13]),o?[4,this.loginManager.checkLogin()]:[3,2];case 1:_.sent(),_.label=2;case 2:return n=this.loginManager,i=n.accessToken,s=n.userId,t&&"default"===t.FamilyId?(a=t,[4,this.getDefaultFamilyId()]):[3,4];case 3:a.FamilyId=_.sent(),_.label=4;case 4:return l=tslib_1.__assign({uin:this._debug?this._defaultUin:s,RegionId:this._apiRegionId},t),i&&(l.AccessToken=i),this._apiPlatform&&(l.Platform=this._apiPlatform),[4,modules_1.requestTokenApi(e,l,tslib_1.__assign({doNotReport:c,url:this._apiUrl},u))];case 5:return[2,_.sent()];case 6:if(d=_.sent(),modules_1.appDevSdkLogger.error("requestApi","fail",{error:d}),!utils_1.isVerifyLoginError(d))return[3,12];if(r)return[3,11];_.label=7;case 7:return _.trys.push([7,9,,10]),[4,this.loginManager.reLogin()];case 8:return _.sent(),[3,10];case 9:return p=_.sent(),modules_1.appDevSdkLogger.error("requestApi","reLogin fail",{error:p}),[2,Promise.reject(utils_1.genVerifyLoginFailError(d))];case 10:return[2,this.requestApi(e,t,tslib_1.__assign({doNotRetry:!0},u))];case 11:return[2,Promise.reject(utils_1.genVerifyLoginFailError(d))];case 12:return[2,Promise.reject(utils_1.normalizeError(d))];case 13:return[2]}}))}))},t.prototype._handlePushEvent=function(e){var t,n,i,r,s=modules_1.appDevSdkLogger.event("handlePushEvent");e||(e={}),this.emit(constants.EventTypes.WsPush,e);var o=e.action,a=e.params;a||(a={});var c=a.DeviceId,u=a.Type,l=a.SubType,d=a.Time,p=a.Payload,_=new Date(d).getTime();if(p)try{p=utils_1.base64.decode(p);var v=String.fromCharCode(0);~p.indexOf(v)&&(p=p.replaceAll(v,"")),p=JSON.parse(p)}catch(e){return void s.error("parse payload fail",{error:e})}if(s.debug("payload",{data:p}),"DeviceChange"===o)switch(u){case"Property":case"Shadow":case"Template":switch(l){case"Report":var h={};try{if(p){var f=p.method,g=p.params,b=p.type,y=p.state;if(b&&"update"===b&&y&&y.reported&&(f="report",g=y.reported),g||(g={}),"report"===f)try{for(var m=tslib_1.__values(Object.keys(g)),w=m.next();!w.done;w=m.next()){h[I=w.value]={Value:g[I],lastUpdate:_,LastUpdate:_}}}catch(e){t={error:e}}finally{try{w&&!w.done&&(n=m.return)&&n.call(m)}finally{if(t)throw t.error}}}}catch(e){s.error("parse template report fail",{error:e})}this.emit(constants.EventTypes.WsReport,{deviceId:c,deviceData:h});break;case"Push":h={};try{if(p){f=p.method,g=p.params,b=p.type;var P=p.payload;if(b&&"delta"===b&&P&&P.state&&(f="control",g=P.state),"control"===f&&g){try{for(var D=tslib_1.__values(Object.keys(g)),k=D.next();!k.done;k=D.next()){var I;h[I=k.value]={Value:g[I],LastUpdate:_}}}catch(e){i={error:e}}finally{try{k&&!k.done&&(r=D.return)&&r.call(D)}finally{if(i)throw i.error}}this.emit(constants.EventTypes.WsControl,{deviceId:c,deviceData:h})}}}catch(e){s.error("parse template push fail",{error:e})}}break;case"Event":if("Report"===l)this.emit(constants.EventTypes.WsEventReport,{Payload:p,deviceId:c});break;case"Action":switch(l){case"Push":this.emit(constants.EventTypes.WsActionPush,{Payload:p,deviceId:c});break;case"Report":this.emit(constants.EventTypes.WsActionReport,{Payload:p,deviceId:c})}break;case"StatusChange":switch(l){case"Bind":this.emit(constants.EventTypes.WsDeviceBind,{Payload:p,deviceId:c});break;case"Unbind":this.emit(constants.EventTypes.WsDeviceUnbind,{Payload:p,deviceId:c});break;case"Online":case"Offline":var W="Online"===l?1:0;this.emit(constants.EventTypes.WsStatusChange,{deviceId:c,deviceStatus:W});break;default:s.warn("unknown subtype 【"+l+"】 of statusChange")}case"Gateway":switch(l){case"bind":this.emit(constants.EventTypes.WsSubDeviceBind,{gatewayDeviceId:c,subDeviceList:p}),this.emit(constants.EventTypes.WsSubDeviceChange,{gatewayDeviceId:c,subDeviceList:p,action:"bind"});break;case"unbind":this.emit(constants.EventTypes.WsSubDeviceUnbind,{gatewayDeviceId:c,subDeviceList:p}),this.emit(constants.EventTypes.WsSubDeviceChange,{gatewayDeviceId:c,subDeviceList:p,action:"unbind"})}}},t.prototype._onWebsocketClose=function(){if(!this.isManuallyClose)return this._reconnectWs();this.isManuallyClose=!1},t.prototype._reconnectWs=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e;return tslib_1.__generator(this,(function(t){switch(t.label){case 0:return t.trys.push([0,3,,4]),modules_1.appDevSdkLogger.info("reconnectWs","websocket reconnecting in 2 seconds"),[4,utils_1.delay(2e3)];case 1:return t.sent(),[4,this.ws.connect()];case 2:return t.sent(),[3,4];case 3:return e=t.sent(),modules_1.appDevSdkLogger.error("reconnectWs","fail",{error:e}),[2,Promise.reject(e)];case 4:return[2]}}))}))},t.utils=utils,t.constants=constants,t}(event_emitter_for_miniprogram_1.default);exports.AppDevSdk=AppDevSdk; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.AppDevSdk=exports.AppDevPlugin=void 0;var tslib_1=require("tslib"),event_emitter_for_miniprogram_1=tslib_1.__importDefault(require("event-emitter-for-miniprogram")),modules_1=require("./modules"),utils_1=require("./utils"),utils=tslib_1.__importStar(require("./utils")),constants=tslib_1.__importStar(require("./constants")),ReportLevel={INFO:0,WARN:1,ERROR:2},AppDevPlugin=function(){function e(){}return e.install=function(e){},e}();exports.AppDevPlugin=AppDevPlugin;var AppDevSdk=function(e){function t(t){var n=t.getAccessToken,i=t.appKey,r=void 0===i?"":i,s=t.apiPlatform,o=void 0===s?"":s,a=t.apiRegionId,c=void 0===a?1:a,u=t.debug,l=void 0!==u&&u,p=t.apiUrl,d=t.wsConfig,_=t.reporter,v=t.plugins,h=void 0===v?[]:v,g=t.defaultUin,f=void 0===g?"unknown":g,b=t.enableAccessTokenCache,y=void 0===b||b,m=e.call(this)||this;m.isManuallyClose=!1,m._defaultFamilyIdPromise=null,m._connectWsWhenInit=!0,m.constants=constants,m.utils=utils,m.plugins={},m._defaultUin=f,m._apiUrl=p,m._debug=l;var w=_||utils_1.noop,P=function(e){modules_1.appDevSdkLogger.warn("reporter","report fail",{error:e})};m.reporter={info:function(e,t){void 0===t&&(t={});try{w(e,tslib_1.__assign({level:ReportLevel.INFO},t))}catch(e){P(e)}},warn:function(e,t){void 0===t&&(t={});try{w(e,tslib_1.__assign({level:ReportLevel.WARN},t))}catch(e){P(e)}},error:function(e,t){void 0===t&&(t={});try{w(e,tslib_1.__assign({level:ReportLevel.ERROR},t))}catch(e){P(e)}}};var D=d||{},k=D.autoReconnect,I=void 0===k||k,W=D.disconnectWhenAppHide,L=void 0===W||W,A=D.connectWhenAppShow,E=void 0===A||A,R=D.connectWhenInit,T=void 0===R||R,S=tslib_1.__rest(D,["autoReconnect","disconnectWhenAppHide","connectWhenAppShow","connectWhenInit"]);return m.ws=new modules_1.IotWebsocket(m,tslib_1.__assign(tslib_1.__assign({},S),{apiPlatform:o})),m.loginManager=new modules_1.LoginManager(m,{getAccessToken:n,appKey:r,enableAccessTokenCache:y}),m._apiPlatform=o,m._apiRegionId=c,m.loginManager.on(constants.EventTypes.LoginStateChange,(function(e){return m.emit(constants.EventTypes.LoginStateChange,{isLogin:e})})),m.ws.on("error",(function(e){m.emit(constants.EventTypes.WsError,e),I&&m._reconnectWs()})),m.ws.on("close",(function(e){var t=void 0===e?{}:e,n=t.code,i=t.reason;m.emit(constants.EventTypes.WsClose,{code:n,reason:i}),I&&m._onWebsocketClose()})),m.ws.on("push",(function(e){m.emit(constants.EventTypes.WsPush,e)})),m.ws.on("pushWithParsedData",(function(e){return m._handlePushEvent(e)})),utils_1.envDetect.isMiniProgram&&(wx.onAppHide((function(){L&&(m.isManuallyClose=!0,m.ws.disconnect())})),wx.onAppShow((function(){E&&m.isLogin&&m.ws.connect()}))),(null==h?void 0:h.length)&&h.forEach((function(e){return m.usePlugin(e)})),m._connectWsWhenInit=T,m}return tslib_1.__extends(t,e),t.prototype.usePlugin=function(e){e.install(this)},Object.defineProperty(t.prototype,"userInfo",{get:function(){return this.loginManager.userInfo},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isLogin",{get:function(){return this.loginManager.isLogin},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"userId",{get:function(){return this.loginManager.userId},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"uin",{get:function(){return this.loginManager.userId},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"nickName",{get:function(){return this.loginManager.nickName},enumerable:!1,configurable:!0}),t.prototype.init=function(e){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t=this;return tslib_1.__generator(this,(function(n){return e||(e={}),e.reload&&(this._initPromise=null),this.isLogin?[2,Promise.resolve()]:[2,this._initPromise||(this._initPromise=new Promise((function(n,i){return tslib_1.__awaiter(t,void 0,void 0,(function(){var t;return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return r.trys.push([0,4,5,6]),[4,this.loginManager.login({reload:e.reload})];case 1:return r.sent(),this._connectWsWhenInit?[4,this.ws.connect()]:[3,3];case 2:r.sent(),r.label=3;case 3:return n(),[3,6];case 4:return t=r.sent(),i(utils_1.normalizeError(t)),[3,6];case 5:return this._initPromise=null,[7];case 6:return[2]}}))}))})))]}))}))},t.prototype.getDefaultFamilyId=function(){var e=this;return this._defaultFamilyIdPromise||(this._defaultFamilyIdPromise=new Promise((function(t,n){return tslib_1.__awaiter(e,void 0,void 0,(function(){var e,i,r,s;return tslib_1.__generator(this,(function(o){switch(o.label){case 0:return o.trys.push([0,4,,5]),[4,this.requestApi("AppGetFamilyList",{Offset:0,Limit:100})];case 1:return e=o.sent(),i=e.FamilyList,e.Total?[3,3]:[4,this.requestApi("AppCreateFamily",{Name:this.loginManager.nickName})];case 2:return r=o.sent().Data.FamilyId,[2,t(r)];case 3:return t(i[0].FamilyId),[3,5];case 4:return s=o.sent(),n(s),this._defaultFamilyIdPromise=null,[3,5];case 5:return[2]}}))}))})))},t.prototype.sendWebsocketMessage=function(e,t){return void 0===t&&(t={}),tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(n){switch(n.label){case 0:return[4,this.init()];case 1:return n.sent(),[2,this.ws.send(e,t)]}}))}))},t.prototype.connectWebsocket=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return[4,this.init()];case 1:return e.sent(),[4,this.ws.connect()];case 2:return e.sent(),[2]}}))}))},t.prototype.disconnectWebsocket=function(){this.ws.disconnect()},t.prototype.subscribeDevices=function(e){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t;return tslib_1.__generator(this,(function(n){switch(n.label){case 0:modules_1.appDevSdkLogger.info("subscribeDevices","start"),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,this.ws.connect((e||[]).map((function(e){return"string"==typeof e?e:(null==e?void 0:e.DeviceId)?e.DeviceId:null})).filter(Boolean))];case 2:return n.sent(),[3,4];case 3:return t=n.sent(),modules_1.appDevSdkLogger.error("subscribeDevices","fail",{error:t}),[3,4];case 4:return[2]}}))}))},t.prototype.requestApi=function(e,t,n){void 0===t&&(t={}),void 0===n&&(n={});var i=n.doNotRetry,r=void 0!==i&&i,s=n.needLogin,o=void 0===s||s,a=n.doNotReport,c=void 0!==a&&a,u=tslib_1.__rest(n,["doNotRetry","needLogin","doNotReport"]);return tslib_1.__awaiter(this,void 0,void 0,(function(){var n,i,s,a,l,p,d;return tslib_1.__generator(this,(function(_){switch(_.label){case 0:return _.trys.push([0,6,,13]),o?[4,this.loginManager.checkLogin()]:[3,2];case 1:_.sent(),_.label=2;case 2:return n=this.loginManager,i=n.accessToken,s=n.userId,t&&"default"===t.FamilyId?(a=t,[4,this.getDefaultFamilyId()]):[3,4];case 3:a.FamilyId=_.sent(),_.label=4;case 4:return l=tslib_1.__assign({uin:this._debug?this._defaultUin:s,RegionId:this._apiRegionId},t),i&&(l.AccessToken=i),this._apiPlatform&&(l.Platform=this._apiPlatform),[4,modules_1.requestTokenApi(e,l,tslib_1.__assign({doNotReport:c,url:this._apiUrl},u))];case 5:return[2,_.sent()];case 6:if(p=_.sent(),modules_1.appDevSdkLogger.error("requestApi","fail",{error:p}),!utils_1.isVerifyLoginError(p))return[3,12];if(r)return[3,11];_.label=7;case 7:return _.trys.push([7,9,,10]),[4,this.loginManager.reLogin()];case 8:return _.sent(),[3,10];case 9:return d=_.sent(),modules_1.appDevSdkLogger.error("requestApi","reLogin fail",{error:d}),[2,Promise.reject(utils_1.genVerifyLoginFailError(p))];case 10:return[2,this.requestApi(e,t,tslib_1.__assign({doNotRetry:!0},u))];case 11:return[2,Promise.reject(utils_1.genVerifyLoginFailError(p))];case 12:return[2,Promise.reject(utils_1.normalizeError(p))];case 13:return[2]}}))}))},t.prototype._handlePushEvent=function(e){var t,n,i,r,s=modules_1.appDevSdkLogger.event("handlePushEvent"),o=e.action,a=e.params;a||(a={});var c=a.DeviceId,u=a.Type,l=a.SubType,p=a.Time,d=a.Payload,_=new Date(p).getTime();if("DeviceChange"===o)switch(u){case"Property":case"Shadow":case"Template":switch(l){case"Report":var v={};try{if(d){var h=d.method,g=d.params,f=d.type,b=d.state;if(f&&"update"===f&&b&&b.reported&&(h="report",g=b.reported),g||(g={}),"report"===h)try{for(var y=tslib_1.__values(Object.keys(g)),m=y.next();!m.done;m=y.next()){v[k=m.value]={Value:g[k],lastUpdate:_,LastUpdate:_}}}catch(e){t={error:e}}finally{try{m&&!m.done&&(n=y.return)&&n.call(y)}finally{if(t)throw t.error}}}}catch(e){s.error("parse template report fail",{error:e})}this.emit(constants.EventTypes.WsReport,{deviceId:c,deviceData:v});break;case"Push":v={};try{if(d){h=d.method,g=d.params,f=d.type;var w=d.payload;if(f&&"delta"===f&&w&&w.state&&(h="control",g=w.state),"control"===h&&g){try{for(var P=tslib_1.__values(Object.keys(g)),D=P.next();!D.done;D=P.next()){var k;v[k=D.value]={Value:g[k],LastUpdate:_}}}catch(e){i={error:e}}finally{try{D&&!D.done&&(r=P.return)&&r.call(P)}finally{if(i)throw i.error}}this.emit(constants.EventTypes.WsControl,{deviceId:c,deviceData:v})}}}catch(e){s.error("parse template push fail",{error:e})}}break;case"Event":if("Report"===l)this.emit(constants.EventTypes.WsEventReport,{Payload:d,deviceId:c});break;case"Action":switch(l){case"Push":this.emit(constants.EventTypes.WsActionPush,{Payload:d,deviceId:c});break;case"Report":this.emit(constants.EventTypes.WsActionReport,{Payload:d,deviceId:c})}break;case"StatusChange":switch(l){case"Bind":this.emit(constants.EventTypes.WsDeviceBind,{Payload:d,deviceId:c});break;case"Unbind":this.emit(constants.EventTypes.WsDeviceUnbind,{Payload:d,deviceId:c});break;case"Online":case"Offline":var I="Online"===l?1:0;this.emit(constants.EventTypes.WsStatusChange,{deviceId:c,deviceStatus:I});break;default:s.warn("unknown subtype 【"+l+"】 of statusChange")}case"Gateway":switch(l){case"bind":this.emit(constants.EventTypes.WsSubDeviceBind,{gatewayDeviceId:c,subDeviceList:d}),this.emit(constants.EventTypes.WsSubDeviceChange,{gatewayDeviceId:c,subDeviceList:d,action:"bind"});break;case"unbind":this.emit(constants.EventTypes.WsSubDeviceUnbind,{gatewayDeviceId:c,subDeviceList:d}),this.emit(constants.EventTypes.WsSubDeviceChange,{gatewayDeviceId:c,subDeviceList:d,action:"unbind"})}}},t.prototype._onWebsocketClose=function(){if(!this.isManuallyClose)return this._reconnectWs();this.isManuallyClose=!1},t.prototype._reconnectWs=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e;return tslib_1.__generator(this,(function(t){switch(t.label){case 0:return t.trys.push([0,3,,4]),modules_1.appDevSdkLogger.info("reconnectWs","websocket reconnecting in 2 seconds"),[4,utils_1.delay(2e3)];case 1:return t.sent(),[4,this.ws.connect()];case 2:return t.sent(),[3,4];case 3:return e=t.sent(),modules_1.appDevSdkLogger.error("reconnectWs","fail",{error:e}),[2,Promise.reject(e)];case 4:return[2]}}))}))},t.utils=utils,t.constants=constants,t}(event_emitter_for_miniprogram_1.default);exports.AppDevSdk=AppDevSdk; | ||
//# sourceMappingURL=AppDevSdk.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.IotWebsocket=void 0;var tslib_1=require("tslib"),event_emitter_for_miniprogram_1=tslib_1.__importDefault(require("event-emitter-for-miniprogram")),shortid_for_miniprogram_1=tslib_1.__importDefault(require("shortid-for-miniprogram")),utils_1=require("../utils"),logger_1=require("./logger"),defaultOptions={url:"wss://iot.cloud.tencent.com/ws/explorer",heartbeatInterval:5e4},IotWebsocket=function(e){function t(t,r){var s=e.call(this)||this;return s.sdk=t,s.requestHandlerMap=new Map,s.options=Object.assign({},defaultOptions,r),s._connected=!1,s._subscribeDeviceIdList=[],s._heartBeatTimer=null,s}return tslib_1.__extends(t,e),t.prototype.isConnected=function(){return!!this._connected},t.prototype.doConnectWs=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e=this;return tslib_1.__generator(this,(function(t){return[2,this._doConnectWsPromise||(this._doConnectWsPromise=new Promise((function(t,r){return tslib_1.__awaiter(e,void 0,void 0,(function(){var e,s,i,n=this;return tslib_1.__generator(this,(function(o){e=shortid_for_miniprogram_1.default(),logger_1.wsLogger.updateGetReportData((function(){return{event:logger_1.WS_EVENT_PREFIX,context:{flowId:e}}})),s=function(e){r(e),logger_1.wsLogger.error("connection","error",{error:e}),n.disconnect(),n.emit("error",e)};try{logger_1.wsLogger.info("connection","start"),i=this.options.url,this.ws=new utils_1.WebSocket(utils_1.appendParams(i,{uin:this.sdk._debug?this.sdk._defaultUin:this.sdk.loginManager.userId,cId:e})),this.ws.onOpen((function(){n._connected=!0,n.emit("connect"),t(),logger_1.wsLogger.info("connection","connected")})),this.ws.onError(s),this.ws.onMessage((function(e){var t=e.data;n.emit("message",t);try{t=JSON.parse(t)}catch(e){return void logger_1.wsLogger.error("message","parseFail",{data:t})}t.push?(n.emit("push",t),logger_1.wsLogger.info("message","pushData",{data:t})):void 0!==t.reqId&&n.requestHandlerMap.has(t.reqId)?n.requestHandlerMap.get(t.reqId)(null,t):logger_1.wsLogger.info("message","otherData",{data:t})})),this.ws.onClose((function(e){return tslib_1.__awaiter(n,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){return logger_1.wsLogger.info("connection","close",{data:e}),this.disconnect(),this.emit("close",e),[2]}))}))}))}catch(e){s(e)}return[2]}))}))})))]}))}))},t.prototype.connect=function(e){var t;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return e&&(this._subscribeDeviceIdList=e),[4,this.sdk.loginManager.checkLogin()];case 1:return r.sent(),this.isConnected()||!(null===(t=this._subscribeDeviceIdList)||void 0===t?void 0:t.length)?[3,3]:[4,this.doConnectWs()];case 2:r.sent(),r.label=3;case 3:return[2,this.activePush()]}}))}))},t.prototype.disconnect=function(){this.ws&&(logger_1.wsLogger.info("connection","disconnect"),this.ws.close(),this._connected=!1,this._doConnectWsPromise=null,this.ws=null,clearInterval(this._heartBeatTimer),this._heartBeatTimer=null)},t.prototype.send=function(e,t,r){void 0===t&&(t={});var s=(void 0===r?{}:r).reqId;return tslib_1.__awaiter(this,void 0,void 0,(function(){var r,i,n,o,a,c=this;return tslib_1.__generator(this,(function(u){switch(u.label){case 0:if(s||(s=shortid_for_miniprogram_1.default()),r="send."+e,logger_1.wsLogger.info(r,{message:"send start",data:{reqBody:utils_1.cutoffLong(JSON.stringify(t))},metric:{type:"start",spanId:s}}),!this.ws)return[3,6];i=Date.now(),this.ws.send({data:JSON.stringify({action:e,reqId:s,params:t})}),n={reqBody:utils_1.cutoffLong(JSON.stringify(t)),action:e,reqId:s,timeCost:null},o="success",u.label=1;case 1:return u.trys.push([1,3,4,5]),[4,Promise.race([new Promise((function(e,t){c.requestHandlerMap.set(s,(function(a,c){if(n.timeCost=Date.now()-i,!a)return c.data||!c.error&&!c.error_message?(logger_1.wsLogger.info(r,{message:"send "+o,metric:{type:o,spanId:s,timeCost:Date.now()-i},data:{resBody:utils_1.cutoffLong(JSON.stringify(c.data))}}),e(c.data)):(t({code:c.error,msg:c.error_message}),void(o="fail"));t(a),o="fail"}))})),new Promise((function(e,t){setTimeout((function(){t({code:"TIMEOUT"}),o="timeout"}),2e4)}))])];case 2:return[2,u.sent()];case 3:return a=u.sent(),logger_1.wsLogger.error(r,{message:"send "+o,metric:{type:o,spanId:s,timeCost:Date.now()-i},error:a}),[3,5];case 4:return this.requestHandlerMap.delete(s),[7];case 5:return[3,7];case 6:logger_1.wsLogger.error(r,{message:"no ws instance",metric:{type:"end",spanId:s}}),u.label=7;case 7:return[2]}}))}))},t.prototype.sendWsHeatBeat=function(){var e;if(null===(e=this._subscribeDeviceIdList)||void 0===e?void 0:e.length){var t=shortid_for_miniprogram_1.default(),r={Action:"AppDeviceTraceHeartBeat",ActionParams:Object.assign({},{Platform:this.sdk._apiPlatform,RequestId:t,AccessToken:this.sdk.loginManager.accessToken,DeviceIds:this._subscribeDeviceIdList})};return this.send("YunApi",r,{reqId:t})}},t.prototype.activePush=function(){var e=this,t=this.sdk.loginManager,r=t.isLogin,s=t.accessToken,i=t.appKey;r&&s&&this._subscribeDeviceIdList&&this.ws&&(this.send("ActivePush",{DeviceIds:this._subscribeDeviceIdList,Platform:"weapp",AccessToken:s,AppKey:i,RegionId:this.sdk._apiRegionId}),this.sendWsHeatBeat(),clearInterval(this._heartBeatTimer),this._heartBeatTimer=setInterval((function(){return e.sendWsHeatBeat()}),this.options.heartbeatInterval))},t}(event_emitter_for_miniprogram_1.default);exports.IotWebsocket=IotWebsocket; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.IotWebsocket=void 0;var tslib_1=require("tslib"),event_emitter_for_miniprogram_1=tslib_1.__importDefault(require("event-emitter-for-miniprogram")),shortid_for_miniprogram_1=tslib_1.__importDefault(require("shortid-for-miniprogram")),utils_1=require("../utils"),logger_1=require("./logger"),defaultOptions={url:"wss://iot.cloud.tencent.com/ws/explorer",heartbeatInterval:5e4},IotWebsocket=function(e){function t(t,r){var s=e.call(this)||this;return s.sdk=t,s.requestHandlerMap=new Map,s.options=Object.assign({},defaultOptions,r),s._connected=!1,s._subscribeDeviceIdList=[],s._heartBeatTimer=null,s}return tslib_1.__extends(t,e),t.prototype.isConnected=function(){return!!this._connected},t.prototype.doConnectWs=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e=this;return tslib_1.__generator(this,(function(t){return[2,this._doConnectWsPromise||(this._doConnectWsPromise=new Promise((function(t,r){return tslib_1.__awaiter(e,void 0,void 0,(function(){var e,s,i,o=this;return tslib_1.__generator(this,(function(n){e=shortid_for_miniprogram_1.default(),logger_1.wsLogger.updateGetReportData((function(){return{event:logger_1.WS_EVENT_PREFIX,context:{flowId:e}}})),s=function(e){r(e),logger_1.wsLogger.error("connection","error",{error:e}),o.disconnect(),o.emit("error",e)};try{logger_1.wsLogger.info("connection","start"),i=this.options.url,this.ws=new utils_1.WebSocket(utils_1.appendParams(i,{uin:this.sdk._debug?this.sdk._defaultUin:this.sdk.loginManager.userId,cId:e})),this.ws.onOpen((function(){o._connected=!0,o.emit("connect"),t(),logger_1.wsLogger.info("connection","connected")})),this.ws.onError(s),this.ws.onMessage((function(e){var t=e.data;o.emit("message",t);try{t=JSON.parse(t)}catch(e){return void logger_1.wsLogger.error("message","parseFail",{data:t})}if(t.push){o.emit("push",tslib_1.__assign({},t));var r=tslib_1.__assign({},t.params);if(t.params=r,r.Payload)try{var s=utils_1.base64.decode(r.Payload);s=s.replace(/\x00/g,""),r.Payload=JSON.parse(s)}catch(e){return void logger_1.wsLogger.error("message","parse push payload fail",{error:e})}o.emit("pushWithParsedData",t),logger_1.wsLogger.info("message","pushData",{data:t})}else void 0!==t.reqId&&o.requestHandlerMap.has(t.reqId)?o.requestHandlerMap.get(t.reqId)(null,t):logger_1.wsLogger.info("message","otherData",{data:t})})),this.ws.onClose((function(e){return tslib_1.__awaiter(o,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){return logger_1.wsLogger.info("connection","close",{data:e}),this.disconnect(),this.emit("close",e),[2]}))}))}))}catch(e){s(e)}return[2]}))}))})))]}))}))},t.prototype.connect=function(e){var t;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return e&&(this._subscribeDeviceIdList=e),[4,this.sdk.loginManager.checkLogin()];case 1:return r.sent(),this.isConnected()||!(null===(t=this._subscribeDeviceIdList)||void 0===t?void 0:t.length)?[3,3]:[4,this.doConnectWs()];case 2:r.sent(),r.label=3;case 3:return[2,this.activePush()]}}))}))},t.prototype.disconnect=function(){this.ws&&(logger_1.wsLogger.info("connection","disconnect"),this.ws.close(),this._connected=!1,this._doConnectWsPromise=null,this.ws=null,clearInterval(this._heartBeatTimer),this._heartBeatTimer=null)},t.prototype.send=function(e,t,r){void 0===t&&(t={});var s=(void 0===r?{}:r).reqId;return tslib_1.__awaiter(this,void 0,void 0,(function(){var r,i,o,n,a,c=this;return tslib_1.__generator(this,(function(d){switch(d.label){case 0:if(s||(s=shortid_for_miniprogram_1.default()),r="send."+e,logger_1.wsLogger.info(r,{message:"send start",data:{reqBody:utils_1.cutoffLong(JSON.stringify(t))},metric:{type:"start",spanId:s}}),!this.ws)return[3,6];i=Date.now(),this.ws.send({data:JSON.stringify({action:e,reqId:s,params:t})}),o={reqBody:utils_1.cutoffLong(JSON.stringify(t)),action:e,reqId:s,timeCost:null},n="success",d.label=1;case 1:return d.trys.push([1,3,4,5]),[4,Promise.race([new Promise((function(e,t){c.requestHandlerMap.set(s,(function(a,c){if(o.timeCost=Date.now()-i,!a)return c.data||!c.error&&!c.error_message?(logger_1.wsLogger.info(r,{message:"send "+n,metric:{type:n,spanId:s,timeCost:Date.now()-i},data:{resBody:utils_1.cutoffLong(JSON.stringify(c.data))}}),e(c.data)):(t({code:c.error,msg:c.error_message}),void(n="fail"));t(a),n="fail"}))})),new Promise((function(e,t){setTimeout((function(){t({code:"TIMEOUT"}),n="timeout"}),2e4)}))])];case 2:return[2,d.sent()];case 3:return a=d.sent(),logger_1.wsLogger.error(r,{message:"send "+n,metric:{type:n,spanId:s,timeCost:Date.now()-i},error:a}),[3,5];case 4:return this.requestHandlerMap.delete(s),[7];case 5:return[3,7];case 6:logger_1.wsLogger.error(r,{message:"no ws instance",metric:{type:"end",spanId:s}}),d.label=7;case 7:return[2]}}))}))},t.prototype.sendWsHeatBeat=function(){var e;if(null===(e=this._subscribeDeviceIdList)||void 0===e?void 0:e.length){var t=shortid_for_miniprogram_1.default(),r={Action:"AppDeviceTraceHeartBeat",ActionParams:Object.assign({},{Platform:this.sdk._apiPlatform,RequestId:t,AccessToken:this.sdk.loginManager.accessToken,DeviceIds:this._subscribeDeviceIdList})};return this.send("YunApi",r,{reqId:t})}},t.prototype.activePush=function(){var e=this,t=this.sdk.loginManager,r=t.isLogin,s=t.accessToken,i=t.appKey;r&&s&&this._subscribeDeviceIdList&&this.ws&&(this.send("ActivePush",{DeviceIds:this._subscribeDeviceIdList,Platform:"weapp",AccessToken:s,AppKey:i,RegionId:this.sdk._apiRegionId}),this.sendWsHeatBeat(),clearInterval(this._heartBeatTimer),this._heartBeatTimer=setInterval((function(){return e.sendWsHeatBeat()}),this.options.heartbeatInterval))},t}(event_emitter_for_miniprogram_1.default);exports.IotWebsocket=IotWebsocket; | ||
//# sourceMappingURL=IotWebsocket.js.map |
@@ -10,3 +10,3 @@ import EventEmitter from 'event-emitter-for-miniprogram'; | ||
export { storage } from './storage'; | ||
export { pify } from './pify'; | ||
export { pify, compatPify } from './pify'; | ||
export * from './request'; | ||
@@ -13,0 +13,0 @@ export * from './utillib'; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.byteUtil=exports.CryptoJS=exports.tips=exports.envDetect=exports.shortid=exports.EventEmitter=exports.WebSocket=void 0;var tslib_1=require("tslib"),event_emitter_for_miniprogram_1=tslib_1.__importDefault(require("event-emitter-for-miniprogram"));exports.EventEmitter=event_emitter_for_miniprogram_1.default;var shortid_for_miniprogram_1=tslib_1.__importDefault(require("shortid-for-miniprogram"));exports.shortid=shortid_for_miniprogram_1.default;var WebSocket_1=require("./WebSocket");Object.defineProperty(exports,"WebSocket",{enumerable:!0,get:function(){return WebSocket_1.WebSocket}});var envDetect=tslib_1.__importStar(require("./env-detect"));exports.envDetect=envDetect;var tips_1=tslib_1.__importDefault(require("./tips"));exports.tips=tips_1.default;var crypto_js_1=tslib_1.__importDefault(require("./crypto-js"));exports.CryptoJS=crypto_js_1.default;var byte_util_1=require("./byte-util");Object.defineProperty(exports,"byteUtil",{enumerable:!0,get:function(){return byte_util_1.byteUtil}});var js_base64_1=require("js-base64");Object.defineProperty(exports,"base64",{enumerable:!0,get:function(){return js_base64_1.Base64}});var storage_1=require("./storage");Object.defineProperty(exports,"storage",{enumerable:!0,get:function(){return storage_1.storage}});var pify_1=require("./pify");Object.defineProperty(exports,"pify",{enumerable:!0,get:function(){return pify_1.pify}}),tslib_1.__exportStar(require("./request"),exports),tslib_1.__exportStar(require("./utillib"),exports),tslib_1.__exportStar(require("./errorHelper"),exports),tslib_1.__exportStar(require("./logger"),exports); | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.byteUtil=exports.CryptoJS=exports.tips=exports.envDetect=exports.shortid=exports.EventEmitter=exports.WebSocket=void 0;var tslib_1=require("tslib"),event_emitter_for_miniprogram_1=tslib_1.__importDefault(require("event-emitter-for-miniprogram"));exports.EventEmitter=event_emitter_for_miniprogram_1.default;var shortid_for_miniprogram_1=tslib_1.__importDefault(require("shortid-for-miniprogram"));exports.shortid=shortid_for_miniprogram_1.default;var WebSocket_1=require("./WebSocket");Object.defineProperty(exports,"WebSocket",{enumerable:!0,get:function(){return WebSocket_1.WebSocket}});var envDetect=tslib_1.__importStar(require("./env-detect"));exports.envDetect=envDetect;var tips_1=tslib_1.__importDefault(require("./tips"));exports.tips=tips_1.default;var crypto_js_1=tslib_1.__importDefault(require("./crypto-js"));exports.CryptoJS=crypto_js_1.default;var byte_util_1=require("./byte-util");Object.defineProperty(exports,"byteUtil",{enumerable:!0,get:function(){return byte_util_1.byteUtil}});var js_base64_1=require("js-base64");Object.defineProperty(exports,"base64",{enumerable:!0,get:function(){return js_base64_1.Base64}});var storage_1=require("./storage");Object.defineProperty(exports,"storage",{enumerable:!0,get:function(){return storage_1.storage}});var pify_1=require("./pify");Object.defineProperty(exports,"pify",{enumerable:!0,get:function(){return pify_1.pify}}),Object.defineProperty(exports,"compatPify",{enumerable:!0,get:function(){return pify_1.compatPify}}),tslib_1.__exportStar(require("./request"),exports),tslib_1.__exportStar(require("./utillib"),exports),tslib_1.__exportStar(require("./errorHelper"),exports),tslib_1.__exportStar(require("./logger"),exports); | ||
//# sourceMappingURL=index.js.map |
/// <reference types="miniprogram-api-typings" /> | ||
export declare function pify<T, U>(api: any, context?: WechatMiniprogram.Wx): (params?: T, ...others: any[]) => Promise<U>; | ||
export declare function compatPify<T, U>(api: any, context?: WechatMiniprogram.Wx): (params?: T, ...others: any[]) => Promise<U>; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.pify=void 0;var tslib_1=require("tslib");function pify(e,t){return void 0===t&&(t=wx),function(i){for(var s=[],o=1;o<arguments.length;o++)s[o-1]=arguments[o];return new Promise((function(o,r){e?e.call.apply(e,tslib_1.__spread([t,tslib_1.__assign(tslib_1.__assign({},i),{success:o,fail:r})],s)):wx.showModal({title:"提示",content:"当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试",complete:function(){return r()},confirmColor:"#006eff",showCancel:!1})}))}}exports.pify=pify; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.compatPify=exports.pify=void 0;var tslib_1=require("tslib");function pify(t,i){return void 0===i&&(i=wx),function(e){for(var s=[],o=1;o<arguments.length;o++)s[o-1]=arguments[o];return new Promise((function(o,n){t?t.call.apply(t,tslib_1.__spread([i,tslib_1.__assign(tslib_1.__assign({},e),{success:o,fail:n})],s)):wx.showModal({title:"提示",content:"当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试",complete:function(){return n()},confirmColor:"#006eff",showCancel:!1})}))}}function compatPify(t,i){return void 0===i&&(i=wx),function(e){for(var s=[],o=1;o<arguments.length;o++)s[o-1]=arguments[o];return new Promise((function(o,n){t?t.call.apply(t,tslib_1.__spread([i,tslib_1.__assign(tslib_1.__assign({},e),{success:o,fail:n})],s)):n({code:"WX_API_NOT_SUPPORTED",msg:"当前小程序基础库不支持该 wx API"})}))}}exports.pify=pify,exports.compatPify=compatPify; | ||
//# sourceMappingURL=pify.js.map |
{ | ||
"name": "qcloud-iotexplorer-appdev-sdk", | ||
"version": "2.3.12", | ||
"version": "2.3.13", | ||
"description": "", | ||
@@ -27,3 +27,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "bb1bad2ea0f91ce15b1dadc287a0bb25d17feb07" | ||
"gitHead": "d208071b4debbdfa750bbe038599f9151748bf0b" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
304246
1445