Socket
Socket
Sign inDemoInstall

qcloud-iotexplorer-bluetooth-adapter

Package Overview
Dependencies
Maintainers
2
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.1.1 to 0.1.2

2

lib/core/BlueToothAdapter.js

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BlueToothAdapter=void 0;var tslib_1=require("tslib"),utillib_1=require("../libs/utillib"),BlueToothBase_1=require("./BlueToothBase"),DeviceAdapter_1=require("./DeviceAdapter"),nativeBluetoothApi_1=tslib_1.__importDefault(require("./nativeBluetoothApi")),throttle_1=require("../libs/throttle"),BluetoothDeviceCacheManager_1=require("./BluetoothDeviceCacheManager"),BlueToothAdapter=function(e){function t(t){var i=t.deviceAdapters,o=void 0===i?[]:i,r=t.actions,n=t.bluetoothApi,c=t.h5Websocket,a=t.devMode,s=e.call(this)||this;return s._devMode=!1,s._bluetoothApi={},s._actions={},s._deviceAdapterMap={},s._productIdMap={},s._deviceMap={},s._inited=!1,s._available=!1,s._discovering=!1,s._onBluetoothDeviceFoundHandler=null,s._initPromise=null,s._searchDevicePromise=null,s._devMode=a,s.addAdapter(o),utillib_1.isEmpty(s._deviceAdapterMap)&&console.warn("无合法的deviceAdapter"),s._h5Websocket=c,s._bluetoothApi=n||nativeBluetoothApi_1.default,s._actions=r,s.deviceCacheManager=new BluetoothDeviceCacheManager_1.BluetoothDeviceCacheManager,s}return tslib_1.__extends(t,e),Object.defineProperty(t.prototype,"devMode",{get:function(){return"function"==typeof this._devMode?this._devMode():this._devMode},enumerable:!1,configurable:!0}),t.prototype.addAdapter=function(e){var t=this,i=function(e){Object.prototype.isPrototypeOf.call(DeviceAdapter_1.DeviceAdapter,e)?e.serviceId?t._deviceAdapterMap[e.serviceId]=e:console.error("非法的设备适配器,未配置serviceId",e):console.error("非法的设备适配器",e)};e&&e.splice?e.forEach(i):i(e)},t.prototype._filterDevices=function(e){var t=this,i=e.devices,o=void 0===i?[]:i,r=e.serviceIds,n=e.deviceName,c=void 0===n?"":n,a=e.productId,s=e.ignoreDeviceIds,u=void 0===s?[]:s,l=e.ignoreServiceIds,d=void 0===l?[]:l,h=e.extendInfo,v=void 0===h?{}:h;r&&r.length||(r=this._getSupportServiceIds());var p={};d&&d.length&&(d.forEach((function(e){return p[e]=!0})),r=r.filter((function(e){return!p[e]}))),console.log("support serviceIds",r);for(var _=[],f=r.map((function(e){return t._deviceAdapterMap[e].deviceFilter})),g=function(e,t){if(u.find((function(t){return o[e].deviceId===t})))return{value:void 0};for(var i=void 0,n=0,s=f.length;n<s;n++)if(i=f[n](o[e],{serviceIds:r,deviceName:c,productId:a,ignoreDeviceIds:u,ignoreServiceIds:d,extendInfo:v}),c){if(i&&i.deviceName===c)return{value:[i]}}else if(i){_.push(i);break}},b=0,D=o.length;b<D;b++){var B=g(b);if("object"==typeof B)return B.value}return _},t.prototype._getSupportServiceIds=function(){return Object.keys(this._deviceAdapterMap)},t.prototype.startBluetoothDevicesDiscovery=function(){return this._bluetoothApi.startBluetoothDevicesDiscovery()},t.prototype.stopBluetoothDevicesDiscovery=function(e){return void 0===e&&(e=!1),this._bluetoothApi.stopBluetoothDevicesDiscovery()},t.prototype.cleanup=function(t){var i=this;e.prototype.cleanup.call(this,t),t||(console.log("cleanup bluetooth adapter"),this._discovering&&this.stopBluetoothDevicesDiscovery(),this._bluetoothApi.closeBluetoothAdapter(),console.log("manually disconnect all device",this._deviceMap),Object.keys(this._deviceMap).forEach((function(e){i._deviceMap[e]&&i._deviceMap[e].disconnectDevice()})))},t.prototype.init=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e=this;return tslib_1.__generator(this,(function(t){return[2,this._initPromise||(this._initPromise=new Promise((function(t,i){return tslib_1.__awaiter(e,void 0,void 0,(function(){var e,o,r,n,c,a,s=this;return tslib_1.__generator(this,(function(u){switch(u.label){case 0:if(u.trys.push([0,4,,5]),this._inited){if(this._available)return this._initPromise=null,[2,t()];throw{errCode:10001}}return[4,Promise.all([this.deviceCacheManager.init(),this.initProductIds()])];case 1:return u.sent(),e=function(e){var i=e.available,o=e.discovering;console.log("onBluetoothAdapterStateChange",{available:i,discovering:o}),s._available=i,s._discovering=o,s.emit("adapterStateChange",{available:i,discovering:o}),i?(s._inited=!0,t(),s._initPromise=null):s.cleanup()},o=function(e){return s.onBleConnectionStateChange(e)},r=function(e){return s.onBLECharacteristicValueChange(e)},n=throttle_1.throttle(1e3,(function(e){return s.onBluetoothDeviceFound(e)})),this._bluetoothApi.onBluetoothAdapterStateChange(e),this._bluetoothApi.onBLEConnectionStateChange(o),this._bluetoothApi.onBLECharacteristicValueChange(r),this._bluetoothApi.onBluetoothDeviceFound(n),this.addCleanupTask("init",(function(){s._available=s._discovering=s._inited=!1,s._initPromise=null,s._bluetoothApi.offBluetoothAdapterStateChange(e),s._bluetoothApi.offBLEConnectionStateChange(o),s._bluetoothApi.offBLECharacteristicValueChange(r),s._bluetoothApi.offBluetoothDeviceFound(n),s._bluetoothApi.closeBluetoothAdapter()})),[4,this._bluetoothApi.openBluetoothAdapter()];case 2:return u.sent(),c=e,[4,this._bluetoothApi.getBluetoothAdapterState()];case 3:return c.apply(void 0,[u.sent()]),[3,5];case 4:return a=u.sent(),this._available=!1,this._inited=!1,this._initPromise=null,i(this._normalizeError(a)),[3,5];case 5:return[2]}}))}))})))]}))}))},t.prototype.initProductIds=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"function"!=typeof this._actions.initProductIds?[3,2]:(e=this,[4,this._actions.initProductIds()]);case 1:e._productIdMap=t.sent(),t.label=2;case 2:return[2]}}))}))},t.prototype.onBleConnectionStateChange=function(e){var t=e.deviceId,i=e.connected;console.log("onBLEConnectionStateChange",t,i);var o=this.getDeviceAdapter(t);o?o.onBleConnectionStateChange({connected:i}):console.warn("on bLEConnectionStateChange, but no adapter")},t.prototype.onBLECharacteristicValueChange=function(e){var t=e.deviceId,i=e.serviceId,o=e.characteristicId,r=e.value;console.log("onBLECharacteristicValueChange",t,i,o,r);var n=this._deviceMap[t];if(n)return n.onBLECharacteristicValueChange({serviceId:i,characteristicId:o,value:r});console.warn("on onBLECharacteristicValueChange, but no adapter")},t.prototype.getBluetoothDevices=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._bluetoothApi.getBluetoothDevices()];case 1:return[2,e.sent().devices.filter((function(e){return"未知设备"!==e.name}))]}}))}))},t.prototype.onBluetoothDeviceFound=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.getBluetoothDevices()];case 1:e=t.sent();try{"function"==typeof this._onBluetoothDeviceFoundHandler&&this._onBluetoothDeviceFoundHandler(e)}catch(e){console.error("_onBluetoothDeviceFoundHandler error",e)}return[2,e]}}))}))},t.prototype.startSearch=function(e){var t=e.serviceId,i=e.serviceIds,o=e.ignoreDeviceIds,r=void 0===o?[]:o,n=e.ignoreServiceIds,c=void 0===n?[]:n,a=e.onSearch,s=void 0===a?utillib_1.noop:a,u=e.onError,l=void 0===u?utillib_1.noop:u,d=e.timeout,h=void 0===d?2e4:d,v=e.extendInfo,p=void 0===v?{}:v;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,o,n,a,u=this;return tslib_1.__generator(this,(function(d){switch(d.label){case 0:return[4,this.init()];case 1:d.sent(),t&&!i&&(i=[t]),e=0,o=function(e){u.stopSearch(),l(e)},d.label=2;case 2:return d.trys.push([2,4,,5]),[4,this.startBluetoothDevicesDiscovery()];case 3:return d.sent(),this._onBluetoothDeviceFoundHandler=function(t){try{var n=u._filterDevices({devices:t,serviceIds:i,ignoreDeviceIds:r,ignoreServiceIds:c,extendInfo:p});e=n.length,s(n)}catch(e){console.log("onSearch error",e),o(u._normalizeError(e))}},this.onBluetoothDeviceFound(),n=function(e){e.available||o(u._normalizeError({errCode:10001}))},this.on("adapterStateChange",n),this.addCleanupTask("startSearch",(function(){u._onBluetoothDeviceFoundHandler=null,u.off("adapterStateChange",n)})),setTimeout((function(){e||o("未发现设备,请确认设备已开启")}),h),[3,5];case 4:throw a=d.sent(),this.cleanup("startSearch"),this._normalizeError(a);case 5:return[2]}}))}))},t.prototype.stopSearch=function(){this.cleanup("startSearch"),this.stopBluetoothDevicesDiscovery()},t.prototype.searchDevice=function(e){var t=e.serviceId,i=e.serviceIds,o=e.deviceName,r=e.productId,n=e.ignoreDeviceIds,c=void 0===n?[]:n,a=e.timeout,s=void 0===a?5e3:a,u=e.extendInfo,l=void 0===u?{}:u;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,n=this;return tslib_1.__generator(this,(function(a){switch(a.label){case 0:return[4,this.init()];case 1:return a.sent(),t&&!i&&(i=[t]),console.log("searching for explorerDeviceId => ",o),(e=this.deviceCacheManager.getDeviceCache(r+"/"+o))?(console.log("find ble deviceInfo for "+o,tslib_1.__assign({deviceName:o,productId:r},e)),[2,Promise.resolve(tslib_1.__assign({deviceName:o,productId:r},e))]):[2,this._searchDevicePromise||(this._searchDevicePromise=new Promise((function(e,t){return tslib_1.__awaiter(n,void 0,void 0,(function(){var n,a,u,d=this;return tslib_1.__generator(this,(function(h){switch(h.label){case 0:n=function(e){d.stopBluetoothDevicesDiscovery(),t(d._normalizeError(e)),d._searchDevicePromise=null},a=function(t){d.stopBluetoothDevicesDiscovery(),e(t),d._searchDevicePromise=null},this._onBluetoothDeviceFoundHandler=function(e){try{var t=d._filterDevices({devices:e,serviceIds:i,deviceName:o,productId:r,ignoreDeviceIds:c,extendInfo:l});console.log("matchedDevices: ",t),t.length>0&&(console.log("doFindDevice",t[0]),a(t[0]))}catch(e){n(e)}},h.label=1;case 1:return h.trys.push([1,3,,4]),[4,this.startBluetoothDevicesDiscovery()];case 2:return h.sent(),console.log("startBluetoothDevicesDiscovery succ"),this.onBluetoothDeviceFound(),setTimeout((function(){a(null)}),s),[3,4];case 3:return u=h.sent(),n(u),[3,4];case 4:return[2]}}))}))})))]}}))}))},t.prototype.getDeviceAdapter=function(e){return this._deviceMap[e]},t.prototype.connectDevice=function(e,t){var i=e.deviceId,o=e.serviceId,r=e.mac,n=e.deviceName,c=e.name,a=e.productId,s=(void 0===t?{}:t).autoNotify;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,t,u,l=this;return tslib_1.__generator(this,(function(d){switch(d.label){case 0:return[4,this.init()];case 1:d.sent(),r&&console.warn("[DEPRECATED] mac is deprecated, please use deviceName instead."),n=n||r,a=a||this._productIdMap[o],d.label=2;case 2:if(d.trys.push([2,4,,5]),!(e=this._deviceAdapterMap[o]))throw"无匹配serviceId为"+o+"的 deviceAdapter";return this._deviceMap[i]&&this._deviceMap[i].isConnected?(console.log("find device adapter",this._deviceMap[i]),[2,this._deviceMap[i]]):[4,(t=this._deviceMap[i]=new e({deviceId:i,deviceName:n,productId:a,name:c,actions:this._actions,bluetoothApi:this._bluetoothApi})).connectDevice({autoNotify:s})];case 3:return d.sent(),this.deviceCacheManager.setDeviceCache(n,{deviceId:i,serviceId:o,name:c,productId:a}),console.log("deviceConnected"),t.on("disconnect",(function(){console.log("ondisconnect, cleanup adapter",t),delete l._deviceMap[i]})),console.log("return adapter"),[2,t];case 4:return u=d.sent(),this.deviceCacheManager.removeDeviceCache(n),delete this._deviceMap[i],[2,Promise.reject(u)];case 5:return[2]}}))}))},t}(BlueToothBase_1.BlueToothBase);exports.BlueToothAdapter=BlueToothAdapter;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BlueToothAdapter=void 0;var tslib_1=require("tslib"),utillib_1=require("../libs/utillib"),BlueToothBase_1=require("./BlueToothBase"),DeviceAdapter_1=require("./DeviceAdapter"),nativeBluetoothApi_1=tslib_1.__importDefault(require("./nativeBluetoothApi")),throttle_1=require("../libs/throttle"),BluetoothDeviceCacheManager_1=require("./BluetoothDeviceCacheManager"),BlueToothAdapter=function(e){function t(t){var i=t.deviceAdapters,o=void 0===i?[]:i,r=t.actions,n=t.bluetoothApi,c=t.h5Websocket,a=t.devMode,s=e.call(this)||this;return s._devMode=!1,s._bluetoothApi={},s._actions={},s._deviceAdapterMap={},s._productIdMap={},s._deviceMap={},s._inited=!1,s._available=!1,s._discovering=!1,s._onBluetoothDeviceFoundHandler=null,s._initPromise=null,s._searchDevicePromise=null,s._devMode=a,s.addAdapter(o),utillib_1.isEmpty(s._deviceAdapterMap)&&console.warn("无合法的deviceAdapter"),s._h5Websocket=c,s._bluetoothApi=n||nativeBluetoothApi_1.default,s._actions=r,s.deviceCacheManager=new BluetoothDeviceCacheManager_1.BluetoothDeviceCacheManager,s}return tslib_1.__extends(t,e),Object.defineProperty(t.prototype,"devMode",{get:function(){return"function"==typeof this._devMode?this._devMode():this._devMode},enumerable:!1,configurable:!0}),t.prototype.addAdapter=function(e){var t=this,i=function(e){Object.prototype.isPrototypeOf.call(DeviceAdapter_1.DeviceAdapter,e)?e.serviceId?t._deviceAdapterMap[e.serviceId]=e:console.error("非法的设备适配器,未配置serviceId",e):console.error("非法的设备适配器",e)};e&&e.splice?e.forEach(i):i(e)},t.prototype._filterDevices=function(e){var t=this,i=e.devices,o=void 0===i?[]:i,r=e.serviceIds,n=e.deviceName,c=void 0===n?"":n,a=e.productId,s=e.ignoreDeviceIds,u=void 0===s?[]:s,l=e.ignoreServiceIds,d=void 0===l?[]:l,h=e.extendInfo,v=void 0===h?{}:h;r&&r.length||(r=this._getSupportServiceIds());var p={};d&&d.length&&(d.forEach((function(e){return p[e]=!0})),r=r.filter((function(e){return!p[e]}))),console.log("support serviceIds",r);for(var _=[],f=r.map((function(e){return t._deviceAdapterMap[e].deviceFilter})),g=function(e,t){if(u.find((function(t){return o[e].deviceId===t})))return{value:void 0};for(var i=void 0,n=0,s=f.length;n<s;n++)if(i=f[n](o[e],{serviceIds:r,deviceName:c,productId:a,ignoreDeviceIds:u,ignoreServiceIds:d,extendInfo:v}),c){if(i&&i.deviceName===c)return{value:[i]}}else if(i){_.push(i);break}},b=0,D=o.length;b<D;b++){var B=g(b);if("object"==typeof B)return B.value}return _},t.prototype._getSupportServiceIds=function(){return Object.keys(this._deviceAdapterMap)},t.prototype.startBluetoothDevicesDiscovery=function(){return this._bluetoothApi.startBluetoothDevicesDiscovery()},t.prototype.stopBluetoothDevicesDiscovery=function(e){return void 0===e&&(e=!1),this._bluetoothApi.stopBluetoothDevicesDiscovery()},t.prototype.cleanup=function(t){var i=this;e.prototype.cleanup.call(this,t),t||(console.log("cleanup bluetooth adapter"),this._discovering&&this.stopBluetoothDevicesDiscovery(),this._bluetoothApi.closeBluetoothAdapter(),console.log("manually disconnect all device",this._deviceMap),Object.keys(this._deviceMap).forEach((function(e){i._deviceMap[e]&&i._deviceMap[e].disconnectDevice()})))},t.prototype.init=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e=this;return tslib_1.__generator(this,(function(t){return[2,this._initPromise||(this._initPromise=new Promise((function(t,i){return tslib_1.__awaiter(e,void 0,void 0,(function(){var e,o,r,n,c,a,s=this;return tslib_1.__generator(this,(function(u){switch(u.label){case 0:if(u.trys.push([0,4,,5]),this._inited){if(this._available)return this._initPromise=null,[2,t()];throw{errCode:10001}}return[4,Promise.all([this.deviceCacheManager.init(),this.initProductIds()])];case 1:return u.sent(),e=function(e){var i=e.available,o=e.discovering;console.log("onBluetoothAdapterStateChange",{available:i,discovering:o}),s._available=i,s._discovering=o,s.emit("adapterStateChange",{available:i,discovering:o}),i?(s._inited=!0,t(),s._initPromise=null):s.cleanup()},o=function(e){return s.onBleConnectionStateChange(e)},r=function(e){return s.onBLECharacteristicValueChange(e)},n=throttle_1.throttle(1e3,(function(e){return s.onBluetoothDeviceFound(e)})),this._bluetoothApi.onBluetoothAdapterStateChange(e),this._bluetoothApi.onBLEConnectionStateChange(o),this._bluetoothApi.onBLECharacteristicValueChange(r),this._bluetoothApi.onBluetoothDeviceFound(n),this.addCleanupTask("init",(function(){s._available=s._discovering=s._inited=!1,s._initPromise=null,s._bluetoothApi.offBluetoothAdapterStateChange(e),s._bluetoothApi.offBLEConnectionStateChange(o),s._bluetoothApi.offBLECharacteristicValueChange(r),s._bluetoothApi.offBluetoothDeviceFound(n),s._bluetoothApi.closeBluetoothAdapter()})),[4,this._bluetoothApi.openBluetoothAdapter()];case 2:return u.sent(),c=e,[4,this._bluetoothApi.getBluetoothAdapterState()];case 3:return c.apply(void 0,[u.sent()]),[3,5];case 4:return a=u.sent(),this._available=!1,this._inited=!1,this._initPromise=null,i(this._normalizeError(a)),[3,5];case 5:return[2]}}))}))})))]}))}))},t.prototype.initProductIds=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"function"!=typeof this._actions.initProductIds?[3,2]:(e=this,[4,this._actions.initProductIds()]);case 1:e._productIdMap=t.sent(),t.label=2;case 2:return[2]}}))}))},t.prototype.onBleConnectionStateChange=function(e){var t=e.deviceId,i=e.connected;console.log("onBLEConnectionStateChange",t,i);var o=this.getDeviceAdapter(t);o?o.onBleConnectionStateChange({connected:i}):console.warn("on bLEConnectionStateChange, but no adapter")},t.prototype.onBLECharacteristicValueChange=function(e){var t=e.deviceId,i=e.serviceId,o=e.characteristicId,r=e.value;console.log("onBLECharacteristicValueChange",t,i,o,r);var n=this._deviceMap[t];if(n)return n.onBLECharacteristicValueChange({serviceId:i,characteristicId:o,value:r});console.warn("on onBLECharacteristicValueChange, but no adapter")},t.prototype.getBluetoothDevices=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._bluetoothApi.getBluetoothDevices()];case 1:return[2,e.sent().devices.filter((function(e){return"未知设备"!==e.name}))]}}))}))},t.prototype.onBluetoothDeviceFound=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.getBluetoothDevices()];case 1:e=t.sent();try{"function"==typeof this._onBluetoothDeviceFoundHandler&&this._onBluetoothDeviceFoundHandler(e)}catch(e){console.error("_onBluetoothDeviceFoundHandler error",e)}return[2,e]}}))}))},t.prototype.startSearch=function(e){var t=e.serviceId,i=e.serviceIds,o=e.ignoreDeviceIds,r=void 0===o?[]:o,n=e.ignoreServiceIds,c=void 0===n?[]:n,a=e.onSearch,s=void 0===a?utillib_1.noop:a,u=e.onError,l=void 0===u?utillib_1.noop:u,d=e.timeout,h=void 0===d?2e4:d,v=e.extendInfo,p=void 0===v?{}:v;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,o,n,a,u=this;return tslib_1.__generator(this,(function(d){switch(d.label){case 0:return[4,this.init()];case 1:d.sent(),t&&!i&&(i=[t]),e=0,o=function(e){u.stopSearch(),l(e)},d.label=2;case 2:return d.trys.push([2,4,,5]),[4,this.startBluetoothDevicesDiscovery()];case 3:return d.sent(),this._onBluetoothDeviceFoundHandler=function(t){try{var n=u._filterDevices({devices:t,serviceIds:i,ignoreDeviceIds:r,ignoreServiceIds:c,extendInfo:p});e=n.length,s(n)}catch(e){console.log("onSearch error",e),o(u._normalizeError(e))}},this.onBluetoothDeviceFound(),n=function(e){e.available||o(u._normalizeError({errCode:10001}))},this.on("adapterStateChange",n),this.addCleanupTask("startSearch",(function(){u._onBluetoothDeviceFoundHandler=null,u.off("adapterStateChange",n)})),setTimeout((function(){e||o("未发现设备,请确认设备已开启")}),h),[3,5];case 4:throw a=d.sent(),this.cleanup("startSearch"),this._normalizeError(a);case 5:return[2]}}))}))},t.prototype.stopSearch=function(){this.cleanup("startSearch"),this.stopBluetoothDevicesDiscovery()},t.prototype.searchDevice=function(e){var t=e.serviceId,i=e.serviceIds,o=e.deviceName,r=e.productId,n=e.ignoreDeviceIds,c=void 0===n?[]:n,a=e.timeout,s=void 0===a?5e3:a,u=e.extendInfo,l=void 0===u?{}:u;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,n=this;return tslib_1.__generator(this,(function(a){switch(a.label){case 0:return[4,this.init()];case 1:return a.sent(),t&&!i&&(i=[t]),console.log("searching for explorerDeviceId => ",o),(e=this.deviceCacheManager.getDeviceCache(o))?(console.log("find ble deviceInfo for "+o,tslib_1.__assign({deviceName:o,productId:r},e)),[2,Promise.resolve(tslib_1.__assign({deviceName:o,productId:r},e))]):[2,this._searchDevicePromise||(this._searchDevicePromise=new Promise((function(e,t){return tslib_1.__awaiter(n,void 0,void 0,(function(){var n,a,u,d=this;return tslib_1.__generator(this,(function(h){switch(h.label){case 0:n=function(e){d.stopBluetoothDevicesDiscovery(),t(d._normalizeError(e)),d._searchDevicePromise=null},a=function(t){d.stopBluetoothDevicesDiscovery(),e(t),d._searchDevicePromise=null},this._onBluetoothDeviceFoundHandler=function(e){try{var t=d._filterDevices({devices:e,serviceIds:i,deviceName:o,productId:r,ignoreDeviceIds:c,extendInfo:l});console.log("matchedDevices: ",t),t.length>0&&(console.log("doFindDevice",t[0]),a(t[0]))}catch(e){n(e)}},h.label=1;case 1:return h.trys.push([1,3,,4]),[4,this.startBluetoothDevicesDiscovery()];case 2:return h.sent(),console.log("startBluetoothDevicesDiscovery succ"),this.onBluetoothDeviceFound(),setTimeout((function(){a(null)}),s),[3,4];case 3:return u=h.sent(),n(u),[3,4];case 4:return[2]}}))}))})))]}}))}))},t.prototype.getDeviceAdapter=function(e){return this._deviceMap[e]},t.prototype.connectDevice=function(e,t){var i=e.deviceId,o=e.serviceId,r=e.mac,n=e.deviceName,c=e.name,a=e.productId,s=(void 0===t?{}:t).autoNotify;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,t,u,l=this;return tslib_1.__generator(this,(function(d){switch(d.label){case 0:return[4,this.init()];case 1:d.sent(),r&&console.warn("[DEPRECATED] mac is deprecated, please use deviceName instead."),n=n||r,a=a||this._productIdMap[o],d.label=2;case 2:if(d.trys.push([2,4,,5]),!(e=this._deviceAdapterMap[o]))throw"无匹配serviceId为"+o+"的 deviceAdapter";return this._deviceMap[i]&&this._deviceMap[i].isConnected?(console.log("find device adapter",this._deviceMap[i]),[2,this._deviceMap[i]]):[4,(t=this._deviceMap[i]=new e({deviceId:i,deviceName:n,productId:a,name:c,actions:this._actions,bluetoothApi:this._bluetoothApi})).connectDevice({autoNotify:s})];case 3:return d.sent(),this.deviceCacheManager.setDeviceCache(n,{deviceId:i,serviceId:o,name:c,productId:a}),console.log("deviceConnected"),t.on("disconnect",(function(){console.log("ondisconnect, cleanup adapter",t),delete l._deviceMap[i]})),console.log("return adapter"),[2,t];case 4:return u=d.sent(),this.deviceCacheManager.removeDeviceCache(n),delete this._deviceMap[i],[2,Promise.reject(u)];case 5:return[2]}}))}))},t}(BlueToothBase_1.BlueToothBase);exports.BlueToothAdapter=BlueToothAdapter;
//# sourceMappingURL=BlueToothAdapter.js.map

