@@ -110,11 +110,29 @@ export class NaoRobotModel extends DOMWidgetModel {
110
110
this . changeStatus ( 'Unavailable' ) ;
111
111
}
112
112
113
- private async createService ( serviceName : string ) {
114
- // Reconnect as needed
113
+ private async checkConnection ( ) {
114
+ // Cannot reconnect without initial connection
115
+ if ( ! this . _ipAddress ) {
116
+ this . send ( {
117
+ isError : true ,
118
+ data : 'Cannot connect without IP Address.' ,
119
+ } ) ;
120
+ this . set ( 'counter' , this . get ( 'counter' ) + 1 ) ;
121
+ this . save_changes ( ) ;
122
+ return false ;
123
+ }
124
+
125
+ // Reconnect if possible
115
126
if ( ! this . qiSession . isConnected ( ) ) {
116
127
await this . connect ( this . _ipAddress , this . _port ) ;
117
128
}
129
+ return true ;
130
+ }
131
+
132
+ private async createService ( serviceName : string ) {
133
+
134
+ const isConnected : boolean = await this . checkConnection ( ) ;
135
+ if ( ! isConnected ) return ;
118
136
119
137
// Skip if service exists already
120
138
if ( this . _services [ serviceName ] !== undefined ) {
@@ -147,10 +165,8 @@ export class NaoRobotModel extends DOMWidgetModel {
147
165
args : any ,
148
166
_kwargs : any
149
167
) {
150
- // Reconnect as needed
151
- if ( ! this . qiSession . isConnected ( ) ) {
152
- await this . connect ( this . _ipAddress , this . _port ) ;
153
- }
168
+ const isConnected : boolean = await this . checkConnection ( ) ;
169
+ if ( ! isConnected ) return ;
154
170
155
171
// Wait for service to become available
156
172
const sleep = ( ms : number ) => new Promise ( ( r ) => setTimeout ( r , ms ) ) ;
0 commit comments