Broker: log why a DECTRIS run ends in a detector error, reuse one SIMPLON connection

"Error in detector operation" was raised whenever the wait-for-idle loop in
JFJochServices::Stop() ended in any state other than idle. For DECTRIS that
includes a single failed status request (connect/read timeout, non-200), which
GetState() silently turned into NOT_CONNECTED, and a cancel whose disarm
failed - none of them distinguishable in the log.

- DectrisDetectorWrapper::GetState() logs the failure reason before returning
  NOT_CONNECTED.
- Stop() logs the state that ended the loop, whether the stop command failed,
  and what the detector reports (DetectorWrapper::GetErrorDescription(); for
  DECTRIS the SIMPLON state and the detector status "error" list).
- Cancel() logs why the stop command failed.
- A non-200 GET now reports the HTTP status and body (it printed "Success").
- DectrisSimplonClient keeps one keep-alive httplib::Client, serialised by a
  mutex, instead of opening a new TCP connection per request.
- The wait-for-idle loop polls every 100 ms instead of every 5 ms.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-10-08 22:45:42 +02:00
1 parent eac0833c88
commit 9807bc40df
8 files changed
+152 -23

No files matched your search

+32 -20
View File
@@ -21,10 +21,12 @@ std::string to_string(SimplonState state) {
}
DectrisSimplonClient::DectrisSimplonClient(const std::string &addr, uint16_t in_port)
: hostname(addr), port(in_port) {
: hostname(addr), port(in_port), http(std::make_unique<httplib::Client>(addr, in_port)) {
http->set_keep_alive(true);
}
DectrisSimplonClient::~DectrisSimplonClient() = default;
void DectrisSimplonClient::SendDetectorCommand(SimplonDetectorCommand cmd) {
std::string key;
switch (cmd) {
@@ -46,16 +48,18 @@ void DectrisSimplonClient::SendDetectorCommand(SimplonDetectorCommand cmd) {
}
auto addr = GenAddr(SimplonModule::Detector, SimplonTask::Command, key);
httplib::Client cli_cmd(hostname, port);
cli_cmd.set_connection_timeout(std::chrono::milliseconds(500));
cli_cmd.set_read_timeout(std::chrono::minutes(20)); // Initialize or disarm might take a lot of time
cli_cmd.set_write_timeout(std::chrono::minutes(20)); // Initialize or disarm might take a lot of time
std::lock_guard lg(http_mutex);
http->set_connection_timeout(std::chrono::milliseconds(500));
http->set_read_timeout(std::chrono::minutes(20)); // Initialize or disarm might take a lot of time
http->set_write_timeout(std::chrono::minutes(20)); // Initialize or disarm might take a lot of time
auto res = cli_cmd.Post(addr);
auto res = http->Post(addr);
if (!res || res->status != httplib::StatusCode::OK_200) {
std::string err_msg = "Command failed " + key;
if (res) {
if (!res)
err_msg += "; " + to_string(res.error());
else {
err_msg += "; HTTP" + std::to_string(res->status);
if (!res->body.empty())
err_msg += "; " + res->body;
@@ -70,16 +74,18 @@ void DectrisSimplonClient::SetConfig(SimplonModule element, const std::string &k
nlohmann::json j;
j["value"] = value;
httplib::Client cli_cmd(hostname, port);
cli_cmd.set_connection_timeout(std::chrono::seconds(1));
cli_cmd.set_read_timeout(std::chrono::minutes(20)); // Initialize or disarm might take a lot of time
cli_cmd.set_write_timeout(std::chrono::minutes(20)); // Initialize or disarm might take a lot of time
std::lock_guard lg(http_mutex);
http->set_connection_timeout(std::chrono::seconds(1));
http->set_read_timeout(std::chrono::minutes(20)); // Initialize or disarm might take a lot of time
http->set_write_timeout(std::chrono::minutes(20)); // Initialize or disarm might take a lot of time
auto res = cli_cmd.Post(addr, j.dump(), "application/json");
auto res = http->Post(addr, j.dump(), "application/json");
if (!res || res->status != httplib::StatusCode::OK_200) {
std::string err_msg = "Configure failed " + key;
if (res) {
if (!res)
err_msg += "; " + to_string(res.error());
else {
err_msg += "; HTTP" + std::to_string(res->status);
if (!res->body.empty())
err_msg += "; " + res->body;
@@ -136,11 +142,11 @@ std::string DectrisSimplonClient::GenAddr(SimplonModule element, SimplonTask tas
SimplonConfig
DectrisSimplonClient::GetConfig(SimplonModule element, SimplonTask task, const std::string &key) {
httplib::Client cli_simple(hostname, port);
cli_simple.set_connection_timeout(std::chrono::milliseconds(500));
cli_simple.set_read_timeout(std::chrono::seconds(1));
cli_simple.set_write_timeout(std::chrono::seconds(1));
auto res = cli_simple.Get(GenAddr(element, task, key));
std::lock_guard lg(http_mutex);
http->set_connection_timeout(std::chrono::milliseconds(500));
http->set_read_timeout(std::chrono::seconds(1));
http->set_write_timeout(std::chrono::seconds(1));
auto res = http->Get(GenAddr(element, task, key));
if (!res) {
throw JFJochException(JFJochExceptionCategory::SimplonError,
@@ -148,7 +154,8 @@ DectrisSimplonClient::GetConfig(SimplonModule element, SimplonTask task, const s
}
if (res->status != httplib::StatusCode::OK_200) {
throw JFJochException(JFJochExceptionCategory::SimplonError,
"Get key " + key + " failed: " + to_string(res.error()));
"Get key " + key + " failed: HTTP" + std::to_string(res->status)
+ (res->body.empty() ? "" : "; " + res->body));
}
try {
@@ -319,6 +326,11 @@ SimplonState DectrisSimplonClient::GetState() {
"State unknown " + res);
}
// List of error messages the detector reports (SIMPLON detector status "error"), as JSON text
std::string DectrisSimplonClient::GetErrorStatus() {
return GetStatus(SimplonModule::Detector, "error").dump();
}
float DectrisSimplonClient::GetHumidity() {
return GetStatus(SimplonModule::Detector, "humidity");
}