@@ -14,4 +14,4 @@ export interface BluetoothDeviceCacheInfo {

setDeviceCache(deviceName: any, deviceInfo: BluetoothDeviceCacheInfo): void;
getDeviceCache(explorerDeviceId: any): BluetoothDeviceCacheInfo;
getDeviceCache(deviceName: any): BluetoothDeviceCacheInfo;
removeDeviceCache(deviceName: any): void;
}

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BluetoothDeviceCacheManager=void 0;var tslib_1=require("tslib"),storage_1=require("../libs/storage"),BluetoothDeviceCacheManager=function(){function e(){this._storageKey="__explorer-bluetooth-deviceCacheMap",this.deviceCacheMap={}}return e.prototype.init=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 e=this,[4,storage_1.storage.getItem(this._storageKey)];case 1:return e.deviceCacheMap=t.sent(),[2]}}))}))},e.prototype.setDeviceCache=function(e,t){console.log("cache ble deviceInfo: ",e,t),this.deviceCacheMap[e]=t,storage_1.storage.setItem(this._storageKey,this.deviceCacheMap)},e.prototype.getDeviceCache=function(e){return this.deviceCacheMap[e]},e.prototype.removeDeviceCache=function(e){this.deviceCacheMap[e]&&(this.deviceCacheMap[e]=null,storage_1.storage.setItem(this._storageKey,this.deviceCacheMap))},e}();exports.BluetoothDeviceCacheManager=BluetoothDeviceCacheManager;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BluetoothDeviceCacheManager=void 0;var tslib_1=require("tslib"),storage_1=require("../libs/storage"),BluetoothDeviceCacheManager=function(){function e(){this._storageKey="__explorer-bluetooth-deviceCacheMap",this.deviceCacheMap={}}return e.prototype.init=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 e=this,[4,storage_1.storage.getItem(this._storageKey)];case 1:return e.deviceCacheMap=t.sent()||{},[2]}}))}))},e.prototype.setDeviceCache=function(e,t){console.log("cache ble deviceInfo: ",e,t),this.deviceCacheMap[e]=t,storage_1.storage.setItem(this._storageKey,this.deviceCacheMap)},e.prototype.getDeviceCache=function(e){return this.deviceCacheMap[e]},e.prototype.removeDeviceCache=function(e){this.deviceCacheMap[e]&&(this.deviceCacheMap[e]=null,storage_1.storage.setItem(this._storageKey,this.deviceCacheMap))},e}();exports.BluetoothDeviceCacheManager=BluetoothDeviceCacheManager;
//# sourceMappingURL=BluetoothDeviceCacheManager.js.map
{
"name": "qcloud-iotexplorer-bluetooth-adapter",
"version": "0.1.1",
"version": "0.1.2",
"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