@@ -14,10 +14,17 @@ import '../css/widget.css';
14
14
15
15
import { QiSession } from './qimessaging' ;
16
16
17
+ interface serviceDict {
18
+ [ key : string ] : {
19
+ [ key : string ] : any ;
20
+ } ;
21
+ }
22
+
17
23
export class NaoRobotModel extends DOMWidgetModel {
18
24
qiSession : QiSession ;
19
25
connected = 'Disconnected' ;
20
26
status = 'Not busy' ;
27
+ _services : serviceDict = { } ;
21
28
synco : string ;
22
29
23
30
defaults ( ) {
@@ -107,19 +114,28 @@ export class NaoRobotModel extends DOMWidgetModel {
107
114
console . log ( 'JS sent something' ) ;
108
115
}
109
116
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 ) ;
117
122
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
+ ) ;
120
133
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
+ }
123
139
}
124
140
125
141
private async callService (
@@ -128,12 +144,24 @@ export class NaoRobotModel extends DOMWidgetModel {
128
144
args : any ,
129
145
_kwargs : any
130
146
) {
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
+ }
132
151
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
+ ) ;
135
164
136
- this . changeStatus ( 'Task completed' ) ;
137
165
}
138
166
139
167
private async onCommand ( commandData : any , buffers : any ) {
@@ -149,6 +177,10 @@ export class NaoRobotModel extends DOMWidgetModel {
149
177
this . disconnect ( ) ;
150
178
break ;
151
179
180
+ case 'createService' :
181
+ this . createService ( commandData [ 'service' ] ) ;
182
+ break ;
183
+
152
184
case 'callService' :
153
185
console . log ( 'RECEIVING COMMAND FOR SERVICE' ) ;
154
186
await this . callService (
0 commit comments