Hello,
We are currently working on creating a dashboard for our robot.
We are trying to get all the contents of a NetworkTable using this code:
public List<NetworkTableEntry> getAllEntries() {
List<NetworkTableEntry> entries = new ArrayList<>();
for (Topic key : this.table.getTopics()) {
System.out.println(key.getName());
}
return entries;
}
But for some reason, I does not return all of the entries in the table.
Why doesn’t it work? and what should we do to fix it?
NT4 is pub/sub. By default clients don’t subscribe to everything. If you want to see all topics, you have to subscribe to them (eg using SubscribeMultiple / MultiSubscriber). You can choose to just get the topic announcements and not the data by specifying the topics only option; this is recommended to save bandwidth, and then you only should subscribe to data values you’re actually going to display.
Note also that NT is asynchronous, so if you’re doing this immediately after the subscribe or right at startup, it may not have finished synchronizing with the server yet.
How can I get the topic announcements?
There’s a few different ways. If you want to see the actual announcements as they arrive, you can add a listener to publish/unpublish events (and poll that listener periodically or have it notify you on a separate thread, whichever works best for your architecture).
Could you please write how that looks in code? I don’t understand how to implement it
See Listening for Changes — FIRST Robotics Competition documentation (wpilib.org)
Specifically the topicListenerHandle
part:
// add a listener to see when new topics are published within datatable
// the string array is an array of topic name prefixes.
topicListenerHandle = inst.addListener(
new String[] { datatable.getPath() + "/" },
EnumSet.of(NetworkTableEvent.Kind.kTopic),
event -> {
if (event.is(NetworkTableEvent.Kind.kPublish)) {
// topicInfo.name is the full topic name, e.g. "/datatable/X"
System.out.println("newly published " + event.topicInfo.name);
}
});