diff --git a/modules/opcua/src/publisher.cpp b/modules/opcua/src/publisher.cpp index 37ea97e7..c9a9dad3 100644 --- a/modules/opcua/src/publisher.cpp +++ b/modules/opcua/src/publisher.cpp @@ -14,7 +14,9 @@ #ifdef UA_ENABLE_PUBSUB #include +#if OPCUA_VERSION < 10400 #include +#endif #include #ifdef UA_ENABLE_PUBSUB_MQTT @@ -36,7 +38,9 @@ Publisher::Publisher(const std::string &pub_name, const s const std::vector &users) : Server(port, pub_name, users), _name(pub_name) { //////////////////// 添加连接配置 //////////////////// +#if OPCUA_VERSION < 10400 UA_ServerConfig_addPubSubTransportLayer(UA_Server_getConfig(_server), UA_PubSubTransportLayerUDPMP()); +#endif UA_PubSubConnectionConfig connect_config{}; std::string cn_name_str = _name + "Connection"; connect_config.name = UA_STRING(helper::to_char(cn_name_str)); @@ -46,7 +50,13 @@ Publisher::Publisher(const std::string &pub_name, const s UA_NetworkAddressUrlDataType address_url{UA_STRING_NULL, UA_STRING(helper::to_char(url_str))}; UA_Variant_setScalar(&connect_config.address, &address_url, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]); // 用哈希值作为发布者 ID - connect_config.publisherId.numeric = _strhash(_name + "Connection") % 0x8000000u; +#if OPCUA_VERSION >= 10400 + connect_config.publisherIdType = UA_PUBLISHERIDTYPE_UINT16; + connect_config.publisherId.uint16 = _strhash(_name + "Connection") % 0x4000u; +#else + connect_config.publisherIdType = UA_PUBSUB_PUBLISHERID_NUMERIC; + connect_config.publisherId.numeric = _strhash(_name + "Connection") % 0x4000u; +#endif auto status = UA_Server_addPubSubConnection(_server, &connect_config, &_connection_id); if (status != UA_STATUSCODE_GOOD) { @@ -106,7 +116,7 @@ bool Publisher::publish(const std::vector::publish(const std::vectoraccessControl.clear(&config->accessControl); -#if OPCUA_VERSION >= 10300 +#if OPCUA_VERSION >= 10400 + UA_AccessControl_default(config, false, nullptr, usr_passwd.size(), usr_passwd.data()); +#elif OPCUA_VERSION >= 10300 && OPCUA_VERSION < 10400 UA_AccessControl_default(config, false, nullptr, &config->securityPolicies[config->securityPoliciesSize - 1].policyUri, usr_passwd.size(), usr_passwd.data()); diff --git a/modules/opcua/src/subscriber.cpp b/modules/opcua/src/subscriber.cpp index 78ff6ecb..123601c1 100644 --- a/modules/opcua/src/subscriber.cpp +++ b/modules/opcua/src/subscriber.cpp @@ -14,7 +14,9 @@ #ifdef UA_ENABLE_PUBSUB #include +#if OPCUA_VERSION < 10400 #include +#endif #include #ifdef UA_ENABLE_PUBSUB_MQTT @@ -33,7 +35,9 @@ Subscriber::Subscriber(const std::string &sub_name, const const std::vector &users) : Server(port, sub_name, users), _name(sub_name) { //////////////////// 添加连接配置 //////////////////// +#if OPCUA_VERSION < 10400 UA_ServerConfig_addPubSubTransportLayer(UA_Server_getConfig(_server), UA_PubSubTransportLayerUDPMP()); +#endif UA_PubSubConnectionConfig connect_config{}; std::string cn_name_str = _name + "Connection"; connect_config.name = UA_STRING(helper::to_char(cn_name_str)); @@ -41,7 +45,11 @@ Subscriber::Subscriber(const std::string &sub_name, const connect_config.enabled = UA_TRUE; UA_NetworkAddressUrlDataType address_url{UA_STRING_NULL, UA_STRING(helper::to_char(address))}; UA_Variant_setScalarCopy(&connect_config.address, &address_url, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]); +#if OPCUA_VERSION >= 10400 + connect_config.publisherId.uint32 = UA_UInt32_random(); +#else connect_config.publisherId.numeric = UA_UInt32_random(); +#endif auto status = UA_Server_addPubSubConnection(_server, &connect_config, &_connection_id); if (status != UA_STATUSCODE_GOOD) { @@ -74,10 +82,10 @@ std::vector Subscriber::subscribe(const std::s UA_DataSetReaderConfig dsr_config{}; std::string dsr_name = pub_name + "DataSetReader"; dsr_config.name = UA_STRING(helper::to_char(dsr_name)); - UA_UInt32 publisher_id = _strhash(pub_name + "Connection") % 0x8000000u; + UA_UInt16 publisher_id = _strhash(pub_name + "Connection") % 0x4000u; UA_Variant_setScalar(&dsr_config.publisherId, &publisher_id, &UA_TYPES[UA_TYPES_UINT16]); - dsr_config.writerGroupId = _strhash(pub_name + "WriterGroup") % 0x8000u; - dsr_config.dataSetWriterId = _strhash(pub_name + "DataSetWriter") % 0x8000u; + dsr_config.writerGroupId = 0x4000u + _strhash(pub_name + "WriterGroup") % 0x4000u; + dsr_config.dataSetWriterId = 0x8000u + _strhash(pub_name + "DataSetWriter") % 0x4000u; // 设置数 DSR 中的元数据配置 std::string dataset_name = _name + "DataSetMetaData";