am-freescan/integrations/freescan-service/src/auto_connection.hpp

114 lines
5.0 KiB
C++
Raw Normal View History

#pragma once
#include "common.hpp"
namespace freescan::detail {
enum class AutoConnectionAction { none, connect, recycle };
// Owns retry scheduling and operator intent, not SDK resources. All decisions
// run under the supervisor's command gate; this mutex only protects reporting.
class AutoConnectionPolicy {
mutable std::mutex gate;
bool enabled, paused = false, blocked = false, protectedSession = false;
int interval;
uint64_t attempts = 0, restartCount = 0;
int64_t nextAttempt = 0, lastRestart = 0;
bool lastRestartForced = false;
std::string phase, message;
Json lastError = nullptr;
public:
AutoConnectionPolicy(bool active, int seconds)
: enabled(active), interval(seconds), phase(active ? "waiting" : "disabled"),
message(active ? "等待自动连接SDK。" : "自动连接已关闭。") {}
void pause() {
std::lock_guard<std::mutex> hold(gate);
paused = true;
nextAttempt = 0;
phase = enabled ? "paused" : "disabled";
message = "已手动断开或请求停止,自动连接暂停;手动连接可恢复。";
}
void resume() {
std::lock_guard<std::mutex> hold(gate);
paused = blocked = false;
nextAttempt = 0;
phase = enabled ? "waiting" : "disabled";
message = "已请求手动连接。";
}
void protect(bool active = true) {
std::lock_guard<std::mutex> hold(gate);
protectedSession = active;
}
bool may_restart() const {
std::lock_guard<std::mutex> hold(gate);
return enabled && !paused && !blocked && !protectedSession;
}
void restarted(bool forced, int64_t now) {
std::lock_guard<std::mutex> hold(gate);
++restartCount;
lastRestart = now;
lastRestartForced = forced;
}
AutoConnectionAction decide(const Json &status, int64_t now) {
std::lock_guard<std::mutex> hold(gate);
if (!enabled || paused || blocked)
return AutoConnectionAction::none;
if (protectedSession || status.value("projectReady", false) ||
status.value("scanning", false) || status.value("calibrationActive", false)) {
phase = "session_active";
message = "当前有工程或标定会话,保留数据,不自动重连或扫描。";
nextAttempt = 0;
return AutoConnectionAction::none;
}
if (status.value("busy", false))
return AutoConnectionAction::none;
const bool reset = status.value("restartRequired", false) || status.value("stateUncertain", false);
const bool offline = status.contains("deviceOnline") && status["deviceOnline"] == false;
if (status.value("initialized", false) && !reset && !offline) {
phase = "connected";
message = "SDK已连接。";
lastError = nullptr;
nextAttempt = 0;
return AutoConnectionAction::none;
}
if (now < nextAttempt)
return AutoConnectionAction::none;
++attempts;
phase = reset || status.value("initialized", false) ? "recovering" : "connecting";
message = phase == "recovering" ? "正在重启异常SDK后台,HTTP服务保持运行。" : "正在自动连接SDK。";
nextAttempt = 0;
return reset || status.value("initialized", false) ? AutoConnectionAction::recycle
: AutoConnectionAction::connect;
}
void failed(const ApiError &error, int64_t now, bool cleanupUnconfirmed = false) {
std::lock_guard<std::mutex> hold(gate);
if (paused)
return;
blocked = cleanupUnconfirmed;
phase = blocked ? "blocked" : "retry_wait";
message = blocked ? "SDK后台身份或退出无法核验,已停止自动重启,请检查日志。"
: "SDK连接未成功,将按间隔重试;需要时自动重启SDK后台。";
lastError = {{"code", error.code}, {"message", error.what()}, {"httpStatus", error.status}};
nextAttempt = blocked ? 0 : now + int64_t(interval) * 1000;
}
void connected() {
std::lock_guard<std::mutex> hold(gate);
if (paused)
return;
phase = "connected";
message = "SDK已连接。";
lastError = nullptr;
nextAttempt = 0;
}
Json snapshot() const {
std::lock_guard<std::mutex> hold(gate);
return {{"enabled", enabled}, {"paused", paused}, {"state", phase},
{"attempts", attempts}, {"retryIntervalSeconds", interval},
{"restartCount", restartCount},
{"lastRestartUtc", lastRestart ? Json(utc_from_ms(lastRestart)) : Json(nullptr)},
{"lastRestartForced", lastRestart ? Json(lastRestartForced) : Json(nullptr)},
{"nextRetryUtc", nextAttempt ? Json(utc_from_ms(nextAttempt)) : Json(nullptr)},
{"lastError", lastError}, {"message", message}};
}
};
} // namespace freescan::detail