|
| 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 "networktables/NetworkTablesTelemetryBackend.h" |
| 6 | + |
| 7 | +#include <atomic> |
| 8 | +#include <string> |
| 9 | +#include <string_view> |
| 10 | + |
| 11 | +#include <fmt/format.h> |
| 12 | +#include <wpi/json.h> |
| 13 | +#include <wpi/mutex.h> |
| 14 | +#include <wpi/telemetry/TelemetryEntry.h> |
| 15 | + |
| 16 | +#include "networktables/GenericEntry.h" |
| 17 | +#include "networktables/NetworkTableInstance.h" |
| 18 | + |
| 19 | +using namespace nt; |
| 20 | + |
| 21 | +class NetworkTablesTelemetryBackend::Entry : public wpi::TelemetryEntry { |
| 22 | + public: |
| 23 | + Entry(NetworkTableInstance& inst, std::string_view prefix, |
| 24 | + std::string_view path) |
| 25 | + : m_inst{inst}, m_path{fmt::format("{}{}", prefix, path)} {} |
| 26 | + |
| 27 | + void KeepDuplicates() override { |
| 28 | + m_keepDuplicates = true; |
| 29 | + // TODO: update publisher |
| 30 | + } |
| 31 | + |
| 32 | + void SetProperty(std::string_view key, std::string_view value) override { |
| 33 | + std::scoped_lock lock{m_mutex}; |
| 34 | + auto& curValue = m_properties[key]; |
| 35 | + if (curValue == value) { |
| 36 | + return; |
| 37 | + } |
| 38 | + curValue = value; |
| 39 | + // TODO |
| 40 | + } |
| 41 | + |
| 42 | + void LogBoolean(bool value) override { |
| 43 | + std::scoped_lock lock{m_mutex}; |
| 44 | + if (!m_pub) { |
| 45 | + m_pub = m_inst.GetTopic(m_path).GenericPublishEx( |
| 46 | + "boolean", m_properties, {.keepDuplicates = m_keepDuplicates}); |
| 47 | + } |
| 48 | + if (!m_pub.SetBoolean(value)) { |
| 49 | + // TODO: warn? |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + void LogInt64(int64_t value) override { |
| 54 | + std::scoped_lock lock{m_mutex}; |
| 55 | + if (!m_pub) { |
| 56 | + m_pub = m_inst.GetTopic(m_path).GenericPublishEx( |
| 57 | + "int", m_properties, {.keepDuplicates = m_keepDuplicates}); |
| 58 | + } |
| 59 | + if (!m_pub.SetInteger(value)) { |
| 60 | + // TODO: warn? |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + void LogFloat(float value) override { |
| 65 | + std::scoped_lock lock{m_mutex}; |
| 66 | + if (!m_pub) { |
| 67 | + m_pub = m_inst.GetTopic(m_path).GenericPublishEx( |
| 68 | + "float", m_properties, {.keepDuplicates = m_keepDuplicates}); |
| 69 | + } |
| 70 | + if (!m_pub.SetFloat(value)) { |
| 71 | + // TODO: warn? |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + void LogDouble(double value) override { |
| 76 | + std::scoped_lock lock{m_mutex}; |
| 77 | + if (!m_pub) { |
| 78 | + m_pub = m_inst.GetTopic(m_path).GenericPublishEx( |
| 79 | + "double", m_properties, {.keepDuplicates = m_keepDuplicates}); |
| 80 | + } |
| 81 | + if (!m_pub.SetDouble(value)) { |
| 82 | + // TODO: warn? |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + void LogString(std::string_view value, std::string_view typeString) override { |
| 87 | + std::scoped_lock lock{m_mutex}; |
| 88 | + if (!m_pub) { |
| 89 | + m_typeString = typeString; |
| 90 | + m_pub = m_inst.GetTopic(m_path).GenericPublishEx( |
| 91 | + typeString, m_properties, {.keepDuplicates = m_keepDuplicates}); |
| 92 | + } |
| 93 | + if (m_typeString != typeString || !m_pub.SetString(value)) { |
| 94 | + // TODO: warn? |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + void LogBooleanArray(std::span<const bool> value) override { |
| 99 | + std::scoped_lock lock{m_mutex}; |
| 100 | + if (!m_pub) { |
| 101 | + m_pub = m_inst.GetTopic(m_path).GenericPublishEx( |
| 102 | + "boolean[]", m_properties, {.keepDuplicates = m_keepDuplicates}); |
| 103 | + } |
| 104 | + if (!m_pub.SetBooleanArray(value)) { |
| 105 | + // TODO: warn? |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + void LogBooleanArray(std::span<const int> value) override { |
| 110 | + std::scoped_lock lock{m_mutex}; |
| 111 | + if (!m_pub) { |
| 112 | + m_pub = m_inst.GetTopic(m_path).GenericPublishEx( |
| 113 | + "boolean[]", m_properties, {.keepDuplicates = m_keepDuplicates}); |
| 114 | + } |
| 115 | + if (!m_pub.SetBooleanArray(value)) { |
| 116 | + // TODO: warn? |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + void LogInt16Array(std::span<const int16_t> value) override { |
| 121 | + // TODO |
| 122 | + } |
| 123 | + |
| 124 | + void LogInt32Array(std::span<const int32_t> value) override { |
| 125 | + // TODO |
| 126 | + } |
| 127 | + |
| 128 | + void LogInt64Array(std::span<const int64_t> value) override { |
| 129 | + std::scoped_lock lock{m_mutex}; |
| 130 | + if (!m_pub) { |
| 131 | + m_pub = m_inst.GetTopic(m_path).GenericPublishEx( |
| 132 | + "int[]", m_properties, {.keepDuplicates = m_keepDuplicates}); |
| 133 | + } |
| 134 | + if (!m_pub.SetIntegerArray(value)) { |
| 135 | + // TODO: warn? |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + void LogFloatArray(std::span<const float> value) override { |
| 140 | + std::scoped_lock lock{m_mutex}; |
| 141 | + if (!m_pub) { |
| 142 | + m_pub = m_inst.GetTopic(m_path).GenericPublishEx( |
| 143 | + "float[]", m_properties, {.keepDuplicates = m_keepDuplicates}); |
| 144 | + } |
| 145 | + if (!m_pub.SetFloatArray(value)) { |
| 146 | + // TODO: warn? |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + void LogDoubleArray(std::span<const double> value) override { |
| 151 | + std::scoped_lock lock{m_mutex}; |
| 152 | + if (!m_pub) { |
| 153 | + m_pub = m_inst.GetTopic(m_path).GenericPublishEx( |
| 154 | + "double[]", m_properties, {.keepDuplicates = m_keepDuplicates}); |
| 155 | + } |
| 156 | + if (!m_pub.SetDoubleArray(value)) { |
| 157 | + // TODO: warn? |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + void LogStringArray(std::span<const std::string> value) override { |
| 162 | + std::scoped_lock lock{m_mutex}; |
| 163 | + if (!m_pub) { |
| 164 | + m_pub = m_inst.GetTopic(m_path).GenericPublishEx( |
| 165 | + "string[]", m_properties, {.keepDuplicates = m_keepDuplicates}); |
| 166 | + } |
| 167 | + if (!m_pub.SetStringArray(value)) { |
| 168 | + // TODO: warn? |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + void LogStringArray(std::span<const std::string_view> value) override { |
| 173 | + // TODO |
| 174 | + } |
| 175 | + |
| 176 | + void LogRaw(std::span<const uint8_t> value, |
| 177 | + std::string_view typeString) override { |
| 178 | + std::scoped_lock lock{m_mutex}; |
| 179 | + if (!m_pub) { |
| 180 | + m_typeString = typeString; |
| 181 | + m_pub = m_inst.GetTopic(m_path).GenericPublishEx( |
| 182 | + typeString, m_properties, {.keepDuplicates = m_keepDuplicates}); |
| 183 | + } |
| 184 | + if (m_typeString != typeString || !m_pub.SetRaw(value)) { |
| 185 | + // TODO: warn? |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private: |
| 190 | + NetworkTableInstance& m_inst; |
| 191 | + std::string m_path; |
| 192 | + wpi::mutex m_mutex; |
| 193 | + GenericPublisher m_pub; |
| 194 | + std::string m_typeString; |
| 195 | + std::atomic_bool m_keepDuplicates{false}; |
| 196 | + wpi::json m_properties = wpi::json::object(); |
| 197 | +}; |
| 198 | + |
| 199 | +NetworkTablesTelemetryBackend::NetworkTablesTelemetryBackend( |
| 200 | + NetworkTableInstance& inst, std::string_view prefix) |
| 201 | + : m_inst{inst}, m_prefix{prefix} {} |
| 202 | + |
| 203 | +NetworkTablesTelemetryBackend::~NetworkTablesTelemetryBackend() = default; |
| 204 | + |
| 205 | +wpi::TelemetryEntry& NetworkTablesTelemetryBackend::GetEntry( |
| 206 | + std::string_view path) { |
| 207 | + std::scoped_lock lock{m_mutex}; |
| 208 | + return m_entries.try_emplace(path, m_inst, m_prefix, path).first->second; |
| 209 | +} |
| 210 | + |
| 211 | +bool NetworkTablesTelemetryBackend::HasSchema( |
| 212 | + std::string_view schemaName) const { |
| 213 | + return m_inst.HasSchema(schemaName); |
| 214 | +} |
| 215 | + |
| 216 | +void NetworkTablesTelemetryBackend::AddSchema(std::string_view schemaName, |
| 217 | + std::string_view type, |
| 218 | + std::span<const uint8_t> schema) { |
| 219 | + m_inst.AddSchema(schemaName, type, schema); |
| 220 | +} |
| 221 | + |
| 222 | +void NetworkTablesTelemetryBackend::AddSchema(std::string_view schemaName, |
| 223 | + std::string_view type, |
| 224 | + std::string_view schema) { |
| 225 | + m_inst.AddSchema(schemaName, type, schema); |
| 226 | +} |
0 commit comments