am-freescan/integrations/freescan-service/src/calibration_completion.hpp
Codex a16f16e19a feat(scanner): show native calibration window and SDK progress
Use the existing successful calibration-enter workflow to display the same SDK manufacturer's window and actual callback progress in the task UI. Bind metadata, JPEG and MJPEG to the execution, SDK session, calibration ID and trusted process/window identity; closing the monitor only stops viewing.

Include source-only FreeScanCpp integration, WGC capture lifecycle fixes and immediate ENTER acknowledgement cache synchronization. Preserve original scanner project guards, regression sources and third-party licensing; exclude private runtime configuration and generated artifacts.

Validated platform Java compilation/regressions, frontend type checks and standalone build, native compilation/offline and HTTP regressions, immediate calibration identity checks and ten synthetic window close/reopen cycles. Production installation/restart and real scanner calibration remain pending.
2026-11-21 19:52:07 +08:00

101 lines
4.2 KiB
C++

#pragma once
#include "common.hpp"
#include <cctype>
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::string>();
std::transform(result.begin(), result.end(), result.begin(),
[](unsigned char c) { return static_cast<char>(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<std::string>();
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<std::string>();
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<std::string> &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<std::string>();
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