Skip to content

Commit 0cda3c5

Browse files
committed
Handle service errors
1 parent 059448c commit 0cda3c5

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

css/widget.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.custom-widget {
2-
background-color: lightseagreen;
2+
color: var(--jp-widgets-color) !important;
3+
font-family: monospace;
34
padding: 0px 2px;
45
}

ipynao/nao_robot.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,8 @@ def connect(self, ip_address="nao.local", port="80"):
9292

9393

9494
def service(self, service_name):
95+
data = {}
96+
data["command"] = str("createService")
97+
data["service"] = str(service_name)
98+
self.send(data)
9599
return NaoRobotService(self, service_name)

src/widget.ts

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ import '../css/widget.css';
1414

1515
import { QiSession } from './qimessaging';
1616

17+
interface serviceDict {
18+
[key: string]: {
19+
[key: string]: any;
20+
};
21+
}
22+
1723
export class NaoRobotModel extends DOMWidgetModel {
1824
qiSession: QiSession;
1925
connected = 'Disconnected';
2026
status = 'Not busy';
27+
_services: serviceDict = {};
2128
synco: string;
2229

2330
defaults() {
@@ -107,19 +114,28 @@ export class NaoRobotModel extends DOMWidgetModel {
107114
console.log('JS sent something');
108115
}
109116

110-
async goSleep(tSeconds: number) {
111-
console.log('IN THE SLEEPING SESH');
112-
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
113-
114-
await sleep(tSeconds * 1000);
115-
116-
console.log('WAKING UP');
117+
private async createService(
118+
serviceName: string,
119+
) {
120+
this.changeStatus('Creating service ' + serviceName);
121+
const servicePromise = this.qiSession.service(serviceName);
117122

118-
this.set('synco', 'something else');
119-
this.save_changes();
123+
const naoService = await servicePromise.then(
124+
(resolution: object) => {
125+
return resolution;
126+
}
127+
).catch(
128+
(rejection: string) => {
129+
this.changeStatus(rejection);
130+
return rejection;
131+
}
132+
);
120133

121-
this.send({ data: 'purple' });
122-
console.log('SETTED THE VALUE');
134+
// Store service only when successfully created
135+
if (typeof(naoService) === 'object') {
136+
this._services[serviceName] = naoService;
137+
this.changeStatus(serviceName + ' available');
138+
}
123139
}
124140

125141
private async callService(
@@ -128,12 +144,24 @@ export class NaoRobotModel extends DOMWidgetModel {
128144
args: any,
129145
_kwargs: any
130146
) {
131-
const naoService = await this.qiSession.service(serviceName);
147+
if (this._services[serviceName][methodName] === undefined) {
148+
this.changeStatus(methodName + ' does not exist for ' + serviceName);
149+
return;
150+
}
132151

133-
this.changeStatus('Running method' + methodName);
134-
await naoService[methodName](...args);
152+
this.changeStatus('Running method ' + methodName);
153+
154+
const servicePromise = this._services[serviceName][methodName](...args);
155+
await servicePromise.then(
156+
() => {
157+
this.changeStatus('Task completed');
158+
}
159+
).catch(
160+
(rejection: string) => {
161+
this.changeStatus(rejection);
162+
}
163+
);
135164

136-
this.changeStatus('Task completed');
137165
}
138166

139167
private async onCommand(commandData: any, buffers: any) {
@@ -149,6 +177,10 @@ export class NaoRobotModel extends DOMWidgetModel {
149177
this.disconnect();
150178
break;
151179

180+
case 'createService':
181+
this.createService(commandData['service']);
182+
break;
183+
152184
case 'callService':
153185
console.log('RECEIVING COMMAND FOR SERVICE');
154186
await this.callService(

0 commit comments

Comments
 (0)