diff --git a/rocon_web_common/www/example/listener.html b/rocon_web_common/www/example/listener.html index 184849b..096e5cc 100644 --- a/rocon_web_common/www/example/listener.html +++ b/rocon_web_common/www/example/listener.html @@ -8,18 +8,18 @@ - + - +

Rocon Interactions Listener Demo

Check your Web Console for Information(hit F12 in google chrome).

Info

diff --git a/rocon_web_remocon/www/index.html b/rocon_web_remocon/www/index.html index a54bcfc..de6a995 100644 --- a/rocon_web_remocon/www/index.html +++ b/rocon_web_remocon/www/index.html @@ -125,8 +125,14 @@

Description

- - +
+ + +
+

+

+ +
diff --git a/rocon_web_remocon/www/index.js b/rocon_web_remocon/www/index.js index cc24bd4..337f181 100644 --- a/rocon_web_remocon/www/index.js +++ b/rocon_web_remocon/www/index.js @@ -9,6 +9,7 @@ var gListRoles = []; var gListInteractions = []; var gFinalUrl; var gFinalHash; +var gFinalIsPairedType; var gUrl; var gCookieCount; @@ -18,6 +19,7 @@ var defaultUrl="ws://192.168.10.32:9090"; //Remocon profile var gPublishers = {} +var gSubscribers = {} var gRunningInteractions = []; var gRoconVersion = 'acdc'; //todo make rocon/version.js fot obtaining var gRemoconUUID = uuid().replace(/-/g,''); @@ -31,6 +33,7 @@ var gRemoconPlatformInfo = { 'data': [] } }; +var gPairing = null; // Starts here $(document).ready(function () { @@ -40,7 +43,9 @@ $(document).ready(function () { addUrl(); deleteUrl(); listItemSelect(); - startApp(); + startInteraction(); + stopInteraction(); + stopAllInteractions(); getBrowser(); if(defaultUrl != undefined) { @@ -49,6 +54,26 @@ $(document).ready(function () { } }); +/** + * Browser Close event + * + * @function initPublisher +*/ + +window.onbeforeunload = function(e){ + var ret_message = ""; + var RunningInteractions = $.extend([] , gRunningInteractions); //deep copy + if(RunningInteractions.length > 0){ + ret_message = RunningInteractions.length + " interactions running. Are you really shutdown?" + for (var i = 0 ; i < RunningInteractions.length ; i ++){ + stopInteractions(RunningInteractions[i].interaction_hash); + } + } + else{ + ret_message = null; + } + return ret_message; +} /** * Initialize ros publishers for sending data @@ -65,6 +90,33 @@ function initPublisher(){ }); } +/** + * Initialize ros subscribers for receiving data + * + * @function initSubscriber +*/ + +function initSubscriber(){ + ros.getTopicsForType('rocon_interaction_msgs/Pair', function(topic_name){ + if (topic_name !== undefined && topic_name.length > 0){ + gSubscribers['pairing_status'] = new ROSLIB.Topic({ + ros : ros, + name : topic_name[0], + messageType : 'rocon_interaction_msgs/Pair', + }); + gSubscribers['pairing_status'].subscribe(function(msg){ + if(gPairing !== null){ + if (msg.rapp.length === 0 && msg.remocon === gRemoconName){ + stopInteractions(gPairing); + } + } + }); + } + }); +} + + + /** * Publish remocon status * @@ -75,12 +127,10 @@ function publishRemoconStatus(){ //todo //getting running interactions how to get interaction info? var runningInteractionHashs = []; - for (i = 0 ; i < gRunningInteractions.length ; i ++){ + for (var i = 0 ; i < gRunningInteractions.length ; i ++){ var hash = gRunningInteractions[i]['interaction_hash']; runningInteractionHashs.push(hash) } - console.log('runningInteractionHashs: ', runningInteractionHashs); - var remocon_status = { 'platform_info' : gRemoconPlatformInfo, 'uuid' : gRemoconUUID, @@ -125,6 +175,7 @@ function setROSCallbacks() { $("#connectBtn").hide(); $("#disconnectBtn").show(); initPublisher(); + initSubscriber(); initList(); displayMasterInfo(); getRoles(); @@ -193,7 +244,6 @@ function connect() { if (newUrl.search(":") < 0) { newUrl += ":9090"; } - console.log(newUrl); ros.connect('ws://' + newUrl); }); @@ -280,13 +330,12 @@ function displayMasterInfo() { $("#selecturl").hide(); $("#masterinfo").show(); ros.getTopicsForType("rocon_std_msgs/MasterInfo", function(topic_name){ - console.log(topic_name); - if(topic_name !== undefined || topic_name.length > 0){ + if(topic_name !== undefined && topic_name.length > 0){ subscribeTopic(ros, topic_name[0], "rocon_std_msgs/MasterInfo", function(message) { - $("#masterinfopanel").append('

