Skip to content

Commit cc42fee

Browse files
author
Russell Toris
committed
Merge pull request #48 from OTL/add_service_fail_callback
Add service fail callback
2 parents a00fc0d + 791844a commit cc42fee

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

build/roslib.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ ROSLIB.Ros.prototype.connect = function(url) {
432432
if (message.op === 'publish') {
433433
that.emit(message.topic, message.msg);
434434
} else if (message.op === 'service_response') {
435-
that.emit(message.id, message.values);
435+
that.emit(message.id, message);
436436
}
437437
}
438438

@@ -611,14 +611,21 @@ ROSLIB.Service = function(options) {
611611
* @param request - the ROSLIB.ServiceRequest to send
612612
* @param callback - function with params:
613613
* * response - the response from the service request
614+
* @param failedCallback - the callback function when the service call failed (optional)
614615
*/
615-
ROSLIB.Service.prototype.callService = function(request, callback) {
616+
ROSLIB.Service.prototype.callService = function(request, callback, failedCallback) {
616617
this.ros.idCounter++;
617618
var serviceCallId = 'call_service:' + this.name + ':' + this.ros.idCounter;
618619

619-
this.ros.once(serviceCallId, function(data) {
620-
var response = new ROSLIB.ServiceResponse(data);
621-
callback(response);
620+
this.ros.once(serviceCallId, function(message) {
621+
if (message.result !== undefined && message.result === false) {
622+
if (typeof failedCallback === 'function') {
623+
failedCallback();
624+
}
625+
} else {
626+
var response = new ROSLIB.ServiceResponse(message.values);
627+
callback(response);
628+
}
622629
});
623630

624631
var requestValues = [];

build/roslib.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/Ros.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ ROSLIB.Ros.prototype.connect = function(url) {
118118
if (message.op === 'publish') {
119119
that.emit(message.topic, message.msg);
120120
} else if (message.op === 'service_response') {
121-
that.emit(message.id, message.values);
121+
that.emit(message.id, message);
122122
}
123123
}
124124

src/core/Service.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@ ROSLIB.Service = function(options) {
2424
* @param request - the ROSLIB.ServiceRequest to send
2525
* @param callback - function with params:
2626
* * response - the response from the service request
27+
* @param failedCallback - the callback function when the service call failed (optional)
2728
*/
28-
ROSLIB.Service.prototype.callService = function(request, callback) {
29+
ROSLIB.Service.prototype.callService = function(request, callback, failedCallback) {
2930
this.ros.idCounter++;
3031
var serviceCallId = 'call_service:' + this.name + ':' + this.ros.idCounter;
3132

32-
this.ros.once(serviceCallId, function(data) {
33-
var response = new ROSLIB.ServiceResponse(data);
34-
callback(response);
33+
this.ros.once(serviceCallId, function(message) {
34+
if (message.result !== undefined && message.result === false) {
35+
if (typeof failedCallback === 'function') {
36+
failedCallback();
37+
}
38+
} else {
39+
var response = new ROSLIB.ServiceResponse(message.values);
40+
callback(response);
41+
}
3542
});
3643

3744
var requestValues = [];

0 commit comments

Comments
 (0)