Skip to content

Commit bee2311

Browse files
committed
Reorganize
1 parent ecbc33f commit bee2311

File tree

2 files changed

+115
-86
lines changed

2 files changed

+115
-86
lines changed

src/qimessaging.ts

Lines changed: 113 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -38,112 +38,137 @@ export class QiSession {
3838
) {
3939
this.connected = connected;
4040
this.disconnected = disconnected;
41+
console.log('REMOVE: isConnected ', this.isConnected());
4142
console.log('DBG Emile qim about to connect w/17');
4243
this._socket = io.connect('nao:nao@' + ipAddress + ':' + port, {
4344
resource: 'libs/qimessaging/2/socket.io',
4445
'force new connection': true,
4546
});
47+
console.log('REMOVE: isConnected ', this.isConnected(), this._socket.socket);
4648
console.log('DBG Emile qim connecting..');
4749
this._dfd = [];
4850
this._sigs = [];
4951
this._idm = 0;
5052

51-
this._socket.on('reply', (data: any) => {
52-
console.log('DBG Emile qim reply');
53-
54-
const idm = data['idm'];
55-
if (
56-
data['result'] !== undefined &&
57-
data['result']['metaobject'] !== undefined
58-
) {
59-
const replyObject: replyType = {
60-
__MetaObject: data['result']['metaobject'],
61-
};
62-
63-
const pyobj = data['result']['pyobject'];
64-
this._sigs[pyobj] = [];
65-
const methods = replyObject.__MetaObject['methods'];
66-
67-
for (const i in methods) {
68-
const methodName = methods[i]['name'];
69-
replyObject[methodName] = this.createMetaCall(
70-
pyobj,
71-
methodName,
72-
'data'
73-
);
74-
}
75-
76-
const signals = replyObject.__MetaObject['signals'];
77-
for (const i in signals) {
78-
const signalName = signals[i]['name'];
79-
replyObject[signalName] = this.createMetaSignal(
80-
pyobj,
81-
signalName,
82-
false
83-
);
84-
}
85-
86-
const properties = replyObject.__MetaObject['properties'];
87-
for (const i in properties) {
88-
const propertyName = properties[i]['name'];
89-
replyObject[propertyName] = this.createMetaSignal(
90-
pyobj,
91-
propertyName,
92-
true
93-
);
94-
}
95-
96-
this._dfd[idm].resolve(replyObject);
97-
} else {
98-
if (this._dfd[idm].__cbi !== undefined) {
99-
const cbi = this._dfd[idm].__cbi;
100-
this._sigs[cbi['obj']][cbi['signal']][data['result']] = cbi['cb'];
101-
}
102-
this._dfd[idm].resolve(data['result']);
103-
}
104-
delete this._dfd[idm];
105-
});
53+
this._socket.on('reply', (data: any) => {this.onReply(data)});
54+
55+
this._socket.on('error', (data: any) => {this.onError(data)});
56+
57+
this._socket.on('signal', (data: any) => {this.onSignal(data)});
58+
59+
this._socket.on('disconnect', this.onDisconnect);
60+
61+
this._socket.on('connect', this.onConnect);
62+
63+
this.service = this.createMetaCall('ServiceDirectory', 'service', 'data');
10664

107-
this._socket.on('error', (data: any) => {
108-
console.log('DBG Emile qim error');
109-
if (data['idm'] !== undefined) {
110-
this._dfd[data['idm']].reject(data['result']);
111-
delete this._dfd[data['idm']];
65+
console.log('REMOVE: isConnected ', this.isConnected(), this._socket.socket);
66+
console.log('DBG Emile qim done with init');
67+
}
68+
69+
isConnected () {
70+
const connected : boolean =
71+
(this._socket !== undefined) ?
72+
this._socket.socket.connected : false;
73+
return connected;
74+
}
75+
76+
onReply(data: any) {
77+
console.log('DBG Emile qim reply');
78+
79+
const idm = data['idm'];
80+
if (
81+
data['result'] !== undefined &&
82+
data['result']['metaobject'] !== undefined
83+
) {
84+
const replyObject: replyType = {
85+
__MetaObject: data['result']['metaobject'],
86+
};
87+
88+
const pyIndex = data['result']['pyobject'];
89+
this._sigs[pyIndex] = [];
90+
const methods = replyObject.__MetaObject['methods'];
91+
92+
for (const i in methods) {
93+
const methodName = methods[i]['name'];
94+
replyObject[methodName] = this.createMetaCall(
95+
pyIndex,
96+
methodName,
97+
'data'
98+
);
11299
}
113-
});
114100

115-
this._socket.on('signal', (data: any) => {
116-
console.log('DBG Emile qim signal');
117-
const result = data['result'];
118-
const callback =
119-
this._sigs[result['obj']][result['signal']][result['link']];
120-
if (callback !== undefined) {
121-
callback.apply(this, result['data']);
101+
const signals = replyObject.__MetaObject['signals'];
102+
for (const i in signals) {
103+
const signalName = signals[i]['name'];
104+
replyObject[signalName] = this.createMetaSignal(
105+
pyIndex,
106+
signalName,
107+
false
108+
);
122109
}
123-
});
124110

125-
this._socket.on('disconnect', (data: any) => {
126-
console.log('DBG Emile qim disconnect');
127-
for (const idm in this._dfd) {
128-
this._dfd[idm].reject('Call ' + idm + ' canceled: disconnected');
129-
delete this._dfd[idm];
111+
const properties = replyObject.__MetaObject['properties'];
112+
for (const i in properties) {
113+
const propertyName = properties[i]['name'];
114+
replyObject[propertyName] = this.createMetaSignal(
115+
pyIndex,
116+
propertyName,
117+
true
118+
);
130119
}
131120

132-
if (this.disconnected) {
133-
this.disconnected();
121+
this._dfd[idm].resolve(replyObject);
122+
} else {
123+
if (this._dfd[idm].__cbi !== undefined) {
124+
const cbi = this._dfd[idm].__cbi;
125+
this._sigs[cbi['obj']][cbi['signal']][data['result']] = cbi['cb'];
134126
}
135-
});
127+
this._dfd[idm].resolve(data['result']);
128+
}
129+
delete this._dfd[idm];
130+
}
136131

137-
this.service = this.createMetaCall('ServiceDirectory', 'service', 'data');
132+
onError(data: any) {
133+
console.log('DBG Emile qim error');
134+
if (data['idm'] !== undefined) {
135+
this._dfd[data['idm']].reject(data['result']);
136+
delete this._dfd[data['idm']];
137+
}
138+
}
138139

139-
this._socket.on('connect', () => {
140-
console.log('DBG Emile qim connect');
141-
if (this.connected) {
142-
this.connected(this);
143-
}
144-
});
140+
onSignal(data: any) {
141+
console.log('DBG Emile qim signal');
142+
const result = data['result'];
143+
const callback =
144+
this._sigs[result['obj']][result['signal']][result['link']];
145+
if (callback !== undefined) {
146+
callback.apply(this, result['data']);
147+
}
148+
}
145149

146-
console.log('DBG Emile qim done with init');
150+
onConnect() {
151+
console.log('DBG Emile qim connect');
152+
if (this.connected) {
153+
this.connected(this);
154+
}
155+
const connected : boolean =
156+
(this._socket !== undefined) ?
157+
this._socket.socket.connected : false;
158+
console.log('REMOVE: isConnected in on connect ', connected);
159+
160+
}
161+
162+
onDisconnect(_data: any) {
163+
console.log('DBG Emile qim disconnect');
164+
for (const idm in this._dfd) {
165+
this._dfd[idm].reject('Call ' + idm + ' canceled: disconnected');
166+
delete this._dfd[idm];
167+
}
168+
169+
if (this.disconnected) {
170+
this.disconnected();
171+
}
147172
}
148173

149174
createMetaCall(obj: any, member: any, data: any) {
@@ -154,6 +179,8 @@ export class QiSession {
154179
this._dfd[this._idm] = { resolve: resolve, reject: reject };
155180
});
156181
if (serviceArgs[0] === 'connect') {
182+
this.isConnected = this._socket.socket.connected;
183+
console.log('REMOVE: isConnected in MetaCall ', this.isConnected(), this._socket);
157184
this._dfd[this._idm].__cbi = data;
158185
}
159186
this._socket.emit('call', {
@@ -197,4 +224,5 @@ export class QiSession {
197224

198225
return signalObject;
199226
}
227+
200228
}

src/widget.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export class NaoRobotModel extends DOMWidgetModel {
3838

3939
initialize(attributes: any, options: any): void {
4040
super.initialize(attributes, options);
41-
4241
this.on('msg:custom', this.onCommand);
4342
}
4443

@@ -54,6 +53,8 @@ export class NaoRobotModel extends DOMWidgetModel {
5453

5554
this.qiSession = new QiSession(ipAddress, port);
5655

56+
console.log('CONNECTED: ', this.qiSession.isConnected());
57+
5758
this.connected = 'Connected';
5859
this.set('connected', 'Connected');
5960
this.save_changes();

0 commit comments

Comments
 (0)