@@ -66,7 +66,7 @@ export class NaoRobotModel extends DOMWidgetModel {
6666 }
6767 }
6868
69- async connect ( ipAddress : string , port : string ) {
69+ async connect ( ipAddress : string , port : string , requestID : number ) {
7070 const sleep = ( ms : number ) => new Promise ( ( r ) => setTimeout ( r , ms ) ) ;
7171
7272 this . changeStatus ( 'Establishing connection' ) ;
@@ -97,25 +97,31 @@ export class NaoRobotModel extends DOMWidgetModel {
9797
9898 // Handle connection failure
9999 if ( ! this . qiSession . isConnected ( ) ) {
100+ this . disconnect ( ) ;
100101 console . error ( 'Connection to ' , ipAddress , ' could not be established.' ) ;
101- this . changeStatus ( 'Unavailable' ) ;
102+ this . changeStatus (
103+ 'Connection to ' + ipAddress + ' could not be established.'
104+ ) ;
102105 }
103106 }
104107
105108 disconnect ( ) {
106- this . qiSession . disconnect ( ) ;
109+ if ( this . qiSession && this . qiSession . isConnected ( ) ) {
110+ this . qiSession . disconnect ( ) ;
111+ }
107112 this . _services = { } ;
108113 this . set ( 'connected' , 'Disconnected' ) ;
109114 this . save_changes ( ) ;
110115 this . changeStatus ( 'Unavailable' ) ;
111116 }
112117
113- private async checkConnection ( ) {
118+ private async checkConnection ( requestID : number ) {
114119 // Cannot reconnect without initial connection
115120 if ( ! this . _ipAddress ) {
116121 this . send ( {
117122 isError : true ,
118123 data : 'Cannot connect without IP Address.' ,
124+ requestID : requestID ,
119125 } ) ;
120126 this . set ( 'counter' , this . get ( 'counter' ) + 1 ) ;
121127 this . save_changes ( ) ;
@@ -124,32 +130,47 @@ export class NaoRobotModel extends DOMWidgetModel {
124130
125131 // Reconnect if possible
126132 if ( ! this . qiSession . isConnected ( ) ) {
127- await this . connect ( this . _ipAddress , this . _port ) ;
133+ this . disconnect ( ) ;
134+ await this . connect ( this . _ipAddress , this . _port , requestID ) ;
128135 }
129136 return true ;
130137 }
131138
132- private async createService ( serviceName : string ) {
133- const isConnected : boolean = await this . checkConnection ( ) ;
139+ private async createService ( serviceName : string , requestID : number ) {
140+ const isConnected : boolean = await this . checkConnection ( requestID ) ;
134141 if ( ! isConnected ) {
135142 return ;
136143 }
137144
138145 // Skip if service exists already
139- if ( this . _services [ serviceName ] !== undefined ) {
146+ if ( this . _services [ serviceName ] ) {
140147 console . log ( 'Service ' + serviceName + ' exists.' ) ;
141148 return ;
142149 }
143150
144151 this . changeStatus ( 'Creating service ' + serviceName ) ;
145152 const servicePromise = this . qiSession . service ( serviceName ) ;
146153
154+ // TODO: This func is not async in the kernel. To show error messages
155+ // the request ID is the next one which is used to call the service
147156 const naoService = await servicePromise
148157 . then ( ( resolution : any ) => {
158+ this . send ( {
159+ isError : false ,
160+ data : true , // TODO: resolution ?? true,
161+ requestID : requestID + 1 , // Note above
162+ } ) ;
149163 return resolution ;
150164 } )
151165 . catch ( ( rejection : string ) => {
152166 this . changeStatus ( rejection ) ;
167+ this . send ( {
168+ isError : true ,
169+ data : rejection ,
170+ requestID : requestID + 1 , // Note above
171+ } ) ;
172+ this . set ( 'counter' , this . get ( 'counter' ) + 1 ) ;
173+ this . save_changes ( ) ;
153174 return rejection ;
154175 } ) ;
155176
@@ -164,27 +185,49 @@ export class NaoRobotModel extends DOMWidgetModel {
164185 serviceName : string ,
165186 methodName : string ,
166187 args : any ,
167- _kwargs : any
188+ _kwargs : any ,
189+ requestID : number
168190 ) {
169- const isConnected : boolean = await this . checkConnection ( ) ;
191+ const isConnected : boolean = await this . checkConnection ( requestID ) ;
170192 if ( ! isConnected ) {
171193 return ;
172194 }
173195
174196 // Wait for service to become available
175197 const sleep = ( ms : number ) => new Promise ( ( r ) => setTimeout ( r , ms ) ) ;
198+ this . changeStatus ( 'Waiting for service ' + serviceName ) ;
176199
177200 // Timeout after ~10 seconds
178201 for ( let i = 0 ; i < 100 ; i ++ ) {
179- if ( this . _services [ serviceName ] !== undefined ) {
202+ if ( this . _services [ serviceName ] ) {
180203 console . log ( 'Service available after ' , i / 10.0 , ' seconds.' ) ;
204+ this . changeStatus ( serviceName + ' available' ) ;
181205 break ;
182206 }
183207 await sleep ( 100 ) ;
184208 }
185209
186- if ( this . _services [ serviceName ] [ methodName ] === undefined ) {
187- this . changeStatus ( methodName + ' does not exist for ' + serviceName ) ;
210+ if ( ! this . _services [ serviceName ] ) {
211+ this . changeStatus ( serviceName + ' not available' ) ;
212+ this . send ( {
213+ isError : true ,
214+ data : serviceName + ' not available' ,
215+ requestID : requestID ,
216+ } ) ;
217+ this . set ( 'counter' , this . get ( 'counter' ) + 1 ) ;
218+ this . save_changes ( ) ;
219+ return ;
220+ }
221+
222+ if ( ! this . _services [ serviceName ] [ methodName ] ) {
223+ this . changeStatus ( `${ methodName } does not exist for ${ serviceName } ` ) ;
224+ this . send ( {
225+ isError : true ,
226+ data : `${ methodName } does not exist for ${ serviceName } ` ,
227+ requestID : requestID ,
228+ } ) ;
229+ this . set ( 'counter' , this . get ( 'counter' ) + 1 ) ;
230+ this . save_changes ( ) ;
188231 return ;
189232 }
190233
@@ -197,13 +240,15 @@ export class NaoRobotModel extends DOMWidgetModel {
197240 this . send ( {
198241 isError : false ,
199242 data : resolution ?? true ,
243+ requestID : requestID ,
200244 } ) ;
201245 } )
202246 . catch ( ( rejection : string ) => {
203247 this . changeStatus ( rejection ) ;
204248 this . send ( {
205249 isError : true ,
206250 data : rejection ,
251+ requestID : requestID ,
207252 } ) ;
208253 } ) ;
209254
@@ -216,23 +261,31 @@ export class NaoRobotModel extends DOMWidgetModel {
216261
217262 switch ( cmd ) {
218263 case 'connect' :
219- await this . connect ( commandData [ 'ipAddress' ] , commandData [ 'port' ] ) ;
264+ await this . connect (
265+ commandData [ 'ipAddress' ] ,
266+ commandData [ 'port' ] ,
267+ commandData [ 'requestID' ]
268+ ) ;
220269 break ;
221270
222271 case 'disconnect' :
223272 this . disconnect ( ) ;
224273 break ;
225274
226275 case 'createService' :
227- this . createService ( commandData [ 'service' ] ) ;
276+ await this . createService (
277+ commandData [ 'service' ] ,
278+ commandData [ 'requestID' ]
279+ ) ;
228280 break ;
229281
230282 case 'callService' :
231283 await this . callService (
232284 commandData [ 'service' ] ,
233285 commandData [ 'method' ] ,
234286 commandData [ 'args' ] ,
235- commandData [ 'kwargs' ]
287+ commandData [ 'kwargs' ] ,
288+ commandData [ 'requestID' ]
236289 ) ;
237290 break ;
238291 }
0 commit comments