@@ -66,7 +66,7 @@ export class NaoRobotModel extends DOMWidgetModel {
66
66
}
67
67
}
68
68
69
- async connect ( ipAddress : string , port : string ) {
69
+ async connect ( ipAddress : string , port : string , requestID : number ) {
70
70
const sleep = ( ms : number ) => new Promise ( ( r ) => setTimeout ( r , ms ) ) ;
71
71
72
72
this . changeStatus ( 'Establishing connection' ) ;
@@ -104,7 +104,7 @@ export class NaoRobotModel extends DOMWidgetModel {
104
104
}
105
105
106
106
disconnect ( ) {
107
- if ( this . qiSession ) {
107
+ if ( this . qiSession && this . qiSession . isConnected ( ) ) {
108
108
this . qiSession . disconnect ( ) ;
109
109
} ;
110
110
this . _services = { } ;
@@ -113,12 +113,13 @@ export class NaoRobotModel extends DOMWidgetModel {
113
113
this . changeStatus ( 'Unavailable' ) ;
114
114
}
115
115
116
- private async checkConnection ( ) {
116
+ private async checkConnection ( requestID : number ) {
117
117
// Cannot reconnect without initial connection
118
118
if ( ! this . _ipAddress ) {
119
119
this . send ( {
120
120
isError : true ,
121
121
data : 'Cannot connect without IP Address.' ,
122
+ requestID : requestID
122
123
} ) ;
123
124
this . set ( 'counter' , this . get ( 'counter' ) + 1 ) ;
124
125
this . save_changes ( ) ;
@@ -127,15 +128,14 @@ export class NaoRobotModel extends DOMWidgetModel {
127
128
128
129
// Reconnect if possible
129
130
if ( ! this . qiSession . isConnected ( ) ) {
130
- this . set ( 'connected' , 'Disconnected' ) ;
131
- this . save_changes ( ) ;
132
- await this . connect ( this . _ipAddress , this . _port ) ;
131
+ this . disconnect ( ) ;
132
+ await this . connect ( this . _ipAddress , this . _port , requestID ) ;
133
133
}
134
134
return true ;
135
135
}
136
136
137
- private async createService ( serviceName : string ) {
138
- const isConnected : boolean = await this . checkConnection ( ) ;
137
+ private async createService ( serviceName : string , requestID : number ) {
138
+ const isConnected : boolean = await this . checkConnection ( requestID ) ;
139
139
if ( ! isConnected ) {
140
140
return ;
141
141
}
@@ -149,11 +149,14 @@ export class NaoRobotModel extends DOMWidgetModel {
149
149
this . changeStatus ( 'Creating service ' + serviceName ) ;
150
150
const servicePromise = this . qiSession . service ( serviceName ) ;
151
151
152
+ // TODO: This func is not async in the kernel. To show error messages
153
+ // the request ID is the next one which is used to call the service
152
154
const naoService = await servicePromise
153
155
. then ( ( resolution : any ) => {
154
156
this . send ( {
155
157
isError : false ,
156
- data : resolution ?? true ,
158
+ data : true , // TODO: resolution ?? true,
159
+ requestID : requestID + 1 // Note above
157
160
} ) ;
158
161
return resolution ;
159
162
} )
@@ -162,6 +165,7 @@ export class NaoRobotModel extends DOMWidgetModel {
162
165
this . send ( {
163
166
isError : true ,
164
167
data : rejection ,
168
+ requestID : requestID + 1 // Note above
165
169
} ) ;
166
170
this . set ( 'counter' , this . get ( 'counter' ) + 1 ) ;
167
171
this . save_changes ( ) ;
@@ -179,25 +183,23 @@ export class NaoRobotModel extends DOMWidgetModel {
179
183
serviceName : string ,
180
184
methodName : string ,
181
185
args : any ,
182
- _kwargs : any
186
+ _kwargs : any ,
187
+ requestID : number = 1000
183
188
) {
184
- const isConnected : boolean = await this . checkConnection ( ) ;
189
+ const isConnected : boolean = await this . checkConnection ( requestID ) ;
185
190
if ( ! isConnected ) {
186
191
return ;
187
192
}
188
193
189
194
// Wait for service to become available
190
195
const sleep = ( ms : number ) => new Promise ( ( r ) => setTimeout ( r , ms ) ) ;
196
+ this . changeStatus ( 'Waiting for service ' + serviceName ) ;
191
197
192
198
// Timeout after ~10 seconds
193
199
for ( let i = 0 ; i < 100 ; i ++ ) {
194
- // Do not wait for service if there is no connection
195
- if ( ! this . qiSession . isConnected ( ) ) {
196
- this . disconnect ( ) ;
197
- break ;
198
- }
199
200
if ( this . _services [ serviceName ] ) {
200
201
console . log ( 'Service available after ' , i / 10.0 , ' seconds.' ) ;
202
+ this . changeStatus ( serviceName + ' available' ) ;
201
203
break ;
202
204
}
203
205
await sleep ( 100 ) ;
@@ -207,7 +209,8 @@ export class NaoRobotModel extends DOMWidgetModel {
207
209
this . changeStatus ( serviceName + ' not available' ) ;
208
210
this . send ( {
209
211
isError : true ,
210
- data : serviceName + ' not available'
212
+ data : serviceName + ' not available' ,
213
+ requestID : requestID
211
214
} ) ;
212
215
this . set ( 'counter' , this . get ( 'counter' ) + 1 ) ;
213
216
this . save_changes ( ) ;
@@ -218,7 +221,8 @@ export class NaoRobotModel extends DOMWidgetModel {
218
221
this . changeStatus ( `${ methodName } does not exist for ${ serviceName } ` ) ;
219
222
this . send ( {
220
223
isError : true ,
221
- data : `${ methodName } does not exist for ${ serviceName } `
224
+ data : `${ methodName } does not exist for ${ serviceName } ` ,
225
+ requestID : requestID
222
226
} ) ;
223
227
this . set ( 'counter' , this . get ( 'counter' ) + 1 ) ;
224
228
this . save_changes ( ) ;
@@ -234,13 +238,15 @@ export class NaoRobotModel extends DOMWidgetModel {
234
238
this . send ( {
235
239
isError : false ,
236
240
data : resolution ?? true ,
241
+ requestID : requestID
237
242
} ) ;
238
243
} )
239
244
. catch ( ( rejection : string ) => {
240
245
this . changeStatus ( rejection ) ;
241
246
this . send ( {
242
247
isError : true ,
243
248
data : rejection ,
249
+ requestID : requestID
244
250
} ) ;
245
251
} ) ;
246
252
@@ -253,23 +259,31 @@ export class NaoRobotModel extends DOMWidgetModel {
253
259
254
260
switch ( cmd ) {
255
261
case 'connect' :
256
- await this . connect ( commandData [ 'ipAddress' ] , commandData [ 'port' ] ) ;
262
+ await this . connect (
263
+ commandData [ 'ipAddress' ] ,
264
+ commandData [ 'port' ] ,
265
+ commandData [ 'requestID' ]
266
+ ) ;
257
267
break ;
258
268
259
269
case 'disconnect' :
260
270
this . disconnect ( ) ;
261
271
break ;
262
272
263
273
case 'createService' :
264
- await this . createService ( commandData [ 'service' ] ) ;
274
+ await this . createService (
275
+ commandData [ 'service' ] ,
276
+ commandData [ 'requestID' ]
277
+ ) ;
265
278
break ;
266
279
267
280
case 'callService' :
268
281
await this . callService (
269
282
commandData [ 'service' ] ,
270
283
commandData [ 'method' ] ,
271
284
commandData [ 'args' ] ,
272
- commandData [ 'kwargs' ]
285
+ commandData [ 'kwargs' ] ,
286
+ commandData [ 'requestID' ]
273
287
) ;
274
288
break ;
275
289
}
0 commit comments