13#include "protective_stop_machine/announce.hpp"
14#include "protective_stop_machine/backend.hpp"
16namespace protective_stop_machine
44inline const char * checkin_state(
const MachineSnapshot & snapshot)
46 if (snapshot.running || snapshot.active_remotes > 0) {
49 if (snapshot.reachable) {
73inline std::string build_checkin_payload(
75 const std::string & app_version,
76 const std::string & idf_version,
79 const std::string & local_ip,
80 const std::string & tailscale_ip,
83 char device_id_text[16];
84 std::snprintf(device_id_text,
sizeof(device_id_text),
"%08x", machine_id);
86 std::string payload =
"{\"device_id\":\"";
87 payload += device_id_text;
88 payload +=
"\",\"app_version\":\"";
89 payload += json_escape(app_version);
90 payload +=
"\",\"idf_version\":\"";
91 payload += json_escape(idf_version);
92 payload +=
"\",\"uptime_s\":";
93 payload += std::to_string(uptime_s);
94 payload +=
",\"device_type\":\"machine\",\"check_interval_s\":";
95 payload += std::to_string(check_interval_s);
96 payload +=
",\"tailscale_ip\":\"";
97 payload += json_escape(tailscale_ip);
98 payload +=
"\",\"local_ip\":\"";
99 payload += json_escape(local_ip);
100 payload +=
"\",\"state\":\"";
101 payload += checkin_state(snapshot);
102 payload +=
"\",\"rollback_occurred\":false,\"running\":";
103 payload += snapshot.running ?
"true" :
"false";
104 payload +=
",\"active_remotes\":";
105 payload += std::to_string(snapshot.active_remotes);
112inline std::string checkin_endpoint(
const std::string & base_url)
114 std::string trimmed_base = base_url;
115 while (!trimmed_base.empty() && trimmed_base.back() ==
'/') {
116 trimmed_base.pop_back();
118 return trimmed_base +
"/api/v1/checkin";
151 return enabled_.load();
158 bool post_once(
const std::string & endpoint,
const std::string & payload,
const std::string & bearer_key);
159 void handle_response(
const std::string & body);
162 uint32_t machine_id_;
164 std::chrono::steady_clock::time_point start_time_{std::chrono::steady_clock::now()};
167 std::atomic<bool> running_{
false};
168 std::atomic<bool> enabled_{
false};
bool start()
Launches the check-in thread.
bool enabled() const
True while the check-in thread is running.
Definition fleet_checkin.hpp:149
FleetCheckin(FleetCheckinConfig config, uint32_t machine_id, std::function< MachineSnapshot()> snapshot_fn)
void stop()
Stops the thread and joins. Idempotent.
Deployment values for the fleet check-in.
Definition fleet_checkin.hpp:25
std::string idf_version
Runtime tag, e.g. "ros2-jazzy".
Definition fleet_checkin.hpp:33
std::string key_file
Path whose first line is the bearer token, chmod 600.
Definition fleet_checkin.hpp:29
std::string base_url
http://host[:port]. Empty disables the check-in.
Definition fleet_checkin.hpp:27
std::string app_version
ament package version of protective_stop_machine.
Definition fleet_checkin.hpp:31
int interval_s
Seconds between check-ins. The ESP32 default is 300.
Definition fleet_checkin.hpp:35
double http_timeout_s
Matches the ESP32's 15s check-in timeout.
Definition fleet_checkin.hpp:37
One immutable read of machine state, mapped straight onto the ROS messages.
Definition backend.hpp:64