2025-08-20 23:06:28 +08:00
|
|
|
|
#ifndef OPCUAMANAGER_H
|
|
|
|
|
|
#define OPCUAMANAGER_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
|
#include <QOpcUaClient>
|
|
|
|
|
|
#include <QOpcUaNode>
|
|
|
|
|
|
#include <QOpcUaEndpointDescription>
|
|
|
|
|
|
#include <QVector>
|
|
|
|
|
|
#include <QUrl>
|
|
|
|
|
|
#include <QTimer>
|
2025-09-15 22:28:43 +08:00
|
|
|
|
#include <QMutex>
|
|
|
|
|
|
#include <QMap>
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
|
|
#include <QEventLoop>
|
|
|
|
|
|
#include <QThread>
|
|
|
|
|
|
// OPC UA错误码常量定义
|
|
|
|
|
|
const qint32 bad_user_access_denied = -2139881472; // 用户访问被拒绝
|
|
|
|
|
|
const qint32 bad_type_mismatch = -2147483053; // 数据类型不匹配
|
|
|
|
|
|
|
|
|
|
|
|
struct WriteRequest {
|
|
|
|
|
|
QString nodeId;
|
|
|
|
|
|
QVariant value;
|
|
|
|
|
|
int retryCount;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-08-20 23:06:28 +08:00
|
|
|
|
class OpcUaManager : public QObject
|
|
|
|
|
|
{
|
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
public:
|
2025-09-15 22:28:43 +08:00
|
|
|
|
static OpcUaManager* instance(QObject* parent = nullptr); // 全局唯一获取方式
|
|
|
|
|
|
// 禁止拷贝和赋值(避免创建多个实例)
|
|
|
|
|
|
OpcUaManager(const OpcUaManager&) = delete;
|
|
|
|
|
|
OpcUaManager& operator=(const OpcUaManager&) = delete;
|
2025-08-20 23:06:28 +08:00
|
|
|
|
~OpcUaManager();
|
|
|
|
|
|
|
|
|
|
|
|
// 连接服务器
|
2025-09-15 22:28:43 +08:00
|
|
|
|
bool connectToServer(const QString &url, const QString &username = "", const QString &password = "");
|
2025-08-20 23:06:28 +08:00
|
|
|
|
// 断开连接
|
|
|
|
|
|
void disconnectFromServer();
|
2025-09-15 22:28:43 +08:00
|
|
|
|
// 读取单个节点值
|
2025-08-20 23:06:28 +08:00
|
|
|
|
bool readNodeValue(const QString &nodeId);
|
2025-09-15 22:28:43 +08:00
|
|
|
|
// 批量读取节点值
|
|
|
|
|
|
Q_INVOKABLE bool readNodesValue(const QList<QString>& nodeIds);
|
|
|
|
|
|
// 写入节点值(异步)
|
|
|
|
|
|
bool writeNodeValue(const QString &nodeId,
|
|
|
|
|
|
const QVariant &value,
|
|
|
|
|
|
int maxRetry =10 /* = 3 */,
|
|
|
|
|
|
int retryIntervalMs = 5 /* = 100 */);
|
2025-08-20 23:06:28 +08:00
|
|
|
|
// 获取连接状态
|
|
|
|
|
|
QOpcUaClient::ClientState connectionState() const;
|
|
|
|
|
|
|
2025-09-15 22:28:43 +08:00
|
|
|
|
QOpcUaClient* getClient() const;
|
|
|
|
|
|
|
|
|
|
|
|
// 获取缓存的节点
|
|
|
|
|
|
QOpcUaNode* getCachedNode(const QString& nodeId);
|
|
|
|
|
|
|
|
|
|
|
|
// 清理节点缓存
|
|
|
|
|
|
void clearNodeCache();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-20 23:06:28 +08:00
|
|
|
|
signals:
|
|
|
|
|
|
// 状态变化信号
|
|
|
|
|
|
void stateChanged(QOpcUaClient::ClientState state);
|
|
|
|
|
|
// 读取完成信号(返回值和节点ID)
|
|
|
|
|
|
void readCompleted(const QVariant &value, const QString &nodeId);
|
2025-09-15 22:28:43 +08:00
|
|
|
|
// 批量读取完成信号
|
|
|
|
|
|
void batchReadCompleted(const QMap<QString, QVariant>& results);
|
2025-08-20 23:06:28 +08:00
|
|
|
|
// 写入完成信号(返回是否成功和节点ID)
|
|
|
|
|
|
void writeCompleted(bool success, const QString &nodeId);
|
|
|
|
|
|
// 错误信号(返回错误信息)
|
|
|
|
|
|
void errorOccurred(const QString &errorString);
|
2025-09-15 22:28:43 +08:00
|
|
|
|
void reconnected(); // 重连成功后广播
|
|
|
|
|
|
|
2025-08-20 23:06:28 +08:00
|
|
|
|
private slots:
|
|
|
|
|
|
// 端点请求完成处理
|
|
|
|
|
|
void onEndpointsRequestFinished(QVector<QOpcUaEndpointDescription> endpoints,
|
|
|
|
|
|
QOpcUa::UaStatusCode statusCode,
|
|
|
|
|
|
QUrl requestedUrl);
|
|
|
|
|
|
// 客户端状态变化处理
|
|
|
|
|
|
void onClientStateChanged(QOpcUaClient::ClientState state);
|
|
|
|
|
|
// 属性读取完成处理(用于普通读取)
|
|
|
|
|
|
void onAttributeRead(QOpcUa::NodeAttributes attrs);
|
2025-09-15 22:28:43 +08:00
|
|
|
|
// 批量读取完成处理
|
|
|
|
|
|
void onBatchReadFinished(const QVector<QOpcUaReadResult>& results);
|
2025-08-20 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-09-15 22:28:43 +08:00
|
|
|
|
void tryReconnect(); // 执行重连
|
2025-08-20 23:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-09-15 22:28:43 +08:00
|
|
|
|
private:
|
|
|
|
|
|
explicit OpcUaManager(QObject *parent = nullptr); // 私有构造函数(仅允许 instance() 创建)
|
|
|
|
|
|
static OpcUaManager* m_instance; // 静态单例实例
|
|
|
|
|
|
static QMutex m_mutex; // 线程安全锁(避免多线程冲突)
|
2025-08-20 23:06:28 +08:00
|
|
|
|
|
2025-09-15 22:28:43 +08:00
|
|
|
|
// 创建客户端实例
|
|
|
|
|
|
void createClient();
|
2025-08-20 23:06:28 +08:00
|
|
|
|
|
2025-09-15 22:28:43 +08:00
|
|
|
|
QOpcUaClient *mClient = nullptr; // OPC UA客户端实例
|
|
|
|
|
|
QMap<QString, QOpcUaNode*> mNodeCache; // 节点缓存
|
|
|
|
|
|
QMap<QOpcUaNode*, WriteRequest> mWriteRequests; // 写入请求缓存
|
2025-08-20 23:06:28 +08:00
|
|
|
|
|
2025-09-15 22:28:43 +08:00
|
|
|
|
QString mCurrentNodeId; // 当前节点ID
|
|
|
|
|
|
QUrl mServerUrl; // 服务器地址
|
|
|
|
|
|
QString mUserName; // 登录用户名
|
|
|
|
|
|
QString mPassword; // 登录密码
|
|
|
|
|
|
QVariant mPendingWriteValue; // 待写入的值(临时存储)
|
2025-08-20 23:06:28 +08:00
|
|
|
|
|
2025-09-15 22:28:43 +08:00
|
|
|
|
QTimer *mReconnectTimer = nullptr; // 重连定时器
|
|
|
|
|
|
uint32_t mReconnectCount = 0; // 重连计数器
|
|
|
|
|
|
const int RECONNECT_INTERVAL = 3000; // 重连时间间隔(ms)
|
|
|
|
|
|
qint64 mLastReconnectAttempt = 0; // 上次重连尝试时间
|
|
|
|
|
|
bool mIsBatchReading = false; // 新增:批量读取状态标记
|
2025-08-20 23:06:28 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // OPCUAMANAGER_H
|