us.ihmc.ros2.ROS2Callback Maven / Gradle / Ivy
The newest version!
package us.ihmc.ros2;
import us.ihmc.log.LogTools;
import us.ihmc.pubsub.subscriber.Subscriber;
import java.util.function.Consumer;
/**
* Callback listener to non-null reception of a message on a ROS 2 topic.
*
* @param messageType
* @deprecated Use {@link ROS2NodeInterface#createSubscription2} instead
*/
@Deprecated
public class ROS2Callback
{
private final Consumer messageCallback;
private final ROS2Subscription subscription;
public ROS2Callback(ROS2NodeInterface ros2Node, ROS2Topic topic, Consumer messageCallback)
{
this(ros2Node, topic.getType(), topic.getName(), topic.getQoS(), messageCallback);
}
public ROS2Callback(ROS2NodeInterface ros2Node, Class messageType, ROS2Topic> topic, Consumer messageCallback)
{
this(ros2Node, messageType, topic.withTypeName(messageType).toString(), topic.getQoS(), messageCallback);
}
public ROS2Callback(ROS2NodeInterface ros2Node, Class messageType, String topicName, ROS2QosProfile qosProfile, Consumer messageCallback)
{
this.messageCallback = messageCallback;
subscription = ros2Node.createSubscription(ROS2TopicNameTools.newMessageTopicDataTypeInstance(messageType),
this::nullOmissionCallback,
topicName,
qosProfile);
}
private void nullOmissionCallback(Subscriber subscriber)
{
T incomingData = subscriber.takeNextData();
if (incomingData != null)
{
messageCallback.accept(incomingData);
}
else
{
LogTools.warn("Received null from takeNextData()");
}
}
public void destroy()
{
if (subscription != null)
{
subscription.remove();
}
}
}