Motion Master 6.0.0-alpha.86
Next-generation motion control software
Loading...
Searching...
No Matches
monitoring_manager.h
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <condition_variable>
5#include <cstddef>
6#include <cstdint>
7#include <expected>
8#include <functional>
9#include <map>
10#include <mutex>
11#include <nlohmann/json_fwd.hpp>
12#include <optional>
13#include <string>
14#include <thread>
15#include <vector>
16
17#include "node/device_manager.h"
18#include "node/monitoring.h"
20
21namespace mm::node {
22
63 public:
66 using PublishFn = std::function<void(std::string topic, std::string json)>;
67
69 explicit MonitoringManager(DeviceManager& deviceManager);
70
73
76
78 void setPublish(PublishFn publish);
79
90 std::expected<Monitoring, std::string> create(Monitoring config);
91
94 bool remove(const std::string& topic);
95
98 std::optional<nlohmann::json> get(const std::string& topic) const;
99
101 nlohmann::json list() const;
102
104 void start();
105
107 void stop();
108
131 void keepFresh(uint16_t devicePosition, uint16_t index, uint8_t subindex,
132 std::chrono::milliseconds period);
133
135 void stopKeepingFresh(uint16_t devicePosition, uint16_t index, uint8_t subindex);
136
139 void sampleAll();
140
142 std::size_t monitoringCount() const;
143
145 std::size_t polledSdoCount() const;
146
147 private:
149 enum class Source { Pdo, Sdo };
150
153 struct ParamPlan {
154 uint16_t devicePosition = 0;
155 uint16_t index = 0;
156 uint8_t subindex = 0;
157 Source source = Source::Pdo;
158 std::optional<DeviceManager::PdoSampleSpec> pdoSpec; // PDO only
159 };
160
164 struct Sample {
165 int64_t timestampUs = 0;
166 std::vector<std::optional<DeviceParameterValue>> values;
167 };
168
170 struct Entry {
171 Monitoring config;
172 std::vector<ParamPlan> plans;
173 uint64_t epoch = 0; // identifies this registration across a remove + re-create
174 uint64_t cursor = 0; // next recorder sequence number to deliver ([cursor, head))
175 bool cursorPrimed = false; // false until the first flush seeds cursor from recorderHead()
176 uint64_t imageGeneration = 0; // processImageGeneration the PDO specs were captured under
177 std::chrono::steady_clock::time_point nextDue{}; // default (epoch) => due on the next wake
178 };
179
189 struct FlushState {
190 std::string topic;
191 uint64_t epoch = 0;
192 std::chrono::milliseconds interval{}; // poll period handed to the refresher on an SDO switch
193 std::vector<ParamPlan> plans;
194 uint64_t cursor = 0;
195 bool cursorPrimed = false;
196 uint64_t imageGeneration = 0;
197 };
198
201 std::vector<FlushState> takeDue(std::chrono::steady_clock::time_point now, bool forceAll);
202
205 void flushDetached(FlushState& state, const PublishFn& publish);
206
209 void commitFlush(const FlushState& state);
210
213 void flushDue(std::unique_lock<std::mutex>& lock, std::chrono::steady_clock::time_point now,
214 bool forceAll);
215
218 void recaptureIfRemapped(FlushState& state);
219
220 static nlohmann::json resourceJson(const Entry& entry); // assumes mutex_ held
221
224 void run();
225
226 DeviceManager& deviceManager_;
227 ParameterRefresher refresher_;
228 mutable std::mutex mutex_;
229 std::condition_variable cv_;
230 std::map<std::string, Entry> entries_;
231 uint64_t nextEpoch_ = 1;
232 PublishFn publish_;
233 bool running_ = false;
234 std::thread thread_;
235};
236
237} // namespace mm::node
Owns the fieldbus driver and node collection, and drives PDO exchange.
Definition device_manager.h:287
Owns the active monitorings and turns each into a lossless stream of recorded rows.
Definition monitoring_manager.h:62
std::size_t polledSdoCount() const
Number of distinct SDO objects currently polled by the refresher. For tests / status.
Definition monitoring_manager.cc:289
std::expected< Monitoring, std::string > create(Monitoring config)
Validates config, classifies its parameters, registers SDO ones with the refresher,...
Definition monitoring_manager.cc:49
void setPublish(PublishFn publish)
Sets the batch publish callback. Call before start().
Definition monitoring_manager.cc:44
MonitoringManager(const MonitoringManager &)=delete
std::optional< nlohmann::json > get(const std::string &topic) const
Returns the monitoring resource as JSON (config + per-parameter source + buffer fill),...
Definition monitoring_manager.cc:154
bool remove(const std::string &topic)
Removes a monitoring, releasing its SDO parameters from the refresher.
Definition monitoring_manager.cc:138
MonitoringManager & operator=(const MonitoringManager &)=delete
void stopKeepingFresh(uint16_t devicePosition, uint16_t index, uint8_t subindex)
Drops a reference taken by keepFresh. Polling stops when the last one is released.
Definition monitoring_manager.cc:223
void keepFresh(uint16_t devicePosition, uint16_t index, uint8_t subindex, std::chrono::milliseconds period)
Keeps an SDO-only object's value fresh in the background, for a cyclic task to read.
Definition monitoring_manager.cc:215
void sampleAll()
Flushes every monitoring once: delivers each one's recorded cycles since its last flush....
Definition monitoring_manager.cc:228
void start()
Starts the owned refresher (and, once added, the sampler thread). Idempotent.
Definition monitoring_manager.cc:172
std::function< void(std::string topic, std::string json)> PublishFn
Publishes one batch: json (a {"type":"monitoring","topic",...} envelope) under topic....
Definition monitoring_manager.h:66
~MonitoringManager()
Stops the owned refresher (and, once added, the sampler thread).
Definition monitoring_manager.cc:42
nlohmann::json list() const
Returns all monitoring resources as a JSON array, in topic order.
Definition monitoring_manager.cc:163
void stop()
Stops the owned refresher (and, once added, the sampler thread). Idempotent.
Definition monitoring_manager.cc:183
std::size_t monitoringCount() const
Number of registered monitorings. For tests / status.
Definition monitoring_manager.cc:284
uint8_t subindex
Definition esi_entry.cc:283
Definition bus_health_source.h:7
A client-defined recording of a set of parameters streamed over time.
Definition monitoring.h:28