2020-12-26 14:31:24 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2020-12-23 00:07:44 -05:00
|
|
|
|
|
|
|
|
#include "glass/networktables/NTCommandScheduler.h"
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
#include <wpi/StringExtras.h>
|
2020-12-23 00:07:44 -05:00
|
|
|
|
|
|
|
|
using namespace glass;
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
NTCommandSchedulerModel::NTCommandSchedulerModel(std::string_view path)
|
2022-10-08 10:01:31 -07:00
|
|
|
: NTCommandSchedulerModel(nt::NetworkTableInstance::GetDefault(), path) {}
|
2020-12-23 00:07:44 -05:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
NTCommandSchedulerModel::NTCommandSchedulerModel(nt::NetworkTableInstance inst,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view path)
|
2022-10-08 10:01:31 -07:00
|
|
|
: m_inst{inst},
|
|
|
|
|
m_name{inst.GetStringTopic(fmt::format("{}/.name", path)).Subscribe("")},
|
|
|
|
|
m_commands{inst.GetStringArrayTopic(fmt::format("{}/Names", path))
|
|
|
|
|
.Subscribe({})},
|
|
|
|
|
m_ids{
|
|
|
|
|
inst.GetIntegerArrayTopic(fmt::format("{}/Ids", path)).Subscribe({})},
|
|
|
|
|
m_cancel{
|
|
|
|
|
inst.GetIntegerArrayTopic(fmt::format("{}/Cancel", path)).Publish()},
|
|
|
|
|
m_nameValue{wpi::rsplit(path, '/').second} {}
|
2020-12-23 00:07:44 -05:00
|
|
|
|
|
|
|
|
void NTCommandSchedulerModel::CancelCommand(size_t index) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (index < m_idsValue.size()) {
|
2022-10-08 10:01:31 -07:00
|
|
|
m_cancel.Set({{m_idsValue[index]}});
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-12-23 00:07:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NTCommandSchedulerModel::Update() {
|
2022-10-08 10:01:31 -07:00
|
|
|
for (auto&& v : m_name.ReadQueue()) {
|
|
|
|
|
m_nameValue = std::move(v.value);
|
|
|
|
|
}
|
|
|
|
|
for (auto&& v : m_commands.ReadQueue()) {
|
|
|
|
|
m_commandsValue = std::move(v.value);
|
|
|
|
|
}
|
|
|
|
|
for (auto&& v : m_ids.ReadQueue()) {
|
|
|
|
|
m_idsValue = std::move(v.value);
|
2020-12-23 00:07:44 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NTCommandSchedulerModel::Exists() {
|
2022-11-20 17:27:28 -08:00
|
|
|
return m_commands.Exists();
|
2020-12-23 00:07:44 -05:00
|
|
|
}
|