Skip to content

Commit 98ab37c

Browse files
committed
Auto-refactor protos to classes via IntelliSense
1 parent 29f0d35 commit 98ab37c

26 files changed

+2126
-2031
lines changed

src/actionlib/ActionClient.js

Lines changed: 112 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -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

146152
module.exports = ActionClient;

src/actionlib/ActionListener.js

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,62 +22,61 @@ var EventEmitter2 = require('eventemitter2').EventEmitter2;
2222
* @param {string} options.serverName - The action server name, like '/fibonacci'.
2323
* @param {string} options.actionName - The action message name, like 'actionlib_tutorials/FibonacciAction'.
2424
*/
25-
function ActionListener(options) {
26-
var that = this;
27-
options = options || {};
28-
this.ros = options.ros;
29-
this.serverName = options.serverName;
30-
this.actionName = options.actionName;
25+
class ActionListener extends EventEmitter2 {
26+
constructor(options) {
27+
super(options);
28+
var that = this;
29+
options = options || {};
30+
this.ros = options.ros;
31+
this.serverName = options.serverName;
32+
this.actionName = options.actionName;
3133

34+
// create the topics associated with actionlib
35+
var goalListener = new Topic({
36+
ros: this.ros,
37+
name: this.serverName + "/goal",
38+
messageType: this.actionName + "Goal",
39+
});
3240

33-
// create the topics associated with actionlib
34-
var goalListener = new Topic({
35-
ros : this.ros,
36-
name : this.serverName + '/goal',
37-
messageType : this.actionName + 'Goal'
38-
});
41+
var feedbackListener = new Topic({
42+
ros: this.ros,
43+
name: this.serverName + "/feedback",
44+
messageType: this.actionName + "Feedback",
45+
});
3946

40-
var feedbackListener = new Topic({
41-
ros : this.ros,
42-
name : this.serverName + '/feedback',
43-
messageType : this.actionName + 'Feedback'
44-
});
47+
var statusListener = new Topic({
48+
ros: this.ros,
49+
name: this.serverName + "/status",
50+
messageType: "actionlib_msgs/GoalStatusArray",
51+
});
4552

46-
var statusListener = new Topic({
47-
ros : this.ros,
48-
name : this.serverName + '/status',
49-
messageType : 'actionlib_msgs/GoalStatusArray'
50-
});
53+
var resultListener = new Topic({
54+
ros: this.ros,
55+
name: this.serverName + "/result",
56+
messageType: this.actionName + "Result",
57+
});
5158

52-
var resultListener = new Topic({
53-
ros : this.ros,
54-
name : this.serverName + '/result',
55-
messageType : this.actionName + 'Result'
56-
});
59+
goalListener.subscribe(function (goalMessage) {
60+
that.emit("goal", goalMessage);
61+
});
5762

58-
goalListener.subscribe(function(goalMessage) {
59-
that.emit('goal', goalMessage);
60-
});
61-
62-
statusListener.subscribe(function(statusMessage) {
63-
statusMessage.status_list.forEach(function(status) {
64-
that.emit('status', status);
63+
statusListener.subscribe(function (statusMessage) {
64+
statusMessage.status_list.forEach(function (status) {
65+
that.emit("status", status);
6566
});
66-
});
67-
68-
feedbackListener.subscribe(function(feedbackMessage) {
69-
that.emit('status', feedbackMessage.status);
70-
that.emit('feedback', feedbackMessage.feedback);
71-
});
67+
});
7268

73-
// subscribe to the result topic
74-
resultListener.subscribe(function(resultMessage) {
75-
that.emit('status', resultMessage.status);
76-
that.emit('result', resultMessage.result);
77-
});
69+
feedbackListener.subscribe(function (feedbackMessage) {
70+
that.emit("status", feedbackMessage.status);
71+
that.emit("feedback", feedbackMessage.feedback);
72+
});
7873

74+
// subscribe to the result topic
75+
resultListener.subscribe(function (resultMessage) {
76+
that.emit("status", resultMessage.status);
77+
that.emit("result", resultMessage.result);
78+
});
79+
}
7980
}
8081

81-
ActionListener.prototype.__proto__ = EventEmitter2.prototype;
82-
8382
module.exports = ActionListener;

0 commit comments

Comments
 (0)