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/NTCommandScheduler.h" |
| 6 | |
| 7 | #include <fmt/format.h> |
| 8 | #include <wpi/StringExtras.h> |
| 9 | |
| 10 | using namespace glass; |
| 11 | |
| 12 | NTCommandSchedulerModel::NTCommandSchedulerModel(std::string_view path) |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 13 | : NTCommandSchedulerModel(nt::NetworkTableInstance::GetDefault(), path) {} |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 14 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 15 | NTCommandSchedulerModel::NTCommandSchedulerModel(nt::NetworkTableInstance inst, |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 16 | std::string_view path) |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 17 | : 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 26 | |
| 27 | void NTCommandSchedulerModel::CancelCommand(size_t index) { |
| 28 | if (index < m_idsValue.size()) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 29 | m_cancel.Set({{m_idsValue[index]}}); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
| 33 | void NTCommandSchedulerModel::Update() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 34 | 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
| 45 | bool NTCommandSchedulerModel::Exists() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 46 | return m_commands.Exists(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 47 | } |