Compare commits

..

3 Commits

Author SHA1 Message Date
LiXiaoqi
ba2369623b Merge branch 'main' of http://git.mcxa.cn:88/LiXiaoqi/roslib_dart 2024-11-21 14:35:01 +08:00
LiXiaoqi
f831233c09 topic 2024-11-21 14:34:17 +08:00
LiXiaoqi
b2c3e6a743 topic 2024-11-21 14:32:20 +08:00

View File

@ -119,33 +119,43 @@ class Topic {
}
}
/// Unsubscribe from the topic.
Future<void> unsubscribe() async {
if (subscribeId != null) {
// Send the request and reset the subscription variables.
await safeSend(Request(
op: 'unsubscribe',
id: subscribeId,
topic: name,
));
await safeSend(unsubscribeReq());
// await ros.requestUnsubscribe(id);
subscription = null;
subscribeId = null;
}
}
Request unsubscribeReq() {
return Request(
op: 'unsubscribe',
id: subscribeId,
topic: name,
);
}
/// Publish a [message] to the topic.
Future<void> publish(dynamic message) async {
// Advertise the topic and then send the publish request.
await advertise();
publishId = ros.requestPublisher(name);
await safeSend(Request(
await safeSend(publishReq(message));
}
Request publishReq(dynamic message) {
return Request(
op: 'publish',
topic: name,
id: publishId,
msg: message,
latch: latch,
));
);
}
/// Advertise the topic.
@ -153,17 +163,21 @@ class Topic {
if (!isAdvertised) {
// Send the advertisement request.
advertiseId = ros.requestAdvertiser(name);
await safeSend(Request(
await safeSend(advertiseReq());
// If the ROS connection closes show that we're not advertising anymore.
watchForClose();
}
}
Request advertiseReq() {
return Request(
op: 'advertise',
id: advertiseId,
type: type,
topic: name,
latch: latch,
queueSize: queueSize,
));
// If the ROS connection closes show that we're not advertising anymore.
watchForClose();
}
);
}
/// Wait for the connection to close and then reset advertising variables.
@ -187,6 +201,8 @@ class Topic {
}
}
/// Safely send a [message] to ROS.
Future<void> safeSend(Request message) async {
// Send the message but if we're not connected and the [reconnectOnClose] flag