#pragma once #include "common.hpp" #include namespace freescan::detail { struct SdkCompletion { bool done = false; std::string error; }; inline std::string normalized_result(const Json &value) { if (!value.is_string()) return {}; auto result = value.get(); std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return static_cast(std::tolower(c)); }); return result; } inline bool nonzero_sdk_code(const Json &value) { if (value.is_number_integer()) return value != 0; if (!value.is_string()) return false; const auto text = value.get(); try { size_t consumed = 0; const int base = text.size() > 2 && text[0] == '0' && (text[1] == 'x' || text[1] == 'X') ? 16 : 10; const auto number = std::stoll(text, &consumed, base); return consumed == text.size() && number != 0; } catch (...) { return false; } } inline bool zero_sdk_code(const Json &value) { if (value.is_number_integer()) return value == 0; if (!value.is_string()) return false; const auto text = value.get(); try { size_t consumed = 0; const int base = text.size() > 2 && text[0] == '0' && (text[1] == 'x' || text[1] == 'X') ? 16 : 10; const auto number = std::stoll(text, &consumed, base); return consumed == text.size() && number == 0; } catch (...) { return false; } } inline bool calibration_compute_succeeded(const Json &message) { const auto result = message.find("calibResult"); if (result == message.end() || (*result != true && normalized_result(*result) != "true")) return false; for (const auto *key : {"errorCode", "errCode"}) { const auto code = message.find(key); if (code != message.end() && !zero_sdk_code(*code)) return false; } return true; } inline std::string calibration_compute_error(const Json &message, const std::string &ordinaryError) { bool failed = !ordinaryError.empty(); for (const auto *key : {"calibResult", "result"}) { const auto found = message.find(key); if (found == message.end()) continue; const auto text = normalized_result(*found); failed = failed || *found == false || text == "false" || text == "failed" || (std::string(key) == "result" && found->is_number_integer() && *found != 0); } for (const auto *key : {"errorCode", "errCode"}) { const auto found = message.find(key); if (found != message.end()) failed = failed || nonzero_sdk_code(*found); } if (!failed) return {}; std::string error = "SDK标定计算失败"; for (const auto *key : {"cmd", "calibTarget", "calibResult", "errorCode", "errCode", "result", "camErr"}) { const auto found = message.find(key); if (found != message.end()) error += std::string("; ") + key + "=" + found->dump().substr(0, 96); } if (!ordinaryError.empty()) error += "; " + ordinaryError.substr(0, 192); return error; } inline SdkCompletion sdk_completion(const std::set &expected, const Json &message, const std::string &ordinaryError) { if (!message.is_object() || !message.contains("cmd") || !message["cmd"].is_string()) return {}; const auto command = message["cmd"].get(); const bool computing = expected.count("freeCalibrationComputeFinish") != 0; if (computing && (command == "calibCalculateResult" || command == "freeCalibrationComputeFinish")) { auto error = calibration_compute_error(message, ordinaryError); if (!error.empty()) return {true, std::move(error)}; // The installed SDK also finishes computation through this result // callback. Both vendor calibration demos use calibResult as the // outcome; the actual successful device run sends no separate ACK. if (command == "calibCalculateResult") return {calibration_compute_succeeded(message), {}}; } return expected.count(command) ? SdkCompletion{true, ordinaryError} : SdkCompletion{}; } } // namespace freescan::detail