Skip to content

Commit

Permalink
Merge pull request #142 from megawac/params
Browse files Browse the repository at this point in the history
Notification on param set/delete completion (+tests)
  • Loading branch information
rctoris committed Dec 15, 2014
2 parents 4397ebc + aa97383 commit a2e0148
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 17 deletions.
10 changes: 4 additions & 6 deletions src/core/Param.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Param.prototype.get = function(callback) {
*
* @param value - value to set param to.
*/
Param.prototype.set = function(value) {
Param.prototype.set = function(value, callback) {
var paramClient = new Service({
ros : this.ros,
name : '/rosapi/set_param',
Expand All @@ -59,14 +59,13 @@ Param.prototype.set = function(value) {
value : JSON.stringify(value)
});

paramClient.callService(request, function() {
});
paramClient.callService(request, callback);
};

/**
* Delete this parameter on the ROS server.
*/
Param.prototype.delete = function() {
Param.prototype.delete = function(callback) {
var paramClient = new Service({
ros : this.ros,
name : '/rosapi/delete_param',
Expand All @@ -77,8 +76,7 @@ Param.prototype.delete = function() {
name : this.name
});

paramClient.callService(request, function() {
});
paramClient.callService(request, callback);
};

module.exports = Param;
22 changes: 11 additions & 11 deletions src/core/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ function Service(options) {
* * error - the error message reported by ROS
*/
Service.prototype.callService = function(request, callback, failedCallback) {
this.ros.idCounter++;
var serviceCallId = 'call_service:' + this.name + ':' + this.ros.idCounter;
var serviceCallId = 'call_service:' + this.name + ':' + (++this.ros.idCounter);

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

var call = {
op : 'call_service',
Expand Down
57 changes: 57 additions & 0 deletions test/examples/params.examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var expect = require('chai').expect;
var ROSLIB = require('../..');

describe('Param setting', function() {
var ros = new ROSLIB.Ros({
url: 'ws://localhost:9090'
});
var param = ros.Param({
name: '/test/foo'
});

it('Param generics', function() {
expect(param).to.be.instanceOf(ROSLIB.Param);
expect(param.name).to.be.equal('/test/foo');
});

it('Param.set no callback', function(done) {
param.set('foo');
setTimeout(done, 500);
});

it('Param.get', function(done) {
param.get(function(result) {
expect(result).to.be.equal('foo');
done();
});
});

it('Param.set w/ callback', function(done) {
param.set('bar', function() {
done();
});
});

it('Param.get', function(done) {
param.get(function(result) {
expect(result).to.be.equal('bar');
done();
});
});

it('ros.getParams', function(done) {
ros.getParams(function(params) {
expect(params).to.include(param.name);
done();
});
});

it('Param.delete', function(done) {
param.delete(function() {
ros.getParams(function(params) {
expect(params).to.not.include(param.name);
done();
});
});
});
});

0 comments on commit a2e0148

Please sign in to comment.