132 lines
5.1 KiB
C++
132 lines
5.1 KiB
C++
#ifndef HIK_VISION_GUIDANCE_QT_H
|
|
#define HIK_VISION_GUIDANCE_QT_H
|
|
|
|
#include "HikVisionGuidance.h"
|
|
|
|
#include <QHash>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QLibrary>
|
|
#include <QStringList>
|
|
|
|
class HikVisionGuidanceQt
|
|
{
|
|
public:
|
|
struct Reply {
|
|
int code = HVG_INTERNAL_ERROR;
|
|
QJsonObject json;
|
|
bool ok() const { return code == HVG_OK; }
|
|
QString message() const { return json.value("message").toString(); }
|
|
};
|
|
|
|
bool load(const QString &path)
|
|
{
|
|
if (m_handle) {
|
|
m_error = "Close the context before loading another library";
|
|
return false;
|
|
}
|
|
m_functions.clear();
|
|
m_library.setFileName(path);
|
|
m_library.setLoadHints(QLibrary::ResolveAllSymbolsHint);
|
|
if (!m_library.load()) {
|
|
m_error = m_library.errorString();
|
|
return false;
|
|
}
|
|
m_initialize = reinterpret_cast<HVG_InitializeFunction>(m_library.resolve("HVG_Initialize"));
|
|
const QStringList names = {
|
|
"InitializeCamera", "InitializeLight", "InitializePlc", "LoadData",
|
|
"ReadRobotPose", "WriteRobotPose", "Measure", "PlanAvoidance",
|
|
"RecognizeGrooves", "Shutdown", "GetVersion", "GetStatus", "GetLastError",
|
|
"CancelOperation", "EnumerateCameras", "ConfigureCamera", "ConfigureLight",
|
|
"CaptureImages", "DisconnectCamera", "ExportResult", "StartCamera", "StopCamera",
|
|
"TurnOnLight", "TurnOffLight", "DisconnectLight", "DisconnectPlc"
|
|
};
|
|
for (const QString &name : names) {
|
|
HVG_Function function = reinterpret_cast<HVG_Function>(
|
|
m_library.resolve(("HVG_" + name).toLatin1().constData()));
|
|
if (!function) {
|
|
m_error = "Missing export HVG_" + name;
|
|
m_functions.clear();
|
|
m_library.unload();
|
|
return false;
|
|
}
|
|
m_functions.insert(name, function);
|
|
}
|
|
if (!m_initialize) {
|
|
m_error = "Missing HVG_Initialize";
|
|
m_library.unload();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
Reply initialize(const QJsonObject &request)
|
|
{
|
|
if (m_handle) return localError(HVG_BUSY, "Already initialized");
|
|
if (!m_initialize) return localError(HVG_NOT_READY, "DLL is not loaded");
|
|
QByteArray buffer(int(HVG_RESPONSE_CAPACITY), 0);
|
|
uint32_t required = 0;
|
|
const QByteArray input = QJsonDocument(request).toJson(QJsonDocument::Compact);
|
|
const int code = m_initialize(input.constData(), &m_handle, buffer.data(),
|
|
uint32_t(buffer.size()), &required);
|
|
return decode(code, buffer, required);
|
|
}
|
|
|
|
Reply call(const QString &name, const QJsonObject &request = QJsonObject())
|
|
{
|
|
HVG_Function function = m_functions.value(name, nullptr);
|
|
if (!function) return localError(HVG_NOT_READY, "Unknown or unloaded export");
|
|
QByteArray buffer(int(HVG_RESPONSE_CAPACITY), 0);
|
|
uint32_t required = 0;
|
|
const QByteArray input = QJsonDocument(request).toJson(QJsonDocument::Compact);
|
|
const int code = function(m_handle, input.constData(), buffer.data(),
|
|
uint32_t(buffer.size()), &required);
|
|
Reply reply = decode(code, buffer, required);
|
|
if (name == "Shutdown" && code == HVG_OK) m_handle = nullptr;
|
|
return reply;
|
|
}
|
|
|
|
Reply close() { return m_handle ? call("Shutdown") : Reply{HVG_OK, {{"success", true}}}; }
|
|
bool unload()
|
|
{
|
|
if (m_handle) return false;
|
|
m_functions.clear();
|
|
m_initialize = nullptr;
|
|
return !m_library.isLoaded() || m_library.unload();
|
|
}
|
|
bool isOpen() const { return m_handle != nullptr; }
|
|
QString errorString() const { return m_error; }
|
|
HVG_Handle handle() const { return m_handle; }
|
|
HVG_Function function(const QString &name) const { return m_functions.value(name, nullptr); }
|
|
int resolvedCount() const { return m_functions.size() + (m_initialize ? 1 : 0); }
|
|
|
|
~HikVisionGuidanceQt() { close(); unload(); }
|
|
HikVisionGuidanceQt() = default;
|
|
HikVisionGuidanceQt(const HikVisionGuidanceQt &) = delete;
|
|
HikVisionGuidanceQt &operator=(const HikVisionGuidanceQt &) = delete;
|
|
|
|
private:
|
|
static Reply localError(int code, const QString &message)
|
|
{
|
|
return {code, {{"success", false}, {"message", message}, {"code", code}}};
|
|
}
|
|
|
|
static Reply decode(int code, const QByteArray &buffer, uint32_t required)
|
|
{
|
|
if (required > uint32_t(buffer.size()))
|
|
return localError(code, "Response buffer insufficient; call was not replayed");
|
|
const QJsonDocument document = QJsonDocument::fromJson(buffer.constData());
|
|
if (!document.isObject())
|
|
return localError(code, "DLL returned no valid JSON response");
|
|
return {code, document.object()};
|
|
}
|
|
|
|
QLibrary m_library;
|
|
QHash<QString, HVG_Function> m_functions;
|
|
HVG_InitializeFunction m_initialize = nullptr;
|
|
HVG_Handle m_handle = nullptr;
|
|
QString m_error;
|
|
};
|
|
|
|
#endif // HIK_VISION_GUIDANCE_QT_H
|