node-switchbot
Advanced tools
Comparing version 3.1.2-beta.1 to 3.1.2-beta.2
import type { Ad, Params } from './types/types.js'; | ||
import { EventEmitter } from 'node:events'; | ||
import * as Noble from '@stoprocent/noble'; | ||
import { SwitchbotDevice } from './device.js'; | ||
@@ -10,3 +9,3 @@ /** | ||
ready: Promise<void>; | ||
noble: typeof Noble; | ||
noble: any; | ||
ondiscover?: (device: SwitchbotDevice) => Promise<void> | void; | ||
@@ -49,6 +48,5 @@ onadvertisement?: (ad: Ad) => Promise<void> | void; | ||
/** | ||
* Discover SwitchBot devices based on the provided parameters. | ||
* | ||
* @param {Params} params - The parameters for discovery. | ||
* @returns {Promise<SwitchbotDevice[]>} - A promise that resolves with a list of discovered devices. | ||
* Discovers Switchbot devices. | ||
* @param params The discovery parameters. | ||
* @returns A Promise that resolves with an array of discovered Switchbot devices. | ||
*/ | ||
@@ -55,0 +53,0 @@ discover(params?: Params): Promise<SwitchbotDevice[]>; |
import { EventEmitter } from 'node:events'; | ||
import * as Noble from '@stoprocent/noble'; | ||
import { Advertising } from './advertising.js'; | ||
@@ -57,3 +56,8 @@ import { SwitchbotDevice } from './device.js'; | ||
try { | ||
this.noble = params?.noble ?? Noble.default; | ||
if (params && params.noble) { | ||
this.noble = params.noble; | ||
} | ||
else { | ||
this.noble = (await import('@stoprocent/noble')).default; | ||
} | ||
} | ||
@@ -85,7 +89,7 @@ catch (e) { | ||
await this.ready; | ||
if (this.noble._state === 'poweredOn') { | ||
if (this.noble && this.noble._state === 'poweredOn') { | ||
return; | ||
} | ||
return new Promise((resolve, reject) => { | ||
this.noble.once('stateChange', (state) => { | ||
this.noble?.once('stateChange', (state) => { | ||
switch (state) { | ||
@@ -111,9 +115,8 @@ case 'unsupported': | ||
/** | ||
* Discover SwitchBot devices based on the provided parameters. | ||
* | ||
* @param {Params} params - The parameters for discovery. | ||
* @returns {Promise<SwitchbotDevice[]>} - A promise that resolves with a list of discovered devices. | ||
* Discovers Switchbot devices. | ||
* @param params The discovery parameters. | ||
* @returns A Promise that resolves with an array of discovered Switchbot devices. | ||
*/ | ||
async discover(params = {}) { | ||
await this.ready; | ||
await this.initialize(params); | ||
await this.validate(params, { | ||
@@ -141,10 +144,12 @@ duration: { required: false, type: 'integer', min: 1, max: 60000 }, | ||
} | ||
this.noble.removeAllListeners('discover'); | ||
try { | ||
await this.noble.stopScanningAsync(); | ||
this.log('info', 'Stopped Scanning for SwitchBot BLE devices.'); | ||
if (this.noble) { | ||
this.noble.removeAllListeners('discover'); | ||
try { | ||
await this.noble.stopScanningAsync(); | ||
this.log('info', 'Stopped Scanning for SwitchBot BLE devices.'); | ||
} | ||
catch (e) { | ||
this.log('error', `discover stopScanningAsync error: ${JSON.stringify(e.message ?? e)}`); | ||
} | ||
} | ||
catch (e) { | ||
this.log('error', `discover stopScanningAsync error: ${JSON.stringify(e.message ?? e)}`); | ||
} | ||
return Object.values(peripherals); | ||
@@ -183,3 +188,3 @@ }; | ||
const ad = await Advertising.parse(peripheral, this.log.bind(this)); | ||
if (ad && await this.filterAd(ad, id, model)) { | ||
if (ad && await this.filterAd(ad, id, model) && this.noble) { | ||
switch (ad.serviceData.model) { | ||
@@ -186,0 +191,0 @@ case SwitchBotBLEModel.Bot: return new WoHand(peripheral, this.noble); |
@@ -154,2 +154,6 @@ import type { Buffer } from 'node:buffer'; | ||
} | ||
export interface NobleTypes { | ||
state: 'unknown' | 'resetting' | 'unsupported' | 'unauthorized' | 'poweredOff' | 'poweredOn'; | ||
peripheral: Noble.Peripheral; | ||
} | ||
export interface ServiceData { | ||
@@ -156,0 +160,0 @@ model: string; |
{ | ||
"name": "node-switchbot", | ||
"type": "module", | ||
"version": "3.1.2-beta.1", | ||
"version": "3.1.2-beta.2", | ||
"description": "The node-switchbot is a Node.js module which allows you to control your Switchbot Devices through Bluetooth (BLE).", | ||
@@ -59,3 +59,3 @@ "author": "OpenWonderLabs (https://github.com/OpenWonderLabs)", | ||
"devDependencies": { | ||
"@antfu/eslint-config": "^3.7.3", | ||
"@antfu/eslint-config": "^3.8.0", | ||
"@types/aes-js": "^3.1.4", | ||
@@ -66,3 +66,3 @@ "@types/debug": "^4.1.12", | ||
"@types/mdast": "^4.0.4", | ||
"@types/node": "^22.7.5", | ||
"@types/node": "^22.7.7", | ||
"@types/semver": "^7.5.8", | ||
@@ -72,11 +72,11 @@ "@types/sinon": "^17.0.3", | ||
"@vitest/coverage-v8": "^2.1.3", | ||
"eslint": "^9.12.0", | ||
"eslint": "^9.13.0", | ||
"eslint-plugin-format": "^0.1.2", | ||
"jest": "^29.7.0", | ||
"nodemon": "^3.1.7", | ||
"npm-check-updates": "^17.1.3", | ||
"npm-check-updates": "^17.1.4", | ||
"shx": "^0.3.4", | ||
"sinon": "^19.0.2", | ||
"ts-node": "^10.9.2", | ||
"typedoc": "^0.26.9", | ||
"typedoc": "^0.26.10", | ||
"typescript": "^5.6.3", | ||
@@ -83,0 +83,0 @@ "vitest": "^2.1.3" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3087183
9072