Skip to content

Commit

Permalink
Merge pull request RobotWebTools#48 from OTL/add_service_fail_callback
Browse files Browse the repository at this point in the history
Add service fail callback
  • Loading branch information
rctoris committed Oct 7, 2013
2 parents a00fc0d + 791844a commit cc42fee
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
17 changes: 12 additions & 5 deletions build/roslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ ROSLIB.Ros.prototype.connect = function(url) {
if (message.op === 'publish') {
that.emit(message.topic, message.msg);
} else if (message.op === 'service_response') {
that.emit(message.id, message.values);
that.emit(message.id, message);
}
}

Expand Down Expand Up @@ -611,14 +611,21 @@ ROSLIB.Service = function(options) {
* @param request - the ROSLIB.ServiceRequest to send
* @param callback - function with params:
* * response - the response from the service request
* @param failedCallback - the callback function when the service call failed (optional)
*/
ROSLIB.Service.prototype.callService = function(request, callback) {
ROSLIB.Service.prototype.callService = function(request, callback, failedCallback) {
this.ros.idCounter++;
var serviceCallId = 'call_service:' + this.name + ':' + this.ros.idCounter;

this.ros.once(serviceCallId, function(data) {
var response = new ROSLIB.ServiceResponse(data);
callback(response);
this.ros.once(serviceCallId, function(message) {
if (message.result !== undefined && message.result === false) {
if (typeof failedCallback === 'function') {
failedCallback();
}
} else {
var response = new ROSLIB.ServiceResponse(message.values);
callback(response);
}
});

var requestValues = [];
Expand Down
2 changes: 1 addition & 1 deletion build/roslib.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/core/Ros.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ ROSLIB.Ros.prototype.connect = function(url) {
if (message.op === 'publish') {
that.emit(message.topic, message.msg);
} else if (message.op === 'service_response') {
that.emit(message.id, message.values);
that.emit(message.id, message);
}
}

Expand Down
15 changes: 11 additions & 4 deletions src/core/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@ ROSLIB.Service = function(options) {
* @param request - the ROSLIB.ServiceRequest to send
* @param callback - function with params:
* * response - the response from the service request
* @param failedCallback - the callback function when the service call failed (optional)
*/
ROSLIB.Service.prototype.callService = function(request, callback) {
ROSLIB.Service.prototype.callService = function(request, callback, failedCallback) {
this.ros.idCounter++;
var serviceCallId = 'call_service:' + this.name + ':' + this.ros.idCounter;

this.ros.once(serviceCallId, function(data) {
var response = new ROSLIB.ServiceResponse(data);
callback(response);
this.ros.once(serviceCallId, function(message) {
if (message.result !== undefined && message.result === false) {
if (typeof failedCallback === 'function') {
failedCallback();
}
} else {
var response = new ROSLIB.ServiceResponse(message.values);
callback(response);
}
});

var requestValues = [];
Expand Down

0 comments on commit cc42fee

Please sign in to comment.