Skip to content

Commit bf8dd24

Browse files
committed
Handle connection errors
1 parent bee2311 commit bf8dd24

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/widget.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,29 @@ export class NaoRobotModel extends DOMWidgetModel {
4848
}
4949

5050
async connect(ipAddress: string, port: string) {
51-
console.log('REMOVE the command was to connect');
52-
this.changeStatus('Establishing connection');
51+
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
5352

53+
this.changeStatus('Establishing connection');
54+
// TODO: check ipAddress is valid format
5455
this.qiSession = new QiSession(ipAddress, port);
5556

56-
console.log('CONNECTED: ', this.qiSession.isConnected());
57-
58-
this.connected = 'Connected';
59-
this.set('connected', 'Connected');
60-
this.save_changes();
57+
// Timeout after ~10 seconds
58+
for (let i = 0; i < 100; i++) {
59+
await sleep(100);
60+
if (this.qiSession.isConnected()) {
61+
this.connected = 'Connected';
62+
this.set('connected', 'Connected');
63+
this.save_changes();
64+
this.changeStatus('Available');
65+
console.log("Connection successful after ", i/10.0, " seconds.");
66+
break;
67+
}
68+
}
6169

62-
this.changeStatus('Not busy');
70+
// Handle connection failure
71+
if (!this.qiSession.isConnected()) {
72+
this.changeStatus('Unavailable');
73+
}
6374
}
6475

6576
disconnect() {
@@ -98,7 +109,7 @@ export class NaoRobotModel extends DOMWidgetModel {
98109
serviceName: string,
99110
methodName: string,
100111
args: any,
101-
kwargs: any
112+
_kwargs: any
102113
) {
103114
const naoService = await this.qiSession.service(serviceName);
104115

0 commit comments

Comments
 (0)