blob: 26a5740c78adcbde6c5ca175c852fbcbcf1726ff [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)
James Kuszmaulcf324122023-01-14 14:07:17 -080013 : NTCommandSchedulerModel(nt::NetworkTableInstance::GetDefault(), path) {}
Austin Schuh812d0d12021-11-04 20:16:48 -070014
James Kuszmaulcf324122023-01-14 14:07:17 -080015NTCommandSchedulerModel::NTCommandSchedulerModel(nt::NetworkTableInstance inst,
Austin Schuh812d0d12021-11-04 20:16:48 -070016 std::string_view path)
James Kuszmaulcf324122023-01-14 14:07:17 -080017 : m_inst{inst},
18 m_name{inst.GetStringTopic(fmt::format("{}/.name", path)).Subscribe("")},
19 m_commands{inst.GetStringArrayTopic(fmt::format("{}/Names", path))
20 .Subscribe({})},
21 m_ids{
22 inst.GetIntegerArrayTopic(fmt::format("{}/Ids", path)).Subscribe({})},
23 m_cancel{
24 inst.GetIntegerArrayTopic(fmt::format("{}/Cancel", path)).Publish()},
25 m_nameValue{wpi::rsplit(path, '/').second} {}
Austin Schuh812d0d12021-11-04 20:16:48 -070026
27void NTCommandSchedulerModel::CancelCommand(size_t index) {
28 if (index < m_idsValue.size()) {
James Kuszmaulcf324122023-01-14 14:07:17 -080029 m_cancel.Set({{m_idsValue[index]}});
Austin Schuh812d0d12021-11-04 20:16:48 -070030 }
31}
32
33void NTCommandSchedulerModel::Update() {
James Kuszmaulcf324122023-01-14 14:07:17 -080034 for (auto&& v : m_name.ReadQueue()) {
35 m_nameValue = std::move(v.value);
36 }
37 for (auto&& v : m_commands.ReadQueue()) {
38 m_commandsValue = std::move(v.value);
39 }
40 for (auto&& v : m_ids.ReadQueue()) {
41 m_idsValue = std::move(v.value);
Austin Schuh812d0d12021-11-04 20:16:48 -070042 }
43}
44
45bool NTCommandSchedulerModel::Exists() {
James Kuszmaulcf324122023-01-14 14:07:17 -080046 return m_commands.Exists();
Austin Schuh812d0d12021-11-04 20:16:48 -070047}