


///////////////////////////////////////////////////////////////////////////////////
// Publishers
///////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////
// Topics
////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////
// Subscribers
////////////////////////////////////////////////////////////////

// Operation Mode
var modeStatus = new ROSLIB.Topic({
  ros : ros,
  name : '/aescape/mode/status',
  messageType : 'std_msgs/String'
});

modeStatus.subscribe(function(message) {
  document.getElementById("stoppedModeButton").className = "btn btn-secondary"
  document.getElementById("standbyModeButton").className = "btn btn-primary"
  document.getElementById("teachingModeButton").className = "btn btn-primary"
  document.getElementById("executionModeButton").className = "btn btn-primary"


  if (message.data === "stopped") {
    document.getElementById("stoppedModeButton").className = "btn btn-warning"
  } else if (message.data === "standby")
  {
    document.getElementById("standbyModeButton").className = "btn btn-primary btn-success"
  } else if (message.data === "teach")
  {
    document.getElementById("teachingModeButton").className = "btn btn-primary btn-success"
  } else if (message.data === "execution")
  {
    document.getElementById("executionModeButton").className = "btn btn-primary btn-success"
  }
});

// Playing Bag
var bagPlayingTopic = new ROSLIB.Topic({
  ros : ros,
  name : '/aescape/bags/playing',
  messageType : 'std_msgs/String'
});

bagPlayingTopic.subscribe(function(message) {
  document.getElementById("bagPlayingText").innerHTML = message.data
});



////////////////////////////////////////////////////////////////
// Services
////////////////////////////////////////////////////////////////


function triggerService(serviceName)
{
  var service = new ROSLIB.Service({
    ros : ros,
    name : serviceName,
    serviceType : 'std_srvs/Trigger'
  });
  
  var request = new ROSLIB.ServiceRequest({});
  
  service.callService(request, function(result) {
    console.log('Result for service call on '
      + serviceName
      + ': '
      + result.sum);
  });
}


function triggerMessageService(serviceName, textInput)
{
  var text = document.getElementById(textInput).value

  var service = new ROSLIB.Service({
    ros : ros,
    name : serviceName,
    serviceType : 'demobot.TriggerMessage'
  });
  
  var request = new ROSLIB.ServiceRequest({
    message : text
  });
  
  service.callService(request, function(result) {
    console.log('Result for service call on '
      + serviceName
      + ': '
      + result.sum);
  });
}