-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.js
619 lines (555 loc) · 22.3 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/**
* Establishes connection to a ROS master via websocket.
**/
function KnowrobClient(options){
var that = this;
// Object that holds user information
this.flask_user = options.flask_user;
// ROS handle
this.ros = undefined;
// URL for ROS server
var rosURL = options.ros_url || 'ws://localhost:9090';
// Use rosauth?
var authentication = options.authentication === '' ? true : options.authentication === 'True';
// URL for rosauth token retrieval
var authURL = options.auth_url || '/wsauth/v1.0/by_session';
// The selected episode
this.episode;
// The openEASE menu that allows to activate episodes/ui/..
this.menu = options.menu;
// global jsonprolog handle
var prolog;
// User interface names (e.g., editor, memory replay, ...)
var user_interfaces = options.user_interfaces || [];
var user_interfaces_flat = options.user_interfaces_flat || [];
// Query parameters encoded in URL
// E.g., localhost/#foo&bar=1 yields in:
// URL_QUERY = {foo: undefined, bar: 1}
var urlQuery = {};
this.pageOverlayDisabled = false;
// true iff connection to ROS master is established
this.isConnected = false;
// true iff json_prolog is connected
this.isPrologConnected = false;
// true iff registerNodes was called before
this.isRegistered = false;
// Prefix for mesh GET URL's
var meshPath = options.meshPath || '/';
// Block the interface until an episode was selected?
var requireEpisode = options.require_episode;
// Viewer used by tutorial page
var globalViewer = options.global_viewer;
// sprite markers and render events
var sprites = [];
var render_event;
// The selected marker object or undefined
this.selectedMarker = undefined;
// ROS messages
var tfClient = undefined;
this.markerClient = undefined;
this.markerArrayClient = undefined;
var designatorClient = undefined;
var imageClient = undefined;
var cameraPoseClient = undefined;
this.snapshotTopic = undefined;
this.nodesRegistered = false;
// Redirects incomming marker messages to currently active canvas.
function CanvasProxy(scene) {
this.viewer = function() {
//if(globalViewer) {
// var w = globalViewer();
// if(w) return w;
//}
var ui = that.getActiveFrame().ui; if(!ui) return undefined;
var v = ui.rosViewer; if(!v) return undefined;
return v.rosViewer;
};
this.add = function(m) {
if(this.viewer()) this.viewer()[scene].add(m);
};
this.remove = function(m) {
if(this.viewer()) this.viewer()[scene].remove(m);
};
};
this.scene = new CanvasProxy('scene');
this.selectableObjects = new CanvasProxy('selectableObjects');
this.backgroundScene = new CanvasProxy('backgroundScene');
this.orthoScene = new CanvasProxy('orthogonalScene');
this.init = function() {
// Connect to ROS.
that.connect();
that.episode = new KnowrobEpisode(that);
that.createOverlay();
if(requireEpisode && !that.episode.hasEpisode()) {
that.showPageOverlay("Please select an Episode");
} else {
that.showPageOverlay("Loading Knowledge Base");
}
setInterval(containerRefresh, 570000);
containerRefresh();
render_event = new CustomEvent('render', {'camera': null});
};
function containerRefresh() {
$.ajax({
url: '/api/v1.0/refresh_by_session',
type: "GET",
contentType: "application/json",
dataType: "json"
});
};
function onRosConnected() {
that.registerNodes();
if(options.category && options.episode)
that.episode.setEpisode(options.category, options.episode);
}
this.connect = function () {
if(that.ros) return;
that.ros = new ROSLIB.Ros({url : rosURL});
that.ros.on('connection', function() {
that.isConnected = true;
console.log('Connected to websocket server.');
if (authentication) {
// Acquire auth token for current user and authenticate, then call registerNodes
that.authenticate(authURL, onRosConnected);
} else {
// No authentication requested, call registerNodes directly
that.waitForJsonProlog();
onRosConnected();
}
});
that.ros.on('close', function() {
console.log('Connection was closed.');
that.showPageOverlay("Connection was closed, reconnecting...");
that.ros = undefined;
that.isRegistered = false;
setTimeout(that.connect, 500);
});
that.ros.on('error', function(error) {
console.log('Error connecting to websocket server: ', error);
that.showPageOverlay("Connection error, reconnecting...");
if(that.ros) that.ros.close();
that.ros = undefined;
that.isRegistered = false;
setTimeout(that.connect, 500);
});
};
this.authenticate = function (authurl, then) {
console.log("Acquiring auth token");
// Call wsauth api to acquire auth token by existing user login session
$.ajax({
url: authurl,
type: "GET",
contentType: "application/json",
dataType: "json"
}).done( function (request) {
if(!that.ros) {
console.warn("Lost connection to ROS master.");
return;
}
console.log("Sending auth token");
that.ros.authenticate(request.mac,
request.client,
request.dest,
request.rand,
request.t,
request.level,
request.end);
that.waitForJsonProlog();
// If a callback function was specified, call it in the context of Knowrob class (that)
if(then) {
then.call(that);
}
});
};
this.registerNodes = function () {
if(that.isRegistered) return;
that.isRegistered = true;
// Setup publisher that sends a dummy message in order to keep alive the socket connection
{
var interval = options.interval || 30000;
// The topic dedicated to keep alive messages
var keepAliveTopic = new ROSLIB.Topic({ ros : that.ros, name : '/keep_alive', messageType : 'std_msgs/Bool' });
// A dummy message for the topic
var keepAliveMsg = new ROSLIB.Message({ data : true });
// Function that publishes the keep alive message
var ping = function() { keepAliveTopic.publish(keepAliveMsg); };
// Call ping at regular intervals.
setInterval(ping, interval);
};
// topic used for publishing canvas snapshots
that.snapshotTopic = new ROSLIB.Topic({
ros : that.ros,
name : '/openease/video/frame',
messageType : 'sensor_msgs/Image'
});
// Setup a client to listen to TFs.
tfClient = new ROSLIB.TFClient({
ros : that.ros,
angularThres : 0.01,
transThres : 0.01,
rate : 10.0,
fixedFrame : '/my_frame'
});
// Setup the marker client.
that.markerClient = new ROS3D.MarkerClient({
ros : that.ros,
tfClient : tfClient,
topic : '/visualization_marker',
sceneObjects : that.scene,
orthogonalObjects : that.orthoScene,
selectableObjects : that.selectableObjects,
backgroundObjects : that.backgroundScene
});
// Setup the marker array client.
that.markerArrayClient = new ROS3D.MarkerArrayClient({
ros : that.ros,
tfClient : tfClient,
topic : '/visualization_marker_array',
sceneObjects : that.scene,
orthogonalObjects : that.orthoScene,
selectableObjects : that.selectableObjects,
backgroundObjects : that.backgroundScene,
markerClient : that.markerClient,
path : meshPath,
on_dblclick: options.on_dblclick || that.on_marker_dblclick,
on_contextmenu: options.on_contextmenu || that.on_marker_contextmenu,
on_delete: options.on_marker_delete || that.on_marker_delete
});
// Setup the designator message client.
designatorClient = new ROSLIB.Topic({
ros : that.ros,
name : '/logged_designators',
messageType : 'designator_integration_msgs/Designator'
});
designatorClient.subscribe(function(message) {
if(message.description.length==0) {
console.warn("Ignoring empty designator.");
}
else {
/*
var designatorHtml = "";
if(message.type==0) {
designatorHtml += "OBJECT DESIGNATOR";
}
else if(message.type==1) {
designatorHtml += "ACTION DESIGNATOR";
}
else if(message.type==2) {
designatorHtml += "LOCATION DESIGNATOR";
}
else if(message.type==3) {
designatorHtml += "HUMAN DESIGNATOR";
}
else {
designatorHtml += "DESIGNATOR";
}
*/
var desig_js = parse_designator(message.description);
var html = undefined;
if(desig_js.type) {
if(desig_js.type=='ADT') {
html = format_adt_designator(that.getActiveFrame().ui, desig_js);
}
}
if(!html) {
html = format_designator(message.description);
}
// TODO: send to all?
if(that.getActiveFrame().on_designator_received)
that.getActiveFrame().on_designator_received(html);
}
});
// Setup the image message client.
imageClient = new ROSLIB.Topic({
ros : that.ros,
name : '/logged_images',
messageType : 'std_msgs/String'
});
imageClient.subscribe(function(message) {
var ext = message.data.substr(message.data.lastIndexOf('.') + 1).toLowerCase();
var url = message.data;
if(!url.startsWith("/knowrob/")) {
if(url.startsWith("/home/ros/user_data")) url = '/user_data/'+url.replace("/home/ros/user_data/", "");
else url = '/knowrob/knowrob_data/'+url;
}
var imageHeight, imageWidth;
var html = '';
if(ext=='jpg' || ext =='png') {
html += '<div class="image_view">';
html += '<img id="mjpeg_image" class="picture" src="'+url+'" width="300" height="240"/>';
html += '</div>';
imageHeight = function(mjpeg_image) { return mjpeg_image.height; };
imageWidth = function(mjpeg_image) { return mjpeg_image.width; };
}
else if(ext =='ogg' || ext =='ogv' || ext =='mp4' || ext =='mov') {
html += '<div class="image_view">';
html += ' <video id="mjpeg_image" controls autoplay loop>';
html += ' <source src="'+url+'" ';
if(ext =='ogg' || ext =='ogv') html += 'type="video/ogg" ';
else if(ext =='mp4') html += 'type="video/mp4" ';
html += '/>';
html += 'Your browser does not support the video tag.';
html += '</video></div>';
imageHeight = function(mjpeg_image) { return mjpeg_image.videoHeight; };
imageWidth = function(mjpeg_image) { return mjpeg_image.videoWidth; };
}
else {
console.warn("Unknown data format on /logged_images topic: " + message.data);
}
if(html.length>0 && that.getActiveFrame().on_image_received) {
// TODO: send to all?
that.getActiveFrame().on_image_received(html, imageWidth, imageHeight);
}
});
cameraPoseClient = new ROSLIB.Topic({
ros : that.ros,
name : '/camera/pose',
messageType : 'geometry_msgs/Pose'
});
cameraPoseClient.subscribe(function(message) {
// TODO: send to all?
if(that.getActiveFrame().on_camera_pose_received)
that.getActiveFrame().on_camera_pose_received(message);
});
// NOTE: frame windows may not be loaded already
for(var i in user_interfaces) {
var frame = document.getElementById(user_interfaces[i].id+"-frame");
if(frame && frame.contentWindow && frame.contentWindow.on_register_nodes)
frame.contentWindow.on_register_nodes();
}
that.nodesRegistered = true;
};
this.waitForJsonProlog = function () {
var client = new JsonProlog(that.ros, {});
client.jsonQuery("true", function(result) {
client.finishClient();
if(result.error) {
// Service /json_prolog/simple_query does not exist
setTimeout(that.waitForJsonProlog, 500);
}
else {
that.hidePageOverlay();
if(requireEpisode && !that.episode.hasEpisode())
that.showPageOverlay("Please select an Episode");
that.isPrologConnected = true;
that.episode.selectMongoDB();
}
});
};
///////////////////////////////
//////////// Marker Visualization
///////////////////////////////
this.newProlog = function() {
return that.ros ? new JsonProlog(that.ros, {}) : undefined;
};
this.newCanvas = function(options) {
var x = new KnowrobCanvas(that, options);
// connect to render event, dispatch to marker clients
x.rosViewer.on('render', function(e) {
if(that.markerClient) that.markerClient.emit('render', e);
if(that.markerArrayClient) that.markerArrayClient.emit('render', e);
});
return x;
};
this.newDataVis = function(options) {
return new DataVisClient(options);
};
this.newTaskTreeVis = function(options) {
return new TaskTreeVisClient(options);
};
this.selectMarker = function(marker) {
if(that.selectedMarker == marker.ns) return;
if(that.selectedMarker) that.unselectMarker(that.selectedMarker);
that.selectedMarker = marker.ns;
var prolog = new JsonProlog(that.ros, {});
prolog.jsonQuery("term_to_atom("+marker.ns+",MarkerName), "+
"marker_highlight(MarkerName), ignore(marker_publish).",
function(result) { prolog.finishClient(); });
};
this.unselectMarker = function() {
var prolog = new JsonProlog(that.ros, {});
prolog.jsonQuery("term_to_atom("+that.selectedMarker+",MarkerName), "+
"marker_highlight_remove(MarkerName), ignore(marker_publish).",
function(result) { prolog.finishClient(); });
that.selectedMarker = undefined;
};
this.on_render = function(camera,scene) {
if(that.getActiveFrame() && that.getActiveFrame().on_render)
that.getActiveFrame().on_render(camera,scene);
var index;
for(index = 0; index < sprites.length; index++) {
//sprites[index].camera = camera;
//render_event.target = sprites[index];
render_event.camera = camera;
sprites[index].dispatchEvent(render_event);
}
};
this.on_marker_dblclick = function(marker) {
that.selectMarker(marker);
if(that.getActiveFrame())
that.getActiveFrame().on_marker_dblclick(marker);
};
this.on_marker_delete = function(ns) {
if(that.getActiveFrame())
that.getActiveFrame().on_marker_delete(ns);
if(ns === that.selectedMarker) {
that.unselectMarker();
}
};
this.on_marker_contextmenu = function(marker) {
if(that.getActiveFrame())
that.getActiveFrame().on_marker_contextmenu(marker);
};
///////////////////////////////
//////////// Edisodes
///////////////////////////////
this.setEpisode = function(category, episode) {
that.episode.setEpisode(category, episode, that.on_episode_selected);
};
this.on_episode_selected = function(library) {
for(var i in user_interfaces) {
var frame = document.getElementById(user_interfaces[i].id+"-frame");
if(frame && frame.contentWindow.on_episode_selected)
frame.contentWindow.on_episode_selected(library);
}
// Hide "Please select an episode" overlay
that.hidePageOverlay();
that.showPageOverlay("Loading Knowledge Base");
if(that.ros) that.ros.close(); // force reconnect
$.ajax({
url: '/knowrob/reset',
type: "POST",
contentType: "application/json",
dataType: "json"
});
};
///////////////////////////////
//////////// Frames
///////////////////////////////
function showFrame(iface_name) {
var frame_name = getInterfaceFrameName(iface_name);
// Hide inactive frames
for(var i in user_interfaces) {
if(user_interfaces[i].id == frame_name) continue;
$("#"+user_interfaces[i].id+"-frame").hide();
$("#"+user_interfaces[i].id+"-frame").removeClass("selected-frame");
$("#"+user_interfaces[i].id+"-menu").removeClass("selected-menu");
}
var new_src = getInterfaceSrc(iface_name);
var frame = document.getElementById(frame_name+"-frame");
var old_src = frame.src;
if(!old_src.endsWith(new_src)) {
frame.src = new_src;
if(frame.contentWindow && frame.contentWindow.on_register_nodes)
frame.contentWindow.on_register_nodes();
}
// Show selected frame
$("#"+frame_name+"-frame").show();
$("#"+frame_name+"-frame").addClass("selected-frame");
$("#"+frame_name+"-menu").addClass("selected-menu");
// Load menu items of active frame
that.menu.updateFrameMenu(document.getElementById(frame_name+"-frame").contentWindow);
};
this.getActiveFrame = function() {
var frame = document.getElementById(getActiveFrameName()+"-frame");
if(frame) return frame.contentWindow;
else return window;
//else return undefined;
};
function getInterfaceFrameName(iface) {
for(var i in user_interfaces) {
var elem = user_interfaces[i];
if(elem.id == iface) return elem.id;
for(var j in elem.interfaces) {
if(elem.interfaces[j].id == iface) return elem.id;
}
}
};
function getInterfaceSrc(iface) {
for(var i in user_interfaces) {
var elem = user_interfaces[i];
if(elem.id == iface) return elem.src;
for(var j in elem.interfaces) {
if(elem.interfaces[j].id == iface) return elem.interfaces[j].src;
}
}
};
function getActiveFrameName() {
return getInterfaceFrameName(getActiveInterfaceName());
};
function getActiveInterfaceName() {
for(var i in user_interfaces_flat) {
if(urlQuery[user_interfaces_flat[i].id]) return user_interfaces_flat[i].id;
}
return "kb";
};
///////////////////////////////
//////////// URL Location
///////////////////////////////
function updateQueryString() {
urlQuery = {};
var query = String(window.location.hash.substring(1));
var vars = query.split("?");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (typeof urlQuery[pair[0]] === "undefined") {
// If first entry with this name
urlQuery[pair[0]] = decodeURIComponent(pair[1]);
}
else if (typeof urlQuery[pair[0]] === "string") {
// If second entry with this name
var arr = [ urlQuery[pair[0]],decodeURIComponent(pair[1]) ];
urlQuery[pair[0]] = arr;
}
else {
// If third or later entry with this name
urlQuery[pair[0]].push(decodeURIComponent(pair[1]));
}
}
};
this.updateLocation = function() {
updateQueryString();
showFrame(getActiveInterfaceName());
// update episode selection from URL query
// e.g., https://data.openease.org/#kb?category=foo?episode=bar
if(urlQuery['category'] && urlQuery['episode']) {
that.setEpisode(urlQuery['category'], urlQuery['episode']);
}
};
///////////////////////////////
//////////// Frame Overlay
///////////////////////////////
this.createOverlay = function() {
// Create page iosOverlay
var page = document.getElementById('page');
if(page) {
var pageOverlay = document.createElement("div");
pageOverlay.setAttribute("id", "page-overlay");
pageOverlay.className = "ui-ios-overlay ios-overlay-hide div-overlay";
pageOverlay.innerHTML += '<span class="title">Please select an Episode</span';
pageOverlay.style.display = 'none';
page.appendChild(pageOverlay);
var spinner = createSpinner();
pageOverlay.appendChild(spinner.el);
}
};
this.showPageOverlay = function(text) {
var pageOverlay = document.getElementById('page-overlay');
if(pageOverlay && !that.pageOverlayDisabled) {
pageOverlay.children[0].innerHTML = text;
pageOverlay.style.display = 'block';
pageOverlay.className = pageOverlay.className.replace("hide","show");
pageOverlay.style.pointerEvents = "auto";
that.pageOverlayDisabled = true;
}
};
this.hidePageOverlay = function() {
var pageOverlay = document.getElementById('page-overlay');
if(pageOverlay && that.pageOverlayDisabled) {
//pageOverlay.style.display = 'none';
pageOverlay.className = pageOverlay.className.replace("show","hide");
pageOverlay.style.pointerEvents = "none";
that.pageOverlayDisabled = false;
}
};
};