@@ -309,7 +309,24 @@ export default function(RED) {
309309 break ;
310310 }
311311 }
312- return peripheral . state ;
312+ if ( peripheral . state === 'connecting' ) {
313+ return new Promise ( ( resolve ) => {
314+ let retry = 0 ;
315+ let connectedHandler = ( ) => {
316+ ++ retry ;
317+ if ( peripheral . state === 'connected' ) {
318+ return resolve ( peripheral . state ) ;
319+ } else if ( retry < 10 ) {
320+ setTimeout ( connectedHandler , 500 ) ;
321+ } else {
322+ return resolve ( peripheral . state ) ;
323+ }
324+ } ;
325+ setTimeout ( connectedHandler , 500 ) ;
326+ } ) ;
327+ } else {
328+ return Promise . resolve ( peripheral . state ) ;
329+ }
313330 } ,
314331 register : ( node ) => {
315332 this . nodes [ node . id ] = node ;
@@ -326,108 +343,110 @@ export default function(RED) {
326343 if ( ! dataObj ) {
327344 return Promise . resolve ( ) ;
328345 }
329- let state = this . operations . preparePeripheral ( ) ;
330- if ( state !== 'connected' ) {
331- this . log ( `[write] Peripheral:${ this . uuid } is NOT ready. state=>${ state } ` ) ;
332- return Promise . resolve ( ) ;
333- }
334- let writables = this . characteristics . filter ( c => c . writable || c . writeWithoutResponse ) ;
335- if ( TRACE ) {
336- this . log ( `characteristics => ${ JSON . stringify ( this . characteristics . map ( ( c ) => {
337- let obj = Object . assign ( { } , c ) ;
338- delete obj . obj ;
339- return obj ;
340- } ) ) } `) ;
341- this . log ( `writables.length => ${ writables . length } ` ) ;
342- }
343- if ( writables . length === 0 ) {
344- return Promise . resolve ( ) ;
345- }
346- let uuidList = Object . keys ( dataObj ) ;
347- writables = writables . filter ( c => uuidList . indexOf ( c . uuid ) >= 0 ) ;
348- if ( TRACE ) {
349- this . log ( `UUIDs to write => ${ uuidList } ` ) ;
350- this . log ( `writables.length => ${ writables . length } ` ) ;
351- }
352- if ( writables . length === 0 ) {
353- return Promise . resolve ( ) ;
354- }
355- // perform write here right now
356- return Promise . all ( writables . map ( w => {
357- // {uuid:'characteristic-uuid-to-write', data:Buffer()}
358- return new Promise ( ( resolve , reject ) => {
359- let buf = valToBuffer ( dataObj [ w . uuid ] ) ;
360- if ( TRACE ) {
361- this . log ( `<Write> uuid => ${ w . uuid } , data => ${ buf } , writeWithoutResponse => ${ w . writeWithoutResponse } ` ) ;
362- }
363- w . object . write (
364- buf ,
365- w . writeWithoutResponse ,
366- ( err ) => {
367- if ( err ) {
346+ return this . operations . preparePeripheral ( ) . then ( ( state ) => {
347+ if ( state !== 'connected' ) {
348+ this . log ( `[write] Peripheral:${ this . uuid } is NOT ready. state=>${ state } ` ) ;
349+ return Promise . resolve ( ) ;
350+ }
351+ let writables = this . characteristics . filter ( c => c . writable || c . writeWithoutResponse ) ;
352+ if ( TRACE ) {
353+ this . log ( `characteristics => ${ JSON . stringify ( this . characteristics . map ( ( c ) => {
354+ let obj = Object . assign ( { } , c ) ;
355+ delete obj . obj ;
356+ return obj ;
357+ } ) ) } `) ;
358+ this . log ( `writables.length => ${ writables . length } ` ) ;
359+ }
360+ if ( writables . length === 0 ) {
361+ return Promise . resolve ( ) ;
362+ }
363+ let uuidList = Object . keys ( dataObj ) ;
364+ writables = writables . filter ( c => uuidList . indexOf ( c . uuid ) >= 0 ) ;
365+ if ( TRACE ) {
366+ this . log ( `UUIDs to write => ${ uuidList } ` ) ;
367+ this . log ( `writables.length => ${ writables . length } ` ) ;
368+ }
369+ if ( writables . length === 0 ) {
370+ return Promise . resolve ( ) ;
371+ }
372+ // perform write here right now
373+ return Promise . all ( writables . map ( w => {
374+ // {uuid:'characteristic-uuid-to-write', data:Buffer()}
375+ return new Promise ( ( resolve , reject ) => {
376+ let buf = valToBuffer ( dataObj [ w . uuid ] ) ;
377+ if ( TRACE ) {
378+ this . log ( `<Write> uuid => ${ w . uuid } , data => ${ buf } , writeWithoutResponse => ${ w . writeWithoutResponse } ` ) ;
379+ }
380+ w . object . write (
381+ buf ,
382+ w . writeWithoutResponse ,
383+ ( err ) => {
384+ if ( err ) {
385+ if ( TRACE ) {
386+ this . log ( `<Write> ${ w . uuid } => FAIL` ) ;
387+ }
388+ return reject ( err ) ;
389+ }
368390 if ( TRACE ) {
369- this . log ( `<Write> ${ w . uuid } => FAIL ` ) ;
391+ this . log ( `<Write> ${ w . uuid } => OK ` ) ;
370392 }
371- return reject ( err ) ;
372- }
373- if ( TRACE ) {
374- this . log ( `<Write> ${ w . uuid } => OK` ) ;
393+ resolve ( true ) ;
375394 }
376- resolve ( true ) ;
377- }
378- ) ;
379- } ) ;
380- } ) ) ;
395+ ) ;
396+ } ) ;
397+ } ) ) ;
398+ } ) ;
381399 } ,
382400 read : ( uuids = '' ) => {
383- let state = this . operations . preparePeripheral ( ) ;
384- if ( state !== 'connected' ) {
385- this . log ( `[read] Peripheral:${ this . uuid } is NOT ready. state=>${ state } ` ) ;
386- return Promise . resolve ( ) ;
387- }
388- uuids = uuids . split ( ',' ) . map ( ( uuid ) => uuid . trim ( ) ) . filter ( ( uuid ) => uuid ) ;
389- let readables = this . characteristics . filter ( c => {
390- if ( c . readable ) {
391- if ( uuids . length === 0 ) {
392- return true ;
401+ return this . operations . preparePeripheral ( ) . then ( ( state ) => {
402+ if ( state !== 'connected' ) {
403+ this . log ( `[read] Peripheral:${ this . uuid } is NOT ready. state=>${ state } ` ) ;
404+ return Promise . resolve ( ) ;
405+ }
406+ uuids = uuids . split ( ',' ) . map ( ( uuid ) => uuid . trim ( ) ) . filter ( ( uuid ) => uuid ) ;
407+ let readables = this . characteristics . filter ( c => {
408+ if ( c . readable ) {
409+ if ( uuids . length === 0 ) {
410+ return true ;
411+ }
412+ return uuids . indexOf ( c . uuid ) >= 0 ;
393413 }
394- return uuids . indexOf ( c . uuid ) >= 0 ;
414+ } ) ;
415+ if ( TRACE ) {
416+ this . log ( `characteristics => ${ JSON . stringify ( this . characteristics . map ( ( c ) => {
417+ let obj = Object . assign ( { } , c ) ;
418+ delete obj . obj ;
419+ return obj ;
420+ } ) ) } `) ;
421+ this . log ( `readables.length => ${ readables . length } ` ) ;
395422 }
396- } ) ;
397- if ( TRACE ) {
398- this . log ( `characteristics => ${ JSON . stringify ( this . characteristics . map ( ( c ) => {
399- let obj = Object . assign ( { } , c ) ;
400- delete obj . obj ;
401- return obj ;
402- } ) ) } `) ;
403- this . log ( `readables.length => ${ readables . length } ` ) ;
404- }
405- if ( readables . length === 0 ) {
406- return Promise . resolve ( ) ;
407- }
408- // perform read here right now
409- let readObj = { } ;
410- return Promise . all ( readables . map ( ( r ) => {
411- // {uuid:'characteristic-uuid-to-read'}
412- return new Promise ( ( resolve , reject ) => {
413- r . object . read (
414- ( err , data ) => {
415- if ( err ) {
423+ if ( readables . length === 0 ) {
424+ return Promise . resolve ( ) ;
425+ }
426+ // perform read here right now
427+ let readObj = { } ;
428+ return Promise . all ( readables . map ( ( r ) => {
429+ // {uuid:'characteristic-uuid-to-read'}
430+ return new Promise ( ( resolve , reject ) => {
431+ r . object . read (
432+ ( err , data ) => {
433+ if ( err ) {
434+ if ( TRACE ) {
435+ this . log ( `<Read> ${ r . uuid } => FAIL` ) ;
436+ }
437+ return reject ( err ) ;
438+ }
416439 if ( TRACE ) {
417- this . log ( `<Read> ${ r . uuid } => FAIL ` ) ;
440+ this . log ( `<Read> ${ r . uuid } => ${ JSON . stringify ( data ) } ` ) ;
418441 }
419- return reject ( err ) ;
442+ readObj [ r . uuid ] = data ;
443+ resolve ( ) ;
420444 }
421- if ( TRACE ) {
422- this . log ( `<Read> ${ r . uuid } => ${ JSON . stringify ( data ) } ` ) ;
423- }
424- readObj [ r . uuid ] = data ;
425- resolve ( ) ;
426- }
427- ) ;
445+ ) ;
446+ } ) ;
447+ } ) ) . then ( ( ) => {
448+ return readObj ;
428449 } ) ;
429- } ) ) . then ( ( ) => {
430- return readObj ;
431450 } ) ;
432451 } ,
433452 subscribe : ( uuids = '' , period = 3000 ) => {
0 commit comments