Motion Master 6.0.0-alpha.50
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
47 public:
50 using PublishFn = std::function<void(std::string topic, std::string json)>;
51
53 explicit MonitoringManager(DeviceManager& deviceManager);
54
57
60
62 void setPublish(PublishFn publish);
63
74 std::expected<Monitoring, std::string> create(Monitoring config);
75
78 bool remove(const std::string& topic);
79
82 std::optional<nlohmann::json> get(const std::string& topic) const;
83
85 nlohmann::json list() const;
86
88 void start();
89
91 void stop();
92
95 void sampleAll();
96
98 std::size_t monitoringCount() const;
99
101 std::size_t polledSdoCount() const;
102
103 private:
105 enum class Source { Pdo, Sdo };
106
109 struct ParamPlan {
110 uint16_t devicePosition = 0;
111 uint16_t index = 0;
112 uint8_t subindex = 0;
113 Source source = Source::Pdo;
114 std::optional<DeviceManager::PdoSampleSpec> pdoSpec; // PDO only
115 };
116
120 struct Sample {
121 int64_t timestampUs = 0;
122 std::vector<std::optional<DeviceParameterValue>> values;
123 };
124
126 struct Entry {
127 Monitoring config;
128 std::vector<ParamPlan> plans;
129 uint64_t cursor = 0; // next recorder sequence number to deliver ([cursor, head))
130 bool cursorPrimed = false; // false until the first flush seeds cursor from recorderHead()
131 uint64_t imageGeneration = 0; // processImageGeneration the PDO specs were captured under
132 std::chrono::steady_clock::time_point nextDue{}; // default (epoch) => due on the next wake
133 };
134
135 void flushEntry(Entry& entry); // decode + publish [cursor, head); assumes mutex_ held
136 void recaptureIfRemapped(
137 Entry& entry); // re-classify source + re-capture specs; assumes mutex_ held
138 static nlohmann::json resourceJson(const Entry& entry); // assumes mutex_ held
139
142 void run();
143
144 DeviceManager& deviceManager_;
145 ParameterRefresher refresher_;
146 mutable std::mutex mutex_;
147 std::condition_variable cv_;
148 std::map<std::string, Entry> entries_;
149 PublishFn publish_;
150 bool running_ = false;
151 std::thread thread_;
152};
153
154} // namespace mm::node
Owns the fieldbus driver and node collection, and drives PDO exchange.
Definition device_manager.h:181
Owns the active monitorings and turns each into a lossless stream of recorded rows.
Definition monitoring_manager.h:46
std::size_t polledSdoCount() const
Number of distinct SDO objects currently polled by the refresher. For tests / status.
Definition monitoring_manager.cc:222
std::expected< Monitoring, std::string > create(Monitoring config)
Validates config, classifies its parameters, registers SDO ones with the refresher,...
Definition monitoring_manager.cc:48
void setPublish(PublishFn publish)
Sets the batch publish callback. Call before start().
Definition monitoring_manager.cc:43
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:144
bool remove(const std::string &topic)
Removes a monitoring, releasing its SDO parameters from the refresher.
Definition monitoring_manager.cc:128
MonitoringManager & operator=(const MonitoringManager &)=delete
void sampleAll()
Flushes every monitoring once: delivers each one's recorded cycles since its last flush....
Definition monitoring_manager.cc:210
void start()
Starts the owned refresher (and, once added, the sampler thread). Idempotent.
Definition monitoring_manager.cc:162
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:50
~MonitoringManager()
Stops the owned refresher (and, once added, the sampler thread).
Definition monitoring_manager.cc:41
nlohmann::json list() const
Returns all monitoring resources as a JSON array, in topic order.
Definition monitoring_manager.cc:153
void stop()
Stops the owned refresher (and, once added, the sampler thread). Idempotent.
Definition monitoring_manager.cc:173
std::size_t monitoringCount() const
Number of registered monitorings. For tests / status.
Definition monitoring_manager.cc:217
Definition http_server.h:16
A client-defined recording of a set of parameters streamed over time.
Definition monitoring.h:28