@@ -26,121 +26,127 @@ var EventEmitter2 = require('eventemitter2').EventEmitter2;
2626 * @param {boolean } [options.omitStatus] - The flag to indicate whether to omit the status channel or not.
2727 * @param {boolean } [options.omitResult] - The flag to indicate whether to omit the result channel or not.
2828 */
29- function ActionClient ( options ) {
30- var that = this ;
31- options = options || { } ;
32- this . ros = options . ros ;
33- this . serverName = options . serverName ;
34- this . actionName = options . actionName ;
35- this . timeout = options . timeout ;
36- this . omitFeedback = options . omitFeedback ;
37- this . omitStatus = options . omitStatus ;
38- this . omitResult = options . omitResult ;
39- this . goals = { } ;
40-
41- // flag to check if a status has been received
42- var receivedStatus = false ;
43-
44- // create the topics associated with actionlib
45- this . feedbackListener = new Topic ( {
46- ros : this . ros ,
47- name : this . serverName + '/feedback' ,
48- messageType : this . actionName + 'Feedback'
49- } ) ;
50-
51- this . statusListener = new Topic ( {
52- ros : this . ros ,
53- name : this . serverName + '/status' ,
54- messageType : 'actionlib_msgs/GoalStatusArray'
55- } ) ;
56-
57- this . resultListener = new Topic ( {
58- ros : this . ros ,
59- name : this . serverName + '/result' ,
60- messageType : this . actionName + 'Result'
61- } ) ;
62-
63- this . goalTopic = new Topic ( {
64- ros : this . ros ,
65- name : this . serverName + '/goal' ,
66- messageType : this . actionName + 'Goal'
67- } ) ;
68-
69- this . cancelTopic = new Topic ( {
70- ros : this . ros ,
71- name : this . serverName + '/cancel' ,
72- messageType : 'actionlib_msgs/GoalID'
73- } ) ;
74-
75- // advertise the goal and cancel topics
76- this . goalTopic . advertise ( ) ;
77- this . cancelTopic . advertise ( ) ;
78-
79- // subscribe to the status topic
80- if ( ! this . omitStatus ) {
81- this . statusListener . subscribe ( function ( statusMessage ) {
82- receivedStatus = true ;
83- statusMessage . status_list . forEach ( function ( status ) {
84- var goal = that . goals [ status . goal_id . id ] ;
85- if ( goal ) {
86- goal . emit ( 'status' , status ) ;
87- }
88- } ) ;
29+ class ActionClient extends EventEmitter2 {
30+ constructor ( options ) {
31+ super ( options ) ;
32+ var that = this ;
33+ options = options || { } ;
34+ this . ros = options . ros ;
35+ this . serverName = options . serverName ;
36+ this . actionName = options . actionName ;
37+ this . timeout = options . timeout ;
38+ this . omitFeedback = options . omitFeedback ;
39+ this . omitStatus = options . omitStatus ;
40+ this . omitResult = options . omitResult ;
41+ this . goals = { } ;
42+
43+ // flag to check if a status has been received
44+ var receivedStatus = false ;
45+
46+ // create the topics associated with actionlib
47+ this . feedbackListener = new Topic ( {
48+ ros : this . ros ,
49+ name : this . serverName + "/feedback" ,
50+ messageType : this . actionName + "Feedback" ,
8951 } ) ;
90- }
9152
92- // subscribe the the feedback topic
93- if ( ! this . omitFeedback ) {
94- this . feedbackListener . subscribe ( function ( feedbackMessage ) {
95- var goal = that . goals [ feedbackMessage . status . goal_id . id ] ;
96- if ( goal ) {
97- goal . emit ( 'status' , feedbackMessage . status ) ;
98- goal . emit ( 'feedback' , feedbackMessage . feedback ) ;
99- }
53+ this . statusListener = new Topic ( {
54+ ros : this . ros ,
55+ name : this . serverName + "/status" ,
56+ messageType : "actionlib_msgs/GoalStatusArray" ,
10057 } ) ;
101- }
10258
103- // subscribe to the result topic
104- if ( ! this . omitResult ) {
105- this . resultListener . subscribe ( function ( resultMessage ) {
106- var goal = that . goals [ resultMessage . status . goal_id . id ] ;
59+ this . resultListener = new Topic ( {
60+ ros : this . ros ,
61+ name : this . serverName + "/result" ,
62+ messageType : this . actionName + "Result" ,
63+ } ) ;
10764
108- if ( goal ) {
109- goal . emit ( 'status' , resultMessage . status ) ;
110- goal . emit ( 'result' , resultMessage . result ) ;
111- }
65+ this . goalTopic = new Topic ( {
66+ ros : this . ros ,
67+ name : this . serverName + "/ goal" ,
68+ messageType : this . actionName + "Goal" ,
11269 } ) ;
113- }
11470
115- // If timeout specified, emit a 'timeout' event if the action server does not respond
116- if ( this . timeout ) {
117- setTimeout ( function ( ) {
118- if ( ! receivedStatus ) {
119- that . emit ( 'timeout' ) ;
120- }
121- } , this . timeout ) ;
122- }
123- }
71+ this . cancelTopic = new Topic ( {
72+ ros : this . ros ,
73+ name : this . serverName + "/cancel" ,
74+ messageType : "actionlib_msgs/GoalID" ,
75+ } ) ;
12476
125- ActionClient . prototype . __proto__ = EventEmitter2 . prototype ;
77+ // advertise the goal and cancel topics
78+ this . goalTopic . advertise ( ) ;
79+ this . cancelTopic . advertise ( ) ;
80+
81+ // subscribe to the status topic
82+ if ( ! this . omitStatus ) {
83+ this . statusListener . subscribe ( function ( statusMessage ) {
84+ receivedStatus = true ;
85+ statusMessage . status_list . forEach ( function ( status ) {
86+ var goal = that . goals [ status . goal_id . id ] ;
87+ if ( goal ) {
88+ goal . emit ( "status" , status ) ;
89+ }
90+ } ) ;
91+ } ) ;
92+ }
12693
127- /**
128- * Cancel all goals associated with this ActionClient.
129- */
130- ActionClient . prototype . cancel = function ( ) {
131- var cancelMessage = new Message ( ) ;
132- this . cancelTopic . publish ( cancelMessage ) ;
133- } ;
94+ // subscribe the the feedback topic
95+ if ( ! this . omitFeedback ) {
96+ this . feedbackListener . subscribe ( function ( feedbackMessage ) {
97+ var goal = that . goals [ feedbackMessage . status . goal_id . id ] ;
98+ if ( goal ) {
99+ goal . emit ( "status" , feedbackMessage . status ) ;
100+ goal . emit ( "feedback" , feedbackMessage . feedback ) ;
101+ }
102+ } ) ;
103+ }
104+
105+ // subscribe to the result topic
106+ if ( ! this . omitResult ) {
107+ this . resultListener . subscribe ( function ( resultMessage ) {
108+ var goal = that . goals [ resultMessage . status . goal_id . id ] ;
109+
110+ if ( goal ) {
111+ goal . emit ( "status" , resultMessage . status ) ;
112+ goal . emit ( "result" , resultMessage . result ) ;
113+ }
114+ } ) ;
115+ }
116+
117+ // If timeout specified, emit a 'timeout' event if the action server does not respond
118+ if ( this . timeout ) {
119+ setTimeout ( function ( ) {
120+ if ( ! receivedStatus ) {
121+ that . emit ( "timeout" ) ;
122+ }
123+ } , this . timeout ) ;
124+ }
125+ }
126+ /**
127+ * Cancel all goals associated with this ActionClient.
128+ */
129+ cancel ( ) {
130+ var cancelMessage = new Message ( ) ;
131+ this . cancelTopic . publish ( cancelMessage ) ;
132+ }
133+ /**
134+ * Unsubscribe and unadvertise all topics associated with this ActionClient.
135+ */
136+ dispose ( ) {
137+ this . goalTopic . unadvertise ( ) ;
138+ this . cancelTopic . unadvertise ( ) ;
139+ if ( ! this . omitStatus ) {
140+ this . statusListener . unsubscribe ( ) ;
141+ }
142+ if ( ! this . omitFeedback ) {
143+ this . feedbackListener . unsubscribe ( ) ;
144+ }
145+ if ( ! this . omitResult ) {
146+ this . resultListener . unsubscribe ( ) ;
147+ }
148+ }
149+ }
134150
135- /**
136- * Unsubscribe and unadvertise all topics associated with this ActionClient.
137- */
138- ActionClient . prototype . dispose = function ( ) {
139- this . goalTopic . unadvertise ( ) ;
140- this . cancelTopic . unadvertise ( ) ;
141- if ( ! this . omitStatus ) { this . statusListener . unsubscribe ( ) ; }
142- if ( ! this . omitFeedback ) { this . feedbackListener . unsubscribe ( ) ; }
143- if ( ! this . omitResult ) { this . resultListener . unsubscribe ( ) ; }
144- } ;
145151
146152module . exports = ActionClient ;
0 commit comments