1818'use strict' ;
1919
2020let RosNode = require ( './RosNode.js' ) ;
21+ const messageUtils = require ( '../utils/message_utils.js' ) ;
2122
2223class NodeHandle {
2324 constructor ( node ) {
@@ -36,52 +37,96 @@ class NodeHandle {
3637// Pubs, Subs, Services
3738//------------------------------------------------------------------
3839 /**
39- * Create a ros publisher with the provided options
40- * @param options {object }
41- * @param options.topic {string} topic to publish on ('/chatter')
42- * @param options.type {string} type of message to publish ('std_msgs/String')
43- * @param options.latching {boolean} latch messages
44- * @param options.tpcNoDelay {boolean} set TCP no delay option on Socket
45- * @param options.queueSize {number} number of messages to queue when publishing
46- * @param options.throttleMs {number} milliseconds to throttle when publishing
40+ * Creates a ros publisher with the provided options
41+ * @param topic {string }
42+ * @param type {string|Object} string representing message type or instance
43+ * @param [ options] {object}
44+ * @param [ options.latching] {boolean} latch messages
45+ * @param [ options.tpcNoDelay] {boolean} set TCP no delay option on Socket
46+ * @param [ options.queueSize] {number} number of messages to queue when publishing
47+ * @param [ options.throttleMs] {number} milliseconds to throttle when publishing
4748 * @return {Publisher }
4849 */
49- advertise ( options ) {
50+ advertise ( topic , type , options ) {
51+ options = options || { } ;
52+ options . topic = topic ;
53+ if ( typeof type === 'string' || type instanceof String ) {
54+ options . type = type ;
55+ options . typeClass = messageUtils . getHandlerForMsgType ( type ) ;
56+ }
57+ else {
58+ options . typeClass = type ;
59+ options . type = type . datatype ( ) ;
60+ }
5061 return this . _node . advertise ( options ) ;
5162 }
5263
5364 /**
54- * Create a ros subscriber with the provided options
55- * @param options {object}
56- * @param options.topic {string} topic to publish on ('/chatter')
57- * @param options.type {string} type of message to publish ('std_msgs/String')
58- * @param options.queueSize {number} number of messages to queue when subscribing
59- * @param options.throttleMs {number} milliseconds to throttle when subscribing
65+ * Creates a ros subscriber with the provided options
66+ * @param topic {string}
67+ * @param type {string|Object} string representing message type or instance
68+ * @param callback {function} function to call when message is received
69+ * @param [options] {object}
70+ * @param [options.queueSize] {number} number of messages to queue when subscribing
71+ * @param [options.throttleMs] {number} milliseconds to throttle when subscribing
6072 * @return {Subscriber }
6173 */
62- subscribe ( options , callback ) {
74+ subscribe ( topic , type , callback , options ) {
75+ options = options || { } ;
76+ options . topic = topic ;
77+ if ( typeof type === 'string' || type instanceof String ) {
78+ options . type = type ;
79+ options . typeClass = messageUtils . getHandlerForMsgType ( type ) ;
80+ }
81+ else {
82+ options . typeClass = type ;
83+ options . type = type . datatype ( ) ;
84+ }
6385 return this . _node . subscribe ( options , callback ) ;
6486 }
6587
6688 /**
67- * Create a ros Service server with the provided options
68- * @param options {object}
69- * @param options.service {string} service to provide e.g ('/add_two_ints')
70- * @param options.type {string} type of service ('tutorial_msgs/AddTwoInts')
89+ * Creates a ros Service server with the provided options
90+ * @param service {string}
91+ * @param type {string|Object} string representing service type or instance
92+ * @param callback {function} function to call when this service is called
93+ * e.g.
94+ * (request, response) => {
95+ * response.data = !request.data;
96+ * return true;
97+ * }
7198 * @return {ServiceServer }
7299 */
73- advertiseService ( options , callback ) {
100+ advertiseService ( service , type , callback ) {
101+ let options = { service : service } ;
102+ if ( typeof type === 'string' || type instanceof String ) {
103+ options . type = type ;
104+ options . typeClass = messageUtils . getHandlerForSrvType ( type ) ;
105+ }
106+ else {
107+ options . typeClass = type ;
108+ // TODO: this is not terribly robust...
109+ options . type = type . Request . datatype ( ) . slice ( 'Request' . length ) ;
110+ }
74111 return this . _node . advertiseService ( options , callback ) ;
75112 }
76113
77114 /**
78- * Create a ros Service server with the provided options
79- * @param options {object}
80- * @param options.service {string} service to provide e.g ('/add_two_ints')
81- * @param options.type {string} type of service ('tutorial_msgs/AddTwoInts')
115+ * Creates a ros Service client with the provided options
116+ * @param service {string}
117+ * @param type {string|Object} string representing service type or instance
82118 * @return {ServiceClient }
83119 */
84- serviceClient ( options ) {
120+ serviceClient ( service , type ) {
121+ let options = { service : service } ;
122+ if ( typeof type === 'string' || type instanceof String ) {
123+ options . type = type ;
124+ options . typeClass = messageUtils . getHandlerForSrvType ( type ) ;
125+ }
126+ else {
127+ options . typeClass = type ;
128+ options . type = type . Request . datatype ( ) . slice ( 'Request' . length ) ;
129+ }
85130 return this . _node . serviceClient ( options ) ;
86131 }
87132
0 commit comments