1#ifndef BITSIM_SENSORS_BACKENDS_MQTT_SENSOR_CHANNEL_H
2#define BITSIM_SENSORS_BACKENDS_MQTT_SENSOR_CHANNEL_H
4#include "bitsim/bitsim_config.h"
9#include "bitsim/extern/nlohmann/json/json.hpp"
36 using Clock = std::chrono::steady_clock;
40 std::string
broker{
"tcp://localhost:1883"};
51 void read(
const nlohmann::json &j);
81 template <
typename Receive>
void read(Receive &&receive);
101 std::unique_ptr<network::MqttSubscriber> own_subscriber_;
104 std::optional<Clock::time_point> last_received_;
111 auto *sub = subscriber();
112 if (!sub || !sub->is_connected())
114 throw std::logic_error(
"The MQTT sensor channel is not connected: call connect()");
117 while (
const auto text = sub->poll(std::chrono::milliseconds(0)))
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
Definition mqtt_sensor_channel.h:39
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