Socket
Socket
Sign inDemoInstall

qcloud-iotexplorer-bluetooth-adapter-llsync

Package Overview
Dependencies
Maintainers
2
Versions
127
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

qcloud-iotexplorer-bluetooth-adapter-llsync - npm Package Compare versions

Comparing version 1.2.2-beta to 1.2.3-beta

2

lib/base/LLSync/LLSyncDeviceAdapter.js

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.StandardDeviceAdapter=exports.LLSyncDeviceAdapter=void 0;var tslib_1=require("tslib"),constants=tslib_1.__importStar(require("../../constants")),qcloud_iotexplorer_appdev_sdk_1=require("qcloud-iotexplorer-appdev-sdk"),constants_1=require("../../constants"),libs_1=require("../../libs"),utils=tslib_1.__importStar(require("../../libs")),Models_1=require("./Models"),LLSyncDeviceAdapterBase_1=require("../LLSyncDeviceAdapterBase"),LLSyncOtaProcessor_1=require("./LLSyncOtaProcessor"),LLSyncProtocol_1=require("./LLSyncProtocol"),arrayBufferToHexStringArray=qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.utils.arrayBufferToHexStringArray,LLSyncDeviceAdapter=function(e){function t(r){var o=e.call(this,r)||this,s=function(e){return function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return tslib_1.__awaiter(o,void 0,void 0,(function(){return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return[4,this.init()];case 1:return r.sent(),[2,e.apply(void 0,tslib_1.__spread(t))]}}))}))}};o.models=new Models_1.Models(t.options.appDevSdk),o.userIdentify=t.getUserIdentify(),o.on("disconnect",(function(){o.authorized=!1})),o.appDevSdk=t.options.appDevSdk,o.protocol=new LLSyncProtocol_1.LLSyncProtocol(o),o.otaProcessor=new LLSyncOtaProcessor_1.LLSyncOtaProcessor(o),Object.assign(o,{bindDevice:s(o.bindDevice.bind(o)),unbindDevice:s(o.unbindDevice.bind(o)),controlDevice:s(o.controlDevice.bind(o)),controlAction:s(o.controlAction.bind(o)),authenticateConnection:s(o.authenticateConnection.bind(o)),startOta:s(o.startOta.bind(o)),cancelOta:s(o.cancelOta.bind(o)),userCancelBindDevice:s(o.protocol.cancelUserCheck.bind(o.protocol,"cancel"))}),o.init(),o.reporter={info:function(e,r){void 0===e&&(e=""),void 0===r&&(r={});var s=r.message,i=void 0===s?"":s,n=r.serviceId,c=tslib_1.__rest(r,["message","serviceId"]),a=o,d=a.explorerDeviceId,l=a.deviceId,u=a.isConnected;t.options.appDevSdk.reporter.info(constants_1.REPORT_EVENT_TYPE,{serviceId:n||t.serviceId,message:i||(e&&constants.ACTION_DESC[e]?e+"("+constants.ACTION_DESC[e]+")":e),timeCost:c.timeCost||0,action:e,data:tslib_1.__assign({deviceId:d,bleDeviceId:l,isConnected:u},c)})},error:function(e,r){void 0===e&&(e=""),void 0===r&&(r={});var s=r.error,i=tslib_1.__rest(r,["error"]),n=o,c=n.explorerDeviceId,a=n.deviceId,d=n.isConnected;s.code&&constants.ERROR_MESSAGES[s.code]&&(s.msg=constants.ERROR_MESSAGES[s.code]),t.options.appDevSdk.reporter.error(constants_1.REPORT_EVENT_TYPE,{message:constants.ERROR_MESSAGES[e]?e+"("+constants.ERROR_MESSAGES[e]+":"+(s&&(s.message||s.errMsg||s.msg||s.code))+")":"",timeCost:i.timeCost||0,action:e,error:s,data:tslib_1.__assign(tslib_1.__assign({deviceId:c,bleDeviceId:a,isConnected:d},i),s)})}};var i=function(e){var t=e.deviceId,r=e.deviceData;if(console.log("llsync receive onControl",t,r),o.ready&&t===o.explorerDeviceId){var s={};Object.keys(r).forEach((function(e){s[e]=r[e].Value})),o.controlDevice({deviceData:s})}},n=function(e){var t=e.deviceId,r=e.Payload;o.ready&&t!==o.explorerDeviceId||o.controlAction({actionData:r})};return o.on("authorized",(function(){o.init(!0),o.appDevSdk.on(qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.constants.EventTypes.WsControl,i),o.appDevSdk.on(qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.constants.EventTypes.WsActionPush,n)})).on("disconnect",(function(){o.appDevSdk.off(qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.constants.EventTypes.WsControl,i),o.appDevSdk.off(qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.constants.EventTypes.WsActionPush,n)})),o}return tslib_1.__extends(t,e),t.getUserIdentify=function(){var e,r,o,s;return libs_1.getUserIdentify(null===(s=null===(o=null===(r=null===(e=t.options)||void 0===e?void 0:e.appDevSdk)||void 0===r?void 0:r.loginManager)||void 0===o?void 0:o.userInfo)||void 0===s?void 0:s.UserID)},t.injectOptions=function(e){t.options=e},Object.defineProperty(t.prototype,"ready",{get:function(){return this.isConnected&&this.authorized},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"needUserCheck",{get:function(){var e,t,r;try{return!!parseInt((null===(r=null===(t=null===(e=null==this?void 0:this.productConfig)||void 0===e?void 0:e.BleConfig)||void 0===t?void 0:t.bindingBootConfig)||void 0===r?void 0:r.confirmRequired)||"0")}catch(e){return console.warn("LLSync check confirmRequired fail",e),!1}},enumerable:!1,configurable:!0}),t.prototype.normalizeError=function(t){return!(t=e.prototype._normalizeError.call(this,t)).msg&&t.code&&constants.ERROR_MESSAGES[t.code]&&(t.msg=constants.ERROR_MESSAGES[t.code]),t},t.prototype.init=function(r){return tslib_1.__awaiter(this,void 0,void 0,(function(){var o=this;return tslib_1.__generator(this,(function(s){switch(s.label){case 0:if(!t.options)throw"标准蓝牙适配器未初始化Options";return[4,e.prototype.init.call(this)];case 1:return s.sent(),r&&(this.productInfo=this.productConfig=this._initPromise=null),[2,this._initPromise||(this._initPromise=new Promise((function(e,t){return tslib_1.__awaiter(o,void 0,void 0,(function(){var r,o,s,i,n,c,a;return tslib_1.__generator(this,(function(d){switch(d.label){case 0:return d.trys.push([0,2,,3]),o=(r=this).productConfig,s=r.productInfo,[4,Promise.all([o?Promise.resolve(o):this.models.getProduct({ProductId:this.productId}),s?Promise.resolve(s):this.models.getProductConfig({ProductId:this.productId})])];case 1:return i=tslib_1.__read.apply(void 0,[d.sent(),2]),n=i[0],c=i[1],this.dataTemplate=libs_1.getProductDateTemplate(n),this.productInfo=n,this.productConfig=c,console.log("init llsync done",{productInfo:this.productInfo,productConfig:this.productConfig,dataTemplate:this.dataTemplate}),e(),[3,3];case 2:return a=d.sent(),console.error("init standard device adapter fail",a),t(a),this._initPromise=null,[3,3];case 3:return[2]}}))}))})))]}}))}))},t.prototype.getDevicePsk=function(e){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,r,o,s,i,n;return tslib_1.__generator(this,(function(c){switch(c.label){case 0:return t={DeviceId:this.productId+"/"+(e||this.deviceName),DeviceKey:LLSyncProtocol_1.LLSyncConfig.BLE_PSK_DEVICE_KEY},[4,Promise.all([this.models.getDeviceConfig(tslib_1.__assign(tslib_1.__assign({},t),{isUserConfig:!0})),this.models.getDeviceConfig(t)])];case 1:if(r=tslib_1.__read.apply(void 0,[c.sent(),2]),o=r[0],s=r[1],!o||s)return[3,5];c.label=2;case 2:return c.trys.push([2,4,,5]),[4,this.models.setDeviceConfig({DeviceId:t.DeviceId,DeviceKey:t.DeviceKey,DeviceValue:o})];case 3:return c.sent(),[3,5];case 4:return i=c.sent(),console.error("update psk to DeviceConfig fail",i),[3,5];case 5:if(!(n=s||o))throw{code:constants.PSK_GET_ERROR};return[2,n]}}))}))},t.prototype.startOta=function(e){var t=e.onProgress;return this.otaProcessor.startOta({onProgress:t})},t.prototype.cancelOta=function(){return this.otaProcessor.cancelOta()},t.prototype.startListenLLEvents=function(){this.on(constants.PROPERTY_REPORT,utils.wrapEventHandler(this.onPropertyReport.bind(this),constants.PROPERTY_REPORT)),this.on(constants.GET_STATUS,this.onGetStatus.bind(this)),this.on(constants.EVENT_REPORT,utils.wrapEventHandler(this.onEventReport.bind(this),constants.EVENT_REPORT))},t.prototype.stopListenLLEvents=function(){this.off(constants.PROPERTY_REPORT),this.off(constants.GET_STATUS),this.off(constants.EVENT_REPORT)},t.prototype.onPropertyReport=function(e){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,r,o,s;return tslib_1.__generator(this,(function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),console.log("onPropertyReport check this",this),t=utils.convertPropertiesTlvToJsObject(e.slice(2),this.dataTemplate),this.reporter.info(constants.REPORT_RESULT,{data:utils.formatArrayToReportString(e),jsObj:t}),[4,this.models.reportBlueToothDeviceData({ProductId:this.productId,DeviceName:this.deviceName,Data:t,DataTimeStamp:Date.now()})];case 1:return(r=i.sent().Data)&&(o=JSON.parse(r),this.protocol.reportPropertyReportResult(o.code)),[3,3];case 2:return s=i.sent(),this.reporter.error(constants.REPORT_RESULT_ERROR,{error:s}),this.protocol.reportPropertyReportResult(-1),[3,3];case 3:return[2]}}))}))},t.prototype.onGetStatus=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,t,r,o,s,i;return tslib_1.__generator(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,this.models.getDeviceData({ProductId:this.productId,DeviceName:this.deviceName})];case 1:return e=n.sent(),t={},Object.keys(e).forEach((function(r){t[r]=e[r].Value})),r=utils.convertPropertiesChangeToTlv(t,this.dataTemplate),o=r.tlvData,s=r.tmpData,this.reporter.info(constants.GET_STATUS,{properties:t,tlvData:utils.formatArrayToReportString(o),tmpData:s}),this.protocol.reportGetStatusResult(0,o,s),[3,3];case 2:return i=n.sent(),this.reporter.error(constants.GET_STATUS_ERROR,{error:i}),this.protocol.reportGetStatusResult(-1),[2,Promise.reject(this.normalizeError(i))];case 3:return[2]}}))}))},t.prototype.onEventReport=function(e){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,r,o,s,i,n,c,a;return tslib_1.__generator(this,(function(d){switch(d.label){case 0:d.trys.push([0,8,,9]),console.log("onEventReport check this",this),t=utils.getStrLength(e),r=utils.convertEventTlvToJsObject(e.slice(2,2+t),this.dataTemplate),o=r.eventId,s=r.params,i=r.eventIndex,n={DeviceId:this.explorerDeviceId,EventId:o,Params:JSON.stringify(s)},this.reporter.info(constants.EVENT_REPLY,tslib_1.__assign(tslib_1.__assign({},n),{eventIndex:i})),d.label=1;case 1:return d.trys.push([1,6,,7]),[4,this.models.reportDeviceEvent(n)];case 2:return d.sent(),e.length-2>t?[4,this.onEventReport({data:e.slice(t+2+1)})]:[3,4];case 3:return d.sent(),[3,5];case 4:this.protocol.reportEventReportResult(0,i),d.label=5;case 5:return[3,7];case 6:throw c=d.sent(),this.protocol.reportEventReportResult(-1,i),c;case 7:return[3,9];case 8:return a=d.sent(),this.reporter.error(constants.EVENT_REPLY_ERROR,{error:a}),[2,Promise.reject(this.normalizeError(a))];case 9:return[2]}}))}))},t.prototype.bindDevice=function(e){var t=e.familyId,r=e.roomId;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,o,s,i,n,c,a,d,l,u;return tslib_1.__generator(this,(function(p){switch(p.label){case 0:return p.trys.push([0,7,,8]),this.reporter.info(constants.BIND_AUTH),e=Date.now(),[4,this.protocol.requestBindDevice({needUserCheck:this.needUserCheck,isDynamicRegister:this.extendInfo.isDynamicRegister})];case 1:o=p.sent(),s=o.sign,i=o.timestamp,n=o.nonce,c=o.deviceName,this.deviceName=c,p.label=2;case 2:return p.trys.push([2,4,,5]),a={Signature:s,DeviceTimestamp:i,DeviceId:this.explorerDeviceId,ConnId:""+n,FamilyId:t,RoomId:r,BindType:"bluetooth_sign"},this.reporter.info(constants.BIND_AUTH_DETAIL,a),[4,this.models.addDeviceBySigInFamily(a)];case 3:return p.sent(),[3,5];case 4:return d=p.sent(),this.protocol.reportBindError(d.code),[2,Promise.reject(this.normalizeError(d))];case 5:return l=this,[4,this.protocol.reportBindSuccess(e)];case 6:return l.localPsk=p.sent(),[2,this.explorerDeviceId];case 7:return u=p.sent(),console.log(u),console.log("error in bindDevice",u),this.reporter.error(constants.BIND_AUTH_FAIL,{error:u}),[2,Promise.reject(this.normalizeError(u))];case 8:return[2]}}))}))},t.prototype.unbindDevice=function(e){var t=e.familyId,r=e.deviceName;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,o,s,i,n;return tslib_1.__generator(this,(function(c){switch(c.label){case 0:if(c.trys.push([0,5,,6]),!r)throw{code:constants.DEVICE_NAME_IS_EMPTY};return e=Date.now(),[4,this.getDevicePsk(r)];case 1:return o=c.sent(),this.reporter.info(constants.UNBIND_AUTH,{localPsk:o}),[4,this.protocol.getUnbindAuthSign()];case 2:if(s=c.sent().sign,i=utils.encrypt(constants.UNBIND_RESPONSE,o),s!==i)throw{code:constants.UNBIND_REPLY_ERROR};return this.localPsk=o,this.deviceName=r,[4,this.models.deleteDeviceFromFamily({FamilyId:t,DeviceId:this.explorerDeviceId})];case 3:return c.sent(),this.reporter.info(constants.UNBIND_RESULT_AUTH_SUCCESS,{timeCost:Date.now()-e}),[4,this.protocol.reportUnbindResult("success")];case 4:return c.sent(),this.stopListenLLEvents(),this.authorized=!1,this.disconnectDevice(),[3,6];case 5:return n=c.sent(),this.protocol.reportUnbindResult("fail"),this.reporter.error(constants.UNBIND_RESULT_AUTH_FAIL,{error:n}),[2,Promise.reject(this.normalizeError(n))];case 6:return[2]}}))}))},t.prototype.controlDevice=function(e){var t=e.deviceData;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e;return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return r.trys.push([0,3,,4]),console.log("controlDevice check this",this),this.reporter.info(constants.CONTROL_DEVICE,{deviceData:t}),[4,this.protocol.controlDeviceProperty(t)];case 1:return r.sent(),[4,this.models.reportBlueToothDeviceData({ProductId:this.productId,DeviceName:this.deviceName,Data:t,DataTimeStamp:Date.now()})];case 2:return r.sent(),[3,4];case 3:return e=r.sent(),this.reporter.error(constants.CONTROL_DEVICE_REPLY_ERROR,{error:e}),[2,Promise.reject(this.normalizeError(e))];case 4:return[2]}}))}))},t.prototype.controlAction=function(e){var t=e.actionData;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,r,o,s,i,n;return tslib_1.__generator(this,(function(c){switch(c.label){case 0:return c.trys.push([0,4,,5]),console.log("controlAction check this",this),[4,this.protocol.controlDeviceAction(t)];case 1:return e=c.sent().output,[4,utils.convertActionOutputTlvToJsObject(e,this.dataTemplate)];case 2:return r=c.sent(),o=r.outputParams,s=r.actionIndex,i=r.actionId,[4,this.models.publishDeviceActionMessage({deviceName:this.deviceName,productId:this.productId,actionId:i,clientToken:t.clientToken,output:o})];case 3:return c.sent(),this.reporter.info(constants.CONTROL_ACTION_SUCCESS,{outputParams:o,actionIndex:s,actionId:i}),[3,5];case 4:return n=c.sent(),this.reporter.error(constants.CONTROL_ACTION_ERROR,{error:n}),[2,Promise.reject(this.normalizeError(n))];case 5:return[2]}}))}))},t.prototype.authenticateConnection=function(e){var t=(void 0===e?{}:e).deviceName;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,r,o,s,i,n,c,a,d,l,u,p;return tslib_1.__generator(this,(function(v){switch(v.label){case 0:if(v.trys.push([0,8,,9]),console.log("start authenticate connection",t),!t)throw{code:constants.DEVICE_NAME_IS_EMPTY};return e=Date.now(),[4,this.getDevicePsk(t)];case 1:return r=v.sent(),this.localPsk=r,this.reporter.info(constants.CONNECT_AUTH,{localPsk:r}),[4,this.protocol.getDeviceAuthInfo()];case 2:if(o=v.sent(),s=o.sign,i=o.timestamp,n=utils.encrypt(""+(i+60)+this.productId+t,r),s!==n)throw{code:constants.CONNECT_SIGN_AUTH_ERROR};return[4,this.protocol.getDeviceInfo()];case 3:if(c=v.sent(),a=c.version,d=c.mtu,l=c.needSetMtu,u=c.otaVersion,this.deviceName=t,this.bleVersion=a,this.mtu=d,!l)return[3,7];v.label=4;case 4:return v.trys.push([4,6,,7]),[4,this.setMtu(this.mtu)];case 5:return v.sent(),this.writeMtuResult("success"),[3,7];case 6:return v.sent(),this.writeMtuResult("fail"),[3,7];case 7:return this.otaVersion=u,u&&this.models.reportOTAVersion({Version:u,DeviceId:this.explorerDeviceId}),this.reporter.info(constants.CONNECT_RESULT_WRITE_SUCCESS,{timeCost:Date.now()-e,version:a,mtu:d,otaVersion:u}),this.authorized=!0,this.emit("authorized",{version:a,mtu:d,otaVersion:u}),this.stopListenLLEvents(),this.startListenLLEvents(),[3,9];case 8:return p=v.sent(),this.protocol.reportConnectError(),this.reporter.error(constants.CONNECT_RESULT_WRITE_FAIL,{error:p}),[2,Promise.reject(this.normalizeError(p))];case 9:return[2]}}))}))},t.serviceId16="0000FFE0-0000-1000-8000-00805F9B34FB",t.serviceId="0000FFE0-65D0-4E20-B56A-E493541BA4E2",t.deviceFilter=function(e,r){if(!e.advertisServiceUUIDs||!e.advertisServiceUUIDs.find((function(e){return e===t.serviceId16}))||!e.advertisData)return null;var o=t.options.appDevSdk.reporter;try{var s=Math.floor(10*Math.random())%10==1||!0,i=arrayBufferToHexStringArray(e.advertisData);s&&o.info(constants_1.REPORT_EVENT_TYPE,{message:"收到广播",serviceId:t.serviceId,hexArr:i.join(",")});var n=parseInt(i[2],16)-(parseInt(i[2],16)>>2<<2),c=parseInt(i[2],16)>>4,a=parseInt(i[2],16)>>3&1,d=r.productId&&r.deviceName?r.productId+"/"+r.deviceName:"",l=d?utils.get8ByteFromStr(""+d.replace("/","")):"",u=!!d,p="",v="",_="",h=constants.DEVICE_STATE_MAP[n],E=[constants.DEVICE_HAS_BINDED,constants.DEVICE_HAS_CONNECTED].indexOf(h)>-1;if(E)_=i.slice(3,11).join("").toLocaleLowerCase(),v=i.slice(11).join("").toLocaleLowerCase(),p=r.productId;else{var f=i.slice(3,9);e.name&&-1===e.name.indexOf("_")&&(e.name=e.name+"_"+f.slice(0,2).join("")),p=libs_1.hex2str(i.slice(9))}s&&o.info(constants_1.REPORT_EVENT_TYPE,{message:"解析广播",serviceId:t.serviceId,data:{bindState:h||"x",targetDeviceId:d,targetDeviceIdentify:l,deviceUserIdentify:v||"x",deviceProductId:p||"x",deviceIdentify:_||"x"}});var D=function(){var o=tslib_1.__assign(tslib_1.__assign({},e),{serviceId:t.serviceId,deviceName:r.deviceName||"",productId:p,extendInfo:{bindState:constants.DEVICE_STATE_MAP[h],moduleVersion:c,isDynamicRegister:!!a,standard:!0}});return console.log("---设备匹配成功---",o),o};return u&&E&&_===l?D():u||E?null:D()}catch(e){console.error("llsync device filter error",e),o.error(constants_1.REPORT_EVENT_TYPE,{message:"协议广播出错",error:e})}},t}(LLSyncDeviceAdapterBase_1.LLSyncDeviceAdapterBase);exports.LLSyncDeviceAdapter=LLSyncDeviceAdapter,exports.StandardDeviceAdapter=LLSyncDeviceAdapter;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.StandardDeviceAdapter=exports.LLSyncDeviceAdapter=void 0;var tslib_1=require("tslib"),constants=tslib_1.__importStar(require("../../constants")),qcloud_iotexplorer_appdev_sdk_1=require("qcloud-iotexplorer-appdev-sdk"),constants_1=require("../../constants"),libs_1=require("../../libs"),utils=tslib_1.__importStar(require("../../libs")),Models_1=require("./Models"),LLSyncDeviceAdapterBase_1=require("../LLSyncDeviceAdapterBase"),LLSyncOtaProcessor_1=require("./LLSyncOtaProcessor"),LLSyncProtocol_1=require("./LLSyncProtocol"),arrayBufferToHexStringArray=qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.utils.arrayBufferToHexStringArray,LLSyncDeviceAdapter=function(e){function t(r){var o=e.call(this,r)||this,s=function(e){return function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return tslib_1.__awaiter(o,void 0,void 0,(function(){return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return[4,this.init()];case 1:return r.sent(),[2,e.apply(void 0,tslib_1.__spread(t))]}}))}))}};o.models=new Models_1.Models(t.options.appDevSdk),o.userIdentify=t.getUserIdentify(),o.on("disconnect",(function(){o.authorized=!1})),o.appDevSdk=t.options.appDevSdk,o.protocol=new LLSyncProtocol_1.LLSyncProtocol(o),o.otaProcessor=new LLSyncOtaProcessor_1.LLSyncOtaProcessor(o),Object.assign(o,{bindDevice:s(o.bindDevice.bind(o)),unbindDevice:s(o.unbindDevice.bind(o)),controlDevice:s(o.controlDevice.bind(o)),controlAction:s(o.controlAction.bind(o)),authenticateConnection:s(o.authenticateConnection.bind(o)),startOta:s(o.startOta.bind(o)),cancelOta:s(o.cancelOta.bind(o)),userCancelBindDevice:s(o.protocol.cancelUserCheck.bind(o.protocol,"cancel"))}),o.init(),o.reporter={info:function(e,r){void 0===e&&(e=""),void 0===r&&(r={});var s=r.message,i=void 0===s?"":s,n=r.serviceId,c=tslib_1.__rest(r,["message","serviceId"]),a=o,d=a.explorerDeviceId,l=a.deviceId,u=a.isConnected;t.options.appDevSdk.reporter.info(constants_1.REPORT_EVENT_TYPE,{serviceId:n||t.serviceId,message:i||(e&&constants.ACTION_DESC[e]?e+"("+constants.ACTION_DESC[e]+")":e),timeCost:c.timeCost||0,action:e,data:tslib_1.__assign({deviceId:d,bleDeviceId:l,isConnected:u},c)})},error:function(e,r){void 0===e&&(e=""),void 0===r&&(r={});var s=r.error,i=tslib_1.__rest(r,["error"]),n=o,c=n.explorerDeviceId,a=n.deviceId,d=n.isConnected;s.code&&constants.ERROR_MESSAGES[s.code]&&(s.msg=constants.ERROR_MESSAGES[s.code]),t.options.appDevSdk.reporter.error(constants_1.REPORT_EVENT_TYPE,{message:constants.ERROR_MESSAGES[e]?e+"("+constants.ERROR_MESSAGES[e]+":"+(s&&(s.message||s.errMsg||s.msg||s.code))+")":"",timeCost:i.timeCost||0,action:e,error:s,data:tslib_1.__assign(tslib_1.__assign({deviceId:c,bleDeviceId:a,isConnected:d},i),s)})}};var i=function(e){var t=e.deviceId,r=e.deviceData;if(console.log("llsync receive onControl",t,r),o.ready&&t===o.explorerDeviceId){var s={};Object.keys(r).forEach((function(e){s[e]=r[e].Value})),o.controlDevice({deviceData:s})}},n=function(e){var t=e.deviceId,r=e.Payload;o.ready&&t!==o.explorerDeviceId||o.controlAction({actionData:r})};return o.on("authorized",(function(){o.init(!0),o.appDevSdk.on(qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.constants.EventTypes.WsControl,i),o.appDevSdk.on(qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.constants.EventTypes.WsActionPush,n)})).on("disconnect",(function(){o.appDevSdk.off(qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.constants.EventTypes.WsControl,i),o.appDevSdk.off(qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.constants.EventTypes.WsActionPush,n)})),o}return tslib_1.__extends(t,e),t.getUserIdentify=function(){var e,r,o,s;return libs_1.getUserIdentify(null===(s=null===(o=null===(r=null===(e=t.options)||void 0===e?void 0:e.appDevSdk)||void 0===r?void 0:r.loginManager)||void 0===o?void 0:o.userInfo)||void 0===s?void 0:s.UserID)},t.injectOptions=function(e){t.options=e},Object.defineProperty(t.prototype,"ready",{get:function(){return this.isConnected&&this.authorized},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"needUserCheck",{get:function(){var e,t,r;try{return!!parseInt((null===(r=null===(t=null===(e=null==this?void 0:this.productConfig)||void 0===e?void 0:e.BleConfig)||void 0===t?void 0:t.bindingBootConfig)||void 0===r?void 0:r.confirmRequired)||"0")}catch(e){return console.warn("LLSync check confirmRequired fail",e),!1}},enumerable:!1,configurable:!0}),t.prototype.normalizeError=function(t){return!(t=e.prototype._normalizeError.call(this,t)).msg&&t.code&&constants.ERROR_MESSAGES[t.code]&&(t.msg=constants.ERROR_MESSAGES[t.code]),t},t.prototype.init=function(r){return tslib_1.__awaiter(this,void 0,void 0,(function(){var o=this;return tslib_1.__generator(this,(function(s){switch(s.label){case 0:if(!t.options)throw"标准蓝牙适配器未初始化Options";return[4,e.prototype.init.call(this)];case 1:return s.sent(),r&&(this.productInfo=this.productConfig=this._initPromise=null),[2,this._initPromise||(this._initPromise=new Promise((function(e,t){return tslib_1.__awaiter(o,void 0,void 0,(function(){var r,o,s,i,n,c,a;return tslib_1.__generator(this,(function(d){switch(d.label){case 0:return d.trys.push([0,2,,3]),o=(r=this).productConfig,s=r.productInfo,[4,Promise.all([o?Promise.resolve(o):this.models.getProduct({ProductId:this.productId}),s?Promise.resolve(s):this.models.getProductConfig({ProductId:this.productId})])];case 1:return i=tslib_1.__read.apply(void 0,[d.sent(),2]),n=i[0],c=i[1],this.dataTemplate=libs_1.getProductDateTemplate(n),this.productInfo=n,this.productConfig=c,console.log("init llsync done",{productInfo:this.productInfo,productConfig:this.productConfig,dataTemplate:this.dataTemplate}),e(),[3,3];case 2:return a=d.sent(),console.error("init standard device adapter fail",a),t(a),this._initPromise=null,[3,3];case 3:return[2]}}))}))})))]}}))}))},t.prototype.getDevicePsk=function(e){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,r,o,s,i,n;return tslib_1.__generator(this,(function(c){switch(c.label){case 0:return t={DeviceId:this.productId+"/"+(e||this.deviceName),DeviceKey:LLSyncProtocol_1.LLSyncConfig.BLE_PSK_DEVICE_KEY},[4,Promise.all([this.models.getDeviceConfig(tslib_1.__assign(tslib_1.__assign({},t),{isUserConfig:!0})),this.models.getDeviceConfig(t)])];case 1:if(r=tslib_1.__read.apply(void 0,[c.sent(),2]),o=r[0],s=r[1],!o||s)return[3,5];c.label=2;case 2:return c.trys.push([2,4,,5]),[4,this.models.setDeviceConfig({DeviceId:t.DeviceId,DeviceKey:t.DeviceKey,DeviceValue:o})];case 3:return c.sent(),[3,5];case 4:return i=c.sent(),console.error("update psk to DeviceConfig fail",i),[3,5];case 5:if(!(n=s||o))throw{code:constants.PSK_GET_ERROR};return[2,n]}}))}))},t.prototype.startOta=function(e){var t=e.onProgress;return this.otaProcessor.startOta({onProgress:t})},t.prototype.cancelOta=function(){return this.otaProcessor.cancelOta()},t.prototype.startListenLLEvents=function(){this.on(constants.PROPERTY_REPORT,utils.wrapEventHandler(this.onPropertyReport.bind(this),constants.PROPERTY_REPORT)),this.on(constants.GET_STATUS,this.onGetStatus.bind(this)),this.on(constants.EVENT_REPORT,utils.wrapEventHandler(this.onEventReport.bind(this),constants.EVENT_REPORT))},t.prototype.stopListenLLEvents=function(){this.off(constants.PROPERTY_REPORT),this.off(constants.GET_STATUS),this.off(constants.EVENT_REPORT)},t.prototype.onPropertyReport=function(e){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,r,o,s;return tslib_1.__generator(this,(function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),console.log("onPropertyReport check this",this),t=utils.convertPropertiesTlvToJsObject(e.slice(2),this.dataTemplate),this.reporter.info(constants.REPORT_RESULT,{data:utils.formatArrayToReportString(e),jsObj:t}),[4,this.models.reportBlueToothDeviceData({ProductId:this.productId,DeviceName:this.deviceName,Data:t,DataTimeStamp:Date.now()})];case 1:return(r=i.sent().Data)&&(o=JSON.parse(r),this.protocol.reportPropertyReportResult(o.code)),[3,3];case 2:return s=i.sent(),this.reporter.error(constants.REPORT_RESULT_ERROR,{error:s}),this.protocol.reportPropertyReportResult(-1),[3,3];case 3:return[2]}}))}))},t.prototype.onGetStatus=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,t,r,o,s,i;return tslib_1.__generator(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,this.models.getDeviceData({ProductId:this.productId,DeviceName:this.deviceName})];case 1:return e=n.sent(),t={},Object.keys(e).forEach((function(r){t[r]=e[r].Value})),r=utils.convertPropertiesChangeToTlv(t,this.dataTemplate),o=r.tlvData,s=r.tmpData,this.reporter.info(constants.GET_STATUS,{properties:t,tlvData:utils.formatArrayToReportString(o),tmpData:s}),this.protocol.reportGetStatusResult(0,o,s),[3,3];case 2:return i=n.sent(),this.reporter.error(constants.GET_STATUS_ERROR,{error:i}),this.protocol.reportGetStatusResult(-1),[2,Promise.reject(this.normalizeError(i))];case 3:return[2]}}))}))},t.prototype.onEventReport=function(e){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,r,o,s,i,n,c,a;return tslib_1.__generator(this,(function(d){switch(d.label){case 0:d.trys.push([0,8,,9]),console.log("onEventReport check this",this),t=utils.getStrLength(e),r=utils.convertEventTlvToJsObject(e.slice(2,2+t),this.dataTemplate),o=r.eventId,s=r.params,i=r.eventIndex,n={DeviceId:this.explorerDeviceId,EventId:o,Params:JSON.stringify(s)},this.reporter.info(constants.EVENT_REPLY,tslib_1.__assign(tslib_1.__assign({},n),{eventIndex:i})),d.label=1;case 1:return d.trys.push([1,6,,7]),[4,this.models.reportDeviceEvent(n)];case 2:return d.sent(),e.length-2>t?[4,this.onEventReport({data:e.slice(t+2+1)})]:[3,4];case 3:return d.sent(),[3,5];case 4:this.protocol.reportEventReportResult(0,i),d.label=5;case 5:return[3,7];case 6:throw c=d.sent(),this.protocol.reportEventReportResult(-1,i),c;case 7:return[3,9];case 8:return a=d.sent(),this.reporter.error(constants.EVENT_REPLY_ERROR,{error:a}),[2,Promise.reject(this.normalizeError(a))];case 9:return[2]}}))}))},t.prototype.bindDevice=function(e){var t=e.familyId,r=e.roomId;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,o,s,i,n,c,a,d,l,u;return tslib_1.__generator(this,(function(p){switch(p.label){case 0:return p.trys.push([0,7,,8]),this.reporter.info(constants.BIND_AUTH),e=Date.now(),[4,this.protocol.requestBindDevice({needUserCheck:this.needUserCheck,isDynamicRegister:this.extendInfo.isDynamicRegister})];case 1:o=p.sent(),s=o.sign,i=o.timestamp,n=o.nonce,c=o.deviceName,this.deviceName=c,p.label=2;case 2:return p.trys.push([2,4,,5]),a={Signature:s,DeviceTimestamp:i,DeviceId:this.explorerDeviceId,ConnId:""+n,FamilyId:t,RoomId:r,BindType:"bluetooth_sign"},this.reporter.info(constants.BIND_AUTH_DETAIL,a),[4,this.models.addDeviceBySigInFamily(a)];case 3:return p.sent(),[3,5];case 4:return d=p.sent(),this.protocol.reportBindError(d.code),[2,Promise.reject(this.normalizeError(d))];case 5:return l=this,[4,this.protocol.reportBindSuccess(e)];case 6:return l.localPsk=p.sent(),[2,this.explorerDeviceId];case 7:return u=p.sent(),console.log(u),console.log("error in bindDevice",u),this.reporter.error(constants.BIND_AUTH_FAIL,{error:u}),[2,Promise.reject(this.normalizeError(u))];case 8:return[2]}}))}))},t.prototype.unbindDevice=function(e){var t=e.familyId,r=e.deviceName;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,o,s,i,n;return tslib_1.__generator(this,(function(c){switch(c.label){case 0:if(c.trys.push([0,5,,6]),!r)throw{code:constants.DEVICE_NAME_IS_EMPTY};return e=Date.now(),[4,this.getDevicePsk(r)];case 1:return o=c.sent(),this.reporter.info(constants.UNBIND_AUTH,{localPsk:o}),[4,this.protocol.getUnbindAuthSign()];case 2:if(s=c.sent().sign,i=utils.encrypt(constants.UNBIND_RESPONSE,o),s!==i)throw{code:constants.UNBIND_REPLY_ERROR};return this.localPsk=o,this.deviceName=r,[4,this.models.deleteDeviceFromFamily({FamilyId:t,DeviceId:this.explorerDeviceId})];case 3:return c.sent(),this.reporter.info(constants.UNBIND_RESULT_AUTH_SUCCESS,{timeCost:Date.now()-e}),[4,this.protocol.reportUnbindResult("success")];case 4:return c.sent(),this.stopListenLLEvents(),this.authorized=!1,this.disconnectDevice(),[3,6];case 5:return n=c.sent(),this.protocol.reportUnbindResult("fail"),this.reporter.error(constants.UNBIND_RESULT_AUTH_FAIL,{error:n}),[2,Promise.reject(this.normalizeError(n))];case 6:return[2]}}))}))},t.prototype.controlDevice=function(e){var t=e.deviceData;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e;return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return r.trys.push([0,3,,4]),console.log("controlDevice check this",this),this.reporter.info(constants.CONTROL_DEVICE,{deviceData:t}),[4,this.protocol.controlDeviceProperty(t)];case 1:return r.sent(),[4,this.models.reportBlueToothDeviceData({ProductId:this.productId,DeviceName:this.deviceName,Data:t,DataTimeStamp:Date.now()})];case 2:return r.sent(),[3,4];case 3:return e=r.sent(),this.reporter.error(constants.CONTROL_DEVICE_REPLY_ERROR,{error:e}),[2,Promise.reject(this.normalizeError(e))];case 4:return[2]}}))}))},t.prototype.controlAction=function(e){var t=e.actionData;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,r,o,s,i,n;return tslib_1.__generator(this,(function(c){switch(c.label){case 0:return c.trys.push([0,4,,5]),console.log("controlAction check this",this),[4,this.protocol.controlDeviceAction(t)];case 1:return e=c.sent().output,[4,utils.convertActionOutputTlvToJsObject(e,this.dataTemplate)];case 2:return r=c.sent(),o=r.outputParams,s=r.actionIndex,i=r.actionId,[4,this.models.publishDeviceActionMessage({deviceName:this.deviceName,productId:this.productId,actionId:i,clientToken:t.clientToken,output:o})];case 3:return c.sent(),this.reporter.info(constants.CONTROL_ACTION_SUCCESS,{outputParams:o,actionIndex:s,actionId:i}),[3,5];case 4:return n=c.sent(),this.reporter.error(constants.CONTROL_ACTION_ERROR,{error:n}),[2,Promise.reject(this.normalizeError(n))];case 5:return[2]}}))}))},t.prototype.authenticateConnection=function(e){var t=(void 0===e?{}:e).deviceName;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,r,o,s,i,n,c,a,d,l,u,p;return tslib_1.__generator(this,(function(v){switch(v.label){case 0:if(v.trys.push([0,8,,9]),console.log("start authenticate connection",t),!t)throw{code:constants.DEVICE_NAME_IS_EMPTY};return e=Date.now(),[4,this.getDevicePsk(t)];case 1:return r=v.sent(),this.localPsk=r,this.reporter.info(constants.CONNECT_AUTH,{localPsk:r}),[4,this.protocol.getDeviceAuthInfo()];case 2:if(o=v.sent(),s=o.sign,i=o.timestamp,n=utils.encrypt(""+(i+60)+this.productId+t,r),s!==n)throw{code:constants.CONNECT_SIGN_AUTH_ERROR};return[4,this.protocol.getDeviceInfo()];case 3:if(c=v.sent(),a=c.version,d=c.mtu,l=c.needSetMtu,u=c.otaVersion,this.deviceName=t,this.bleVersion=a,this.mtu=d,!l)return[3,7];v.label=4;case 4:return v.trys.push([4,6,,7]),[4,this.setMtu(this.mtu)];case 5:return v.sent(),this.writeMtuResult("success"),[3,7];case 6:return v.sent(),this.writeMtuResult("fail"),[3,7];case 7:return this.otaVersion=u,u&&this.models.reportOTAVersion({Version:u,DeviceId:this.explorerDeviceId}),this.reporter.info(constants.CONNECT_RESULT_WRITE_SUCCESS,{timeCost:Date.now()-e,version:a,mtu:d,otaVersion:u}),this.authorized=!0,this.emit("authorized",{version:a,mtu:d,otaVersion:u}),this.stopListenLLEvents(),this.startListenLLEvents(),[3,9];case 8:return p=v.sent(),this.protocol.reportConnectError(),this.reporter.error(constants.CONNECT_RESULT_WRITE_FAIL,{error:p}),[2,Promise.reject(this.normalizeError(p))];case 9:return[2]}}))}))},t.serviceId16="0000FFE0-0000-1000-8000-00805F9B34FB",t.serviceId="0000FFE0-65D0-4E20-B56A-E493541BA4E2",t.deviceFilter=function(e,r){if(!e.advertisServiceUUIDs||!e.advertisServiceUUIDs.find((function(e){return e===t.serviceId16}))||!e.advertisData)return null;var o=t.options.appDevSdk.reporter;try{var s=Math.floor(10*Math.random())%10==1||!0,i=arrayBufferToHexStringArray(e.advertisData);s&&o.info(constants_1.REPORT_EVENT_TYPE,{message:"收到广播",serviceId:t.serviceId,hexArr:i.join(",")});var n=parseInt(i[2],16)-(parseInt(i[2],16)>>2<<2),c=parseInt(i[2],16)>>4,a=parseInt(i[2],16)>>2&1,d=r.productId&&r.deviceName?r.productId+"/"+r.deviceName:"",l=d?utils.get8ByteFromStr(""+d.replace("/","")):"",u=!!d,p="",v="",_="",h=constants.DEVICE_STATE_MAP[n],E=[constants.DEVICE_HAS_BINDED,constants.DEVICE_HAS_CONNECTED].indexOf(h)>-1;if(E)_=i.slice(3,11).join("").toLocaleLowerCase(),v=i.slice(11).join("").toLocaleLowerCase(),p=r.productId;else{var f=i.slice(3,9);e.name&&-1===e.name.indexOf("_")&&(e.name=e.name+"_"+f.slice(0,2).join("")),p=libs_1.hex2str(i.slice(9))}s&&o.info(constants_1.REPORT_EVENT_TYPE,{message:"解析广播",serviceId:t.serviceId,data:{bindState:h||"x",targetDeviceId:d,targetDeviceIdentify:l,deviceUserIdentify:v||"x",deviceProductId:p||"x",deviceIdentify:_||"x",isDynamicRegister:!!a}});var D=function(){var o=tslib_1.__assign(tslib_1.__assign({},e),{serviceId:t.serviceId,deviceName:r.deviceName||"",productId:p,extendInfo:{bindState:constants.DEVICE_STATE_MAP[h],moduleVersion:c,isDynamicRegister:!!a,standard:!0}});return console.log("---设备匹配成功---",o),o};return u&&E&&_===l?D():u||E?null:D()}catch(e){console.error("llsync device filter error",e),o.error(constants_1.REPORT_EVENT_TYPE,{message:"协议广播出错",error:e})}},t}(LLSyncDeviceAdapterBase_1.LLSyncDeviceAdapterBase);exports.LLSyncDeviceAdapter=LLSyncDeviceAdapter,exports.StandardDeviceAdapter=LLSyncDeviceAdapter;
//# sourceMappingURL=LLSyncDeviceAdapter.js.map

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.LLSyncProtocol=exports.LLSyncConfig=void 0;var tslib_1=require("tslib"),constants=tslib_1.__importStar(require("../../constants")),qcloud_iotexplorer_appdev_sdk_1=require("qcloud-iotexplorer-appdev-sdk"),libs_1=require("../../libs"),_a=qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.utils,noop=_a.noop,genPromise=_a.genPromise,byteUtil=_a.byteUtil;exports.LLSyncConfig={BLE_PSK_DEVICE_KEY:"ble_psk_device_ket",waitConnectReplyTime:1e4,waitBindReplyTime:1e4,waitGetRegisterDeviceInfo:1e4,waitGetCheckTimeoutReplyTime:1e4,waitGetCheckTimeoutDefaultReplyTime:6e4,waitControlReplyTime:1e4,waitGetDeviceInfoTime:1e4,waitUpdateReplyInt:1e4,mtuDefaultMap:{0:void 0,1:20,2:20}};var LLSyncProtocol=function(){function t(t){this._cleanupUserCheckHandleAfterCancel=noop,this.deviceAdapter=t}return Object.defineProperty(t.prototype,"reporter",{get:function(){return this.deviceAdapter.reporter},enumerable:!1,configurable:!0}),t.prototype.requestBindDevice=function(t){var e=t.needUserCheck,n=void 0!==e&&e,i=t.isDynamicRegister,o=void 0!==i&&i;return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,e,i,s,r,a=this;return tslib_1.__generator(this,(function(c){switch(c.label){case 0:return t=Math.floor(Date.now()/1e3),e=parseInt(libs_1.gen4BytesIntHex(),16),i=this._parseDataBeforeConnect(""+constants.DEVICE_INFO_WRITE_PREFIX[constants.TIME_SYNC]+e.toString(16)+t.toString(16),constants.TIME_SYNC),this.reporter.info(constants.TIME_SYNC,{timestamp:t,nonce:e,bindDeviceData:i,needUserCheck:n,isDynamicRegister:o}),o?[4,this.registerDevice({timestamp:t,bindDeviceData:i,nonce:e})]:[3,2];case 1:s=c.sent(),i=s.bleData,t=s.timestamp,c.label=2;case 2:return r={bindDeviceData:i,dataHandler:function(i){if(!i.length)throw{code:constants.CONNECT_REPLY_INVALID};if(n){var o=!(libs_1.getStrLength(i)>>15);if(console.log("bind auth response userCheckResult",o),!o)throw{code:constants.GET_USER_CHECK_REJECT}}var s=i.slice(2,22).join(""),r=libs_1.hex2str(i.slice(22)),c={sign:s.toLocaleLowerCase(),deviceName:r,nonce:e,timestamp:t+60};return a.reporter.info(constants.TIME_SYNC,c),c}},this.reporter.info(constants.TIME_SYNC,r),[2,n?this.waitUserCheckAndBindDevice(r):this.bindDeviceDirectly(r)]}}))}))},t.prototype.registerDevice=function(t){var e=t.timestamp,n=t.nonce,i=t.bindDeviceData;return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,o,s,r,a,c,_,E;return tslib_1.__generator(this,(function(I){switch(I.label){case 0:return[4,this.deviceAdapter.writeAndWait4Response(i,constants.REGISTER_DEVICE_INFO_REPLY,(function(t){if(!t.length)throw{code:constants.REGISTER_DEVICE_INFO_REPLY_INVALID};var e=libs_1.getStrLength(t),n=parseInt(t.slice(2,3).join(""),16),i=libs_1.hex2str(t.slice(3,3+n));return{sign:libs_1.hex2str(t.slice(3+n,3+e)),deviceName:i}}),{timeout:exports.LLSyncConfig.waitGetRegisterDeviceInfo,timeoutCode:constants.WAIT_REGISTER_DEVICE_INFO_REPLY_TIMEOUT,writeId:constants.DEVICE_INFO_WRITE_ID})];case 1:t=I.sent(),o=t.deviceName,s=t.sign,this.reporter.info(constants.TIME_SYNC,{deviceName:o,sign:s}),r=0,I.label=2;case 2:return I.trys.push([2,4,,5]),[4,this.deviceAdapter.models.dynamicRegisterDevice({deviceName:o,sign:s,productId:this.deviceAdapter.productId,timestamp:e,nonce:n})];case 3:return a=I.sent().payload,c=Math.floor(Date.now()/1e3),r=1,_=this._parseDataBeforeConnect(constants.DEVICE_INFO_WRITE_PREFIX[constants.SEND_REGISTER_DEVICE_PAYLOAD]+""+libs_1.U8ToHexString(r)+libs_1.U8ToHexString(a.length)+libs_1.str2hexStr(a)+n.toString(16)+c.toString(16),constants.SEND_REGISTER_DEVICE_PAYLOAD),this.reporter.info(constants.REGISTER_DEVICE_SUCCESS,{payload:a,bleData:_}),[2,{bleData:_,timestamp:c}];case 4:throw E=I.sent(),tslib_1.__assign(tslib_1.__assign({},E),{code:constants.REGISTER_DEVICE_ERROR});case 5:return[2]}}))}))},t.prototype.bindDeviceDirectly=function(t){var e=t.bindDeviceData,n=t.dataHandler;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){return[2,this.deviceAdapter.writeAndWait4Response(e,constants.BIND_AUTH,n,{timeout:exports.LLSyncConfig.waitBindReplyTime,timeoutCode:constants.WAIT_CONNECT_REPLY_TIMEOUT,writeId:constants.DEVICE_INFO_WRITE_ID})]}))}))},t.prototype.waitUserCheckAndBindDevice=function(t){var e=t.bindDeviceData,n=t.dataHandler;return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,i,o,s=this;return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return t=genPromise(),o=function(e){clearTimeout(i),console.log("registerUserCheckTimer",e),i=setTimeout((function(){console.log("user check timeout",e),s.cancelUserCheck("timeout"),t.reject({code:constants.WAIT_USER_CHECK_TIMEOUT})}),e)},this.deviceAdapter.wait4EventResponse(constants.USER_CHECK_TIMEOUT_CALLBACK,(function(t){if(!t.length)return console.log("get user check timeout duration fail, trigger default timeout timer"),s.reporter.info(constants.GET_USER_CHECK_TIMEOUT_ERROR),o(exports.LLSyncConfig.waitGetCheckTimeoutDefaultReplyTime);var e=1e3*parseInt(t.slice(2).join(""),16);s.reporter.info(constants.GET_USER_CHECK_TIMEOUT_SUCCESS,{timeoutDuration:e}),o(e)}),{timeout:exports.LLSyncConfig.waitGetCheckTimeoutReplyTime,timeoutHandler:function(){s.reporter.info(constants.GET_USER_CHECK_TIMEOUT_TIMEOUT),o(exports.LLSyncConfig.waitGetCheckTimeoutDefaultReplyTime-exports.LLSyncConfig.waitGetCheckTimeoutReplyTime)}}),this._cleanupUserCheckHandleAfterCancel=function(){console.log("process cleanup user check handler"),t.reject(null),s.deviceAdapter.off(constants.BIND_AUTH),s._cleanupUserCheckHandleAfterCancel=noop},[4,Promise.race([t.promise,this.deviceAdapter.writeAndWait4Response(e,constants.BIND_AUTH,n,{timeout:exports.LLSyncConfig.waitBindReplyTime,timeoutCode:constants.WAIT_CONNECT_REPLY_TIMEOUT,writeId:constants.DEVICE_INFO_WRITE_ID})])];case 1:return[2,r.sent()]}}))}))},t.prototype.cancelUserCheck=function(t){void 0===t&&(t="timeout"),console.log("cancel user check, reason: ",t),"cancel"===t&&this._cleanupUserCheckHandleAfterCancel();var e=""+constants.DEVICE_INFO_WRITE_PREFIX[constants.USER_CHECK_TIMEOUT]+libs_1.U8ToHexString("timeout"===t?constants.WRITE_USER_CHECK_TIMEOUT:constants.WRITE_USER_CHECK_CANCEL);this.deviceAdapter.write(e,{writeId:constants.DEVICE_INFO_WRITE_ID})},t.prototype.getDeviceAuthInfo=function(){var t=Math.floor(Date.now()/1e3),e=this._parseDataBeforeConnect(""+constants.DEVICE_INFO_WRITE_PREFIX[constants.CONNECT_AUTH]+t.toString(16)+libs_1.encrypt(t,this.deviceAdapter.localPsk),constants.CONNECT_AUTH);return this.deviceAdapter.writeAndWait4Response(e,constants.CONNECT_AUTH,(function(e){if(!e.length)throw{code:constants.CONNECT_REPLY_INVALID};return{sign:e.slice(2,22).join("").toLocaleLowerCase(),timestamp:t}}),{timeout:exports.LLSyncConfig.waitConnectReplyTime,timeoutCode:constants.WAIT_CONNECT_REPLY_TIMEOUT,writeId:constants.DEVICE_INFO_WRITE_ID})},t.prototype.getDeviceInfo=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){return[2,this.deviceAdapter.writeAndWait4Response(constants.DEVICE_INFO_WRITE_PREFIX[constants.CONNECT_RESULT_WRITE_SUCCESS],constants.DEVICE_INFO,(function(t){if(!t.length)throw{code:constants.DEVICE_INFO_INVALID};var e=parseInt(t.slice(2,3).join(""),16),n=parseInt(t.slice(3,5).join(""),16),i=!!(n>>15),o=8191&n,s=t.slice(6).join("");return{version:e,mtu:o,needSetMtu:i,otaVersion:s?libs_1.hex2str(s):s}}),{timeout:exports.LLSyncConfig.waitGetDeviceInfoTime,timeoutCode:constants.WAIT_GET_DEVICE_INFO_TIMEOUT,writeId:constants.DEVICE_INFO_WRITE_ID})]}))}))},t.prototype.getUnbindAuthSign=function(){var t=this._parseDataBeforeConnect(""+constants.DEVICE_INFO_WRITE_PREFIX[constants.UNBIND_AUTH]+libs_1.encrypt(constants.UNBIND_REQUEST,this.deviceAdapter.localPsk),constants.UNBIND_AUTH);return this.deviceAdapter.writeAndWait4Response(t,constants.UNBIND_AUTH,(function(t){if(!t.length)throw{code:constants.UNBIND_REPLY_INVALID};return{sign:t.slice(2,22).join("").toLocaleLowerCase()}}),{timeout:exports.LLSyncConfig.waitConnectReplyTime,timeoutCode:constants.WAIT_UNBIND_REPLY_TIMEOUT,writeId:constants.DEVICE_INFO_WRITE_ID})},t.prototype.controlDeviceAction=function(t){var e=libs_1.convertActionControlToTlv(t,this.deviceAdapter.dataTemplate),n=e.actionIndex,i=e.tlvData,o=e.tmpData,s=this.deviceAdapter.sliceData(tslib_1.__spread([libs_1.getTypeHead(constants.DEVICE_DATA_WRITE_HEAD[constants.CONTROL_ACTION],n),libs_1.U16ToHexString(i.length)],i),o,constants.CONTROL_ACTION);return this.deviceAdapter.writeAndWait4Response(s,constants.ACTION_REPLY,(function(t){if(!t.length)throw{code:constants.CONTROL_REPLY_INVALID};var e=parseInt(t.slice(2,3).join(""),16),n=t.slice(3);if(0===e)return{success:!0,output:n};throw{code:constants.CONTROL_REPLY_CODE_INVALID,errCode:e}}),{timeout:exports.LLSyncConfig.waitControlReplyTime,timeoutCode:constants.WAIT_CONTROL_ACTION_REPLY_TIMEOUT,writeId:constants.DEVICE_DATA_WRITE_ID,wrapSplitDataMode:constants.ACTION_REPLY})},t.prototype.controlDeviceProperty=function(t){var e=libs_1.convertPropertiesChangeToTlv(t,this.deviceAdapter.dataTemplate),n=e.tlvData,i=e.tmpData,o=this.deviceAdapter.sliceData(tslib_1.__spread([libs_1.getTypeHead(constants.DEVICE_DATA_WRITE_HEAD[constants.CONTROL_DEVICE],constants.DEVICE_DATA_WRITE_SUFFIX[constants.CONTROL_DEVICE]),libs_1.U16ToHexString(n.length)],n),i,constants.CONTROL_DEVICE);return this.deviceAdapter.writeAndWait4Response(o,constants.CONTROL_REPLY,(function(t){if(!t.length)throw{code:constants.CONTROL_REPLY_INVALID};var e=parseInt(t.slice(2).join(""),16);if(0===e)return{code:0};throw{code:constants.CONTROL_REPLY_CODE_INVALID,errCode:e}}),{timeout:exports.LLSyncConfig.waitControlReplyTime,timeoutCode:constants.WAIT_CONTROL_DEVICE_REPLY_TIMEOUT,writeId:constants.DEVICE_DATA_WRITE_ID})},t.prototype.reportBindSuccess=function(t){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,n;return tslib_1.__generator(this,(function(i){switch(i.label){case 0:return e=libs_1.gen4BytesIntHex(),this.reporter.info(constants.BIND_AUTH_SUCCESS,{identify:this.deviceAdapter.userIdentify,localPsk:e,timeCost:Date.now()-t}),n=""+constants.DEVICE_INFO_WRITE_PREFIX[constants.BIND_AUTH_SUCCESS]+constants.DEVICE_INFO_WRITE_PREFIX[constants.BIND_AUTH_SUCCESS]+e+this.deviceAdapter.userIdentify,this.deviceAdapter.writeData(this._parseDataBeforeConnect(n,constants.BIND_AUTH_SUCCESS),{writeId:constants.DEVICE_INFO_WRITE_ID}),[4,this.deviceAdapter.models.setDeviceConfig({DeviceId:this.deviceAdapter.explorerDeviceId,DeviceKey:exports.LLSyncConfig.BLE_PSK_DEVICE_KEY,DeviceValue:e})];case 1:return i.sent(),[2,e]}}))}))},t.prototype.reportBindError=function(t){var e=""+constants.DEVICE_INFO_WRITE_PREFIX[constants.BIND_AUTH_FAIL]+libs_1.str2hexStr(t);this.deviceAdapter.write(e,{writeId:constants.DEVICE_INFO_WRITE_ID})},t.prototype.reportConnectError=function(){var t=constants.DEVICE_INFO_WRITE_PREFIX[constants.CONNECT_RESULT_WRITE_FAIL];this.deviceAdapter.write(t,{writeId:constants.DEVICE_INFO_WRITE_ID})},t.prototype.reportPropertyReportResult=function(t){void 0===t&&(t=0);var e=constants.DEVICE_DATA_WRITE_HEAD,n=constants.DEVICE_DATA_WRITE_SUFFIX,i=constants.REPORT_RESULT,o=constants.DEVICE_DATA_WRITE_ID,s=""+libs_1.getTypeHead(e[i],n[i])+byteUtil.byteToHex(byteUtil.convertNumberToByte(t));this.deviceAdapter.write(s,{writeId:o})},t.prototype.reportEventReportResult=function(t,e){void 0===t&&(t=0);var n=constants.DEVICE_DATA_WRITE_HEAD,i=constants.EVENT_REPLY,o=constants.DEVICE_DATA_WRITE_ID,s=""+libs_1.getTypeHead(n[i],e)+byteUtil.byteToHex(byteUtil.convertNumberToByte(t));this.deviceAdapter.write(s,{writeId:o})},t.prototype.reportGetStatusResult=function(t,e,n){void 0===t&&(t=0);var i=constants.DEVICE_DATA_WRITE_HEAD,o=constants.DEVICE_DATA_WRITE_SUFFIX,s=constants.GET_STATUS,r=constants.DEVICE_DATA_WRITE_ID,a=[""+libs_1.getTypeHead(i[s],o[s]),byteUtil.byteToHex(byteUtil.convertNumberToByte(t))];e&&(a=this.deviceAdapter.sliceData(a.concat(tslib_1.__spread([libs_1.U16ToHexString(e.length)],e)),n,constants.GET_STATUS)),this.deviceAdapter.writeData(a,{writeId:r})},t.prototype.reportUnbindResult=function(t){var e=constants.DEVICE_INFO_WRITE_PREFIX[constants.UNBIND_RESULT_AUTH_SUCCESS];return"fail"===t&&(e=constants.DEVICE_INFO_WRITE_PREFIX[constants.UNBIND_RESULT_AUTH_FAIL]),this.deviceAdapter.write(e,{writeId:constants.DEVICE_INFO_WRITE_ID})},t.prototype._parseDataBeforeConnect=function(t,e){var n,i=this.deviceAdapter.extendInfo.moduleVersion;i&&(n=exports.LLSyncConfig.mtuDefaultMap[i],i>=2&&(n=exports.LLSyncConfig.mtuDefaultMap[2])),this.reporter.info(constants.SET_MTU_BEFORE_CONNECT,{version:i,mtuLength:n}),this.deviceAdapter.bleVersion=i,this.deviceAdapter.mtu=n;var o=byteUtil.hexString2hexArray(t.slice(2)),s=[o],r=[t.slice(0,2)];return i>0&&r.push(libs_1.U16ToHexString(o.length)),r=r.concat(o),this.deviceAdapter.sliceData(r,s,e)},t}();exports.LLSyncProtocol=LLSyncProtocol;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.LLSyncProtocol=exports.LLSyncConfig=void 0;var tslib_1=require("tslib"),constants=tslib_1.__importStar(require("../../constants")),qcloud_iotexplorer_appdev_sdk_1=require("qcloud-iotexplorer-appdev-sdk"),libs_1=require("../../libs"),_a=qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.utils,noop=_a.noop,genPromise=_a.genPromise,byteUtil=_a.byteUtil;exports.LLSyncConfig={BLE_PSK_DEVICE_KEY:"ble_psk_device_ket",waitConnectReplyTime:1e4,waitBindReplyTime:1e4,waitGetRegisterDeviceInfo:1e4,waitGetCheckTimeoutReplyTime:1e4,waitGetCheckTimeoutDefaultReplyTime:6e4,waitControlReplyTime:1e4,waitGetDeviceInfoTime:1e4,waitUpdateReplyInt:1e4,mtuDefaultMap:{0:void 0,1:20,2:20}};var LLSyncProtocol=function(){function t(t){this._cleanupUserCheckHandleAfterCancel=noop,this.deviceAdapter=t}return Object.defineProperty(t.prototype,"reporter",{get:function(){return this.deviceAdapter.reporter},enumerable:!1,configurable:!0}),t.prototype.requestBindDevice=function(t){var e=t.needUserCheck,n=void 0!==e&&e,i=t.isDynamicRegister,o=void 0!==i&&i;return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,e,i,s,r,a=this;return tslib_1.__generator(this,(function(c){switch(c.label){case 0:return t=Math.floor(Date.now()/1e3),e=parseInt(libs_1.gen4BytesIntHex(),16),i=this._parseDataBeforeConnect(""+constants.DEVICE_INFO_WRITE_PREFIX[constants.TIME_SYNC]+e.toString(16)+t.toString(16),constants.TIME_SYNC),this.reporter.info(constants.TIME_SYNC,{timestamp:t,nonce:e,bindDeviceData:i,needUserCheck:n,isDynamicRegister:o}),o?[4,this.registerDevice({timestamp:t,bindDeviceData:i,nonce:e})]:[3,2];case 1:s=c.sent(),i=s.bleData,t=s.timestamp,c.label=2;case 2:return r={bindDeviceData:i,dataHandler:function(i){if(!i.length)throw{code:constants.CONNECT_REPLY_INVALID};if(n){var o=!(libs_1.getStrLength(i)>>15);if(console.log("bind auth response userCheckResult",o),!o)throw{code:constants.GET_USER_CHECK_REJECT}}var s=i.slice(2,22).join(""),r=libs_1.hex2str(i.slice(22)),c={sign:s.toLocaleLowerCase(),deviceName:r,nonce:e,timestamp:t+60};return a.reporter.info(constants.TIME_SYNC,c),c}},this.reporter.info(constants.TIME_SYNC,r),[2,n?this.waitUserCheckAndBindDevice(r):this.bindDeviceDirectly(r)]}}))}))},t.prototype.registerDevice=function(t){var e=t.timestamp,n=t.nonce,i=t.bindDeviceData;return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,o,s,r,a,c,_,E,I=this;return tslib_1.__generator(this,(function(d){switch(d.label){case 0:return[4,this.deviceAdapter.writeAndWait4Response(i,constants.REGISTER_DEVICE_INFO_REPLY,(function(t){if(console.log("-----回包啦-----",t),!t.length)throw{code:constants.REGISTER_DEVICE_INFO_REPLY_INVALID};var e=libs_1.getStrLength(t),n=parseInt(t.slice(2,3).join(""),16),i=libs_1.hex2str(t.slice(3,3+n)),o=libs_1.hex2str(t.slice(3+n,3+e));return console.log("-----解析回包啦----",o),I.reporter.info(constants.REGISTER_DEVICE_INFO_REPLY,{allLength:e,deviceNameLength:n,deviceName:i,sign:o}),{sign:o,deviceName:i}}),{timeout:exports.LLSyncConfig.waitGetRegisterDeviceInfo,timeoutCode:constants.WAIT_REGISTER_DEVICE_INFO_REPLY_TIMEOUT,writeId:constants.DEVICE_INFO_WRITE_ID})];case 1:t=d.sent(),o=t.deviceName,s=t.sign,this.reporter.info(constants.TIME_SYNC,{deviceName:o,sign:s}),r=0,d.label=2;case 2:return d.trys.push([2,4,,5]),[4,this.deviceAdapter.models.dynamicRegisterDevice({deviceName:o,sign:s,productId:this.deviceAdapter.productId,timestamp:e,nonce:n})];case 3:return a=d.sent().payload,c=Math.floor(Date.now()/1e3),r=1,_=this._parseDataBeforeConnect(constants.DEVICE_INFO_WRITE_PREFIX[constants.SEND_REGISTER_DEVICE_PAYLOAD]+""+libs_1.U8ToHexString(r)+libs_1.U8ToHexString(a.length)+libs_1.str2hexStr(a)+n.toString(16)+c.toString(16),constants.SEND_REGISTER_DEVICE_PAYLOAD),this.reporter.info(constants.REGISTER_DEVICE_SUCCESS,{payload:a,bleData:_}),[2,{bleData:_,timestamp:c}];case 4:throw E=d.sent(),tslib_1.__assign(tslib_1.__assign({},E),{code:constants.REGISTER_DEVICE_ERROR});case 5:return[2]}}))}))},t.prototype.bindDeviceDirectly=function(t){var e=t.bindDeviceData,n=t.dataHandler;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){return[2,this.deviceAdapter.writeAndWait4Response(e,constants.BIND_AUTH,n,{timeout:exports.LLSyncConfig.waitBindReplyTime,timeoutCode:constants.WAIT_CONNECT_REPLY_TIMEOUT,writeId:constants.DEVICE_INFO_WRITE_ID})]}))}))},t.prototype.waitUserCheckAndBindDevice=function(t){var e=t.bindDeviceData,n=t.dataHandler;return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,i,o,s=this;return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return t=genPromise(),o=function(e){clearTimeout(i),console.log("registerUserCheckTimer",e),i=setTimeout((function(){console.log("user check timeout",e),s.cancelUserCheck("timeout"),t.reject({code:constants.WAIT_USER_CHECK_TIMEOUT})}),e)},this.deviceAdapter.wait4EventResponse(constants.USER_CHECK_TIMEOUT_CALLBACK,(function(t){if(!t.length)return console.log("get user check timeout duration fail, trigger default timeout timer"),s.reporter.info(constants.GET_USER_CHECK_TIMEOUT_ERROR),o(exports.LLSyncConfig.waitGetCheckTimeoutDefaultReplyTime);var e=1e3*parseInt(t.slice(2).join(""),16);s.reporter.info(constants.GET_USER_CHECK_TIMEOUT_SUCCESS,{timeoutDuration:e}),o(e)}),{timeout:exports.LLSyncConfig.waitGetCheckTimeoutReplyTime,timeoutHandler:function(){s.reporter.info(constants.GET_USER_CHECK_TIMEOUT_TIMEOUT),o(exports.LLSyncConfig.waitGetCheckTimeoutDefaultReplyTime-exports.LLSyncConfig.waitGetCheckTimeoutReplyTime)}}),this._cleanupUserCheckHandleAfterCancel=function(){console.log("process cleanup user check handler"),t.reject(null),s.deviceAdapter.off(constants.BIND_AUTH),s._cleanupUserCheckHandleAfterCancel=noop},[4,Promise.race([t.promise,this.deviceAdapter.writeAndWait4Response(e,constants.BIND_AUTH,n,{timeout:exports.LLSyncConfig.waitBindReplyTime,timeoutCode:constants.WAIT_CONNECT_REPLY_TIMEOUT,writeId:constants.DEVICE_INFO_WRITE_ID})])];case 1:return[2,r.sent()]}}))}))},t.prototype.cancelUserCheck=function(t){void 0===t&&(t="timeout"),console.log("cancel user check, reason: ",t),"cancel"===t&&this._cleanupUserCheckHandleAfterCancel();var e=""+constants.DEVICE_INFO_WRITE_PREFIX[constants.USER_CHECK_TIMEOUT]+libs_1.U8ToHexString("timeout"===t?constants.WRITE_USER_CHECK_TIMEOUT:constants.WRITE_USER_CHECK_CANCEL);this.deviceAdapter.write(e,{writeId:constants.DEVICE_INFO_WRITE_ID})},t.prototype.getDeviceAuthInfo=function(){var t=Math.floor(Date.now()/1e3),e=this._parseDataBeforeConnect(""+constants.DEVICE_INFO_WRITE_PREFIX[constants.CONNECT_AUTH]+t.toString(16)+libs_1.encrypt(t,this.deviceAdapter.localPsk),constants.CONNECT_AUTH);return this.deviceAdapter.writeAndWait4Response(e,constants.CONNECT_AUTH,(function(e){if(!e.length)throw{code:constants.CONNECT_REPLY_INVALID};return{sign:e.slice(2,22).join("").toLocaleLowerCase(),timestamp:t}}),{timeout:exports.LLSyncConfig.waitConnectReplyTime,timeoutCode:constants.WAIT_CONNECT_REPLY_TIMEOUT,writeId:constants.DEVICE_INFO_WRITE_ID})},t.prototype.getDeviceInfo=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){return[2,this.deviceAdapter.writeAndWait4Response(constants.DEVICE_INFO_WRITE_PREFIX[constants.CONNECT_RESULT_WRITE_SUCCESS],constants.DEVICE_INFO,(function(t){if(!t.length)throw{code:constants.DEVICE_INFO_INVALID};var e=parseInt(t.slice(2,3).join(""),16),n=parseInt(t.slice(3,5).join(""),16),i=!!(n>>15),o=8191&n,s=t.slice(6).join("");return{version:e,mtu:o,needSetMtu:i,otaVersion:s?libs_1.hex2str(s):s}}),{timeout:exports.LLSyncConfig.waitGetDeviceInfoTime,timeoutCode:constants.WAIT_GET_DEVICE_INFO_TIMEOUT,writeId:constants.DEVICE_INFO_WRITE_ID})]}))}))},t.prototype.getUnbindAuthSign=function(){var t=this._parseDataBeforeConnect(""+constants.DEVICE_INFO_WRITE_PREFIX[constants.UNBIND_AUTH]+libs_1.encrypt(constants.UNBIND_REQUEST,this.deviceAdapter.localPsk),constants.UNBIND_AUTH);return this.deviceAdapter.writeAndWait4Response(t,constants.UNBIND_AUTH,(function(t){if(!t.length)throw{code:constants.UNBIND_REPLY_INVALID};return{sign:t.slice(2,22).join("").toLocaleLowerCase()}}),{timeout:exports.LLSyncConfig.waitConnectReplyTime,timeoutCode:constants.WAIT_UNBIND_REPLY_TIMEOUT,writeId:constants.DEVICE_INFO_WRITE_ID})},t.prototype.controlDeviceAction=function(t){var e=libs_1.convertActionControlToTlv(t,this.deviceAdapter.dataTemplate),n=e.actionIndex,i=e.tlvData,o=e.tmpData,s=this.deviceAdapter.sliceData(tslib_1.__spread([libs_1.getTypeHead(constants.DEVICE_DATA_WRITE_HEAD[constants.CONTROL_ACTION],n),libs_1.U16ToHexString(i.length)],i),o,constants.CONTROL_ACTION);return this.deviceAdapter.writeAndWait4Response(s,constants.ACTION_REPLY,(function(t){if(!t.length)throw{code:constants.CONTROL_REPLY_INVALID};var e=parseInt(t.slice(2,3).join(""),16),n=t.slice(3);if(0===e)return{success:!0,output:n};throw{code:constants.CONTROL_REPLY_CODE_INVALID,errCode:e}}),{timeout:exports.LLSyncConfig.waitControlReplyTime,timeoutCode:constants.WAIT_CONTROL_ACTION_REPLY_TIMEOUT,writeId:constants.DEVICE_DATA_WRITE_ID,wrapSplitDataMode:constants.ACTION_REPLY})},t.prototype.controlDeviceProperty=function(t){var e=libs_1.convertPropertiesChangeToTlv(t,this.deviceAdapter.dataTemplate),n=e.tlvData,i=e.tmpData,o=this.deviceAdapter.sliceData(tslib_1.__spread([libs_1.getTypeHead(constants.DEVICE_DATA_WRITE_HEAD[constants.CONTROL_DEVICE],constants.DEVICE_DATA_WRITE_SUFFIX[constants.CONTROL_DEVICE]),libs_1.U16ToHexString(n.length)],n),i,constants.CONTROL_DEVICE);return this.deviceAdapter.writeAndWait4Response(o,constants.CONTROL_REPLY,(function(t){if(!t.length)throw{code:constants.CONTROL_REPLY_INVALID};var e=parseInt(t.slice(2).join(""),16);if(0===e)return{code:0};throw{code:constants.CONTROL_REPLY_CODE_INVALID,errCode:e}}),{timeout:exports.LLSyncConfig.waitControlReplyTime,timeoutCode:constants.WAIT_CONTROL_DEVICE_REPLY_TIMEOUT,writeId:constants.DEVICE_DATA_WRITE_ID})},t.prototype.reportBindSuccess=function(t){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,n;return tslib_1.__generator(this,(function(i){switch(i.label){case 0:return e=libs_1.gen4BytesIntHex(),this.reporter.info(constants.BIND_AUTH_SUCCESS,{identify:this.deviceAdapter.userIdentify,localPsk:e,timeCost:Date.now()-t}),n=""+constants.DEVICE_INFO_WRITE_PREFIX[constants.BIND_AUTH_SUCCESS]+constants.DEVICE_INFO_WRITE_PREFIX[constants.BIND_AUTH_SUCCESS]+e+this.deviceAdapter.userIdentify,this.deviceAdapter.writeData(this._parseDataBeforeConnect(n,constants.BIND_AUTH_SUCCESS),{writeId:constants.DEVICE_INFO_WRITE_ID}),[4,this.deviceAdapter.models.setDeviceConfig({DeviceId:this.deviceAdapter.explorerDeviceId,DeviceKey:exports.LLSyncConfig.BLE_PSK_DEVICE_KEY,DeviceValue:e})];case 1:return i.sent(),[2,e]}}))}))},t.prototype.reportBindError=function(t){var e=""+constants.DEVICE_INFO_WRITE_PREFIX[constants.BIND_AUTH_FAIL]+libs_1.str2hexStr(t);this.deviceAdapter.write(e,{writeId:constants.DEVICE_INFO_WRITE_ID})},t.prototype.reportConnectError=function(){var t=constants.DEVICE_INFO_WRITE_PREFIX[constants.CONNECT_RESULT_WRITE_FAIL];this.deviceAdapter.write(t,{writeId:constants.DEVICE_INFO_WRITE_ID})},t.prototype.reportPropertyReportResult=function(t){void 0===t&&(t=0);var e=constants.DEVICE_DATA_WRITE_HEAD,n=constants.DEVICE_DATA_WRITE_SUFFIX,i=constants.REPORT_RESULT,o=constants.DEVICE_DATA_WRITE_ID,s=""+libs_1.getTypeHead(e[i],n[i])+byteUtil.byteToHex(byteUtil.convertNumberToByte(t));this.deviceAdapter.write(s,{writeId:o})},t.prototype.reportEventReportResult=function(t,e){void 0===t&&(t=0);var n=constants.DEVICE_DATA_WRITE_HEAD,i=constants.EVENT_REPLY,o=constants.DEVICE_DATA_WRITE_ID,s=""+libs_1.getTypeHead(n[i],e)+byteUtil.byteToHex(byteUtil.convertNumberToByte(t));this.deviceAdapter.write(s,{writeId:o})},t.prototype.reportGetStatusResult=function(t,e,n){void 0===t&&(t=0);var i=constants.DEVICE_DATA_WRITE_HEAD,o=constants.DEVICE_DATA_WRITE_SUFFIX,s=constants.GET_STATUS,r=constants.DEVICE_DATA_WRITE_ID,a=[""+libs_1.getTypeHead(i[s],o[s]),byteUtil.byteToHex(byteUtil.convertNumberToByte(t))];e&&(a=this.deviceAdapter.sliceData(a.concat(tslib_1.__spread([libs_1.U16ToHexString(e.length)],e)),n,constants.GET_STATUS)),this.deviceAdapter.writeData(a,{writeId:r})},t.prototype.reportUnbindResult=function(t){var e=constants.DEVICE_INFO_WRITE_PREFIX[constants.UNBIND_RESULT_AUTH_SUCCESS];return"fail"===t&&(e=constants.DEVICE_INFO_WRITE_PREFIX[constants.UNBIND_RESULT_AUTH_FAIL]),this.deviceAdapter.write(e,{writeId:constants.DEVICE_INFO_WRITE_ID})},t.prototype._parseDataBeforeConnect=function(t,e){var n,i=this.deviceAdapter.extendInfo.moduleVersion;i&&(n=exports.LLSyncConfig.mtuDefaultMap[i],i>=2&&(n=exports.LLSyncConfig.mtuDefaultMap[2])),this.reporter.info(constants.SET_MTU_BEFORE_CONNECT,{version:i,mtuLength:n}),this.deviceAdapter.bleVersion=i,this.deviceAdapter.mtu=n;var o=byteUtil.hexString2hexArray(t.slice(2)),s=[o],r=[t.slice(0,2)];return i>0&&r.push(libs_1.U16ToHexString(o.length)),r=r.concat(o),this.deviceAdapter.sliceData(r,s,e)},t}();exports.LLSyncProtocol=LLSyncProtocol;
//# sourceMappingURL=LLSyncProtocol.js.map
{
"name": "qcloud-iotexplorer-bluetooth-adapter-llsync",
"version": "1.2.2-beta",
"version": "1.2.3-beta",
"description": "",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc