Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPC UA 模块修改日志输出等级 #95

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ devel
*.jpg
*.jpeg
!rmvl-logo*
!doc/img/*
!doc/img/**

# 模型文件
*.onnx
Expand Down
14 changes: 0 additions & 14 deletions modules/opcua/include/rmvl/opcua/method.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,4 @@ struct Method final

//! @} opcua

namespace helper
{

/**
* @brief `rm::Argument` 转化为 `UA_Argument`
*
* @warning 此方法一般不直接使用
* @param[in] arg `rm::Argument` 表示的方法
* @return `UA_Argument` 表示的方法
*/
UA_Argument cvtArgument(const Argument &arg);

} // namespace helper

} // namespace rm
34 changes: 18 additions & 16 deletions modules/opcua/include/rmvl/opcua/utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

#pragma once

#include <string_view>
#include <string>
#include <typeindex>
#include <string_view>
#include <unordered_map>

#include <open62541/types_generated_handling.h>
Expand All @@ -30,9 +31,10 @@ namespace rm
//! @addtogroup opcua
//! @{

//! OPC UA 节点 ID
struct NodeId final
{
UA_NodeId nid{}; //!< 节点 ID
UA_NodeId nid{}; //!< open62541 的节点 ID 结构体

//! 默认构造节点 ID
NodeId() = default;
Expand Down Expand Up @@ -88,27 +90,27 @@ struct NodeId final
*/
struct UserConfig final
{
::std::string id; //!< 用户名
::std::string passwd; //!< 密码
std::string id; //!< 用户名
std::string passwd; //!< 密码
};

//! 类型标志位,可通过 `typeflag.at(xxx)` 进行获取
using UA_TypeFlag = UA_UInt32;

//! 获取形如 `UA_TYPES_<xxx>` 的类型标志位
inline const std::unordered_map<std::type_index, UA_TypeFlag> typeflag =
{{::std::type_index(typeid(bool)), UA_TYPES_BOOLEAN},
{::std::type_index(typeid(int8_t)), UA_TYPES_SBYTE},
{::std::type_index(typeid(uint8_t)), UA_TYPES_BYTE},
{::std::type_index(typeid(UA_Int16)), UA_TYPES_INT16},
{::std::type_index(typeid(UA_UInt16)), UA_TYPES_UINT16},
{::std::type_index(typeid(UA_Int32)), UA_TYPES_INT32},
{::std::type_index(typeid(UA_UInt32)), UA_TYPES_UINT32},
{::std::type_index(typeid(UA_Int64)), UA_TYPES_INT64},
{::std::type_index(typeid(UA_UInt64)), UA_TYPES_UINT64},
{::std::type_index(typeid(UA_Float)), UA_TYPES_FLOAT},
{::std::type_index(typeid(UA_Double)), UA_TYPES_DOUBLE},
{::std::type_index(typeid(const char *)), UA_TYPES_STRING}};
{{std::type_index(typeid(bool)), UA_TYPES_BOOLEAN},
{std::type_index(typeid(int8_t)), UA_TYPES_SBYTE},
{std::type_index(typeid(uint8_t)), UA_TYPES_BYTE},
{std::type_index(typeid(UA_Int16)), UA_TYPES_INT16},
{std::type_index(typeid(UA_UInt16)), UA_TYPES_UINT16},
{std::type_index(typeid(UA_Int32)), UA_TYPES_INT32},
{std::type_index(typeid(UA_UInt32)), UA_TYPES_UINT32},
{std::type_index(typeid(UA_Int64)), UA_TYPES_INT64},
{std::type_index(typeid(UA_UInt64)), UA_TYPES_UINT64},
{std::type_index(typeid(UA_Float)), UA_TYPES_FLOAT},
{std::type_index(typeid(UA_Double)), UA_TYPES_DOUBLE},
{std::type_index(typeid(const char *)), UA_TYPES_STRING}};

//! 传输协议
enum class TransportID : uint8_t
Expand Down
25 changes: 16 additions & 9 deletions modules/opcua/src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ Client::Client(std::string_view address, UserConfig usr)
{
_client = UA_Client_new();
UA_ClientConfig *config = UA_Client_getConfig(_client);
// 修改日志
#if OPCUA_VERSION < 10400
config->logger = UA_Log_Stdout_withLevel(UA_LOGLEVEL_ERROR);
#else
config->logging = UA_Log_Stdout_new(UA_LOGLEVEL_ERROR);
#endif
// 设置默认配置
auto status = UA_ClientConfig_setDefault(config);
if (status == UA_STATUSCODE_GOOD)
{
Expand All @@ -40,7 +47,7 @@ Client::Client(std::string_view address, UserConfig usr)
}
if (status != UA_STATUSCODE_GOOD)
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "Failed to create client");
ERROR_("Failed to create client");
UA_Client_delete(_client);
_client = nullptr;
}
Expand Down Expand Up @@ -96,7 +103,7 @@ bool Client::write(const NodeId &node, const Variable &val)
UA_Variant_clear(&new_variant);
if (status != UA_STATUSCODE_GOOD)
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "Failed to write value to the specific node, error: %s", UA_StatusCode_name(status));
ERROR_("Failed to write value to the specific node, error: %s", UA_StatusCode_name(status));
return false;
}
return true;
Expand All @@ -115,7 +122,7 @@ bool Client::call(const NodeId &obj_node, const std::string &name, const std::ve
NodeId method_node = obj_node | find(name);
if (method_node.empty())
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "Failed to find the method node: %s", name.c_str());
ERROR_("Failed to find the method node: %s", name.c_str());
return false;
}
// 调用方法
Expand All @@ -125,7 +132,7 @@ bool Client::call(const NodeId &obj_node, const std::string &name, const std::ve
UA_Variant_clear(&input_variant);
if (status != UA_STATUSCODE_GOOD)
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "Failed to call the method, node id: %d, error code: %s",
ERROR_("Failed to call the method, node id: %d, error code: %s",
method_node.nid.identifier.numeric, UA_StatusCode_name(status));
return false;
}
Expand All @@ -150,7 +157,7 @@ NodeId Client::addViewNode(const View &view)
UA_QUALIFIEDNAME(view.ns, helper::to_char(view.browse_name)), attr, &retval);
if (status != UA_STATUSCODE_GOOD)
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "Failed to add view node, error: %s", UA_StatusCode_name(status));
ERROR_("Failed to add view node, error: %s", UA_StatusCode_name(status));
return {};
}
// 添加引用
Expand All @@ -161,7 +168,7 @@ NodeId Client::addViewNode(const View &view)
status = UA_Client_addReference(_client, retval, nodeOrganizes, true, UA_STRING_NULL, exp, UA_NODECLASS_VARIABLE);
if (status != UA_STATUSCODE_GOOD)
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "Failed to add reference, error: %s", UA_StatusCode_name(status));
ERROR_("Failed to add reference, error: %s", UA_StatusCode_name(status));
return {};
}
}
Expand All @@ -186,7 +193,7 @@ bool Client::monitor(NodeId node, UA_Client_DataChangeNotificationCallback on_ch
_client, resp.subscriptionId, UA_TIMESTAMPSTORETURN_BOTH, request, &node, on_change, nullptr);
if (result.statusCode != UA_STATUSCODE_GOOD)
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "Failed to create variable monitor, error: %s", UA_StatusCode_name(result.statusCode));
ERROR_("Failed to create variable monitor, error: %s", UA_StatusCode_name(result.statusCode));
return false;
}
return true;
Expand Down Expand Up @@ -234,7 +241,7 @@ bool Client::monitor(NodeId node, const std::vector<std::string> &names, UA_Clie
_client, sub_resp.subscriptionId, UA_TIMESTAMPSTORETURN_BOTH, request_item, &monitor_id, on_event, nullptr);
if (result.statusCode != UA_STATUSCODE_GOOD)
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "Failed to create event monitor, error: %s", UA_StatusCode_name(result.statusCode));
ERROR_("Failed to create event monitor, error: %s", UA_StatusCode_name(result.statusCode));
return false;
}
else
Expand All @@ -259,7 +266,7 @@ bool Client::createSubscription(UA_CreateSubscriptionResponse &response)
response = UA_Client_Subscriptions_create(_client, request, nullptr, nullptr, nullptr);
if (response.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "Failed to create subscription, error: %s",
ERROR_("Failed to create subscription, error: %s",
UA_StatusCode_name(response.responseHeader.serviceResult));
return false;
}
Expand Down
10 changes: 10 additions & 0 deletions modules/opcua/src/cvt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#pragma once

#include "rmvl/opcua/variable.hpp"
#include "rmvl/opcua/method.hpp"

namespace rm::helper
{
Expand Down Expand Up @@ -43,4 +44,13 @@ Variable cvtVariable(const UA_Variant &p_val);
*/
UA_Variant cvtVariable(const VariableType &vtype);

/**
* @brief `rm::Argument` 转化为 `UA_Argument`
*
* @warning 此方法一般不直接使用
* @param[in] arg `rm::Argument` 表示的方法
* @return `UA_Argument` 表示的方法
*/
UA_Argument cvtArgument(const Argument &arg);

} // namespace rm::helper
5 changes: 2 additions & 3 deletions modules/opcua/src/helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <open62541/plugin/log_stdout.h>
#include <open62541/server.h>

#include "rmvl/opcua/method.hpp"
#include "rmvl/opcua/utilities.hpp"

#include "cvt.hpp"
Expand Down Expand Up @@ -58,8 +57,8 @@ NodeId operator|(NodeId origin, FindNodeInClient &&fnic)
if (response.resultsSize == 1 && response.results[0].targetsSize == 1)
return response.results[0].targets[0].targetId.nodeId;

UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "Failed to find node, name: %s, error code: %s",
browse_name.c_str(), UA_StatusCode_name(response.responseHeader.serviceResult));
ERROR_("Failed to find node, name: %s, error code: %s",
browse_name.c_str(), UA_StatusCode_name(response.responseHeader.serviceResult));
return {};
}

