Skip to content

Commit d2bfac8

Browse files
authored
Merge pull request #30 from chfritz/get_system_state
Implemented getSystemState and added example
2 parents e5668f7 + 1df3a6c commit d2bfac8

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

example_turtle.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ rosnodejs.initNode('/my_node', {
2020
services: ["turtlesim/TeleportRelative"]
2121
}).then((rosNode) => {
2222

23+
// get list of existing publishers, subscribers, and services
24+
rosNode._node._masterApi.getSystemState("/my_node").then((data) => {
25+
console.log("getSystemState, result", data, data.publishers[0]);
26+
});
27+
2328
// ---------------------------------------------------------
2429
// Service Call
2530

lib/MasterApiClient.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,23 @@ class MasterApiClient {
126126
throw new Error('NOT SUPPORTED');
127127
}
128128

129+
/** return an object containing all current publishers (by topic),
130+
subscribers (by topic), and services (by name) */
129131
getSystemState(callerId) {
130-
throw new Error('NOT SUPPORTED');
132+
function toObject(memo, sublist) {
133+
memo[sublist[0]] = sublist[1];
134+
return memo;
135+
}
136+
let data = [callerId];
137+
return new Promise((resolve, reject) => {
138+
this._call('getSystemState', data, function(data) {
139+
return resolve({
140+
publishers: data[2][0].reduce(toObject, {}),
141+
subscribers: data[2][1].reduce(toObject, {}),
142+
services: data[2][2].reduce(toObject, {})
143+
});
144+
}, reject);
145+
});
131146
}
132147

133148
getUri(callerId) {

0 commit comments

Comments
 (0)