Skip to content

Commit 8414599

Browse files
committed
Add reconnect functionality
1 parent 92cc9a3 commit 8414599

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/widget.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export class NaoRobotModel extends DOMWidgetModel {
2525
connected = 'Disconnected';
2626
status = 'Not busy';
2727
_services: serviceDict = {};
28+
_ipAddress: string;
29+
_port: string;
2830

2931
defaults() {
3032
return {
@@ -75,6 +77,9 @@ export class NaoRobotModel extends DOMWidgetModel {
7577
return;
7678
}
7779

80+
this._ipAddress = ipAddress;
81+
this._port = port;
82+
7883
this.qiSession = new QiSession(ipAddress, port);
7984

8085
// Timeout after ~10 seconds
@@ -98,14 +103,19 @@ export class NaoRobotModel extends DOMWidgetModel {
98103
}
99104

100105
disconnect() {
101-
console.log('REMOVE disconnecting');
102106
this.qiSession.disconnect();
107+
this._services = {};
103108
this.set('connected', 'Disconnected');
104109
this.save_changes();
105110
this.changeStatus('Unavailable');
106111
}
107112

108113
private async createService(serviceName: string) {
114+
// Reconnect as needed
115+
if (!this.qiSession.isConnected()) {
116+
await this.connect(this._ipAddress, this._port);
117+
}
118+
109119
// Skip if service exists already
110120
if (this._services[serviceName] !== undefined) {
111121
console.log("Service " + serviceName + " exists.");
@@ -137,6 +147,12 @@ export class NaoRobotModel extends DOMWidgetModel {
137147
args: any,
138148
_kwargs: any
139149
) {
150+
151+
// Reconnect as needed
152+
if (!this.qiSession.isConnected()) {
153+
await this.connect(this._ipAddress, this._port);
154+
}
155+
140156
// Wait for service to become available
141157
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
142158

0 commit comments

Comments
 (0)