bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
mqtt_sensor_channel.h
Go to the documentation of this file.
1#ifndef BITSIM_SENSORS_BACKENDS_MQTT_SENSOR_CHANNEL_H
2#define BITSIM_SENSORS_BACKENDS_MQTT_SENSOR_CHANNEL_H
3
4#include "bitsim/bitsim_config.h"
5
6#ifdef BITSIM_MQTT
7
9#include "bitsim/extern/nlohmann/json/json.hpp"
11
12#include <chrono>
13#include <memory>
14#include <optional>
15#include <string>
16
17namespace bitsim
18{
19namespace sensors::backends
20{
21
34{
35 public:
36 using Clock = std::chrono::steady_clock;
37
38 struct Config
39 {
40 std::string broker{"tcp://localhost:1883"};
41 std::string topic;
42
47
51 void read(const nlohmann::json &j);
52 };
53
57 MQTT_SensorChannel() = default;
58
63 explicit MQTT_SensorChannel(network::MqttSubscriber &subscriber);
64
65 const Config &config() const noexcept { return config_; }
66 void set_config(const Config &config) { config_ = config; }
67
72 void connect();
73
74 bool is_connected() const;
75
81 template <typename Receive> void read(Receive &&receive);
82
87 void mark_received();
88
92 bool is_alive() const;
93
94 uint_t n_received() const noexcept { return n_received_; }
95 uint_t n_rejected() const noexcept { return n_rejected_; }
96
97 private:
98 network::MqttSubscriber *subscriber();
99
100 Config config_;
101 std::unique_ptr<network::MqttSubscriber> own_subscriber_;
102 network::MqttSubscriber *borrowed_subscriber_{nullptr};
103
104 std::optional<Clock::time_point> last_received_;
105 uint_t n_received_{0};
106 uint_t n_rejected_{0};
107};
108
109template <typename Receive> void MQTT_SensorChannel::read(Receive &&receive)
110{
111 auto *sub = subscriber();
112 if (!sub || !sub->is_connected())
113 {
114 throw std::logic_error("The MQTT sensor channel is not connected: call connect()");
115 }
116
117 while (const auto text = sub->poll(std::chrono::milliseconds(0)))
118 {
119 if (receive(*text))
120 {
122 }
123 else
124 {
125 ++n_rejected_;
126 }
127 }
128}
129
130} // namespace sensors::backends
131} // namespace bitsim
132
133#endif
134#endif // BITSIM_SENSORS_BACKENDS_MQTT_SENSOR_CHANNEL_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
Definition mqtt_subscriber.h:24
void read(Receive &&receive)
Pass every message that has arrived to receive, oldest first, without waiting. receive returns true i...
Definition mqtt_sensor_channel.h:109
const Config & config() const noexcept
Definition mqtt_sensor_channel.h:65
uint_t n_rejected() const noexcept
Definition mqtt_sensor_channel.h:95
void connect()
Connect to the broker, unless the subscriber already is.
Definition mqtt_sensor_channel.cpp:32
void mark_received()
Record that a valid reading arrived now. read() does it for the messages receive accepts; call it whe...
Definition mqtt_sensor_channel.cpp:56
bool is_connected() const
Definition mqtt_sensor_channel.cpp:50
std::chrono::steady_clock Clock
Definition mqtt_sensor_channel.h:36
uint_t n_received() const noexcept
Definition mqtt_sensor_channel.h:94
bool is_alive() const
Definition mqtt_sensor_channel.cpp:62
void set_config(const Config &config)
Definition mqtt_sensor_channel.h:66
MQTT_SensorChannel()=default
A channel that makes its own subscriber when connect() is called.
Definition camera_backend_base.h:19
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
std::string broker
Definition mqtt_sensor_channel.h:40
std::string topic
Definition mqtt_sensor_channel.h:41
real_t timeout
No reading for this long means the board is gone [s].
Definition mqtt_sensor_channel.h:46
void read(const nlohmann::json &j)
Read the keys broker, topic and timeout, all optional.
Definition mqtt_sensor_channel.cpp:14