|
| 1 | +#include "DummyClient.h" |
| 2 | +#include <cover/coVRPluginSupport.h> |
| 3 | + |
| 4 | +#include <iostream> |
| 5 | +#include <algorithm> |
| 6 | + |
| 7 | +namespace opencover { namespace dataclient { |
| 8 | + |
| 9 | +DummyClient::DummyClient(const std::string& name) |
| 10 | + : ui::Owner(name, cover->ui) |
| 11 | + , m_name(name) |
| 12 | +{ |
| 13 | + // Create the main menu for the dummy client |
| 14 | + m_menu = std::make_unique<ui::Menu>(m_name, this); |
| 15 | + |
| 16 | + // Create connect/disconnect button |
| 17 | + m_connectButton = std::make_unique<ui::Button>(m_menu.get(), "Connect"); |
| 18 | + m_connectButton->setState(false); |
| 19 | + m_connectButton->setCallback([this](bool state) { |
| 20 | + if (state) { |
| 21 | + connect(); |
| 22 | + } else { |
| 23 | + disconnect(); |
| 24 | + } |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +DummyClient::~DummyClient() |
| 29 | +{ |
| 30 | + disconnect(); |
| 31 | +} |
| 32 | + |
| 33 | +void DummyClient::connect() |
| 34 | +{ |
| 35 | + if (m_connected) |
| 36 | + return; |
| 37 | + |
| 38 | + m_connected = true; |
| 39 | + m_connectButton->setState(true); |
| 40 | + m_connectButton->setText("Disconnect"); |
| 41 | + statusChanged(); |
| 42 | + std::cout << "DummyClient '" << m_name << "' connected" << std::endl; |
| 43 | +} |
| 44 | + |
| 45 | +void DummyClient::disconnect() |
| 46 | +{ |
| 47 | + if (!m_connected) |
| 48 | + return; |
| 49 | + |
| 50 | + m_connected = false; |
| 51 | + m_connectButton->setState(false); |
| 52 | + m_connectButton->setText("Connect"); |
| 53 | + statusChanged(); |
| 54 | + std::cout << "DummyClient '" << m_name << "' disconnected" << std::endl; |
| 55 | +} |
| 56 | + |
| 57 | +bool DummyClient::isConnected() const |
| 58 | +{ |
| 59 | + return m_connected; |
| 60 | +} |
| 61 | + |
| 62 | +ObserverHandle DummyClient::observeNode(const std::string& name) |
| 63 | +{ |
| 64 | + // Check if node already exists |
| 65 | + for (auto& pair : m_nodes) { |
| 66 | + if (pair.second && pair.second->name == name) { |
| 67 | + std::cout << "DummyClient: Node '" << name << "' already observed" << std::endl; |
| 68 | + return ObserverHandle(pair.first, this); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Create new node with default type (double) and as scalar |
| 73 | + size_t nodeId = m_nextNodeId++; |
| 74 | + auto node = std::make_unique<DummyNode>(); |
| 75 | + node->name = name; |
| 76 | + |
| 77 | + // Create slider for this node |
| 78 | + createSliderForNode(node.get()); |
| 79 | + |
| 80 | + m_nodes[nodeId] = std::move(node); |
| 81 | + |
| 82 | + std::cout << "DummyClient: Observing node '" << name << "' with ID " << nodeId << std::endl; |
| 83 | + |
| 84 | + return ObserverHandle(nodeId, this); |
| 85 | +} |
| 86 | + |
| 87 | +void DummyClient::createSliderForNode(DummyNode* node) |
| 88 | +{ |
| 89 | + if (!node || node->slider) |
| 90 | + return; |
| 91 | + |
| 92 | + // Create a slider for this node |
| 93 | + node->slider = new ui::Slider(m_menu.get(), node->name); |
| 94 | + node->slider->setBounds(0.0, 360.0); |
| 95 | + |
| 96 | + // Set callback to update the node value when slider changes |
| 97 | + node->slider->setCallback([node](double value, bool released) { |
| 98 | + node->values.push_back(value); |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +double DummyClient::getNumericScalar(const std::string& name, double* timestep) |
| 103 | +{ |
| 104 | + auto* node = findNode(name); |
| 105 | + if (!node) { |
| 106 | + std::cerr << "DummyClient: Node '" << name << "' not found" << std::endl; |
| 107 | + return 0.0; |
| 108 | + } |
| 109 | + |
| 110 | + if (timestep) { |
| 111 | + *timestep = 0.0; // Dummy client doesn't track real timestamps |
| 112 | + } |
| 113 | + auto v = node->values.front(); |
| 114 | + if(node->values.size() > 1) |
| 115 | + node->values.pop_front(); |
| 116 | + return v; |
| 117 | +} |
| 118 | + |
| 119 | +double DummyClient::getNumericScalar(const ObserverHandle& handle, double* timestep) |
| 120 | +{ |
| 121 | + DummyNode *node = nullptr; |
| 122 | + for(const auto& pair : m_nodes) { |
| 123 | + if (handle == pair.first) { |
| 124 | + node = pair.second.get(); |
| 125 | + break; |
| 126 | + } |
| 127 | + } |
| 128 | + if(!node) |
| 129 | + return 0.0; |
| 130 | + |
| 131 | + if (timestep) { |
| 132 | + *timestep = 0.0; // Dummy client doesn't track real timestamps |
| 133 | + } |
| 134 | + auto v = node->values.front(); |
| 135 | + if(node->values.size() > 1) |
| 136 | + node->values.pop_front(); |
| 137 | + return v; |
| 138 | +} |
| 139 | + |
| 140 | +size_t DummyClient::numNodeUpdates(const std::string& name) |
| 141 | +{ |
| 142 | + auto* node = findNode(name); |
| 143 | + if (!node) { |
| 144 | + return 0; |
| 145 | + } |
| 146 | + return node->values.size(); |
| 147 | +} |
| 148 | + |
| 149 | +std::unique_ptr<detail::MultiDimensionalArrayBase> |
| 150 | +DummyClient::getArrayImpl(std::type_index type, const std::string& name) |
| 151 | +{ |
| 152 | + if(type != std::type_index(typeid(double))) |
| 153 | + return nullptr; |
| 154 | + auto node = findNode(name); |
| 155 | + if (!node) |
| 156 | + return nullptr; |
| 157 | + auto value = std::make_unique<dataclient::MultiDimensionalArray<double>>(); |
| 158 | + value->dimensions = {1}; |
| 159 | + value->data = {getNumericScalar(name)}; |
| 160 | + return value; |
| 161 | +} |
| 162 | + |
| 163 | +std::vector<std::string> DummyClient::getNodesWith(std::type_index type, bool isScalar) const |
| 164 | +{ |
| 165 | + std::vector<std::string> result; |
| 166 | + if(type != std::type_index(typeid(double))) |
| 167 | + return result; // Only double type supported in dummy client |
| 168 | + for (const auto& pair : m_nodes) { |
| 169 | + result.push_back(pair.second->name); |
| 170 | + } |
| 171 | + return result; |
| 172 | +} |
| 173 | + |
| 174 | +std::vector<std::string> DummyClient::getNodesWith(bool isArithmetic, bool isScalar) const |
| 175 | +{ |
| 176 | + std::vector<std::string> result; |
| 177 | + if(!isScalar) |
| 178 | + return result; // Only scalars supported in dummy client |
| 179 | + for (const auto& pair : m_nodes) { |
| 180 | + result.push_back(pair.second->name); |
| 181 | + } |
| 182 | + return result; |
| 183 | +} |
| 184 | + |
| 185 | +DummyNode* DummyClient::findNode(const std::string& name) |
| 186 | +{ |
| 187 | + for (auto& pair : m_nodes) { |
| 188 | + if (pair.second && pair.second->name == name) { |
| 189 | + return pair.second.get(); |
| 190 | + } |
| 191 | + } |
| 192 | + return nullptr; |
| 193 | +} |
| 194 | + |
| 195 | +DummyNode* DummyClient::findNode(size_t id) |
| 196 | +{ |
| 197 | + auto it = m_nodes.find(id); |
| 198 | + if (it != m_nodes.end()) { |
| 199 | + return it->second.get(); |
| 200 | + } |
| 201 | + return nullptr; |
| 202 | +} |
| 203 | + |
| 204 | +void DummyClient::unregisterNode(size_t id) |
| 205 | +{ |
| 206 | + auto it = m_nodes.find(id); |
| 207 | + if (it != m_nodes.end()) { |
| 208 | + std::cout << "DummyClient: Unregistering node '" << it->second->name << "'" << std::endl; |
| 209 | + m_nodes.erase(it); |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +}} // namespace opencover::dataclient |
0 commit comments