Expand Down
12 changes: 6 additions & 6 deletions modules/opcua/src/publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Publisher<TransportID::UDP_UADP>::Publisher(const std::string &pub_name, const s
auto status = UA_Server_addPubSubConnection(_server, &connect_config, &_connection_id);
if (status != UA_STATUSCODE_GOOD)
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Failed to add connection, \"%s\"", UA_StatusCode_name(status));
ERROR_("Failed to add connection, \"%s\"", UA_StatusCode_name(status));
return;
}
//////////// 添加 PublishedDataSet (PDS) /////////////
Expand All @@ -71,7 +71,7 @@ Publisher<TransportID::UDP_UADP>::Publisher(const std::string &pub_name, const s
auto pds_status = UA_Server_addPublishedDataSet(_server, &pds_config, &_pds_id);
if (pds_status.addResult != UA_STATUSCODE_GOOD)
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Failed to add published dataset, \"%s\"",
ERROR_("Failed to add published dataset, \"%s\"",
UA_StatusCode_name(pds_status.addResult));
return;
}
Expand Down Expand Up @@ -104,7 +104,7 @@ bool Publisher<TransportID::UDP_UADP>::publish(const std::vector<PublishedDataSe
auto result = UA_Server_addDataSetField(_server, _pds_id, &dsf_config, &dsf_node_id);
if (result.result != UA_STATUSCODE_GOOD)
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Failed to add dataset field, name: \"%s\", status code: \"%s\"",
ERROR_("Failed to add dataset field, name: \"%s\", status code: \"%s\"",
pds.name.c_str(), UA_StatusCode_name(result.result));
return false;
}
Expand All @@ -130,13 +130,13 @@ bool Publisher<TransportID::UDP_UADP>::publish(const std::vector<PublishedDataSe
auto status = UA_Server_addWriterGroup(_server, _connection_id, &wg_config, &_wg_id);
if (status != UA_STATUSCODE_GOOD)
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Failed to add writer group, \"%s\"", UA_StatusCode_name(status));
ERROR_("Failed to add writer group, \"%s\"", UA_StatusCode_name(status));
return false;
}
status = UA_Server_setWriterGroupOperational(_server, _wg_id);
if (status != UA_STATUSCODE_GOOD)
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Failed to set writer group operational, \"%s\"",
ERROR_("Failed to set writer group operational, \"%s\"",
UA_StatusCode_name(status));
return false;
}
Expand All @@ -149,7 +149,7 @@ bool Publisher<TransportID::UDP_UADP>::publish(const std::vector<PublishedDataSe
status = UA_Server_addDataSetWriter(_server, _wg_id, _pds_id, &dsw_config, &_dsw_id);
if (status != UA_STATUSCODE_GOOD)
{
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Failed to add dataset writer, \"%s\"", UA_StatusCode_name(status));
ERROR_("Failed to add dataset writer, \"%s\"", UA_StatusCode_name(status));
return false;
}
return true;
Expand Down
Loading
Loading