Socket
Socket
Sign inDemoInstall

qcloud-iotexplorer-bluetooth-adapter

Package Overview
Dependencies
Maintainers
1
Versions
164
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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

Comparing version 0.0.14 to 0.0.15

2

lib/core/DeviceAdapter.js

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DeviceAdapter=void 0;var tslib_1=require("tslib"),utillib_1=require("../libs/utillib"),BlueToothBase_1=require("./BlueToothBase"),DeviceAdapter=function(e){function t(t){var i=t.deviceId,r=t.productId,c=t.deviceName,s=t.name,o=t.actions,n=t.bluetoothApi,a=e.call(this)||this;if(a._name="",a._deviceId="",a._deviceName="",a._deviceConnected=!1,a._productId="",a._deviceRegistered=!1,a._services=[],a.characteristicsMap={},a._actions={},a._bluetoothApi={},!i)throw"无deviceId";if(!r)throw"productId为空";return a._bluetoothApi=n,a._actions=o,a._name=s,a._deviceName=c,a._deviceId=i,a._productId=r,a}return tslib_1.__extends(t,e),t.prototype._getNotifyId=function(e){var t=(void 0===e?{}:e).serviceId,i=void 0===t?"":t;i=i||this.serviceId;var r=this.characteristicsMap[i]||{writeIds:[],notifyIds:[],readIds:[],indicateIds:[]};return r.notifyIds[0]||r.indicateIds[0]},Object.defineProperty(t.prototype,"_writeId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).writeIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"_notifyId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).notifyIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"_readId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).readIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"_indicateId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).indicateIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"deviceId",{get:function(){return this._deviceId},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"productId",{get:function(){return this._productId},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"deviceName",{get:function(){return this._deviceName},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isConnected",{get:function(){return this._deviceConnected},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"originName",{get:function(){return this._name},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"explorerDeviceId",{get:function(){return this._productId+"/"+this._deviceName},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"serviceId",{get:function(){return this.constructor.serviceId},enumerable:!1,configurable:!0}),t.prototype.handleBLEMessage=function(e,t){t.serviceId,t.characteristicId;return{}},t.prototype.registerDevice=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return this._deviceRegistered?[3,2]:[4,this._actions.registerDevice({deviceId:this.explorerDeviceId,deviceName:this._deviceName,productId:this._productId})];case 1:e.sent(),this._deviceRegistered=!0,e.label=2;case 2:return[2]}}))}))},t.prototype.bindDevice=function(e){var t=void 0===e?{}:e,i=t.familyId,r=void 0===i?"":i,c=t.roomId,s=void 0===c?"":c;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]),[4,this.registerDevice()];case 1:return t.sent(),[4,this._actions.bindDevice({deviceId:this.explorerDeviceId,deviceName:this._deviceName,productId:this._productId,familyId:r,roomId:s})];case 2:return t.sent(),[2,this.explorerDeviceId];case 3:return e=t.sent(),[2,Promise.reject(this._normalizeError(e))];case 4:return[2]}}))}))},t.prototype.onBleConnectionStateChange=function(e){var t=e.connected;console.log("onBleConnectionStateChange",t,this._deviceConnected),t?this.emit("connect"):this._deviceConnected&&this.disconnectDevice(),this._deviceConnected=t,this.emit("bLEConnectionStateChange",{connected:t})},t.prototype.onBLECharacteristicValueChange=function(e){var t=e.serviceId,i=e.characteristicId,r=e.value;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,c,s,o,n,a,d,u;return tslib_1.__generator(this,(function(h){switch(h.label){case 0:return h.trys.push([0,3,,4]),e=utillib_1.arrayBufferToHexStringArray(r),c=this.handleBLEMessage(e,{serviceId:t,characteristicId:i}),s=c.shouldIgnore,o=c.reportData,n=tslib_1.__rest(c,["shouldIgnore","reportData"]),console.log("shouldIgnore?",s),s?[2]:(console.log("receive data",e,n),console.log("should report?",!!o,o),a=Date.now(),d=!1,this._deviceName&&o?(d=!0,[4,this._actions.reportDeviceData({deviceId:this.explorerDeviceId,deviceName:this._deviceName,productId:this._productId,data:o,timestamp:a})]):[3,2]);case 1:h.sent(),h.label=2;case 2:return this.emit("message",tslib_1.__assign(tslib_1.__assign({},n),{timestamp:a,dataReported:d})),[3,4];case 3:return u=h.sent(),console.error("onBLECharacteristicValueChange onError,",u),[3,4];case 4:return[2]}}))}))},t.prototype.disconnectDevice=function(){this._bluetoothApi.closeBLEConnection({deviceId:this._deviceId}),this.emit("disconnect")},t.prototype.connectDevice=function(e){var t=(void 0===e?{}:e).autoNotify,i=void 0===t||t;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,t,r;return tslib_1.__generator(this,(function(c){switch(c.label){case 0:return c.trys.push([0,7,,8]),this._deviceConnected?(console.log("Device已经连接",this._deviceId),[2]):[4,this.registerDevice()];case 1:return c.sent(),[4,this._bluetoothApi.createBLEConnection({deviceId:this._deviceId})];case 2:return c.sent(),console.log("createBLEConnection succ"),i?[4,this.getBLEDeviceServices()]:[3,6];case 3:return e=c.sent(),console.log("getBLEDeviceServices succ",e),this.emit("onGetBLEDeviceServices",e),[4,this.getBLEDeviceCharacteristics()];case 4:return t=c.sent(),console.log("getBLEDeviceCharacteristics succ",t),this.emit("onGetBLEDeviceCharacteristics",t),[4,this.notifyBLECharacteristicValueChange()];case 5:c.sent(),console.log("notifyBLECharacteristicValueChange succ"),c.label=6;case 6:return[3,8];case 7:throw r=c.sent(),console.error("connectDevice error",r),this._normalizeError(r);case 8:return[2]}}))}))},t.prototype.write=function(e,t){var i=void 0===t?{}:t,r=i.writeId,c=void 0===r?"":r,s=i.serviceId,o=void 0===s?"":s;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){if("string"==typeof e)console.log("writeBLECharacteristicValue",e),e=utillib_1.hexToArrayBuffer(e);else if(e instanceof ArrayBuffer)try{console.log("writeBLECharacteristicValue",utillib_1.arrayBufferToHexStringArray(e).join(""))}catch(e){}return[2,this._write(e,{writeId:c,serviceId:o})]}))}))},t.prototype._write=function(e,t){var i=void 0===t?{}:t,r=i.writeId,c=void 0===r?"":r,s=i.serviceId,o=void 0===s?"":s;return tslib_1.__awaiter(this,void 0,void 0,(function(){var t;return tslib_1.__generator(this,(function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,this._bluetoothApi.writeBLECharacteristicValue({deviceId:this._deviceId,characteristicId:c||this._writeId,serviceId:o||this.serviceId,value:e})];case 1:return i.sent(),[3,3];case 2:return t=i.sent(),[2,Promise.reject(this._normalizeError(t))];case 3:return[2]}}))}))},t.prototype.getBLEDeviceServices=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[4,this._bluetoothApi.getBLEDeviceServices({deviceId:this._deviceId})];case 1:return e=t.sent().services,this._services=e,[2,e]}}))}))},t.prototype.setCharacteristicsIds=function(e,t){var i={notifyIds:[],writeIds:[],indicateIds:[],readIds:[]},r=function(e,t){-1===e.indexOf(t)&&e.push(t)};t.forEach((function(e){var t=e.uuid,c=e.properties,s=c.notify,o=c.write,n=c.indicate,a=c.read;s?r(i.notifyIds,t):o?r(i.writeIds,t):n?r(i.indicateIds,t):a&&r(i.readIds,t)})),this.characteristicsMap[e]=i},t.prototype.getBLEDeviceCharacteristics=function(e){var t=(void 0===e?{}:e).serviceId,i=void 0===t?"":t;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 i=i||this.serviceId,[4,this._bluetoothApi.getBLEDeviceCharacteristics({deviceId:this._deviceId,serviceId:i})];case 1:return e=t.sent().characteristics,this.setCharacteristicsIds(i,e),[2,e]}}))}))},t.prototype.notifyBLECharacteristicValueChange=function(e){var t=void 0===e?{}:e,i=t.characteristicId,r=void 0===i?"":i,c=t.serviceId,s=void 0===c?"":c,o=t.state,n=void 0===o||o;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return r=r||this._getNotifyId(),s=s||this.serviceId,r?[3,1]:(console.warn("未找到指定service下的notifyId,该设备可能不支持notify"),[3,3]);case 1:return[4,this._bluetoothApi.notifyBLECharacteristicValueChange({deviceId:this._deviceId,characteristicId:r,serviceId:s,state:n})];case 2:e.sent(),e.label=3;case 3:return[2]}}))}))},t.prototype.readBLECharacteristicValue=function(e){var t=void 0===e?{}:e,i=t.serviceId,r=void 0===i?"":i,c=t.characteristicId,s=void 0===c?"":c;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return r=r||this.serviceId,s||(s=((this.characteristicsMap[this.serviceId]||{}).readIds||[])[0]),s?[3,1]:(console.warn("未找到指定service下的readId,该设备可能不支持read"),[3,3]);case 1:return[4,this._bluetoothApi.readBLECharacteristicValue({deviceId:this._deviceId,characteristicId:s,serviceId:r})];case 2:e.sent(),e.label=3;case 3:return[2]}}))}))},t.prototype.setBLEMTU=function(e){return this._bluetoothApi.setBLEMTU(e)},t.prototype.getBLEDeviceRSSI=function(){return this._bluetoothApi.getBLEDeviceRSSI({deviceId:this._deviceId})},t.deviceFilter=function(e,t){},t.serviceId="",t}(BlueToothBase_1.BlueToothBase);exports.DeviceAdapter=DeviceAdapter;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DeviceAdapter=void 0;var tslib_1=require("tslib"),utillib_1=require("../libs/utillib"),BlueToothBase_1=require("./BlueToothBase"),DeviceAdapter=function(e){function t(t){var i=t.deviceId,r=t.productId,c=t.deviceName,s=t.name,o=t.actions,n=t.bluetoothApi,a=e.call(this)||this;if(a._name="",a._deviceId="",a._deviceName="",a._deviceConnected=!1,a._productId="",a._deviceRegistered=!1,a._services=[],a.characteristicsMap={},a._actions={},a._bluetoothApi={},!i)throw"无deviceId";if(!r)throw"productId为空";return a._bluetoothApi=n,a._actions=o,a._name=s,a._deviceName=c,a._deviceId=i,a._productId=r,a}return tslib_1.__extends(t,e),t.prototype._getNotifyId=function(e){var t=(void 0===e?{}:e).serviceId,i=void 0===t?"":t;i=i||this.serviceId;var r=this.characteristicsMap[i]||{writeIds:[],notifyIds:[],readIds:[],indicateIds:[]};return r.notifyIds[0]||r.indicateIds[0]},Object.defineProperty(t.prototype,"_writeId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).writeIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"_notifyId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).notifyIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"_readId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).readIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"_indicateId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).indicateIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"deviceId",{get:function(){return this._deviceId},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"productId",{get:function(){return this._productId},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"deviceName",{get:function(){return this._deviceName},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isConnected",{get:function(){return this._deviceConnected},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"originName",{get:function(){return this._name},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"explorerDeviceId",{get:function(){return this._productId+"/"+this._deviceName},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"serviceId",{get:function(){return this.constructor.serviceId},enumerable:!1,configurable:!0}),t.prototype.handleBLEMessage=function(e,t){t.serviceId,t.characteristicId;return{}},t.prototype.registerDevice=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return this._deviceRegistered?[3,2]:[4,this._actions.registerDevice({deviceId:this.explorerDeviceId,deviceName:this._deviceName,productId:this._productId})];case 1:e.sent(),this._deviceRegistered=!0,e.label=2;case 2:return[2]}}))}))},t.prototype.bindDevice=function(e){var t=void 0===e?{}:e,i=t.familyId,r=void 0===i?"":i,c=t.roomId,s=void 0===c?"":c;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]),[4,this.registerDevice()];case 1:return t.sent(),[4,this._actions.bindDevice({deviceId:this.explorerDeviceId,deviceName:this._deviceName,productId:this._productId,familyId:r,roomId:s})];case 2:return t.sent(),[2,this.explorerDeviceId];case 3:return e=t.sent(),[2,Promise.reject(this._normalizeError(e))];case 4:return[2]}}))}))},t.prototype.onBleConnectionStateChange=function(e){var t=e.connected;console.log("onBleConnectionStateChange",t,this._deviceConnected),t?this.emit("connect"):this._deviceConnected&&this.disconnectDevice(),this._deviceConnected=t,this.emit("bLEConnectionStateChange",{connected:t})},t.prototype.onBLECharacteristicValueChange=function(e){var t=e.serviceId,i=e.characteristicId,r=e.value;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,c,s,o,n,a,d,u;return tslib_1.__generator(this,(function(h){switch(h.label){case 0:return h.trys.push([0,3,,4]),e=utillib_1.arrayBufferToHexStringArray(r),c=this.handleBLEMessage(e,{serviceId:t,characteristicId:i}),s=c.shouldIgnore,o=c.reportData,n=tslib_1.__rest(c,["shouldIgnore","reportData"]),console.log("shouldIgnore?",s),s?[2]:(console.log("receive data",e,n),console.log("should report?",!!o,o),a=Date.now(),d=!1,this._deviceName&&o?(d=!0,[4,this._actions.reportDeviceData({deviceId:this.explorerDeviceId,deviceName:this._deviceName,productId:this._productId,data:o,timestamp:a})]):[3,2]);case 1:h.sent(),h.label=2;case 2:return this.emit("message",tslib_1.__assign(tslib_1.__assign({},n),{timestamp:a,dataReported:d})),[3,4];case 3:return u=h.sent(),console.error("onBLECharacteristicValueChange onError,",u),[3,4];case 4:return[2]}}))}))},t.prototype.disconnectDevice=function(){this._bluetoothApi.closeBLEConnection({deviceId:this._deviceId}),this.emit("disconnect")},t.prototype.connectDevice=function(e){var t=(void 0===e?{}:e).autoNotify,i=void 0===t||t;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,t,r;return tslib_1.__generator(this,(function(c){switch(c.label){case 0:return c.trys.push([0,6,,7]),this._deviceConnected?(console.log("Device已经连接",this._deviceId),[2]):[4,this._bluetoothApi.createBLEConnection({deviceId:this._deviceId})];case 1:return c.sent(),console.log("createBLEConnection succ"),i?[4,this.getBLEDeviceServices()]:[3,5];case 2:return e=c.sent(),console.log("getBLEDeviceServices succ",e),this.emit("onGetBLEDeviceServices",e),[4,this.getBLEDeviceCharacteristics()];case 3:return t=c.sent(),console.log("getBLEDeviceCharacteristics succ",t),this.emit("onGetBLEDeviceCharacteristics",t),[4,this.notifyBLECharacteristicValueChange()];case 4:c.sent(),console.log("notifyBLECharacteristicValueChange succ"),c.label=5;case 5:return[3,7];case 6:throw r=c.sent(),console.error("connectDevice error",r),this._normalizeError(r);case 7:return[2]}}))}))},t.prototype.write=function(e,t){var i=void 0===t?{}:t,r=i.writeId,c=void 0===r?"":r,s=i.serviceId,o=void 0===s?"":s;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){if("string"==typeof e)console.log("writeBLECharacteristicValue",e),e=utillib_1.hexToArrayBuffer(e);else if(e instanceof ArrayBuffer)try{console.log("writeBLECharacteristicValue",utillib_1.arrayBufferToHexStringArray(e).join(""))}catch(e){}return[2,this._write(e,{writeId:c,serviceId:o})]}))}))},t.prototype._write=function(e,t){var i=void 0===t?{}:t,r=i.writeId,c=void 0===r?"":r,s=i.serviceId,o=void 0===s?"":s;return tslib_1.__awaiter(this,void 0,void 0,(function(){var t;return tslib_1.__generator(this,(function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,this._bluetoothApi.writeBLECharacteristicValue({deviceId:this._deviceId,characteristicId:c||this._writeId,serviceId:o||this.serviceId,value:e})];case 1:return i.sent(),[3,3];case 2:return t=i.sent(),[2,Promise.reject(this._normalizeError(t))];case 3:return[2]}}))}))},t.prototype.getBLEDeviceServices=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[4,this._bluetoothApi.getBLEDeviceServices({deviceId:this._deviceId})];case 1:return e=t.sent().services,this._services=e,[2,e]}}))}))},t.prototype.setCharacteristicsIds=function(e,t){var i={notifyIds:[],writeIds:[],indicateIds:[],readIds:[]},r=function(e,t){-1===e.indexOf(t)&&e.push(t)};t.forEach((function(e){var t=e.uuid,c=e.properties,s=c.notify,o=c.write,n=c.indicate,a=c.read;s?r(i.notifyIds,t):o?r(i.writeIds,t):n?r(i.indicateIds,t):a&&r(i.readIds,t)})),this.characteristicsMap[e]=i},t.prototype.getBLEDeviceCharacteristics=function(e){var t=(void 0===e?{}:e).serviceId,i=void 0===t?"":t;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 i=i||this.serviceId,[4,this._bluetoothApi.getBLEDeviceCharacteristics({deviceId:this._deviceId,serviceId:i})];case 1:return e=t.sent().characteristics,this.setCharacteristicsIds(i,e),[2,e]}}))}))},t.prototype.notifyBLECharacteristicValueChange=function(e){var t=void 0===e?{}:e,i=t.characteristicId,r=void 0===i?"":i,c=t.serviceId,s=void 0===c?"":c,o=t.state,n=void 0===o||o;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return r=r||this._getNotifyId(),s=s||this.serviceId,r?[3,1]:(console.warn("未找到指定service下的notifyId,该设备可能不支持notify"),[3,3]);case 1:return[4,this._bluetoothApi.notifyBLECharacteristicValueChange({deviceId:this._deviceId,characteristicId:r,serviceId:s,state:n})];case 2:e.sent(),e.label=3;case 3:return[2]}}))}))},t.prototype.readBLECharacteristicValue=function(e){var t=void 0===e?{}:e,i=t.serviceId,r=void 0===i?"":i,c=t.characteristicId,s=void 0===c?"":c;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return r=r||this.serviceId,s||(s=((this.characteristicsMap[this.serviceId]||{}).readIds||[])[0]),s?[3,1]:(console.warn("未找到指定service下的readId,该设备可能不支持read"),[3,3]);case 1:return[4,this._bluetoothApi.readBLECharacteristicValue({deviceId:this._deviceId,characteristicId:s,serviceId:r})];case 2:e.sent(),e.label=3;case 3:return[2]}}))}))},t.prototype.setBLEMTU=function(e){return this._bluetoothApi.setBLEMTU(e)},t.prototype.getBLEDeviceRSSI=function(){return this._bluetoothApi.getBLEDeviceRSSI({deviceId:this._deviceId})},t.deviceFilter=function(e,t){},t.serviceId="",t}(BlueToothBase_1.BlueToothBase);exports.DeviceAdapter=DeviceAdapter;
//# sourceMappingURL=DeviceAdapter.js.map
{
"name": "qcloud-iotexplorer-bluetooth-adapter",
"version": "0.0.14",
"version": "0.0.15",
"description": "",

@@ -17,3 +17,3 @@ "main": "lib/index.js",

"dependencies": {
"event-emitter-for-miniprogram": "0.0.3",
"event-emitter-for-miniprogram": "0.0.4",
"shortid-for-miniprogram": "^2.2.15"

@@ -20,0 +20,0 @@ },

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