添加OpenCv
This commit is contained in:
@@ -2,33 +2,46 @@
|
||||
#include <QOpcUaProvider>
|
||||
#include <QMap>
|
||||
#include <QDebug>
|
||||
#include <QDateTime>
|
||||
#include <GlobalDefinitions/Variable.h>
|
||||
OpcUaManager* OpcUaManager::m_instance = nullptr;
|
||||
QMutex OpcUaManager::m_mutex;
|
||||
|
||||
// OPC UA错误码常量定义
|
||||
const qint32 bad_user_access_denied = -2139881472; // 用户访问被拒绝
|
||||
const qint32 bad_type_mismatch = -2147483053; // 数据类型不匹配
|
||||
|
||||
// 构造函数
|
||||
OpcUaManager::OpcUaManager(QObject *parent)
|
||||
: QObject(parent)
|
||||
, mClient(nullptr)
|
||||
, mCurrentNode(nullptr)
|
||||
, mReconnectTimer(new QTimer(this))
|
||||
{
|
||||
connect(mReconnectTimer, &QTimer::timeout, // 3. 铃声(timeout)连到槽
|
||||
this, &OpcUaManager::tryReconnect); // 到时就会执行 tryReconnect()
|
||||
connect(mReconnectTimer, &QTimer::timeout, this, &OpcUaManager::tryReconnect);
|
||||
mReconnectTimer->setInterval(RECONNECT_INTERVAL);
|
||||
|
||||
}
|
||||
|
||||
OpcUaManager* OpcUaManager::instance(QObject *parent)
|
||||
{
|
||||
if (!m_instance) {
|
||||
QMutexLocker locker(&m_mutex);
|
||||
if (!m_instance) {
|
||||
m_instance = new OpcUaManager(parent);
|
||||
}
|
||||
}
|
||||
return m_instance;
|
||||
}
|
||||
|
||||
// 析构函数
|
||||
OpcUaManager::~OpcUaManager()
|
||||
{
|
||||
disconnectFromServer();
|
||||
clearNodeCache();
|
||||
if (mClient) {
|
||||
delete mClient;
|
||||
mClient = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// 连接到OPC UA服务器
|
||||
QOpcUaClient* OpcUaManager::getClient() const
|
||||
{
|
||||
return mClient;
|
||||
}
|
||||
|
||||
bool OpcUaManager::connectToServer(const QString &url, const QString &username, const QString &password)
|
||||
{
|
||||
mServerUrl = QUrl(url);
|
||||
@@ -44,111 +57,204 @@ bool OpcUaManager::connectToServer(const QString &url, const QString &username,
|
||||
createClient();
|
||||
if (!mClient) {
|
||||
emit errorOccurred("无法创建OPC UA客户端");
|
||||
// ✅ 启动重连定时器
|
||||
onClientStateChanged(QOpcUaClient::Disconnected);
|
||||
return false;
|
||||
}
|
||||
|
||||
mClient->requestEndpoints(mServerUrl);
|
||||
connect(mClient, &QOpcUaClient::readNodeAttributesFinished,this, &OpcUaManager::onBatchReadFinished);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 断开与服务器的连接
|
||||
void OpcUaManager::disconnectFromServer()
|
||||
{
|
||||
if (mClient) {
|
||||
mClient->disconnectFromEndpoint(); // 断开端点连接
|
||||
}
|
||||
|
||||
// 清理当前节点
|
||||
if (mCurrentNode) {
|
||||
mCurrentNode->disconnect(this);
|
||||
mCurrentNode = nullptr;
|
||||
mClient->disconnectFromEndpoint();
|
||||
}
|
||||
|
||||
clearNodeCache();
|
||||
mCurrentNodeId.clear();
|
||||
mPendingWriteValue = QVariant(); // 清空待写入值
|
||||
mPendingWriteValue = QVariant();
|
||||
}
|
||||
|
||||
// 读取节点值
|
||||
bool OpcUaManager::readNodeValue(const QString &nodeId)
|
||||
{
|
||||
// 检查客户端状态
|
||||
if (!mClient || mClient->state() != QOpcUaClient::Connected) {
|
||||
emit errorOccurred("客户端未连接");
|
||||
qWarning()<<("客户端未连接");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 清理旧节点
|
||||
if (mCurrentNode) {
|
||||
mCurrentNode->disconnect(this);
|
||||
mCurrentNode = nullptr;
|
||||
}
|
||||
|
||||
// 获取目标节点
|
||||
mCurrentNodeId = nodeId;
|
||||
mCurrentNode = mClient->node(nodeId);
|
||||
if (!mCurrentNode) {
|
||||
QOpcUaNode* node = getCachedNode(nodeId);
|
||||
if (!node) {
|
||||
QString error = QString("无法获取节点: %1").arg(nodeId);
|
||||
emit errorOccurred(error);
|
||||
qWarning()<<(error);
|
||||
return false;
|
||||
}
|
||||
mCurrentNode->setParent(this);
|
||||
|
||||
// 连接读取信号
|
||||
connect(mCurrentNode,
|
||||
mCurrentNodeId = nodeId;
|
||||
|
||||
disconnect(node, &QOpcUaNode::attributeRead, this, &OpcUaManager::onAttributeRead);
|
||||
connect(node,
|
||||
static_cast<void (QOpcUaNode::*)(QOpcUa::NodeAttributes)>(&QOpcUaNode::attributeRead),
|
||||
this,
|
||||
&OpcUaManager::onAttributeRead);
|
||||
|
||||
// 读取节点值和访问级别
|
||||
mCurrentNode->readAttributes(
|
||||
QOpcUa::NodeAttribute::Value | // 节点值
|
||||
QOpcUa::NodeAttribute::AccessLevel // 访问级别(可读/可写)
|
||||
node->readAttributes(
|
||||
QOpcUa::NodeAttribute::Value |
|
||||
QOpcUa::NodeAttribute::AccessLevel
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 写入节点值(动态适配类型)
|
||||
bool OpcUaManager::writeNodeValue(const QString &nodeId, const QVariant &value)
|
||||
void OpcUaManager::onAttributeRead(QOpcUa::NodeAttributes attrs)
|
||||
{
|
||||
// 检查客户端状态
|
||||
QOpcUaNode* node = qobject_cast<QOpcUaNode*>(sender());
|
||||
if (!node) return;
|
||||
|
||||
QString nodeId = node->nodeId();
|
||||
if (attrs.testFlag(QOpcUa::NodeAttribute::Value)) {
|
||||
gOPC_NodeValue[nodeId] = node->attribute(QOpcUa::NodeAttribute::Value);
|
||||
|
||||
} else {
|
||||
gOPC_NodeValue[nodeId] = "";
|
||||
}
|
||||
}
|
||||
bool OpcUaManager::readNodesValue(const QList<QString>& nodeIds)
|
||||
{
|
||||
// 前置校验:客户端连接状态 + 节点列表非空 + 避免并发读取
|
||||
if (!mClient || mClient->state() != QOpcUaClient::Connected) {
|
||||
emit errorOccurred("客户端未连接");
|
||||
qWarning()<<("批量读取失败:OPC客户端未连接");
|
||||
return false;
|
||||
}
|
||||
if (nodeIds.isEmpty()) {
|
||||
qWarning()<<("批量读取失败:节点列表为空");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 清理旧节点
|
||||
if (mCurrentNode) {
|
||||
mCurrentNode->disconnect(this);
|
||||
mCurrentNode = nullptr;
|
||||
|
||||
// 1. 构建批量读取请求(正确构造QOpcUaReadItem)
|
||||
QVector<QOpcUaReadItem> readItems;
|
||||
for (const QString& nodeId : nodeIds) {
|
||||
QOpcUaReadItem item;
|
||||
item.setNodeId(nodeId);
|
||||
readItems.append(item);
|
||||
}
|
||||
|
||||
// 保存待写入值和节点ID
|
||||
mPendingWriteValue = value;
|
||||
mCurrentNodeId = nodeId;
|
||||
|
||||
// 获取目标节点
|
||||
mCurrentNode = mClient->node(nodeId);
|
||||
if (!mCurrentNode) {
|
||||
QString error = QString("无法获取节点: %1").arg(nodeId);
|
||||
emit errorOccurred(error);
|
||||
// 发送批量读取请求
|
||||
if(!mClient->readNodeAttributes(readItems)){
|
||||
qDebug()<<("批量读取请求发送失败");
|
||||
return false;
|
||||
}
|
||||
mCurrentNode->setParent(this);
|
||||
|
||||
// 连接类型读取信号(写入前先读类型)
|
||||
connect(mCurrentNode,
|
||||
static_cast<void (QOpcUaNode::*)(QOpcUa::NodeAttributes)>(&QOpcUaNode::attributeRead),
|
||||
this,
|
||||
&OpcUaManager::onPreWriteValueTypeRead);
|
||||
|
||||
// 读取节点当前值(用于判断类型)
|
||||
mCurrentNode->readAttributes(QOpcUa::NodeAttribute::Value);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 获取当前连接状态
|
||||
// 批量读取结果处理槽函数(参数需与信号匹配)
|
||||
void OpcUaManager::onBatchReadFinished(const QVector<QOpcUaReadResult>& results)
|
||||
{
|
||||
// 遍历结果(使用QOpcUaReadResult的方法获取数据)
|
||||
for (const auto& result : results) {
|
||||
if (result.statusCode() == QOpcUa::Good) {
|
||||
gOPC_NodeValue[result.nodeId()] = result.value();
|
||||
emit readCompleted(result.value(), result.nodeId());
|
||||
} else {
|
||||
gOPC_NodeValue[result.nodeId()] = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//bool OpcUaManager::writeNodeValue(const QString &nodeId, const QVariant &value, int maxRetry)
|
||||
//{
|
||||
// if (!mClient || mClient->state() != QOpcUaClient::Connected) {
|
||||
// emit errorOccurred("客户端未连接");
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// QOpcUaNode* node = getCachedNode(nodeId);
|
||||
// if (!node) {
|
||||
// emit errorOccurred(QString("无法获取节点: %1").arg(nodeId));
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// WriteRequest request;
|
||||
// request.nodeId = nodeId;
|
||||
// request.value = value;
|
||||
// request.retryCount = maxRetry;
|
||||
// mWriteRequests[node] = request;
|
||||
|
||||
// disconnect(node, &QOpcUaNode::attributeRead, this, &OpcUaManager::onPreWriteValueTypeRead);
|
||||
// connect(node,
|
||||
// static_cast<void (QOpcUaNode::*)(QOpcUa::NodeAttributes)>(&QOpcUaNode::attributeRead),
|
||||
// this,
|
||||
// &OpcUaManager::onPreWriteValueTypeRead);
|
||||
|
||||
// node->readAttributes(QOpcUa::NodeAttribute::Value);
|
||||
// return writeNodeValue(nodeId,value,maxRetry,100);
|
||||
//}
|
||||
|
||||
bool OpcUaManager::writeNodeValue(const QString &nodeId,
|
||||
const QVariant &value,
|
||||
int maxRetry /*= 3*/,
|
||||
int retryIntervalMs /*= 200*/)
|
||||
{
|
||||
if (!mClient || mClient->state() != QOpcUaClient::Connected) {
|
||||
qWarning().noquote() << "[OPC] 客户端未连接,写入" << nodeId << "失败";
|
||||
return false;
|
||||
}
|
||||
|
||||
QOpcUaNode *node = mClient->node(nodeId);
|
||||
if (!node) {
|
||||
qWarning().noquote() << "[OPC] 无法写入节点:" << nodeId;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int attempt = 1; attempt <= maxRetry; ++attempt) {
|
||||
/* ---------- 1. 先读当前值 ---------- */
|
||||
QVariant cur = gOPC_NodeValue[nodeId];
|
||||
if (!cur.isValid()) {
|
||||
//qWarning().noquote() << "读到无效值:" << nodeId<<gOPC_NodeValue[nodeId];
|
||||
}
|
||||
|
||||
/* ---------- 2. 类型转换 ---------- */
|
||||
QVariant v = value;
|
||||
QOpcUa::Types typeHint = QOpcUa::Types::Undefined;
|
||||
QString tn = cur.typeName();
|
||||
if (tn == "int") { v.convert(QMetaType::Int); typeHint = QOpcUa::Types::Int32; }
|
||||
else if (tn == "float") { v.convert(QMetaType::Float); typeHint = QOpcUa::Types::Float; }
|
||||
else if (tn == "double"){ v.convert(QMetaType::Double); typeHint = QOpcUa::Types::Double; }
|
||||
else if (tn == "ushort"){ v.convert(QMetaType::UShort); typeHint = QOpcUa::Types::UInt16; }
|
||||
|
||||
/* ---------- 3. 同步写 ---------- */
|
||||
struct WriteWait : QObject {
|
||||
bool ok = false;
|
||||
QEventLoop loop;
|
||||
} waiter;
|
||||
|
||||
connect(node,
|
||||
static_cast<void(QOpcUaNode::*)(QOpcUa::NodeAttribute, QOpcUa::UaStatusCode)>(
|
||||
&QOpcUaNode::attributeWritten),
|
||||
&waiter, [&waiter](QOpcUa::NodeAttribute, QOpcUa::UaStatusCode code) mutable {
|
||||
waiter.ok = (code == QOpcUa::UaStatusCode::Good);
|
||||
waiter.loop.quit();
|
||||
}, Qt::QueuedConnection);
|
||||
|
||||
node->writeAttribute(QOpcUa::NodeAttribute::Value, v, typeHint);
|
||||
QTimer::singleShot(2000, &waiter.loop, &QEventLoop::quit);
|
||||
waiter.loop.exec();
|
||||
|
||||
if (waiter.ok) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ---------- 4. 失败日志 & 重试 ---------- */
|
||||
// qWarning().noquote() << "[OPC] 第" << attempt << "/" << maxRetry
|
||||
// << "次写入失败:" << nodeId << " 等待重试 ...";
|
||||
if (attempt < maxRetry)
|
||||
QThread::msleep(retryIntervalMs);
|
||||
}
|
||||
|
||||
qCritical().noquote() << "[OPC] 最终写入失败:" << nodeId;
|
||||
return false;
|
||||
}
|
||||
QOpcUaClient::ClientState OpcUaManager::connectionState() const
|
||||
{
|
||||
if (mClient) {
|
||||
@@ -157,7 +263,6 @@ QOpcUaClient::ClientState OpcUaManager::connectionState() const
|
||||
return QOpcUaClient::Disconnected;
|
||||
}
|
||||
|
||||
// 创建OPC UA客户端
|
||||
void OpcUaManager::createClient()
|
||||
{
|
||||
QOpcUaProvider provider;
|
||||
@@ -169,11 +274,15 @@ void OpcUaManager::createClient()
|
||||
clientParams["password"] = mPassword;
|
||||
}
|
||||
|
||||
// 禁用安全策略(解决部分服务器权限误判)
|
||||
// 禁用安全策略(根据实际需求调整)
|
||||
clientParams["securityMode"] = "None";
|
||||
clientParams["securityPolicy"] = "None";
|
||||
|
||||
// 创建客户端(使用open62541后端,兼容性较好)
|
||||
// 性能参数设置
|
||||
clientParams["requestTimeout"] = 5000;
|
||||
clientParams["maxArrayLength"] = 10000;
|
||||
|
||||
// 创建客户端(使用open62541后端)
|
||||
mClient = provider.createClient("open62541", clientParams);
|
||||
if (!mClient) {
|
||||
return;
|
||||
@@ -194,16 +303,27 @@ void OpcUaManager::createClient()
|
||||
this,
|
||||
&OpcUaManager::onClientStateChanged);
|
||||
}
|
||||
uint32_t mReconnectCount =0;
|
||||
|
||||
void OpcUaManager::tryReconnect()
|
||||
{
|
||||
if (mClient && mClient->state() == QOpcUaClient::Disconnected) {
|
||||
qDebug() << "正在执行第" << mReconnectCount << "次重连...";
|
||||
connectToServer(mServerUrl.toString(), mUserName, mPassword);
|
||||
qint64 now = QDateTime::currentMSecsSinceEpoch();
|
||||
|
||||
// 限制重连频率
|
||||
if (now - mLastReconnectAttempt > 1000) {
|
||||
mLastReconnectAttempt = now;
|
||||
qDebug() << "正在执行第" << mReconnectCount << "次重连...";
|
||||
connectToServer(mServerUrl.toString(), mUserName, mPassword);
|
||||
mReconnectCount++;
|
||||
|
||||
// 重连次数过多时增加间隔时间
|
||||
if (mReconnectCount > 10) {
|
||||
mReconnectTimer->setInterval(RECONNECT_INTERVAL * 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
mReconnectCount ++;
|
||||
}
|
||||
// 端点请求完成处理
|
||||
|
||||
void OpcUaManager::onEndpointsRequestFinished(QVector<QOpcUaEndpointDescription> endpoints,
|
||||
QOpcUa::UaStatusCode statusCode,
|
||||
QUrl requestedUrl)
|
||||
@@ -212,7 +332,6 @@ void OpcUaManager::onEndpointsRequestFinished(QVector<QOpcUaEndpointDescription>
|
||||
if (statusCode != QOpcUa::UaStatusCode::Good || endpoints.isEmpty()) {
|
||||
QString error = QString("获取端点失败,状态码: %1").arg(static_cast<int>(statusCode));
|
||||
emit errorOccurred(error);
|
||||
// ✅ 启动重连定时器
|
||||
mReconnectTimer->start(RECONNECT_INTERVAL);
|
||||
return;
|
||||
}
|
||||
@@ -220,131 +339,40 @@ void OpcUaManager::onEndpointsRequestFinished(QVector<QOpcUaEndpointDescription>
|
||||
mClient->connectToEndpoint(endpoints.first());
|
||||
}
|
||||
|
||||
// 客户端状态变化处理
|
||||
void OpcUaManager::onClientStateChanged(QOpcUaClient::ClientState state)
|
||||
{
|
||||
emit stateChanged(state); // 转发状态变化信号
|
||||
emit stateChanged(state);
|
||||
|
||||
if (state == QOpcUaClient::Disconnected) {
|
||||
// 无限重连
|
||||
qDebug() << "OPC UA 连接断开," << RECONNECT_INTERVAL
|
||||
<< "ms 后自动重连...";
|
||||
qDebug() << "OPC UA 连接断开," << RECONNECT_INTERVAL << "ms 后自动重连...";
|
||||
mReconnectTimer->start(RECONNECT_INTERVAL);
|
||||
} else if (state == QOpcUaClient::Connected) {
|
||||
mReconnectTimer->stop();
|
||||
emit reconnected(); // 广播重连成功
|
||||
mReconnectCount = 0; // 重置计数器
|
||||
mReconnectTimer->setInterval(RECONNECT_INTERVAL); // 重置间隔
|
||||
emit reconnected();
|
||||
}
|
||||
}
|
||||
|
||||
// 属性读取完成处理(普通读取)
|
||||
void OpcUaManager::onAttributeRead(QOpcUa::NodeAttributes attrs)
|
||||
QOpcUaNode* OpcUaManager::getCachedNode(const QString& nodeId)
|
||||
{
|
||||
if (attrs.testFlag(QOpcUa::NodeAttribute::Value) && mCurrentNode) {
|
||||
// 读取值并转发
|
||||
QVariant value = mCurrentNode->attribute(QOpcUa::NodeAttribute::Value);
|
||||
emit readCompleted(value, mCurrentNodeId);
|
||||
|
||||
// // 检查访问级别
|
||||
// if (attrs.testFlag(QOpcUa::NodeAttribute::AccessLevel)) {
|
||||
// quint8 accessLevel = mCurrentNode->attribute(QOpcUa::NodeAttribute::AccessLevel).toUInt();
|
||||
|
||||
// bool canWrite = (accessLevel & 0x02); // 0x02 = 可写
|
||||
// bool canRead = (accessLevel & 0x01); // 0x01 = 可读
|
||||
// qDebug() << "节点访问级别: " << accessLevel
|
||||
// << ",可读: " << canRead
|
||||
// << ",可写: " << canWrite;
|
||||
|
||||
// if (!canWrite) {
|
||||
// emit errorOccurred("节点当前不可写(服务器限制)");
|
||||
// }
|
||||
// }
|
||||
} else {
|
||||
emit readCompleted(QVariant(), mCurrentNodeId);
|
||||
emit errorOccurred(QString("读取节点 %1 失败").arg(mCurrentNodeId));
|
||||
if (mNodeCache.contains(nodeId)) {
|
||||
return mNodeCache[nodeId];
|
||||
}
|
||||
|
||||
if (!mClient) return nullptr;
|
||||
|
||||
QOpcUaNode* node = mClient->node(nodeId);
|
||||
if (node) {
|
||||
node->setParent(this);
|
||||
mNodeCache[nodeId] = node;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
// 写入前读取值类型(使用QOpcUa::Types枚举强制指定类型)
|
||||
void OpcUaManager::onPreWriteValueTypeRead(QOpcUa::NodeAttributes attrs)
|
||||
void OpcUaManager::clearNodeCache()
|
||||
{
|
||||
if (!mCurrentNode || !attrs.testFlag(QOpcUa::NodeAttribute::Value)) {
|
||||
emit errorOccurred("读取节点当前值失败,无法判断类型");
|
||||
return;
|
||||
}
|
||||
|
||||
// 断开类型读取信号(避免重复触发)
|
||||
disconnect(mCurrentNode, &QOpcUaNode::attributeRead, this, &OpcUaManager::onPreWriteValueTypeRead);
|
||||
|
||||
// 获取当前值的类型
|
||||
QVariant currentValue = mCurrentNode->attribute(QOpcUa::NodeAttribute::Value);
|
||||
QString typeName = currentValue.typeName();
|
||||
qDebug() << "节点当前值类型:" << typeName;
|
||||
|
||||
// 转换值并指定QOpcUa类型枚举(核心修复)
|
||||
QVariant convertedValue;
|
||||
QOpcUa::Types targetType = QOpcUa::Types::Undefined; // 使用枚举类型
|
||||
|
||||
if (typeName == "int") {
|
||||
convertedValue = mPendingWriteValue.toInt();
|
||||
targetType = QOpcUa::Types::Int32; // int对应Int32
|
||||
} else if (typeName == "float") {
|
||||
convertedValue = static_cast<float>(mPendingWriteValue.toDouble());
|
||||
targetType = QOpcUa::Types::Float; // float对应Float
|
||||
} else if (typeName == "double") {
|
||||
convertedValue = mPendingWriteValue.toDouble();
|
||||
targetType = QOpcUa::Types::Double; // double对应Double
|
||||
} else if (typeName == "ushort") {
|
||||
quint16 value = static_cast<quint16>(mPendingWriteValue.toUInt());
|
||||
if (value > 65535) {
|
||||
value = 65535;
|
||||
qWarning() << "值超过ushort最大值65535,已自动截断";
|
||||
}
|
||||
convertedValue = value;
|
||||
targetType = QOpcUa::Types::UInt16; // ushort对应UInt16
|
||||
} else {
|
||||
convertedValue = mPendingWriteValue;
|
||||
targetType = QOpcUa::Types::Undefined; // 自动类型
|
||||
}
|
||||
|
||||
qDebug() << "转换为" << typeName << "类型写入,QOpcUa枚举值:" << static_cast<int>(targetType);
|
||||
|
||||
// 连接写入信号并执行写入
|
||||
connect(mCurrentNode,
|
||||
static_cast<void (QOpcUaNode::*)(QOpcUa::NodeAttribute, QOpcUa::UaStatusCode)>(&QOpcUaNode::attributeWritten),
|
||||
this,
|
||||
&OpcUaManager::onAttributeWritten);
|
||||
|
||||
// 关键:使用QOpcUa::Types枚举作为参数,解决类型不匹配
|
||||
mCurrentNode->writeAttribute(
|
||||
QOpcUa::NodeAttribute::Value,
|
||||
convertedValue,
|
||||
targetType
|
||||
);
|
||||
}
|
||||
|
||||
// 属性写入完成处理
|
||||
void OpcUaManager::onAttributeWritten(QOpcUa::NodeAttribute attribute, QOpcUa::UaStatusCode statusCode)
|
||||
{
|
||||
if (attribute == QOpcUa::NodeAttribute::Value) {
|
||||
bool success = (statusCode == QOpcUa::UaStatusCode::Good);
|
||||
emit writeCompleted(success, mCurrentNodeId);
|
||||
|
||||
if (!success) {
|
||||
QString errorMsg;
|
||||
// 根据错误码解析原因
|
||||
switch (static_cast<qint32>(statusCode)) {
|
||||
case bad_user_access_denied:
|
||||
errorMsg = QString("写入节点 %1 失败:用户没有写入权限(可能是类型不匹配导致的误报)").arg(mCurrentNodeId);
|
||||
break;
|
||||
case bad_type_mismatch:
|
||||
errorMsg = QString("写入节点 %1 失败:数据类型不匹配").arg(mCurrentNodeId);
|
||||
break;
|
||||
default:
|
||||
errorMsg = QString("写入节点 %1 失败,状态码: %2")
|
||||
.arg(mCurrentNodeId)
|
||||
.arg(static_cast<int>(statusCode));
|
||||
}
|
||||
emit errorOccurred(errorMsg);
|
||||
}
|
||||
}
|
||||
qDeleteAll(mNodeCache.values());
|
||||
mNodeCache.clear();
|
||||
mWriteRequests.clear();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user