1#ifndef DAQ_ELODIN_CLIENT_HPP
2#define DAQ_ELODIN_CLIENT_HPP
14#include <unordered_map>
40 bool connect(
const std::string& host, uint16_t
port = 2240);
59 template <
typename MessageType>
60 bool publish(uint16_t message_id,
const MessageType& message);
63 template <
typename MessageType>
64 bool publish(
const std::array<uint8_t, 2>& table_id,
const MessageType& message) {
65 uint16_t
msg_id = (
static_cast<uint16_t
>(table_id[0]) << 8) | table_id[1];
85 bool subscribe_tables(
const std::vector<std::pair<uint8_t, uint8_t>>& table_ids);
118 void register_table(
const std::array<uint8_t, 2>& table_id,
const std::string& name);
126 bool send_msg(
const std::array<uint8_t, 2>& packet_id,
const std::vector<uint8_t>& data);
150 ssize_t
read_data(
void* buffer,
size_t max_len);
173 ssize_t
read_packet(uint8_t* packet_buffer,
size_t max_len);
178 template <
typename MessageType>
179 std::vector<uint8_t>
serialize_msg(uint16_t message_id,
const MessageType& msg);
190 std::unique_ptr<daq_comms::transport::TCPClient>
socket_;
206 return {
static_cast<uint8_t
>((message_id >> 8) & 0xFF),
207 static_cast<uint8_t
>(message_id & 0xFF)};
215template <
typename MessageType>
230 std::array<uint8_t, 2>,
242 ElodinMsg elodin_msg(header, msg);
248 constexpr size_t header_size = 8;
249 std::vector<uint8_t>
result(header_size + msg_size);
250 elodin_msg.serialize(
reinterpret_cast<uint8_t*
>(
result.data()));
259template <
typename MessageType>
278 if (!
socket_->write_all(buf.data(), buf.size())) {
284 }
catch (
const std::exception& e) {
285 last_error_ =
"Publish error: " + std::string(e.what());
bool flush_batch()
Flush (send) the accumulated batch in a single TCP write.
bool publish(const std::array< uint8_t, 2 > &table_id, const MessageType &message)
bool is_connected() const
Check if connected.
bool send_msg(const std::array< uint8_t, 2 > &packet_id, const std::vector< uint8_t > &data)
Send a MSG packet (for VTable registration, etc.).
bool read_bytes_exact(uint8_t *buffer, size_t len)
Blocking read of exactly len bytes (for payload reads after header).
ssize_t read_data(void *buffer, size_t max_len)
Read incoming data packet from Elodin (non-blocking).
bool connect_locked(const std::string &host, uint16_t port)
std::string last_error() const
Get last error message.
void set_recv_timeout_ms(int timeout_ms)
Set socket receive timeout so read_packet() can yield.
bool publish(uint16_t message_id, const MessageType &message)
Publish a message to a table.
void disconnect()
Disconnect from database.
bool subscribe_tables(const std::vector< std::pair< uint8_t, uint8_t > > &table_ids)
Subscribe to specific tables by ID.
std::unique_ptr< daq_comms::transport::TCPClient > socket_
void begin_batch()
Begin batching — subsequent publish() calls go into an internal buffer.
bool is_batching() const
Returns true if we're between begin_batch() and flush_batch().
void register_table(const std::array< uint8_t, 2 > &table_id, const std::string &name)
Register a table ID with a name (for debugging/logging).
std::vector< uint8_t > serialize_msg(uint16_t message_id, const MessageType &msg)
Serialize a message for Elodin (public for testing).
bool connect(const std::string &host, uint16_t port=2240)
Connect to Elodin database.
ssize_t read_packet(uint8_t *packet_buffer, size_t max_len)
Read complete packet from Elodin (header + payload).
std::mutex publish_mutex_
std::vector< uint8_t > batch_buffer_
bool subscribe_table(uint8_t hi, uint8_t lo, uint8_t request_id)
One table, under request_id, which the DB echoes if it refuses it (ErrorResponse [0xE0,...
void flush_buffer()
Flush the write buffer to the database.
bool read_packet_header(uint8_t *header_buffer)
Read packet header from Elodin (12 bytes).
bool reconnect()
Attempt to reconnect using the last-used host/port.
std::unordered_map< uint16_t, std::string > table_names_
static const emmc_port_t port
std::array< uint8_t, 2 > message_id_to_packet_id(uint16_t message_id)
std::array< uint8_t, 2 > msg_id(std::string_view str)
Motorized ball valve position: raw ADC counts -> % open.
static constexpr std::size_t value
CommsMessage - Template-based message serialization.