2026-11-12 09:26:26 +08:00
|
|
|
|
#include "visiondllworker.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
|
#include <QDir>
|
|
|
|
|
|
#include <QElapsedTimer>
|
|
|
|
|
|
#include <QFile>
|
|
|
|
|
|
#include <QFileInfo>
|
2026-11-12 14:39:30 +08:00
|
|
|
|
#include <QJsonArray>
|
2026-11-12 09:26:26 +08:00
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
|
#include <QJsonParseError>
|
|
|
|
|
|
#include <QMutexLocker>
|
|
|
|
|
|
#include <QThread>
|
|
|
|
|
|
#include <QUuid>
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
|
#include <qt_windows.h>
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
QJsonObject readObject(const QString &path, QString *error)
|
|
|
|
|
|
{
|
|
|
|
|
|
QFile file(path);
|
|
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
|
|
if (error) *error = QString("无法读取视觉配置:%1").arg(path);
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
QJsonParseError parseError;
|
|
|
|
|
|
const QJsonDocument document = QJsonDocument::fromJson(file.readAll(), &parseError);
|
|
|
|
|
|
if (parseError.error != QJsonParseError::NoError || !document.isObject()) {
|
|
|
|
|
|
if (error) *error = QString("视觉配置 JSON 无效:%1").arg(parseError.errorString());
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
return document.object();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString operationMessage(const QJsonObject &result)
|
|
|
|
|
|
{
|
2026-11-12 14:39:30 +08:00
|
|
|
|
QString message = result.value("message").toString();
|
|
|
|
|
|
for (const QJsonValue &value : result.value("cameras").toArray()) {
|
|
|
|
|
|
const QJsonObject camera = value.toObject();
|
|
|
|
|
|
const QString error = camera.value("error").toString();
|
|
|
|
|
|
if (error.isEmpty()) continue;
|
|
|
|
|
|
const QString side = camera.value("camera").toString();
|
|
|
|
|
|
message += QString(";%1(%2):%3")
|
|
|
|
|
|
.arg(side == "left" ? QStringLiteral("左相机")
|
|
|
|
|
|
: side == "right" ? QStringLiteral("右相机") : QStringLiteral("相机"))
|
|
|
|
|
|
.arg(camera.value("serial").toString()).arg(error);
|
|
|
|
|
|
}
|
2026-11-12 09:26:26 +08:00
|
|
|
|
return message.isEmpty() ? QStringLiteral("DLL 未返回详细原因") : message;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VisionDllWorker::VisionDllWorker(QObject *parent)
|
|
|
|
|
|
: QObject(parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HikVisionGuidanceQt::Reply VisionDllWorker::call(const QString &name,
|
|
|
|
|
|
const QJsonObject &request)
|
|
|
|
|
|
{
|
|
|
|
|
|
const QString requestId = request.value("request_id").toString();
|
|
|
|
|
|
if (!requestId.isEmpty()) {
|
|
|
|
|
|
QMutexLocker locker(&m_apiMutex);
|
|
|
|
|
|
m_activeOperationId = requestId;
|
|
|
|
|
|
}
|
|
|
|
|
|
const HikVisionGuidanceQt::Reply reply = m_api.call(name, request);
|
|
|
|
|
|
if (!requestId.isEmpty()) {
|
|
|
|
|
|
QMutexLocker locker(&m_apiMutex);
|
|
|
|
|
|
if (m_activeOperationId == requestId) m_activeOperationId.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
return reply;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString VisionDllWorker::replyError(const QString &operation,
|
|
|
|
|
|
const HikVisionGuidanceQt::Reply &reply) const
|
|
|
|
|
|
{
|
|
|
|
|
|
return QString("%1失败(DLL code=%2):%3")
|
|
|
|
|
|
.arg(operation).arg(reply.code).arg(operationMessage(reply.json));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VisionDllWorker::loadSdk(const QString &sdkRoot, int initializeTimeoutMs)
|
|
|
|
|
|
{
|
|
|
|
|
|
Q_UNUSED(sdkRoot)
|
|
|
|
|
|
if (m_api.isOpen()) {
|
|
|
|
|
|
emit sdkLoaded(true, QJsonObject(), "视觉 DLL 已加载");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_sdkRoot = QCoreApplication::applicationDirPath();
|
|
|
|
|
|
const QString dllPath = QDir(m_sdkRoot).filePath("HikVisionGuidance.dll");
|
|
|
|
|
|
const QString configPath = QDir(m_sdkRoot).filePath("config/sdk.json");
|
|
|
|
|
|
if (!QFileInfo::exists(dllPath) || !QFileInfo::exists(configPath)) {
|
|
|
|
|
|
emit sdkLoaded(false, QJsonObject(),
|
|
|
|
|
|
"运行目录下缺少 HikVisionGuidance.dll 或 config/sdk.json");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
|
DLL_DIRECTORY_COOKIE directoryCookie = AddDllDirectory(
|
|
|
|
|
|
reinterpret_cast<LPCWSTR>(m_sdkRoot.utf16()));
|
|
|
|
|
|
if (!directoryCookie) {
|
|
|
|
|
|
emit sdkLoaded(false, QJsonObject(),
|
|
|
|
|
|
QString("注册视觉 DLL 依赖目录失败(Windows error=%1)")
|
|
|
|
|
|
.arg(GetLastError()));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
HMODULE bootstrap = LoadLibraryExW(reinterpret_cast<LPCWSTR>(dllPath.utf16()), nullptr,
|
|
|
|
|
|
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
|
|
|
|
|
|
| LOAD_LIBRARY_SEARCH_USER_DIRS);
|
|
|
|
|
|
if (!bootstrap) {
|
|
|
|
|
|
RemoveDllDirectory(directoryCookie);
|
|
|
|
|
|
emit sdkLoaded(false, QJsonObject(),
|
|
|
|
|
|
QString("加载视觉 DLL 或其依赖失败(Windows error=%1)")
|
|
|
|
|
|
.arg(GetLastError()));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
const bool loaded = m_api.load(dllPath);
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
|
FreeLibrary(bootstrap);
|
|
|
|
|
|
RemoveDllDirectory(directoryCookie);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
if (!loaded || m_api.resolvedCount() != 27) {
|
|
|
|
|
|
m_api.unload();
|
|
|
|
|
|
emit sdkLoaded(false, QJsonObject(), "加载视觉 DLL 失败:" + m_api.errorString());
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const HikVisionGuidanceQt::Reply version = call("GetVersion");
|
|
|
|
|
|
if (!version.ok()
|
|
|
|
|
|
|| version.json.value("abi_version").toInt() != HVG_ABI_VERSION
|
|
|
|
|
|
|| version.json.value("architecture").toString() != "windows-x64-msvc"
|
|
|
|
|
|
|| version.json.value("export_count").toInt() != 27) {
|
|
|
|
|
|
m_api.unload();
|
|
|
|
|
|
emit sdkLoaded(false, version.json, "视觉 DLL 的 ABI、架构或导出接口不兼容");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const QJsonObject overrides{{"modules", QJsonObject{
|
|
|
|
|
|
{"measurement", true}, {"grooves", true}, {"distance", true}}}};
|
|
|
|
|
|
const HikVisionGuidanceQt::Reply initialized = m_api.initialize({
|
|
|
|
|
|
{"config_path", configPath},
|
|
|
|
|
|
{"connect_cameras", false},
|
|
|
|
|
|
{"connect_light", false},
|
|
|
|
|
|
{"load_data", false},
|
|
|
|
|
|
{"max_time_ms", initializeTimeoutMs},
|
|
|
|
|
|
{"overrides", overrides}
|
|
|
|
|
|
});
|
|
|
|
|
|
if (!initialized.ok()) {
|
|
|
|
|
|
m_api.unload();
|
|
|
|
|
|
emit sdkLoaded(false, initialized.json, replyError("创建视觉上下文", initialized));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
{
|
|
|
|
|
|
QMutexLocker locker(&m_apiMutex);
|
|
|
|
|
|
m_concurrentHandle = m_api.handle();
|
|
|
|
|
|
m_cancelFunction = m_api.function("CancelOperation");
|
|
|
|
|
|
}
|
|
|
|
|
|
QJsonObject status = version.json;
|
|
|
|
|
|
status.insert("context", initialized.json);
|
|
|
|
|
|
emit sdkLoaded(true, status, "视觉 DLL 已加载,等待初始化设备");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VisionDllWorker::initializeDevices(int initializeTimeoutMs)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_api.isOpen()) {
|
|
|
|
|
|
emit devicesInitialized(false, QJsonObject(), "视觉 DLL 尚未加载");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString configError;
|
|
|
|
|
|
const QJsonObject sdkConfig = readObject(QDir(m_sdkRoot).filePath("config/sdk.json"),
|
|
|
|
|
|
&configError);
|
|
|
|
|
|
if (sdkConfig.isEmpty()) {
|
|
|
|
|
|
emit devicesInitialized(false, QJsonObject(), configError);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const QJsonObject light = sdkConfig.value("light").toObject();
|
|
|
|
|
|
QElapsedTimer timer;
|
|
|
|
|
|
timer.start();
|
|
|
|
|
|
auto timedCall = [this, &timer, initializeTimeoutMs](const QString &name,
|
|
|
|
|
|
QJsonObject request = QJsonObject()) {
|
|
|
|
|
|
const int remaining = initializeTimeoutMs - int(timer.elapsed());
|
|
|
|
|
|
if (remaining <= 0) {
|
|
|
|
|
|
return HikVisionGuidanceQt::Reply{
|
|
|
|
|
|
HVG_TIMEOUT, {{"success", false}, {"message", "设备初始化总等待时间已用尽"}}};
|
|
|
|
|
|
}
|
|
|
|
|
|
request.insert("request_id", "initialize-"
|
|
|
|
|
|
+ QUuid::createUuid().toString(QUuid::WithoutBraces));
|
|
|
|
|
|
request.insert("max_time_ms", remaining);
|
|
|
|
|
|
return call(name, request);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
HikVisionGuidanceQt::Reply reply = timedCall("InitializeCamera", {{"camera", "both"}});
|
2026-11-12 14:39:30 +08:00
|
|
|
|
// A failed initialization can still leave partially opened or stale camera handles.
|
|
|
|
|
|
m_cameraConnected = true;
|
2026-11-12 09:26:26 +08:00
|
|
|
|
if (!reply.ok()) {
|
2026-11-12 14:39:30 +08:00
|
|
|
|
rollbackDevices();
|
2026-11-12 09:26:26 +08:00
|
|
|
|
emit devicesInitialized(false, reply.json, replyError("初始化相机", reply));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
reply = timedCall("InitializeLight");
|
|
|
|
|
|
if (!reply.ok()) {
|
|
|
|
|
|
rollbackDevices();
|
|
|
|
|
|
emit devicesInitialized(false, reply.json, replyError("初始化光源", reply));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
m_lightConnected = true;
|
|
|
|
|
|
|
|
|
|
|
|
reply = timedCall("LoadData");
|
|
|
|
|
|
if (!reply.ok()) {
|
|
|
|
|
|
rollbackDevices();
|
|
|
|
|
|
emit devicesInitialized(false, reply.json, replyError("加载视觉数据", reply));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
reply = timedCall("TurnOnLight", {
|
|
|
|
|
|
{"left_brightness", light.value("ch1").toInt(255)},
|
|
|
|
|
|
{"right_brightness", light.value("ch2").toInt(255)}
|
|
|
|
|
|
});
|
|
|
|
|
|
if (!reply.ok()) {
|
|
|
|
|
|
rollbackDevices();
|
|
|
|
|
|
emit devicesInitialized(false, reply.json, replyError("打开光源", reply));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
m_lightOn = true;
|
|
|
|
|
|
|
|
|
|
|
|
reply = timedCall("StartCamera", {{"camera", "both"}});
|
|
|
|
|
|
if (!reply.ok()) {
|
|
|
|
|
|
rollbackDevices();
|
|
|
|
|
|
emit devicesInitialized(false, reply.json, replyError("启动相机采集", reply));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
m_cameraStarted = true;
|
|
|
|
|
|
|
|
|
|
|
|
reply = timedCall("ConfigureCamera", {{"camera", "both"}});
|
|
|
|
|
|
if (!reply.ok()) {
|
|
|
|
|
|
rollbackDevices();
|
|
|
|
|
|
emit devicesInitialized(false, reply.json, replyError("配置相机", reply));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const HikVisionGuidanceQt::Reply status = call("GetStatus");
|
|
|
|
|
|
QJsonObject result = status.ok() ? status.json : reply.json;
|
|
|
|
|
|
emit devicesInitialized(true, result, "视觉设备初始化完成");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VisionDllWorker::measure(const QJsonObject &request)
|
|
|
|
|
|
{
|
|
|
|
|
|
const QString requestId = request.value("request_id").toString();
|
|
|
|
|
|
const HikVisionGuidanceQt::Reply reply = call("Measure", request);
|
|
|
|
|
|
emit operationCompleted(requestId, reply.code, reply.json);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VisionDllWorker::recognizeGrooves(const QJsonObject &request)
|
|
|
|
|
|
{
|
|
|
|
|
|
const QString requestId = request.value("request_id").toString();
|
|
|
|
|
|
const HikVisionGuidanceQt::Reply reply = call("RecognizeGrooves", request);
|
|
|
|
|
|
emit operationCompleted(requestId, reply.code, reply.json);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VisionDllWorker::planAvoidance(const QJsonObject &request)
|
|
|
|
|
|
{
|
|
|
|
|
|
const QString requestId = request.value("request_id").toString();
|
|
|
|
|
|
const HikVisionGuidanceQt::Reply reply = call("PlanAvoidance", request);
|
|
|
|
|
|
emit operationCompleted(requestId, reply.code, reply.json);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool VisionDllWorker::cancelOperation(const QString &requestId, QString *error)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (requestId.isEmpty()) return false;
|
|
|
|
|
|
QMutexLocker locker(&m_apiMutex);
|
|
|
|
|
|
if (!m_concurrentHandle || !m_cancelFunction) {
|
|
|
|
|
|
if (error) *error = "视觉 DLL 没有可取消的活动上下文";
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
QByteArray buffer(int(HVG_RESPONSE_CAPACITY), 0);
|
|
|
|
|
|
uint32_t required = 0;
|
|
|
|
|
|
const QByteArray input = QJsonDocument(QJsonObject{{"request_id", requestId}})
|
|
|
|
|
|
.toJson(QJsonDocument::Compact);
|
|
|
|
|
|
const int code = m_cancelFunction(m_concurrentHandle, input.constData(), buffer.data(),
|
|
|
|
|
|
uint32_t(buffer.size()), &required);
|
|
|
|
|
|
if (code != HVG_OK) {
|
|
|
|
|
|
if (error) *error = QString("取消视觉操作失败(DLL code=%1)").arg(code);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool VisionDllWorker::cancelActiveOperation()
|
|
|
|
|
|
{
|
|
|
|
|
|
QString requestId;
|
|
|
|
|
|
{
|
|
|
|
|
|
QMutexLocker locker(&m_apiMutex);
|
|
|
|
|
|
requestId = m_activeOperationId;
|
|
|
|
|
|
}
|
|
|
|
|
|
return !requestId.isEmpty() && cancelOperation(requestId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VisionDllWorker::rollbackDevices()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_cameraStarted) call("StopCamera", {{"camera", "both"}});
|
|
|
|
|
|
m_cameraStarted = false;
|
|
|
|
|
|
if (m_cameraConnected) call("DisconnectCamera", {{"camera", "both"}});
|
|
|
|
|
|
m_cameraConnected = false;
|
|
|
|
|
|
if (m_lightOn) call("TurnOffLight");
|
|
|
|
|
|
m_lightOn = false;
|
|
|
|
|
|
if (m_lightConnected) call("DisconnectLight");
|
|
|
|
|
|
m_lightConnected = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VisionDllWorker::clearConcurrentApi()
|
|
|
|
|
|
{
|
|
|
|
|
|
QMutexLocker locker(&m_apiMutex);
|
|
|
|
|
|
m_concurrentHandle = nullptr;
|
|
|
|
|
|
m_cancelFunction = nullptr;
|
|
|
|
|
|
m_activeOperationId.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VisionDllWorker::release(bool quitThread)
|
|
|
|
|
|
{
|
|
|
|
|
|
QStringList errors;
|
|
|
|
|
|
auto cleanup = [this, &errors](bool enabled, const QString &name,
|
|
|
|
|
|
const QJsonObject &request = QJsonObject()) {
|
|
|
|
|
|
if (!enabled) return;
|
|
|
|
|
|
const HikVisionGuidanceQt::Reply reply = call(name, request);
|
|
|
|
|
|
if (!reply.ok()) errors.append(replyError(name, reply));
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
cleanup(m_cameraStarted, "StopCamera", {{"camera", "both"}});
|
|
|
|
|
|
m_cameraStarted = false;
|
|
|
|
|
|
cleanup(m_cameraConnected, "DisconnectCamera", {{"camera", "both"}});
|
|
|
|
|
|
m_cameraConnected = false;
|
|
|
|
|
|
cleanup(m_lightOn, "TurnOffLight");
|
|
|
|
|
|
m_lightOn = false;
|
|
|
|
|
|
cleanup(m_lightConnected, "DisconnectLight");
|
|
|
|
|
|
m_lightConnected = false;
|
|
|
|
|
|
if (m_api.isOpen()) {
|
|
|
|
|
|
const HikVisionGuidanceQt::Reply reply = m_api.close();
|
|
|
|
|
|
if (!reply.ok()) errors.append(replyError("Shutdown", reply));
|
|
|
|
|
|
}
|
|
|
|
|
|
clearConcurrentApi();
|
|
|
|
|
|
if (!m_api.unload()) errors.append("卸载 HikVisionGuidance.dll 失败");
|
|
|
|
|
|
emit released(errors.isEmpty(), errors.isEmpty() ? "视觉资源已释放" : errors.join(";"));
|
|
|
|
|
|
if (quitThread) QThread::currentThread()->quit();
|
|
|
|
|
|
}
|