12#include "protective_stop_machine/backend.hpp"
14namespace protective_stop_machine
35inline std::string json_escape(
const std::string & text)
38 escaped.reserve(text.size() + 8);
39 for (
char character : text) {
57 if (
static_cast<unsigned char>(character) < 0x20) {
58 char escape_sequence[8];
59 std::snprintf(escape_sequence,
sizeof(escape_sequence),
"\\u%04x", character);
60 escaped += escape_sequence;
76inline std::string build_announce_payload(
77 const std::string & name,
int port, uint32_t machine_id,
const MachineSnapshot & snapshot)
79 char machine_id_text[16];
80 std::snprintf(machine_id_text,
sizeof(machine_id_text),
"%08x", machine_id);
82 std::string payload =
"{\"name\":\"";
83 payload += json_escape(name);
84 payload +=
"\",\"port\":";
85 payload += std::to_string(port);
86 payload +=
",\"machine_id\":\"";
87 payload += machine_id_text;
88 payload +=
"\",\"device_type\":\"machine\",\"running\":";
89 payload += snapshot.running ?
"true" :
"false";
90 payload +=
",\"active_remotes\":";
91 payload += std::to_string(snapshot.active_remotes);
92 payload +=
",\"remotes\":[";
94 for (
const auto & remote : snapshot.remotes) {
99 payload +=
"{\"id\":\"";
100 payload += json_escape(remote.device_id);
101 payload +=
"\",\"bond_state\":";
102 payload += std::to_string(
static_cast<int>(remote.bond_state));
103 payload +=
",\"stop_only\":";
104 payload += remote.stop_only ?
"true" :
"false";
105 payload +=
",\"in_use\":";
106 payload += remote.in_use ?
"true" :
"false";
141 return enabled_.load();
146 bool post_once(
const std::string & payload,
const std::string & bearer_key);
150 uint32_t machine_id_;
154 std::atomic<bool> running_{
false};
155 std::atomic<bool> enabled_{
false};
bool enabled() const
True while the check-in thread is running.
Definition announce.hpp:139
bool start()
Launches the check-in thread.
void stop()
Stops the thread and joins. Idempotent.
MachineAnnouncer(AnnounceConfig config, int port, uint32_t machine_id, std::function< MachineSnapshot()> snapshot_fn)
Deployment values for the check-in.
Definition announce.hpp:20
int interval_s
Seconds between check-ins.
Definition announce.hpp:28
std::string url
http://host[:port]/path. Empty disables the announcer.
Definition announce.hpp:22
std::string key_file
Path whose first line is the bearer token, chmod 600.
Definition announce.hpp:24
std::string name
Display name on the console. Empty uses this host's hostname.
Definition announce.hpp:26
double http_timeout_s
HTTP request timeout, in seconds.
Definition announce.hpp:30
One immutable read of machine state, mapped straight onto the ROS messages.
Definition backend.hpp:64