@@ -80,10 +80,10 @@ function getAddressOrUUID(peripheral) {
8080 return peripheral . address ;
8181}
8282
83- function deleteBleDevice ( addressOrUUID ) {
83+ function deleteBleDevice ( addressOrUUID , RED ) {
8484 let value = bleDevices . del ( addressOrUUID ) ;
8585 if ( value && TRACE ) {
86- console . log ( `[GenericBLE:TRACE] Delete => ${ addressOrUUID } ` ) ;
86+ RED . log . info ( `[GenericBLE:TRACE] Delete => ${ addressOrUUID } ` ) ;
8787 }
8888}
8989
@@ -350,11 +350,11 @@ function characteristicsTask(services, bleDevice) {
350350 } ) ;
351351}
352352
353- function disconnectPeripheral ( peripheral , done ) {
353+ function disconnectPeripheral ( peripheral , done , RED ) {
354354 if ( peripheral . state === 'disconnected' || peripheral . state === 'disconnecting' ) {
355355 delete peripheral . _lock ;
356356 if ( TRACE ) {
357- console . log ( `<disconnectPeripheral> <${ peripheral . uuid } > Skipped to disconnect` ) ;
357+ RED . log . info ( `<disconnectPeripheral> <${ peripheral . uuid } > Skipped to disconnect` ) ;
358358 }
359359 if ( done ) {
360360 return done ( ) ;
@@ -364,7 +364,7 @@ function disconnectPeripheral(peripheral, done) {
364364 if ( peripheral . _skipDisconnect ) {
365365 delete peripheral . _skipDisconnect ;
366366 if ( TRACE ) {
367- console . log ( `<disconnectPeripheral> <${ peripheral . uuid } > Skipped to disconnect <LOCKED>` ) ;
367+ RED . log . info ( `<disconnectPeripheral> <${ peripheral . uuid } > Skipped to disconnect <LOCKED>` ) ;
368368 }
369369 if ( done ) {
370370 return done ( ) ;
@@ -375,7 +375,7 @@ function disconnectPeripheral(peripheral, done) {
375375 let timeout ;
376376 let onDisconnected = ( ) => {
377377 if ( TRACE ) {
378- console . log ( `<disconnectPeripheral> <${ peripheral . uuid } > DISCONNECTED` ) ;
378+ RED . log . info ( `<disconnectPeripheral> <${ peripheral . uuid } > DISCONNECTED` ) ;
379379 }
380380 if ( timeout ) {
381381 deleteTimeout ( timeout ) ;
@@ -390,7 +390,7 @@ function disconnectPeripheral(peripheral, done) {
390390 } ;
391391 timeout = addTimeout ( ( ) => {
392392 if ( TRACE ) {
393- console . log ( `<disconnectPeripheral> <${ peripheral . uuid } > DISCONNECT TIMEOUT` ) ;
393+ RED . log . info ( `<disconnectPeripheral> <${ peripheral . uuid } > DISCONNECT TIMEOUT` ) ;
394394 }
395395 deleteTimeout ( timeout ) ;
396396 timeout = null ;
@@ -407,20 +407,22 @@ function disconnectPeripheral(peripheral, done) {
407407 delete peripheral . _lock ;
408408}
409409
410- function connectToPeripheral ( peripheral ) {
410+ function connectToPeripheral ( peripheral , RED , forceConnect = false ) {
411411 if ( peripheral . _lock ) {
412412 if ( TRACE ) {
413- console . log ( `<connectToPeripheral> <${ peripheral . uuid } > Gave up to connect` ) ;
413+ RED . log . info ( `<connectToPeripheral> <${ peripheral . uuid } > Gave up to connect` ) ;
414414 }
415415 peripheral . _skipDisconnect = true ;
416416 return Promise . reject ( `<${ peripheral . uuid } > Try again` ) ;
417417 }
418418 let bleDevice = configBleDevices [ getAddressOrUUID ( peripheral ) ] ;
419- if ( ! hasPendingOperations ( bleDevice ) ) {
420- if ( TRACE ) {
421- console . log ( `<connectToPeripheral> <${ peripheral . uuid } > Skip to connect as there's nothing to do` ) ;
419+ if ( ! forceConnect ) {
420+ if ( ! hasPendingOperations ( bleDevice ) ) {
421+ if ( TRACE ) {
422+ RED . log . info ( `<connectToPeripheral> <${ peripheral . uuid } > Skip to connect as there's nothing to do` ) ;
423+ }
424+ return Promise . resolve ( ) ;
422425 }
423- return Promise . resolve ( ) ;
424426 }
425427 return new Promise ( ( resolve , reject ) => {
426428 let timeout ;
@@ -439,14 +441,14 @@ function connectToPeripheral(peripheral) {
439441 deleteTimeout ( timeout ) ;
440442 timeout = null ;
441443 if ( TRACE ) {
442- console . log ( `<connectToPeripheral> <${ peripheral . uuid } > discovering all services and characteristics...` ) ;
444+ RED . log . info ( `<connectToPeripheral> <${ peripheral . uuid } > discovering all services and characteristics...` ) ;
443445 }
444446 if ( onDiscover ) {
445447 onDiscover ( peripheral ) ;
446448 }
447449 if ( peripheral . services ) {
448450 if ( TRACE ) {
449- console . log ( `<connectToPeripheral> <${ peripheral . uuid } > discovered 00` ) ;
451+ RED . log . info ( `<connectToPeripheral> <${ peripheral . uuid } > discovered 00` ) ;
450452 }
451453 return resolve ( [ peripheral . services , bleDevice ] ) ;
452454 }
@@ -455,7 +457,7 @@ function connectToPeripheral(peripheral) {
455457 deleteTimeout ( discoveryTimeout ) ;
456458 if ( peripheral . services ) {
457459 if ( TRACE ) {
458- console . log ( `<connectToPeripheral> <${ peripheral . uuid } > discovered 00` ) ;
460+ RED . log . info ( `<connectToPeripheral> <${ peripheral . uuid } > discovered 00` ) ;
459461 }
460462 return resolve ( [ peripheral . services , bleDevice ] ) ;
461463 }
@@ -464,11 +466,11 @@ function connectToPeripheral(peripheral) {
464466 return ;
465467 }
466468 if ( TRACE ) {
467- console . log ( `<connectToPeripheral> <${ peripheral . uuid } > Setting up discoveryTimeout` ) ;
469+ RED . log . info ( `<connectToPeripheral> <${ peripheral . uuid } > Setting up discoveryTimeout` ) ;
468470 }
469471 let discoveryTimeout = addTimeout ( ( ) => {
470472 if ( TRACE ) {
471- console . log ( `<connectToPeripheral> <${ peripheral . uuid } > discoveryTimeout fired` ) ;
473+ RED . log . info ( `<connectToPeripheral> <${ peripheral . uuid } > discoveryTimeout fired` ) ;
472474 }
473475 if ( bleDevice ) {
474476 bleDevice . emit ( 'timeout' ) ;
@@ -485,18 +487,18 @@ function connectToPeripheral(peripheral) {
485487 ( err , services ) => {
486488 peripheral . _discovering = false ;
487489 if ( TRACE ) {
488- console . log ( `<connectToPeripheral> <${ peripheral . uuid } > discoverAllServicesAndCharacteristics OK` ) ;
490+ RED . log . info ( `<connectToPeripheral> <${ peripheral . uuid } > discoverAllServicesAndCharacteristics OK` ) ;
489491 }
490492 deleteTimeout ( discoveryTimeout ) ;
491493 discoveryTimeout = null ;
492494 if ( err ) {
493495 if ( TRACE ) {
494- console . log ( `<connectToPeripheral> <${ peripheral . uuid } > err` , err ) ;
496+ RED . log . info ( `<connectToPeripheral> <${ peripheral . uuid } > err` , err ) ;
495497 }
496498 return reject ( `<${ peripheral . uuid } > ${ err } \n=>${ err . stack } ` ) ;
497499 }
498500 if ( TRACE ) {
499- console . log ( `<connectToPeripheral> <${ peripheral . uuid } > discovered 01` ) ;
501+ RED . log . info ( `<connectToPeripheral> <${ peripheral . uuid } > discovered 01` ) ;
500502 }
501503 return resolve ( [ services , bleDevice ] ) ;
502504 } ) ;
@@ -511,11 +513,11 @@ function connectToPeripheral(peripheral) {
511513 onConnected = null ;
512514 reject ( `<${ peripheral . uuid } > Connection Timeout` ) ;
513515 if ( TRACE ) {
514- console . log ( `<connectToPeripheral> <${ peripheral . uuid } > Connection Timeout` ) ;
516+ RED . log . info ( `<connectToPeripheral> <${ peripheral . uuid } > Connection Timeout` ) ;
515517 }
516518 } , BLE_CONNECTION_TIMEOUT_MS ) ;
517519 if ( TRACE ) {
518- console . log ( `<connectToPeripheral> <${ peripheral . uuid } > peripheral.state=>${ peripheral . state } ` ) ;
520+ RED . log . info ( `<connectToPeripheral> <${ peripheral . uuid } > peripheral.state=>${ peripheral . state } ` ) ;
519521 }
520522 if ( peripheral . state === 'connected' ) {
521523 return onConnected ( ) ;
@@ -526,7 +528,7 @@ function connectToPeripheral(peripheral) {
526528 } ) ;
527529}
528530
529- function peripheralTask ( uuid , task , done , RED ) {
531+ function peripheralTask ( uuid , task , done , RED , forceConnect = false ) {
530532 return ( next ) => {
531533 if ( TRACE ) {
532534 RED . log . info ( `<peripheralTask> <${ uuid } > START` ) ;
@@ -560,10 +562,10 @@ function peripheralTask(uuid, task, done, RED) {
560562 done ( err ) ;
561563 }
562564 next ( err ) ;
563- } ) ;
565+ } , RED ) ;
564566 }
565567
566- connectToPeripheral ( peripheral ) . then ( ( result ) => {
568+ connectToPeripheral ( peripheral , RED , forceConnect ) . then ( ( result ) => {
567569 if ( ! result ) {
568570 return new Promise ( ( resolve ) => {
569571 setTimeout ( ( ) => {
@@ -580,11 +582,11 @@ function peripheralTask(uuid, task, done, RED) {
580582 } ;
581583}
582584
583- function schedulePeripheralTask ( uuid , task , done , RED ) {
585+ function schedulePeripheralTask ( uuid , task , done , RED , forceConnect = false ) {
584586 if ( ! task ) {
585587 return ;
586588 }
587- q . push ( peripheralTask ( uuid , task , done , RED ) ) ;
589+ q . push ( peripheralTask ( uuid , task , done , RED , forceConnect ) ) ;
588590}
589591
590592function addErrorListenerToQueue ( RED ) {
@@ -627,7 +629,7 @@ function onDiscoverFunc(RED) {
627629 schedulePeripheralTask ( peripheral . uuid , characteristicsTask , null , RED ) ;
628630 }
629631 } else {
630- deleteBleDevice ( addressOrUUID ) ;
632+ deleteBleDevice ( addressOrUUID , RED ) ;
631633 }
632634 } ;
633635}
@@ -1051,6 +1053,6 @@ export default function(RED) {
10511053 return res . status ( 500 ) . send ( { status : 500 , message : ( err . message || err ) } ) . end ( ) ;
10521054 }
10531055 }
1054- } , RED ) ;
1056+ } , RED , true ) ;
10551057 } ) ;
10561058}
0 commit comments