Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification on param set/delete completion (+tests) #142

Merged
merged 3 commits into from
Dec 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});
});