Skip to content

Commit

Permalink
add metadata to all dashboard ".type" entries
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue committed Jul 2, 2024
1 parent 1ccd8d1 commit 8993ac8
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 22 deletions.
16 changes: 13 additions & 3 deletions wpilibc/src/main/native/cpp/DriverStation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <wpi/DataLog.h>
#include <wpi/EventVector.h>
#include <wpi/condition_variable.h>
#include <wpi/json.h>
#include <wpi/mutex.h>
#include <wpi/timestamp.h>

Expand All @@ -45,8 +46,11 @@ class MatchDataSenderEntry {
public:
MatchDataSenderEntry(const std::shared_ptr<nt::NetworkTable>& table,
std::string_view key,
typename Topic::ParamType initialVal)
: publisher{Topic{table->GetTopic(key)}.Publish()}, prevVal{initialVal} {
typename Topic::ParamType initialVal,
wpi::json topicProperties = {{}})
: publisher{Topic{table->GetTopic(key)}.PublishEx(Topic::kTypeString,
topicProperties)},
prevVal{initialVal} {
publisher.Set(initialVal);
}

Expand All @@ -62,10 +66,16 @@ class MatchDataSenderEntry {
typename Topic::ValueType prevVal;
};

static constexpr std::string_view kSmartDashboardType = "FMSInfo";

struct MatchDataSender {
std::shared_ptr<nt::NetworkTable> table =
nt::NetworkTableInstance::GetDefault().GetTable("FMSInfo");
MatchDataSenderEntry<nt::StringTopic> typeMetaData{table, ".type", "FMSInfo"};
MatchDataSenderEntry<nt::StringTopic> typeMetaData{
table,
".type",
kSmartDashboardType,
{{"SmartDashboard", kSmartDashboardType}}};
MatchDataSenderEntry<nt::StringTopic> gameSpecificMessage{
table, "GameSpecificMessage", ""};
MatchDataSenderEntry<nt::StringTopic> eventName{table, "EventName", ""};
Expand Down
8 changes: 5 additions & 3 deletions wpilibc/src/main/native/cpp/Preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@
#include <networktables/NetworkTableInstance.h>
#include <networktables/NetworkTableListener.h>
#include <networktables/StringTopic.h>
#include <wpi/json.h>

using namespace frc;

// The Preferences table name
static constexpr std::string_view kTableName{"Preferences"};

static constexpr std::string_view kSmartDashboardType = "RobotPreferences";
namespace {
struct Instance {
Instance();

std::shared_ptr<nt::NetworkTable> table{
nt::NetworkTableInstance::GetDefault().GetTable(kTableName)};
nt::StringPublisher typePublisher{table->GetStringTopic(".type").Publish()};
nt::StringPublisher typePublisher{table->GetStringTopic(".type").PublishEx(
nt::StringTopic::kTypeString, {{"SmartDashboard", kSmartDashboardType}})};
nt::MultiSubscriber tableSubscriber{nt::NetworkTableInstance::GetDefault(),
{{fmt::format("{}/", table->GetPath())}}};
nt::NetworkTableListener listener;
Expand Down Expand Up @@ -165,7 +167,7 @@ void Preferences::RemoveAll() {
}

Instance::Instance() {
typePublisher.Set("RobotPreferences");
typePublisher.Set(kSmartDashboardType);
listener = nt::NetworkTableListener::CreateListener(
tableSubscriber, NT_EVENT_PUBLISH | NT_EVENT_IMMEDIATE,
[typeTopic = typePublisher.GetTopic().GetHandle()](auto& event) {
Expand Down
9 changes: 7 additions & 2 deletions wpilibc/src/main/native/cpp/livewindow/LiveWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <networktables/NetworkTable.h>
#include <networktables/NetworkTableInstance.h>
#include <networktables/StringTopic.h>
#include <wpi/json.h>
#include <wpi/mutex.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableRegistry.h>
Expand All @@ -16,6 +17,8 @@

using namespace frc;

static constexpr std::string_view kSmartDashboardType = "LW Subsystem";

namespace {
struct Component {
bool firstTime = true;
Expand Down Expand Up @@ -207,8 +210,10 @@ void LiveWindow::UpdateValuesUnsafe() {
comp.namePub.Set(cbdata.name);
static_cast<SendableBuilderImpl&>(cbdata.builder).SetTable(table);
cbdata.sendable->InitSendable(cbdata.builder);
comp.typePub = nt::StringTopic{ssTable->GetTopic(".type")}.Publish();
comp.typePub.Set("LW Subsystem");
comp.typePub = nt::StringTopic{ssTable->GetTopic(".type")}.PublishEx(
nt::StringTopic::kTypeString,
{{"SmartDashboard", kSmartDashboardType}});
comp.typePub.Set(kSmartDashboardType);

comp.firstTime = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

#include "frc/shuffleboard/ShuffleboardLayout.h"

#include <wpi/json.h>

using namespace frc;

static constexpr std::string_view kSmartDashboardType = "ShuffleboardLayout";

ShuffleboardLayout::ShuffleboardLayout(ShuffleboardContainer& parent,
std::string_view title,
std::string_view type)
Expand All @@ -20,7 +24,9 @@ void ShuffleboardLayout::BuildInto(
std::shared_ptr<nt::NetworkTable> metaTable) {
BuildMetadata(metaTable);
auto table = parentTable->GetSubTable(GetTitle());
table->GetEntry(".type").SetString("ShuffleboardLayout");
table->GetEntry(".type").SetString(kSmartDashboardType);
table->GetEntry(".type").GetTopic().SetProperty("SmartDashboard",
kSmartDashboardType);
for (auto& component : GetComponents()) {
component->BuildInto(table, metaTable->GetSubTable(component->GetTitle()));
}
Expand Down
8 changes: 7 additions & 1 deletion wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

#include "frc/shuffleboard/ShuffleboardTab.h"

#include <wpi/json.h>

using namespace frc;

static constexpr std::string_view kSmartDashboardType = "ShuffleboardLayout";

ShuffleboardTab::ShuffleboardTab(ShuffleboardRoot& root, std::string_view title)
: ShuffleboardValue(title), ShuffleboardContainer(title), m_root(root) {}

Expand All @@ -16,7 +20,9 @@ ShuffleboardRoot& ShuffleboardTab::GetRoot() {
void ShuffleboardTab::BuildInto(std::shared_ptr<nt::NetworkTable> parentTable,
std::shared_ptr<nt::NetworkTable> metaTable) {
auto tabTable = parentTable->GetSubTable(GetTitle());
tabTable->GetEntry(".type").SetString("ShuffleboardTab");
tabTable->GetEntry(".type").SetString(kSmartDashboardType);
tabTable->GetEntry(".type").GetTopic().SetProperty("SmartDashboard",
kSmartDashboardType);
for (auto& component : GetComponents()) {
component->BuildInto(tabTable,
metaTable->GetSubTable(component->GetTitle()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
#include "frc/smartdashboard/MechanismLigament2d.h"

#include <wpi/StringExtras.h>
#include <wpi/json.h>

using namespace frc;

static constexpr std::string_view kSmartDashboardType = "line";

MechanismLigament2d::MechanismLigament2d(std::string_view name, double length,
units::degree_t angle,
double lineWeight,
Expand All @@ -21,8 +24,9 @@ MechanismLigament2d::MechanismLigament2d(std::string_view name, double length,

void MechanismLigament2d::UpdateEntries(
std::shared_ptr<nt::NetworkTable> table) {
m_typePub = table->GetStringTopic(".type").Publish();
m_typePub.Set("line");
m_typePub = table->GetStringTopic(".type").PublishEx(
nt::StringTopic::kTypeString, {{"SmartDashboard", kSmartDashboardType}});
m_typePub.Set(kSmartDashboardType);

m_colorEntry = table->GetStringTopic("color").GetEntry("");
m_colorEntry.Set(m_color);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import edu.wpi.first.networktables.IntegerPublisher;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.networktables.StringPublisher;
import edu.wpi.first.networktables.StringTopic;
import edu.wpi.first.util.EventVector;
import edu.wpi.first.util.WPIUtilJNI;
import edu.wpi.first.util.datalog.BooleanArrayLogEntry;
Expand Down Expand Up @@ -93,6 +94,8 @@ public enum MatchType {

@SuppressWarnings("MemberName")
private static class MatchDataSender {
private static final String kSmartDashboardType = "FMSInfo";

final StringPublisher gameSpecificMessage;
final StringPublisher eventName;
final IntegerPublisher matchNumber;
Expand All @@ -112,7 +115,11 @@ private static class MatchDataSender {

MatchDataSender() {
var table = NetworkTableInstance.getDefault().getTable("FMSInfo");
table.getStringTopic(".type").publish().set("FMSInfo");
table
.getStringTopic(".type")
.publishEx(
StringTopic.kTypeString, "{\"SmartDashboard\":\"" + kSmartDashboardType + "\"}")
.set(kSmartDashboardType);
gameSpecificMessage = table.getStringTopic("GameSpecificMessage").publish();
gameSpecificMessage.set("");
eventName = table.getStringTopic("EventName").publish();
Expand Down
15 changes: 11 additions & 4 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.networktables.NetworkTableListener;
import edu.wpi.first.networktables.StringPublisher;
import edu.wpi.first.networktables.StringTopic;
import edu.wpi.first.networktables.Topic;
import java.util.Collection;
import java.util.EnumSet;
Expand All @@ -34,7 +35,9 @@
*/
public final class Preferences {
/** The Preferences table name. */
private static final String TABLE_NAME = "Preferences";
private static final String kTableName = "Preferences";

private static final String kSmartDashboardType = "RobotPreferences";

/** The network table. */
private static NetworkTable m_table;
Expand All @@ -57,12 +60,16 @@ private Preferences() {}
* @param inst NetworkTable instance
*/
public static synchronized void setNetworkTableInstance(NetworkTableInstance inst) {
m_table = inst.getTable(TABLE_NAME);
m_table = inst.getTable(kTableName);
if (m_typePublisher != null) {
m_typePublisher.close();
}
m_typePublisher = m_table.getStringTopic(".type").publish();
m_typePublisher.set("RobotPreferences");
m_typePublisher =
m_table
.getStringTopic(".type")
.publishEx(
StringTopic.kTypeString, "{\"SmartDashboard\":\"" + kSmartDashboardType + "\"}");
m_typePublisher.set(kSmartDashboardType);

// Subscribe to all Preferences; this ensures we get the latest values
// ahead of a getter call.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public void close() {
StringPublisher m_typePub;
}

private static final String kSmartDashboardType = "LW Subsystem";

private static final int dataHandle = SendableRegistry.getDataHandle();
private static final NetworkTable liveWindowTable =
NetworkTableInstance.getDefault().getTable("LiveWindow");
Expand Down Expand Up @@ -224,8 +226,12 @@ public static synchronized void updateValues() {
component.m_namePub.set(cbdata.name);
((SendableBuilderImpl) cbdata.builder).setTable(table);
cbdata.sendable.initSendable(cbdata.builder);
component.m_typePub = new StringTopic(ssTable.getTopic(".type")).publish();
component.m_typePub.set("LW Subsystem");
component.m_typePub =
new StringTopic(ssTable.getTopic(".type"))
.publishEx(
StringTopic.kTypeString,
"{\"SmartDashboard\":\"" + kSmartDashboardType + "\"}");
component.m_typePub.set(kSmartDashboardType);

component.m_firstTime = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
/** A layout in a Shuffleboard tab. Layouts can contain widgets and other layouts. */
public final class ShuffleboardLayout extends ShuffleboardComponent<ShuffleboardLayout>
implements ShuffleboardContainer {
private static final String kSmartDashboardType = "ShuffleboardLayout";

private final ContainerHelper m_helper = new ContainerHelper(this);

ShuffleboardLayout(ShuffleboardContainer parent, String title, String type) {
Expand Down Expand Up @@ -127,7 +129,8 @@ public SuppliedValueWidget<byte[]> addRaw(
public void buildInto(NetworkTable parentTable, NetworkTable metaTable) {
buildMetadata(metaTable);
NetworkTable table = parentTable.getSubTable(getTitle());
table.getEntry(".type").setString("ShuffleboardLayout");
table.getEntry(".type").setString(kSmartDashboardType);
table.getEntry(".type").getTopic().setProperty("SmartDashboard", kSmartDashboardType);
for (ShuffleboardComponent<?> component : getComponents()) {
component.buildInto(table, metaTable.getSubTable(component.getTitle()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.StringEntry;
import edu.wpi.first.networktables.StringPublisher;
import edu.wpi.first.networktables.StringTopic;
import edu.wpi.first.wpilibj.util.Color8Bit;

/**
Expand All @@ -28,6 +29,8 @@ public class MechanismLigament2d extends MechanismObject2d {
private double m_weight;
private DoubleEntry m_weightEntry;

private static String kSmartDashboardType = "line";

/**
* Create a new ligament.
*
Expand Down Expand Up @@ -201,8 +204,12 @@ protected void updateEntries(NetworkTable table) {
if (m_typePub != null) {
m_typePub.close();
}
m_typePub = table.getStringTopic(".type").publish();
m_typePub.set("line");
m_typePub =
table
.getStringTopic(".type")
.publishEx(
StringTopic.kTypeString, "{\"SmartDashboard\":\"" + kSmartDashboardType + "\"}");
m_typePub.set(kSmartDashboardType);

if (m_angleEntry != null) {
m_angleEntry.close();
Expand Down

0 comments on commit 8993ac8

Please sign in to comment.