66* Date: 2019-11-18
77* ---------------------------------------------------------------- */
88'use strict' ;
9- const parameterChecker = require ( './parameter-checker.js' ) ;
10- const switchbotAdvertising = require ( './switchbot-advertising.js' ) ;
9+ import { check , error as _error } from './parameter-checker.js' ;
10+ import { parse } from './switchbot-advertising.js' ;
1111
12- const SwitchbotDevice = require ( './switchbot-device.js' ) ;
13- const SwitchbotDeviceWoHand = require ( './switchbot-device-wohand.js' ) ;
14- const SwitchbotDeviceWoSensorTH = require ( './switchbot-device-wosensorth.js' ) ;
15- const SwitchbotDeviceWoCurtain = require ( './switchbot-device-wocurtain.js' ) ;
12+ import SwitchbotDevice from './switchbot-device.js' ;
13+ import SwitchbotDeviceWoHand from './switchbot-device-wohand.js' ;
14+ import SwitchbotDeviceWoSensorTH from './switchbot-device-wosensorth.js' ;
15+ import SwitchbotDeviceWoCurtain from './switchbot-device-wocurtain.js' ;
1616
1717class Switchbot {
1818 /* ------------------------------------------------------------------
@@ -79,15 +79,15 @@ class Switchbot {
7979 discover ( params = { } ) {
8080 let promise = new Promise ( ( resolve , reject ) => {
8181 // Check the parameters
82- let valid = parameterChecker . check ( params , {
82+ let valid = check ( params , {
8383 duration : { required : false , type : 'integer' , min : 1 , max : 60000 } ,
8484 model : { required : false , type : 'string' , enum : [ 'H' , 'T' , 'c' ] } ,
8585 id : { required : false , type : 'string' , min : 12 , max : 17 } ,
8686 quick : { required : false , type : 'boolean' }
8787 } , false ) ;
8888
8989 if ( ! valid ) {
90- reject ( new Error ( parameterChecker . error . message ) ) ;
90+ reject ( new Error ( _error . message ) ) ;
9191 return ;
9292 }
9393
@@ -158,30 +158,30 @@ class Switchbot {
158158
159159 _init ( ) {
160160 let promise = new Promise ( ( resolve , reject ) => {
161- if ( this . _initialized === true ) {
162- resolve ( ) ;
163- return ;
164- }
165- if ( this . noble . state === 'poweredOn' ) {
166- this . _initialized = true ;
167- resolve ( ) ;
168- } else {
169- this . noble . once ( 'stateChange ', ( state ) => {
170- if ( state === 'poweredOn' ) {
171- this . _initialized = true ;
172- resolve ( ) ;
173- } else {
174- let err = new Error ( 'Failed to initialize the Noble object: ' + state ) ;
175- reject ( err ) ;
176- }
177- } ) ;
161+ switch ( this . noble . state ) {
162+ case 'poweredOn' :
163+ resolve ( ) ;
164+ return ;
165+ case 'unsupported' , 'unauthorized' , 'poweredOff' :
166+ let err = new Error ( 'Failed to initialize the Noble object: ' + this . noble . state ) ;
167+ reject ( err ) ;
168+ return ;
169+ default : // 'resetting ', 'unknown'
170+ this . noble . once ( 'stateChange' , ( state ) => {
171+ if ( state === 'poweredOn' ) {
172+ resolve ( ) ;
173+ } else {
174+ let err = new Error ( 'Failed to initialize the Noble object: ' + state ) ;
175+ reject ( err ) ;
176+ }
177+ } ) ;
178178 }
179179 } ) ;
180180 return promise ;
181181 }
182182
183183 _getDeviceObject ( peripheral , id , model ) {
184- let ad = switchbotAdvertising . parse ( peripheral ) ;
184+ let ad = parse ( peripheral ) ;
185185 if ( this . _filterAdvertising ( ad , id , model ) ) {
186186 let device = null ;
187187 if ( ad . serviceData . model === 'H' ) {
@@ -248,13 +248,13 @@ class Switchbot {
248248 startScan ( params ) {
249249 let promise = new Promise ( ( resolve , reject ) => {
250250 // Check the parameters
251- let valid = parameterChecker . check ( params , {
251+ let valid = check ( params , {
252252 model : { required : false , type : 'string' , enum : [ 'H' , 'T' , 'c' ] } ,
253253 id : { required : false , type : 'string' , min : 12 , max : 17 } ,
254254 } , false ) ;
255255
256256 if ( ! valid ) {
257- reject ( new Error ( parameterChecker . error . message ) ) ;
257+ reject ( new Error ( _error . message ) ) ;
258258 return ;
259259 }
260260
@@ -273,7 +273,7 @@ class Switchbot {
273273
274274 // Set an handler for the 'discover' event
275275 this . noble . on ( 'discover' , ( peripheral ) => {
276- let ad = switchbotAdvertising . parse ( peripheral ) ;
276+ let ad = parse ( peripheral ) ;
277277 if ( this . _filterAdvertising ( ad , p . id , p . model ) ) {
278278 if ( this . onadvertisement && typeof ( this . onadvertisement ) === 'function' ) {
279279 this . onadvertisement ( ad ) ;
@@ -325,12 +325,12 @@ class Switchbot {
325325 wait ( msec ) {
326326 return new Promise ( ( resolve , reject ) => {
327327 // Check the parameters
328- let valid = parameterChecker . check ( { msec : msec } , {
328+ let valid = check ( { msec : msec } , {
329329 msec : { required : true , type : 'integer' , min : 0 }
330330 } ) ;
331331
332332 if ( ! valid ) {
333- reject ( new Error ( parameterChecker . error . message ) ) ;
333+ reject ( new Error ( _error . message ) ) ;
334334 return ;
335335 }
336336 // Set a timer
@@ -340,4 +340,4 @@ class Switchbot {
340340
341341}
342342
343- module . exports = Switchbot ;
343+ export default Switchbot ;
0 commit comments