Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
| 4 | |
| 5 | #include "glass/networktables/NTSubsystem.h" |
| 6 | |
| 7 | #include <fmt/format.h> |
| 8 | |
| 9 | using namespace glass; |
| 10 | |
| 11 | NTSubsystemModel::NTSubsystemModel(std::string_view path) |
| 12 | : NTSubsystemModel(nt::GetDefaultInstance(), path) {} |
| 13 | |
| 14 | NTSubsystemModel::NTSubsystemModel(NT_Inst instance, std::string_view path) |
| 15 | : m_nt(instance), |
| 16 | m_name(m_nt.GetEntry(fmt::format("{}/.name", path))), |
| 17 | m_defaultCommand(m_nt.GetEntry(fmt::format("{}/.default", path))), |
| 18 | m_currentCommand(m_nt.GetEntry(fmt::format("{}/.command", path))) { |
| 19 | m_nt.AddListener(m_name); |
| 20 | m_nt.AddListener(m_defaultCommand); |
| 21 | m_nt.AddListener(m_currentCommand); |
| 22 | } |
| 23 | |
| 24 | void NTSubsystemModel::Update() { |
| 25 | for (auto&& event : m_nt.PollListener()) { |
| 26 | if (event.entry == m_name) { |
| 27 | if (event.value && event.value->IsString()) { |
| 28 | m_nameValue = event.value->GetString(); |
| 29 | } |
| 30 | } else if (event.entry == m_defaultCommand) { |
| 31 | if (event.value && event.value->IsString()) { |
| 32 | m_defaultCommandValue = event.value->GetString(); |
| 33 | } |
| 34 | } else if (event.entry == m_currentCommand) { |
| 35 | if (event.value && event.value->IsString()) { |
| 36 | m_currentCommandValue = event.value->GetString(); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | bool NTSubsystemModel::Exists() { |
| 43 | return m_nt.IsConnected() && |
| 44 | nt::GetEntryType(m_defaultCommand) != NT_UNASSIGNED; |
| 45 | } |