完成框架目录和模块重构;新增核心、单例、工具、加密及框架 SDK;统一任务、相机和 RPC 接口,并增加离线检测、相机标定、备份恢复、版本管理、崩溃守护和心跳监控功能。
273 lines
13 KiB
C++
273 lines
13 KiB
C++
#pragma once
|
||
|
||
#include <global.h>
|
||
#include <eui_frameworkSdk/IMissionBridge.h>
|
||
#include <nlohmannJson.hpp>
|
||
#include <eui_tools/sql/qSqlBase.h>
|
||
#include <type.h>
|
||
#include <QtCore/qglobal.h>
|
||
#include <QList>
|
||
#include <QByteArray>
|
||
#include <QMetaType>
|
||
#include <QString>
|
||
#include <QStringList>
|
||
#include <QUuid>
|
||
#include <atomic>
|
||
#include <memory>
|
||
#include <utility>
|
||
#include <vector>
|
||
|
||
#if defined(EUI_CORE_LIBRARY)
|
||
# define EUI_CORE_API Q_DECL_EXPORT
|
||
#elif defined(EUI_FRAMEWORK_SDK_CONSUMER)
|
||
# define EUI_CORE_API Q_DECL_IMPORT
|
||
#else
|
||
# define EUI_CORE_API
|
||
#endif
|
||
|
||
using PlcValueViewerRows = std::vector<std::pair<QString, int>>;
|
||
Q_DECLARE_METATYPE(PlcValueViewerRows)
|
||
|
||
// 参数编辑器一次确认操作对应的变更集。使用完整键名,避免前后端重复拆分分组。
|
||
struct ParamValueChange {
|
||
QString fullKey;
|
||
QVariant value;
|
||
};
|
||
|
||
struct ParamBatch {
|
||
std::vector<ParamValueChange> updates;
|
||
QStringList removedKeys;
|
||
QStringList orderedKeys;
|
||
bool updateOrder = false;
|
||
|
||
bool isEmpty() const {
|
||
return updates.empty() && removedKeys.isEmpty() && !updateOrder;
|
||
}
|
||
};
|
||
Q_DECLARE_METATYPE(ParamBatch)
|
||
|
||
using MissionDllUpdateCompleted = std::shared_ptr<std::atomic<bool>>;
|
||
using MissionDllUpdateDecision = std::shared_ptr<std::atomic<int>>;
|
||
using MissionDllUpdateText = std::shared_ptr<QString>;
|
||
Q_DECLARE_METATYPE(MissionDllUpdateText)
|
||
|
||
// 离线检测任务使用结构化数据传递,避免文件路径中的特殊字符影响解析。
|
||
struct offlineDetectionTask {
|
||
QString inputPath;
|
||
QUuid functionUuid;
|
||
QString fileNameRegex;
|
||
QString folderPathRegex;
|
||
int threadIndex = 1;
|
||
int intervalMs = 1000;
|
||
};
|
||
using offlineDetectionTaskList = QList<offlineDetectionTask>;
|
||
Q_DECLARE_METATYPE(offlineDetectionTask)
|
||
Q_DECLARE_METATYPE(offlineDetectionTaskList)
|
||
|
||
using offlineDetectionThumbnailList = QList<QByteArray>;
|
||
Q_DECLARE_METATYPE(offlineDetectionThumbnailList)
|
||
|
||
class bServiceSignalHub : public QObject {
|
||
Q_OBJECT
|
||
public:
|
||
Q_DISABLE_COPY(bServiceSignalHub)
|
||
explicit bServiceSignalHub(QObject* parent = nullptr) : QObject(parent) {}
|
||
|
||
signals:
|
||
// 注册运行命令、PPM 测试或设置页使用的操作按钮。
|
||
void runCommand_addBtn(runCommandWidgetType type, const QString& btnName, const QString& tip, const QString& uuid);
|
||
// 向设置页追加指定相机的配置按钮。
|
||
void addCameraDialogButton(QString cameraKey);
|
||
// 向加载窗口追加带颜色的初始化日志。
|
||
void loadingWindow_appendInitLog(const QString& text, const QColor& color);
|
||
void setCamConnectionError();
|
||
void setCamConnectionOk();
|
||
void setPlcConnectionError();
|
||
void setPlcConnectionOk();
|
||
// 向软件数据页报告触发循环次数和最大触发间隔。
|
||
void sendTriggerManagerLoopData(int loopCounterPerMin, int maxInterval);
|
||
// 向运行命令页注册 PLC 值监视器及其地址。
|
||
void runCommand_addPlcValueViewer(const QString& groupName, const PlcValueViewerRows& viewerNameAndAddress);
|
||
// 更新指定 PLC 地址的监视值。
|
||
void runCommand_refeshPlcValueViewer(int address, const QString& value);
|
||
// 请求保存当前任务配置。
|
||
void saveMissionJson();
|
||
// 为算法耗时监控页创建一组查看项。
|
||
void addTimeCostViewer(const QString& groupName, const std::vector<QString>& viewerNames);
|
||
// 更新算法耗时监控项的平均、最大和最小耗时。
|
||
void refreshTimeCostViewer(const QString& address, const QString& avgTime, const QString& maxTime, const QString& minTime);
|
||
// 通知算法管理页当前算法文件路径。
|
||
void algoFilePathChanged(const QString& algoFilePath);
|
||
// 为模型耗时监控页创建一组查看项。
|
||
void modelAddTimeCostViewer(const QString& groupName, const std::vector<QString>& viewerNames);
|
||
// 更新模型耗时监控项的平均、最大和最小耗时。
|
||
void modelRefreshTimeCostViewer(const QString& address, const QString& avgTime, const QString& maxTime, const QString& minTime);
|
||
// 初始化阶段的确认框允许前端追加“退出程序”按钮。
|
||
void waitForFrontendInitComfirm(const QString& message, const std::vector<QString>& options, std::shared_ptr<std::atomic<bool>> recivedResult, std::shared_ptr<std::atomic<int>> result);
|
||
// 非初始化场景只显示传入选项,不允许前端追加“退出程序”按钮。
|
||
void waitForFrontendConfirm(const QString& message, const std::vector<QString>& options, std::shared_ptr<std::atomic<bool>> recivedResult, std::shared_ptr<std::atomic<int>> result);
|
||
void showMessageBox(const QString& message);
|
||
// 更新模型文件和初始化文件的 MD5 校验状态。
|
||
void refreshModelMD5Viewer(const QString& groupName, const QString& modelMD5, bool modelMatched, const QString& geforceMD5, bool geforceMatched);
|
||
// 向模型初始化页添加一个操作按钮。
|
||
void modelInit_addBtn(const QString& btnName, const QString& tip, const QString& command);
|
||
// 返回模型初始化结果及提示信息。
|
||
void modelInit_modelInitResult(bool ok, const QString& message);
|
||
// 请求后端预览模型更新,并比较新旧模型 MD5。
|
||
void requestModelUpdatePreview(const QString& requestId, const QString& oldModelPath, const QString& newModelPath);
|
||
// 返回模型更新预览的路径、MD5 和匹配结果。
|
||
void modelUpdatePreviewReady(const QString& requestId, const QString& oldModelPath, const QString& newModelPath, const QString& oldModelMD5, const QString& newModelMD5, bool md5Matched);
|
||
// 启动一批离线检测任务。
|
||
void offlineDetection_startBatch(const offlineDetectionTaskList& taskList);
|
||
void offlineDetection_stop();
|
||
// 通知前端离线检测算法已注册;两个参数为空时清空算法列表。
|
||
void offlineDetection_functionRegistered(const QString& uuid, const QString& funcName);
|
||
// 通知前端离线检测算法及其展示信息;两个字符串为空时清空算法列表。
|
||
void offlineDetection_algorithmRegistered(const QString& uuid, const QString& funcName,
|
||
const QStringList& algorithmDetails);
|
||
// 返回单个离线检测任务的结果图像、缩略图和算法耗时。
|
||
void offlineDetection_resultReady(int threadIndex, const QString& inputPath, const QString& functionName, bool ok, const QStringList& resultImagePaths, const offlineDetectionThumbnailList& thumbnails, const QString& message, qint64 algoTimeMs);
|
||
// 报告离线检测批次的完成进度。
|
||
void offlineDetection_progress(int current, int total, const QString& currentTask);
|
||
// 报告离线检测批次结束及是否被停止。
|
||
void offlineDetection_finished(bool stopped, const QString& message);
|
||
// 通知指定离线任务因算法未注册而无法触发。
|
||
void offlineDetection_triggerFailed(const QString& taskName);
|
||
// 命令、相机和算法维护相关信号。
|
||
// 向命令管理器注册一个预设命令及其按钮信息。
|
||
void addPreset(QString btnName, QString tips, QStringList commands);
|
||
// 异步执行完整命令字符串。
|
||
void ansyncExcuteCliCommand(const QString& command);
|
||
// 按 UUID 异步执行已注册的命令预设。
|
||
void ansyncExcuteCliCommandPreset(const QString& uuid);
|
||
void onCheckAllCameraConnection();
|
||
// 同步获取相机名称列表并写入输出参数。
|
||
void getCameraNames(std::vector<std::string>* cameraNames);
|
||
// 设置相机像素精度,并通过 ok 返回操作结果。
|
||
void setPrecision(QString cameraName, cameraPixelPrecision precision, bool* ok);
|
||
// 读取相机像素精度,并通过输出参数返回结果。
|
||
void getPrecision(QString cameraName, cameraPixelPrecision* precision, bool* ok);
|
||
// 保存相机标定记录,并通过 ok 返回操作结果。
|
||
void saveCalibrationRecord(QString cameraName, cameraPixelPrecision precision, QString resultImagePath, bool* ok);
|
||
// 读取指定相机的标定记录到 JSON 输出参数。
|
||
void getCalibrationRecords(QString cameraName, nlohmann::ordered_json* records);
|
||
void cameraTrigger(std::string Name);
|
||
// 请求触发指定相机并获取一张图像。
|
||
void triggerAndGetImage(std::string cameraName);
|
||
// 将所有相机重新加载为虚拟相机。
|
||
void reloadAllCameraAsVirtual();
|
||
// 请求显示指定相机的配置窗口。
|
||
void showCameraDialog(QString cameraKey);
|
||
// 请求显示触发配置窗口。
|
||
void showTriggerDialog();
|
||
// 备份当前算法文件,并替换为指定路径的算法文件。
|
||
void backupAndReplaceAlgoFile(const std::string& oldAlgoFilePath);
|
||
// 恢复指定目录中的算法备份并重新加载。
|
||
void restoreAlgoBackup(const QString& backupDir);
|
||
// 恢复指定目录中的模型备份。
|
||
void restoreModelBackup(const QString& backupDir);
|
||
// 重新加载当前本地算法文件。
|
||
void reloadLocalAlgoFile();
|
||
};
|
||
|
||
class bSignalHub : public QObject {
|
||
Q_OBJECT
|
||
public:
|
||
Q_DISABLE_COPY(bSignalHub)
|
||
explicit bSignalHub(QObject* parent = nullptr) : QObject(parent) {}
|
||
|
||
signals:
|
||
// 向加载窗口追加初始化日志。
|
||
void loadingWindow_appendInitLog(const QString& text, const QColor& color);
|
||
// 通知前端核心初始化结果。
|
||
void initResult(initStatus result);
|
||
// 向主显示区域发送检测图像。
|
||
void displayWidget_sendImg(showImgPack pack);
|
||
// 增量更新 OK/NG 计数。
|
||
void pieWidgetModifyData(int modifyOkData, int modiftNgData);
|
||
// 增量更新 OK/NG 与缺陷柱状图。
|
||
void histogramWidgetAddData(int modifyOkData, int modiftNgData, std::vector<std::string>);
|
||
// 向指定波形窗口追加数据点。
|
||
void waveFormAddData(QString windowName, double value);
|
||
// 通知算法版本变更。
|
||
void algorithmVersionChanged(const QString& version);
|
||
// 请求前端确认版本更新;category 取 algorithm 或 detectionModule。
|
||
void versionUpdateInfoRequired(const QString& category, const QString& currentVersion,
|
||
const QString& proposedVersion);
|
||
// 请求前端确认任务 DLL 更新。
|
||
// completed 由前端置位以解除等待,decision 由前端写入处理决定,updateInfo/errorDescription 用于回传说明或错误。
|
||
void missionDllUpdateInfoRequired(const QString& dllPath, const QString& previousMd5,
|
||
const QString& currentMd5, const QString& currentVersion,
|
||
MissionDllUpdateCompleted completed, MissionDllUpdateDecision decision,
|
||
MissionDllUpdateText updateInfo, MissionDllUpdateText errorDescription);
|
||
|
||
};
|
||
|
||
class MissionBridge final
|
||
: public QObject
|
||
, public eui_frameworkSdk::IMissionBridge {
|
||
Q_OBJECT
|
||
public:
|
||
Q_DISABLE_COPY(MissionBridge)
|
||
explicit MissionBridge(QObject* parent = nullptr) : QObject(parent) {}
|
||
|
||
// 请求前端临时切换用户权限。
|
||
// level:1=PROD(生产)、2=QC、3=PE、4=ENG(工程)、5=ADMIN(管理员);超出范围会被限制到 1~5,结束后恢复 GUEST。
|
||
void updateUserPermission(
|
||
const QString& userName, int duration, int level) override {
|
||
emit updateUserPermissionRequested(userName, duration, level);
|
||
}
|
||
|
||
signals:
|
||
// 请求前端临时切换用户权限。
|
||
// level:1=PROD(生产)、2=QC、3=PE、4=ENG(工程)、5=ADMIN(管理员);超出范围会被限制到 1~5,结束后恢复 GUEST。
|
||
void updateUserPermissionRequested(
|
||
const QString& userName, int duration, int level);
|
||
};
|
||
|
||
class IBackend : public QThread {
|
||
Q_OBJECT
|
||
public:
|
||
bSignalHub _signalHub;
|
||
bServiceSignalHub _bServiceSignalHub;
|
||
MissionBridge _missionBridge;
|
||
virtual ~IBackend() = default;
|
||
virtual void init() = 0;
|
||
virtual nlohmann::ordered_json getBackendJson() = 0;
|
||
virtual QString currentAlgoFilePath() const = 0;
|
||
virtual void submitVersionUpdate(const QString& category, const QString& updateInfo) = 0;
|
||
virtual void submitVersionMd5Only(const QString& category) = 0;
|
||
|
||
public slots:
|
||
virtual void triggerAndCaptureImage(std::string cameraName, std::vector<cv::Mat>* mats) = 0;
|
||
virtual void setCameraPixelPrecision(std::string cameraName, cameraPixelPrecision precision, bool* ok) = 0;
|
||
virtual void saveCameraCalibrationRecord(std::string cameraName, cameraPixelPrecision precision, bool* ok) = 0;
|
||
virtual void getCameraCalibrationRecords(std::string cameraName, nlohmann::ordered_json* records) = 0;
|
||
virtual void getAllParams(std::vector<std::pair<QString, QVariant>>* params) = 0;
|
||
virtual void editParam(QString group, QString name, QVariant value) = 0;
|
||
virtual void applyParamBatch(const ParamBatch& batch) = 0;
|
||
virtual void reorderParams(QStringList orderedKeys) = 0;
|
||
virtual void removeParam(QString fullKey) = 0;
|
||
};
|
||
|
||
extern "C" {
|
||
EUI_CORE_API IBackend* CreateBackend();
|
||
EUI_CORE_API void ReleaseBackend(IBackend* backend);
|
||
}
|
||
|
||
typedef IBackend* (*CreateBackendFunc)();
|
||
typedef void (*ReleaseBackendFunc)(IBackend*);
|
||
|
||
class detData;
|
||
|
||
class IImageStorage {
|
||
public:
|
||
virtual ~IImageStorage() = default;
|
||
virtual QStringList saveImage(std::shared_ptr<detData> data) = 0;
|
||
};
|
||
|
||
//class IimageSaveExcuter : public QThread {
|
||
//public:
|
||
// virtual write(std::shared_ptr<saveJob> job, type type_ = normal);
|
||
//};
|