blob: b2bdf8c837d7b1eb43085f8fcffb193588cedede [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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
9using namespace glass;
10
11NTSubsystemModel::NTSubsystemModel(std::string_view path)
12 : NTSubsystemModel(nt::GetDefaultInstance(), path) {}
13
14NTSubsystemModel::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
24void 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
42bool NTSubsystemModel::Exists() {
43 return m_nt.IsConnected() &&
44 nt::GetEntryType(m_defaultCommand) != NT_UNASSIGNED;
45}