@@ -96,10 +96,12 @@ noble.on('stateChange', (state) => {
9696 setTimeout ( ( ) => {
9797 log ( 'stopping scan after timeout' ) ;
9898 noble . stopScanning ( ) ;
99- if ( Object . entries ( devices ) . filter ( ( [ , device ] ) => device . connectionState === 'connected' ) . length === 0 ) {
99+ if ( Object . keys ( devices ) . length === 0 ) {
100100 log ( 'No devices found, exiting' ) ;
101101 process . exit ( - 2 ) ;
102102 }
103+
104+ Object . values ( devices ) . forEach ( ( device ) => { device . connect ( ) ; } ) ;
103105 } , 1000 * argv . discoveryTimeout ) ;
104106 }
105107
@@ -171,17 +173,16 @@ noble.on('discover', peripheral => {
171173 devices [ id ] = new SomaShade ( id , peripheral , noble ) ;
172174 if ( argv . debug ) { devices [ id ] . log . enabled = true ; }
173175
174- peripheral . on ( 'connect' , ( ) => {
175- log ( 'connected to %s' , id ) ;
176- if ( argv . expectedDevices &&
177- Object . entries ( devices ) . filter ( ( [ , device ] ) => device . connectionState === 'connected' ) . length === argv . expectedDevices ) {
178- if ( ! loggedStop ) {
179- log ( 'all expected devices connected, stopping scan' ) ;
180- loggedStop = true ;
181- }
182- noble . stopScanning ( ) ;
176+ log ( 'discovered %s' , id ) ;
177+ if ( argv . expectedDevices &&
178+ Object . keys ( devices ) . length === argv . expectedDevices ) {
179+ if ( ! loggedStop ) {
180+ log ( 'all expected devices connected, stopping scan' ) ;
181+ loggedStop = true ;
183182 }
184- } ) ;
183+ noble . stopScanning ( ) ;
184+ Object . values ( devices ) . forEach ( ( device ) => { device . connect ( ) ; } ) ;
185+ }
185186
186187 if ( mqttUrl ) {
187188 new mqttBinding ( devices [ id ] , mqttUrl , baseTopic , mqttUsername , mqttPassword ) ;
0 commit comments