@@ -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' ) ;
@@ -97,25 +97,31 @@ export class NaoRobotModel extends DOMWidgetModel {
97
97
98
98
// Handle connection failure
99
99
if ( ! this . qiSession . isConnected ( ) ) {
100
+ this . disconnect ( ) ;
100
101
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
+ ) ;
102
105
}
103
106
}
104
107
105
108
disconnect ( ) {
106
- this . qiSession . disconnect ( ) ;
109
+ if ( this . qiSession && this . qiSession . isConnected ( ) ) {
110
+ this . qiSession . disconnect ( ) ;
111
+ }
107
112
this . _services = { } ;
108
113
this . set ( 'connected' , 'Disconnected' ) ;
109
114
this . save_changes ( ) ;
110
115
this . changeStatus ( 'Unavailable' ) ;
111
116
}
112
117
113
- private async checkConnection ( ) {
118
+ private async checkConnection ( requestID : number ) {
114
119
// Cannot reconnect without initial connection
115
120
if ( ! this . _ipAddress ) {
116
121
this . send ( {
117
122
isError : true ,
118
123
data : 'Cannot connect without IP Address.' ,
124
+ requestID : requestID ,
119
125
} ) ;
120
126
this . set ( 'counter' , this . get ( 'counter' ) + 1 ) ;
121
127
this . save_changes ( ) ;
@@ -124,32 +130,47 @@ export class NaoRobotModel extends DOMWidgetModel {
124
130
125
131
// Reconnect if possible
126
132
if ( ! this . qiSession . isConnected ( ) ) {
127
- await this . connect ( this . _ipAddress , this . _port ) ;
133
+ this . disconnect ( ) ;
134
+ await this . connect ( this . _ipAddress , this . _port , requestID ) ;
128
135
}
129
136
return true ;
130
137
}
131
138
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 ) ;
134
141
if ( ! isConnected ) {
135
142
return ;
136
143
}
137
144
138
145
// Skip if service exists already
139
- if ( this . _services [ serviceName ] !== undefined ) {
146
+ if ( this . _services [ serviceName ] ) {
140
147
console . log ( 'Service ' + serviceName + ' exists.' ) ;
141
148
return ;
142
149
}
143
150
144
151
this . changeStatus ( 'Creating service ' + serviceName ) ;
145
152
const servicePromise = this . qiSession . service ( serviceName ) ;
146
153
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
147
156
const naoService = await servicePromise
148
157
. then ( ( resolution : any ) => {
158
+ this . send ( {
159
+ isError : false ,
160
+ data : true , // TODO: resolution ?? true,
161
+ requestID : requestID + 1 , // Note above
162
+ } ) ;
149
163
return resolution ;
150
164
} )
151
165
. catch ( ( rejection : string ) => {
152
166
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 ( ) ;
153
174
return rejection ;
154
175
} ) ;
155
176
@@ -164,27 +185,49 @@ export class NaoRobotModel extends DOMWidgetModel {
164
185
serviceName : string ,
165
186
methodName : string ,
166
187
args : any ,
167
- _kwargs : any
188
+ _kwargs : any ,
189
+ requestID : number
168
190
) {
169
- const isConnected : boolean = await this . checkConnection ( ) ;
191
+ const isConnected : boolean = await this . checkConnection ( requestID ) ;
170
192
if ( ! isConnected ) {
171
193
return ;
172
194
}
173
195
174
196
// Wait for service to become available
175
197
const sleep = ( ms : number ) => new Promise ( ( r ) => setTimeout ( r , ms ) ) ;
198
+ this . changeStatus ( 'Waiting for service ' + serviceName ) ;
176
199
177
200
// Timeout after ~10 seconds
178
201
for ( let i = 0 ; i < 100 ; i ++ ) {
179
- if ( this . _services [ serviceName ] !== undefined ) {
202
+ if ( this . _services [ serviceName ] ) {
180
203
console . log ( 'Service available after ' , i / 10.0 , ' seconds.' ) ;
204
+ this . changeStatus ( serviceName + ' available' ) ;
181
205
break ;
182
206
}
183
207
await sleep ( 100 ) ;
184
208
}
185
209
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 ( ) ;
188
231
return ;
189
232
}
190
233
@@ -197,13 +240,15 @@ export class NaoRobotModel extends DOMWidgetModel {
197
240
this . send ( {
198
241
isError : false ,
199
242
data : resolution ?? true ,
243
+ requestID : requestID ,
200
244
} ) ;
201
245
} )
202
246
. catch ( ( rejection : string ) => {
203
247
this . changeStatus ( rejection ) ;
204
248
this . send ( {
205
249
isError : true ,
206
250
data : rejection ,
251
+ requestID : requestID ,
207
252
} ) ;
208
253
} ) ;
209
254
@@ -216,23 +261,31 @@ export class NaoRobotModel extends DOMWidgetModel {
216
261
217
262
switch ( cmd ) {
218
263
case 'connect' :
219
- await this . connect ( commandData [ 'ipAddress' ] , commandData [ 'port' ] ) ;
264
+ await this . connect (
265
+ commandData [ 'ipAddress' ] ,
266
+ commandData [ 'port' ] ,
267
+ commandData [ 'requestID' ]
268
+ ) ;
220
269
break ;
221
270
222
271
case 'disconnect' :
223
272
this . disconnect ( ) ;
224
273
break ;
225
274
226
275
case 'createService' :
227
- this . createService ( commandData [ 'service' ] ) ;
276
+ await this . createService (
277
+ commandData [ 'service' ] ,
278
+ commandData [ 'requestID' ]
279
+ ) ;
228
280
break ;
229
281
230
282
case 'callService' :
231
283
await this . callService (
232
284
commandData [ 'service' ] ,
233
285
commandData [ 'method' ] ,
234
286
commandData [ 'args' ] ,
235
- commandData [ 'kwargs' ]
287
+ commandData [ 'kwargs' ] ,
288
+ commandData [ 'requestID' ]
236
289
) ;
237
290
break ;
238
291
}
0 commit comments