[ntcore] NetworkTables 4 (#3217)

This commit is contained in:
Peter Johnson
2022-10-08 10:01:31 -07:00
committed by GitHub
parent 90cfa00115
commit 77301b126c
380 changed files with 34573 additions and 22095 deletions

View File

@@ -9,67 +9,56 @@
using namespace glass;
NTStringChooserModel::NTStringChooserModel(std::string_view path)
: NTStringChooserModel{nt::GetDefaultInstance(), path} {}
: NTStringChooserModel{nt::NetworkTableInstance::GetDefault(), path} {}
NTStringChooserModel::NTStringChooserModel(NT_Inst inst, std::string_view path)
: m_nt{inst},
m_default{m_nt.GetEntry(fmt::format("{}/default", path))},
m_selected{m_nt.GetEntry(fmt::format("{}/selected", path))},
m_active{m_nt.GetEntry(fmt::format("{}/active", path))},
m_options{m_nt.GetEntry(fmt::format("{}/options", path))} {
m_nt.AddListener(m_default);
m_nt.AddListener(m_selected);
m_nt.AddListener(m_active);
m_nt.AddListener(m_options);
}
void NTStringChooserModel::SetDefault(std::string_view val) {
nt::SetEntryValue(m_default, nt::Value::MakeString(val));
NTStringChooserModel::NTStringChooserModel(nt::NetworkTableInstance inst,
std::string_view path)
: m_inst{inst},
m_default{
m_inst.GetStringTopic(fmt::format("{}/default", path)).Subscribe("")},
m_selected{
m_inst.GetStringTopic(fmt::format("{}/selected", path)).GetEntry("")},
m_active{
m_inst.GetStringTopic(fmt::format("{}/active", path)).Subscribe("")},
m_options{m_inst.GetStringArrayTopic(fmt::format("{}/options", path))
.Subscribe({})} {
m_selected.GetTopic().SetRetained(true);
}
void NTStringChooserModel::SetSelected(std::string_view val) {
nt::SetEntryValue(m_selected, nt::Value::MakeString(val));
}
void NTStringChooserModel::SetActive(std::string_view val) {
nt::SetEntryValue(m_active, nt::Value::MakeString(val));
}
void NTStringChooserModel::SetOptions(wpi::span<const std::string> val) {
nt::SetEntryValue(m_options, nt::Value::MakeStringArray(val));
m_selected.Set(val);
}
void NTStringChooserModel::Update() {
for (auto&& event : m_nt.PollListener()) {
if (event.entry == m_default) {
if ((event.flags & NT_NOTIFY_DELETE) != 0) {
m_defaultValue.clear();
} else if (event.value && event.value->IsString()) {
m_defaultValue = event.value->GetString();
}
} else if (event.entry == m_selected) {
if ((event.flags & NT_NOTIFY_DELETE) != 0) {
m_selectedValue.clear();
} else if (event.value && event.value->IsString()) {
m_selectedValue = event.value->GetString();
}
} else if (event.entry == m_active) {
if ((event.flags & NT_NOTIFY_DELETE) != 0) {
m_activeValue.clear();
} else if (event.value && event.value->IsString()) {
m_activeValue = event.value->GetString();
}
} else if (event.entry == m_options) {
if ((event.flags & NT_NOTIFY_DELETE) != 0) {
m_optionsValue.clear();
} else if (event.value && event.value->IsStringArray()) {
auto arr = event.value->GetStringArray();
m_optionsValue.assign(arr.begin(), arr.end());
}
}
if (!m_default.Exists()) {
m_defaultValue.clear();
}
for (auto&& v : m_default.ReadQueue()) {
m_defaultValue = std::move(v.value);
}
if (!m_selected.Exists()) {
m_selectedValue.clear();
}
for (auto&& v : m_selected.ReadQueue()) {
m_selectedValue = std::move(v.value);
}
if (!m_active.Exists()) {
m_activeValue.clear();
}
for (auto&& v : m_active.ReadQueue()) {
m_activeValue = std::move(v.value);
}
if (!m_options.Exists()) {
m_optionsValue.clear();
}
for (auto&& v : m_options.ReadQueue()) {
m_optionsValue = std::move(v.value);
}
}
bool NTStringChooserModel::Exists() {
return m_nt.IsConnected() && nt::GetEntryType(m_options) != NT_UNASSIGNED;
return m_inst.IsConnected() && m_options.Exists();
}