Red dot

'); - $("#masterinfopanel").append('

   name : ' + message["name"] +'

'); + $("#masterinfopanel").append('

Red dot

'); + $("#masterinfopanel").append('

   name : ' + message.name +'

'); $("#masterinfopanel").append('

   master_url : ' + gUrl +'

'); - $("#masterinfopanel").append('

   description : ' + message["description"] +'

'); + $("#masterinfopanel").append('

   description : ' + message.description +'

'); }); } }); @@ -304,7 +353,7 @@ function getRoles() { uri : 'rocon:/*/*/*/' + browser }); ros.getServicesForType('rocon_interaction_msgs/GetRoles', function(service_name){ - if (service_name !== undefined || service_name.length > 0){ + if (service_name !== undefined && service_name.length > 0){ callService(ros, service_name[0], 'rocon_interaction_msgs/GetRoles', request, function(result) { for (var i = 0; i < result.roles.length; i++) { gListRoles.push(result.roles[i]); @@ -348,10 +397,14 @@ function getInteractions(selectedRole) { uri : 'rocon:/*/*/*/' + browser }); ros.getServicesForType('rocon_interaction_msgs/GetInteractions', function(service_name){ - if (service_name !== undefined || service_name.length > 0){ + if (service_name !== undefined && service_name.length > 0){ callService(ros, service_name[0], 'rocon_interaction_msgs/GetInteractions', request, function(result) { for (var i = 0; i < result.interactions.length; i++) { - gListInteractions.push(result.interactions[i]); + var interaction = result.interactions[i]; + if (interaction.hasOwnProperty('pairing')){ + interaction['is_paired_type'] = true; + } + gListInteractions.push(interaction); } displayInteractions(); }); @@ -442,7 +495,15 @@ function prepareWebappUrl(interaction, baseUrl) { * @param {interaction} interaction */ function displayDescription(interaction) { - $("#startappBtn").show(); + $("#startInteractionBtn").show(); + $("#stopInteractionBtn").show(); + if (checkIsRunningInteraction(interaction) === false){ + $("#stopInteractionBtn").attr('disabled',true); + } + else{ + $("#stopInteractionBtn").attr('disabled',false); + } + stopAllInteractionsBtnCtrl(); $("#descriptionpanel").append('

name : ' + interaction["name"] + '


'); $("#descriptionpanel").append('

display_name : ' + interaction["display_name"] + '

'); @@ -461,6 +522,23 @@ function displayDescription(interaction) { $("#descriptionpanel").append('

parameters : ' + interaction["parameters"] + '

