@@ -35,6 +35,7 @@ import {
3535 interval
3636} from 'rxjs' ;
3737import { parseMessage , initSocket , initPluginUrl } from './readMessages' ;
38+ import { debug } from 'util' ;
3839// Required agent version
3940const MIN_VERSION = '1.1.71' ;
4041
@@ -50,8 +51,8 @@ const LOOKUP_PORT_END = 9000;
5051
5152const CANT_FIND_AGENT_MESSAGE = 'Arduino Create Agent cannot be found' ;
5253
53- export const AGENT_STATUS_CONNECTED = 'AGENT_CONNECTED ' ;
54- export const AGENT_STATUS_DISCONNECTED = 'AGENT_DISCONNECTED ' ;
54+ export const AGENT_STATUS_FOUND = 'AGENT_FOUND ' ;
55+ export const AGENT_STATUS_NOT_FOUND = 'AGENT_NOT_FOUND ' ;
5556export const WS_STATUS_CONNECTED = 'WS_CONNECTED' ;
5657export const WS_STATUS_DISCONNECTED = 'WS_DISCONNECTED' ;
5758
@@ -61,7 +62,7 @@ export default class SocketDaemon {
6162 this . agentInfo = { } ;
6263 this . found = false ;
6364
64- this . agentConnectionStatus = new BehaviorSubject ( AGENT_STATUS_DISCONNECTED ) ;
65+ this . agentDiscoveryStatus = new BehaviorSubject ( AGENT_STATUS_NOT_FOUND ) ;
6566 this . wsConnectionStatus = new BehaviorSubject ( WS_STATUS_DISCONNECTED ) ;
6667 this . wsError = new Subject ( ) ;
6768 }
@@ -71,19 +72,26 @@ export default class SocketDaemon {
7172 * First search in http://LOOPBACK_ADDRESS, after in https://LOOPBACK_HOSTNAME.
7273 * @return {object } The found agent info values.
7374 */
74- connect ( ) {
75- if ( this . found ) {
76- return fetch ( this . agentInfo [ this . selectedProtocol ] )
77- . then ( response => response . json ( ) )
78- . catch ( ( ) => {
79- this . found = false ;
80- return Promise . reject ( new Error ( CANT_FIND_AGENT_MESSAGE ) ) ;
75+ findAgent ( ) {
76+ const find = ( ) => {
77+ return this . tryAllPorts ( )
78+ . catch ( err => {
79+ this . agentDiscoveryStatus . next ( AGENT_STATUS_NOT_FOUND ) ;
80+ return err ;
81+ } )
82+ . finally ( ( ) => {
83+ if ( ! this . isConnected ( ) ) {
84+ setTimeout ( find , 3000 ) ;
85+ }
8186 } ) ;
82- }
87+ } ;
88+ return find ( ) ;
89+ }
8390
91+ tryAllPorts ( ) {
8492 return this . tryPorts ( LOOPBACK_ADDRESS )
8593 . catch ( ( ) => this . tryPorts ( LOOPBACK_HOSTNAME )
86- . catch ( ( ) => Promise . reject ( new Error ( CANT_FIND_AGENT_MESSAGE ) ) ) ) ;
94+ . catch ( err => Promise . reject ( err ) ) ) ;
8795 }
8896
8997 /**
@@ -92,6 +100,7 @@ export default class SocketDaemon {
92100 * @return {object } info - The agent info values.
93101 */
94102 tryPorts ( hostname ) {
103+ console . log ( 'tryPorts\n' ) ;
95104 const pluginLookups = [ ] ;
96105
97106 for ( let port = LOOKUP_PORT_START ; port < LOOKUP_PORT_END ; port += 1 ) {
@@ -102,27 +111,27 @@ export default class SocketDaemon {
102111 // So we have to resolve them with a false value to let the Promise.all catch all the deferred data
103112 }
104113
105- return Promise . all ( pluginLookups ) . then ( responses => {
106- this . found = responses . some ( r => {
107- if ( r && r . response && r . response . status === 200 ) {
108- this . agentInfo = r . data ;
109- this . agentConnectionStatus . next ( AGENT_STATUS_CONNECTED ) ;
110- this . wsConnect ( ) ;
111- if ( r . response . url . indexOf ( PROTOCOL . HTTPS ) === 0 ) {
112- this . selectedProtocol = PROTOCOL . HTTPS ;
114+ return Promise . all ( pluginLookups )
115+ . then ( responses => {
116+ this . found = responses . some ( r => {
117+ if ( r && r . response && r . response . status === 200 ) {
118+ this . agentInfo = r . data ;
119+ this . agentDiscoveryStatus . next ( AGENT_STATUS_FOUND ) ;
120+ this . wsConnect ( ) ;
121+ if ( r . response . url . indexOf ( PROTOCOL . HTTPS ) === 0 ) {
122+ this . selectedProtocol = PROTOCOL . HTTPS ;
123+ }
124+ initPluginUrl ( this . agentInfo [ this . selectedProtocol ] ) ;
125+ return true ;
113126 }
114- initPluginUrl ( this . agentInfo [ this . selectedProtocol ] ) ;
115- return true ;
127+ return false ;
128+ } ) ;
129+
130+ if ( this . found ) {
131+ return this . update ( ) ;
116132 }
117- return false ;
133+ return Promise . reject ( new Error ( ` ${ CANT_FIND_AGENT_MESSAGE } at ${ hostname } ` ) ) ;
118134 } ) ;
119-
120- if ( this . found ) {
121- return this . update ( )
122- . then ( ( ) => this . agentInfo ) ;
123- }
124- return Promise . reject ( new Error ( `${ CANT_FIND_AGENT_MESSAGE } at ${ hostname } ` ) ) ;
125- } ) ;
126135 }
127136
128137 /**
@@ -163,7 +172,7 @@ export default class SocketDaemon {
163172 this . portsPollingSubscription . unsubscribe ( ) ;
164173 }
165174 this . wsConnectionStatus . next ( WS_STATUS_DISCONNECTED ) ;
166- this . wsConnect ( ) ;
175+ this . findAgent ( ) ;
167176 } ) ;
168177
169178 // Parse messages
@@ -176,7 +185,7 @@ export default class SocketDaemon {
176185 update ( ) {
177186 return new Promise ( ( resolve , reject ) => {
178187 if ( this . agentInfo . version && ( semVerCompare ( this . agentInfo . version , MIN_VERSION ) >= 0 || this . agentInfo . version . indexOf ( 'dev' ) !== - 1 ) ) {
179- resolve ( this . agentInfo ) ;
188+ return resolve ( this . agentInfo ) ;
180189 }
181190
182191 return fetch ( `${ this . agentInfo [ this . selectedProtocol ] } /update` , {
0 commit comments