blob: 26074fc481410be3fa22ebee96cdc0cf0e9f7a44 [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/NTStringChooser.h"
6
7#include <fmt/format.h>
Maxwell Henderson80bec322024-01-09 15:48:44 -08008#include <wpi/json.h>
Austin Schuh812d0d12021-11-04 20:16:48 -07009
10using namespace glass;
11
12NTStringChooserModel::NTStringChooserModel(std::string_view path)
James Kuszmaulcf324122023-01-14 14:07:17 -080013 : NTStringChooserModel{nt::NetworkTableInstance::GetDefault(), path} {}
Austin Schuh812d0d12021-11-04 20:16:48 -070014
James Kuszmaulcf324122023-01-14 14:07:17 -080015NTStringChooserModel::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 Henderson80bec322024-01-09 15:48:44 -080020 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 Kuszmaulcf324122023-01-14 14:07:17 -080024 m_active{
25 m_inst.GetStringTopic(fmt::format("{}/active", path)).Subscribe("")},
26 m_options{m_inst.GetStringArrayTopic(fmt::format("{}/options", path))
Maxwell Henderson80bec322024-01-09 15:48:44 -080027 .Subscribe({})} {}
Austin Schuh812d0d12021-11-04 20:16:48 -070028
29void NTStringChooserModel::SetSelected(std::string_view val) {
Maxwell Henderson80bec322024-01-09 15:48:44 -080030 m_selectedPub.Set(val);
Austin Schuh812d0d12021-11-04 20:16:48 -070031}
32
33void NTStringChooserModel::Update() {
James Kuszmaulcf324122023-01-14 14:07:17 -080034 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 Schuh812d0d12021-11-04 20:16:48 -070060 }
61}
62
63bool NTStringChooserModel::Exists() {
James Kuszmaulcf324122023-01-14 14:07:17 -080064 return m_options.Exists();
Austin Schuh812d0d12021-11-04 20:16:48 -070065}