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/NTStringChooser.h" |
| 6 | |
| 7 | #include <fmt/format.h> |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 8 | #include <wpi/json.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 9 | |
| 10 | using namespace glass; |
| 11 | |
| 12 | NTStringChooserModel::NTStringChooserModel(std::string_view path) |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 13 | : NTStringChooserModel{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 | NTStringChooserModel::NTStringChooserModel(nt::NetworkTableInstance inst, |
| 16 | std::string_view path) |
| 17 | : m_inst{inst}, |
| 18 | m_default{ |
| 19 | m_inst.GetStringTopic(fmt::format("{}/default", path)).Subscribe("")}, |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 20 | m_selected{m_inst.GetStringTopic(fmt::format("{}/selected", path)) |
| 21 | .Subscribe("")}, |
| 22 | m_selectedPub{m_inst.GetStringTopic(fmt::format("{}/selected", path)) |
| 23 | .PublishEx("string", {{"retained", true}})}, |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 24 | m_active{ |
| 25 | m_inst.GetStringTopic(fmt::format("{}/active", path)).Subscribe("")}, |
| 26 | m_options{m_inst.GetStringArrayTopic(fmt::format("{}/options", path)) |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 27 | .Subscribe({})} {} |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 28 | |
| 29 | void NTStringChooserModel::SetSelected(std::string_view val) { |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 30 | m_selectedPub.Set(val); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void NTStringChooserModel::Update() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 34 | if (!m_default.Exists()) { |
| 35 | m_defaultValue.clear(); |
| 36 | } |
| 37 | for (auto&& v : m_default.ReadQueue()) { |
| 38 | m_defaultValue = std::move(v.value); |
| 39 | } |
| 40 | |
| 41 | if (!m_selected.Exists()) { |
| 42 | m_selectedValue.clear(); |
| 43 | } |
| 44 | for (auto&& v : m_selected.ReadQueue()) { |
| 45 | m_selectedValue = std::move(v.value); |
| 46 | } |
| 47 | |
| 48 | if (!m_active.Exists()) { |
| 49 | m_activeValue.clear(); |
| 50 | } |
| 51 | for (auto&& v : m_active.ReadQueue()) { |
| 52 | m_activeValue = std::move(v.value); |
| 53 | } |
| 54 | |
| 55 | if (!m_options.Exists()) { |
| 56 | m_optionsValue.clear(); |
| 57 | } |
| 58 | for (auto&& v : m_options.ReadQueue()) { |
| 59 | m_optionsValue = std::move(v.value); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | bool NTStringChooserModel::Exists() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 64 | return m_options.Exists(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 65 | } |