blob: ccc64127be302752e20746d4ff89045edec0c016 [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/NTCommandScheduler.h"
6
7#include <fmt/format.h>
8#include <wpi/StringExtras.h>
9
10using namespace glass;
11
12NTCommandSchedulerModel::NTCommandSchedulerModel(std::string_view path)
13 : NTCommandSchedulerModel(nt::GetDefaultInstance(), path) {}
14
15NTCommandSchedulerModel::NTCommandSchedulerModel(NT_Inst instance,
16 std::string_view path)
17 : m_nt(instance),
18 m_name(m_nt.GetEntry(fmt::format("{}/.name", path))),
19 m_commands(m_nt.GetEntry(fmt::format("{}/Names", path))),
20 m_ids(m_nt.GetEntry(fmt::format("{}/Ids", path))),
21 m_cancel(m_nt.GetEntry(fmt::format("{}/Cancel", path))),
22 m_nameValue(wpi::rsplit(path, '/').second) {
23 m_nt.AddListener(m_name);
24 m_nt.AddListener(m_commands);
25 m_nt.AddListener(m_ids);
26 m_nt.AddListener(m_cancel);
27}
28
29void NTCommandSchedulerModel::CancelCommand(size_t index) {
30 if (index < m_idsValue.size()) {
31 nt::SetEntryValue(
32 m_cancel, nt::NetworkTableValue::MakeDoubleArray({m_idsValue[index]}));
33 }
34}
35
36void NTCommandSchedulerModel::Update() {
37 for (auto&& event : m_nt.PollListener()) {
38 if (event.entry == m_name) {
39 if (event.value && event.value->IsString()) {
40 m_nameValue = event.value->GetString();
41 }
42 } else if (event.entry == m_commands) {
43 if (event.value && event.value->IsStringArray()) {
44 auto arr = event.value->GetStringArray();
45 m_commandsValue.assign(arr.begin(), arr.end());
46 }
47 } else if (event.entry == m_ids) {
48 if (event.value && event.value->IsDoubleArray()) {
49 auto arr = event.value->GetDoubleArray();
50 m_idsValue.assign(arr.begin(), arr.end());
51 }
52 }
53 }
54}
55
56bool NTCommandSchedulerModel::Exists() {
57 return m_nt.IsConnected() && nt::GetEntryType(m_commands) != NT_UNASSIGNED;
58}