'); } +/** + * Event function when item in role list and interaction list is clicked + * + * @function listItemSelect +*/ +function checkIsRunningInteraction(interaction){ + var targetHash = interaction.hash; + var isRunning = false; + for (var i = 0 ; i < gRunningInteractions.length ; i ++){ + if (gRunningInteractions[i].interaction_hash === targetHash){ + isRunning = true; + break; + } + } + return isRunning; +} + /** * Event function when item in role list and interaction list is clicked * @@ -499,56 +577,145 @@ function listItemSelect() { var index = $(this).attr('id').charAt($(this).attr('id').length - 1); gFinalUrl = classifyInteraction(gListInteractions[index]); gFinalHash = gListInteractions[index].hash; + gFinalIsPairedType = gListInteractions[index].is_paired_type; displayDescription(gListInteractions[index]); }); } /** * Check whether a new window is closed or not every time. - * If it is closed, the check function is also stopped. + * When it is closed, the check function is also stopped. * * @function checkRunningInteraction */ function checkRunningInteraction (window_handler, window_key){ if (window_handler.closed === true){ - for (i = 0 ; i < gRunningInteractions.length ; i ++){ + for (var i = 0 ; i < gRunningInteractions.length ; i ++){ if (gRunningInteractions[i].hasOwnProperty(window_key) === true){ clearInterval(gRunningInteractions[i][window_key]); gRunningInteractions.splice(i, 1); publishRemoconStatus(); - } } } } /** - * Event function when 'Start App' button is clicked + * Event function when 'Start Interaction' button is clicked * - * @function startApp + * @function startInteraction */ -function startApp() { - $("#startappBtn").hide(); - $("#startappBtn").click(function () { +function startInteraction() { + $("#startInteractionBtn").click(function () { var finalUrl = gFinalUrl; var finalHash = gFinalHash; var runningInteraction = {} var id = uuid(); + var request = new ROSLIB.ServiceRequest({ + remocon : gRemoconName, + hash : finalHash + }); + ros.getServicesForType('rocon_interaction_msgs/RequestInteraction', function(service_name){ + if (service_name !== undefined && service_name.length > 0){ + callService(ros, service_name[0], 'rocon_interaction_msgs/RequestInteraction', request, function(result){ + if (result.error_code === 0){ //https://raw.githubusercontent.com/robotics-in-concert/rocon_msgs/indigo/rocon_app_manager_msgs/msg/ErrorCodes.msg + (function(){ + if (finalUrl !== null){ + var new_window = window.open(finalUrl); + runningInteraction['window_handler'] = new_window; + runningInteraction[id] = setInterval(function(){ + checkRunningInteraction(new_window, id); + }, 1000); + } + runningInteraction['interaction_hash'] = finalHash; + gRunningInteractions.push(runningInteraction); + publishRemoconStatus(); + if (gFinalIsPairedType === true){ + gPairing = finalHash; + } + })(); + //button ctrl + $("#stopInteractionBtn").attr('disabled',false); + stopAllInteractionsBtnCtrl(); + } + else{ + alert('interaction request rejected [' + result.message + ']'); + } + }); + } + }); + }); +} - if (finalUrl == null) { - alert("not available on this platform"); - return; +/** + * Event function when 'Stop Interaction' button is clicked + * + * @function stopInteraction +*/ +function stopInteraction() { + $("#stopInteractionBtn").click(function () { + var finalHash = gFinalHash; + stopInteractions(finalHash); + $("#stopInteractionBtn").attr('disabled',true); + }); + +} + +/** + * Event function when 'Stop All Interactions' button is clicked + * + * @function stopAllInteractions +*/ +function stopAllInteractions() { + $("#stopAllInteractionsBtn").click(function () { + var RunningInteractions = $.extend([] , gRunningInteractions); //deep copy + for (var i = 0 ; i < RunningInteractions.length ; i ++){ + stopInteractions(RunningInteractions[i].interaction_hash); } - var new_window = window.open(finalUrl); - runningInteraction['interaction_hash'] = finalHash; - runningInteraction[id] = setInterval(function(){ - checkRunningInteraction(new_window, id); - }, 1000); - gRunningInteractions.push(runningInteraction); - publishRemoconStatus(); + $("#stopInteractionBtn").attr('disabled',true); }); } +/** + * Stop interaction with interaction hash. + * + * @function stopInteractions +*/ +function stopAllInteractionsBtnCtrl(){ + if(gRunningInteractions.length > 0){ + $("#stopAllInteractionsBtn").attr('disabled', false); + } + else{ + $("#stopAllInteractionsBtn").attr('disabled', true); + $("#stopInteractionBtn").attr('disabled', true); + } +} + +/** + * Stop interaction with interaction hash. + * + * @function stopInteractions +*/ + +function stopInteractions(interactionHash) { + for (var i = 0 ; i < gRunningInteractions.length ; i ++){ + if (gRunningInteractions[i].interaction_hash === interactionHash){ + if (gRunningInteractions[i].hasOwnProperty('window_handler') === true){ + var window_handler = gRunningInteractions[i].window_handler; + if (window_handler.closed === false){ + window_handler.close(); + } + } + gRunningInteractions.splice(i, 1); + publishRemoconStatus(); + if(gPairing !== null){ + gPairing = null; + } + } + } + stopAllInteractionsBtnCtrl(); +} + /** * Initialize all lists * @@ -589,7 +756,8 @@ function initRoleList() { function initInteractionList() { gListInteractions = []; $("#interactions_listgroup").children().remove(); - $("#startappBtn").hide(); + $("#startInteractionBtn").hide(); + $("#stopInteractionBtn").hide(); } /** @@ -599,7 +767,8 @@ function initInteractionList() { */ function initDescriptionList() { $("#descriptionpanel").children().remove(); - $("#startappBtn").hide(); + $("#startInteractionBtn").hide(); + $("#stopInteractionBtn").hide(); } /**