Skip to content

Commit

Permalink
🔧 OPC UA 客户端定时器 Callback 移除客户端视图的传入参数
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoxi-scut authored and TooPretty0108 committed Feb 14, 2025
1 parent aeb027c commit a8ec59b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions modules/opcua/include/rmvl/opcua/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class RMVL_EXPORTS_W Client

/**
* @brief 直接以底层数据调用指定对象节点中的方法
*
*
* @param[in] obj_nd 对象节点
* @param[in] name 方法名
* @param[in] args 方法的所有传入参数
Expand Down Expand Up @@ -315,7 +315,7 @@ class RMVL_EXPORTS_W ClientTimer final
* @param[in] period 定时器周期,单位:毫秒 `ms`
* @param[in] callback 定时器回调函数
*/
RMVL_W ClientTimer(ClientView cv, double period, std::function<void(ClientView)> callback);
RMVL_W ClientTimer(ClientView cv, double period, std::function<void()> callback);

//! @cond
ClientTimer(const ClientTimer &) = delete;
Expand All @@ -332,9 +332,9 @@ class RMVL_EXPORTS_W ClientTimer final
RMVL_W void cancel();

private:
ClientView _cv; //!< 客户端视图
std::function<void(ClientView)> _cb; //!< 定时器回调函数
uint64_t _id{}; //!< 定时器 ID
ClientView _cv; //!< 客户端视图
std::function<void()> _cb; //!< 定时器回调函数
uint64_t _id{}; //!< 定时器 ID
};

//! @} opcua
Expand Down
10 changes: 5 additions & 5 deletions modules/opcua/include/rmvl/opcua/variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ class RMVL_EXPORTS_W Variable final
*/
template <typename Tp, typename DecayT = typename std::decay_t<Tp>, typename = std::enable_if_t<std::is_fundamental_v<DecayT>>>
RMVL_W_SUBST("V")
Variable(Tp val) : access_level(3U), _value(val), _data_type(DataType(typeid(DecayT))), _size(1) {}
Variable(Tp val) : _value(val), _data_type(DataType(typeid(DecayT))), _size(1) {}

/**
* @brief 字符串构造
*
* @param[in] str 字符串
*/
RMVL_W Variable(const std::string &str) : access_level(3U), _value(str), _data_type(DataType(typeid(std::string))), _size(1) {}
RMVL_W Variable(const std::string &str) : _value(str), _data_type(DataType(typeid(std::string))), _size(1) {}

/**
* @brief 字符串字面量构造
Expand All @@ -176,7 +176,7 @@ class RMVL_EXPORTS_W Variable final
*/
template <typename Tp, typename Enable = std::enable_if_t<std::is_fundamental_v<Tp> && !std::is_same_v<bool, Tp>>>
RMVL_W_SUBST("V_List")
Variable(const std::vector<Tp> &arr) : access_level(3U), _value(arr), _data_type(DataType(typeid(Tp))), _size(static_cast<UA_UInt32>(arr.size())) {}
Variable(const std::vector<Tp> &arr) : _value(arr), _data_type(DataType(typeid(Tp))), _size(static_cast<UA_UInt32>(arr.size())) {}

/**
* @brief 从变量类型创建新的变量节点
Expand Down Expand Up @@ -257,7 +257,7 @@ class RMVL_EXPORTS_W Variable final
RMVL_W inline uint32_t size() const { return _size; }

private:
explicit Variable(const VariableType &vtype) : access_level(3U), _type(vtype), _value(vtype.data()), _data_type(vtype.getDataType()), _size(vtype.size()) {}
explicit Variable(const VariableType &vtype) : _type(vtype), _value(vtype.data()), _data_type(vtype.getDataType()), _size(vtype.size()) {}

public:
//! 命名空间索引,默认为 `1`
Expand All @@ -283,7 +283,7 @@ class RMVL_EXPORTS_W Variable final
//! 变量的描述
RMVL_W_RW std::string description{};
//! 访问性
RMVL_W_RW uint8_t access_level{};
RMVL_W_RW uint8_t access_level{3U};

private:
//! 变量类型
Expand Down
8 changes: 4 additions & 4 deletions modules/opcua/src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,13 @@ bool ClientView::write(const NodeId &nd, const Variable &val) const { return cli

/////////////////////// 客户端定时器 ///////////////////////

static void timer_cb(UA_Client *p_server, void *data)
static void timer_cb(UA_Client *, void *data)
{
auto &func = *reinterpret_cast<std::function<void(ClientView)> *>(data);
func(p_server);
auto &func = *reinterpret_cast<std::function<void()> *>(data);
func();
}

ClientTimer::ClientTimer(ClientView cv, double period, std::function<void(ClientView)> callback) : _cv(cv), _cb(callback)
ClientTimer::ClientTimer(ClientView cv, double period, std::function<void()> callback) : _cv(cv), _cb(callback)
{
auto status = UA_Client_addRepeatedCallback(_cv.get(), timer_cb, &_cb, period, &_id);
if (status != UA_STATUSCODE_GOOD)
Expand Down
2 changes: 1 addition & 1 deletion modules/opcua/test/test_opcua_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ TEST(OPC_UA_Client, timer_test)
std::this_thread::sleep_for(10ms);
rm::Client cli("opc.tcp://127.0.0.1:5015");
int times{};
auto timer = rm::ClientTimer(cli, 10, [&](rm::ClientView) { times++; });
auto timer = rm::ClientTimer(cli, 10, [&]() { times++; });
std::this_thread::sleep_for(60ms);
cli.spinOnce();
std::this_thread::sleep_for(60ms);
Expand Down

0 comments on commit a8ec59b

Please sign in to comment.