Skip to content
Open
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: 2 additions & 0 deletions modules/opcua_client_module/src/opcua_client_module_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <opendaq/custom_log.h>
#include <coreobjects/property_object_factory.h>
#include <opendaq/device_type_factory.h>
#include <opendaq/device_private_ptr.h>
#include <opendaq/mirrored_signal_config_ptr.h>
#include <opendaq/search_filter_factory.h>
#include <regex>
Expand Down Expand Up @@ -115,6 +116,7 @@ DevicePtr OpcUaClientModule::onCreateDevice(const StringPtr& connectionString,
.addAddressInfo(addressInfo)
.freeze();

device.asPtr<IDevicePrivate>(true).setAsRoot();
return device;
}

Expand Down
2 changes: 2 additions & 0 deletions modules/opcua_client_module/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ set(MODULE_NAME opcua_client_module)
set(TEST_APP test_${MODULE_NAME})

set(TEST_SOURCES test_opcua_client_module.cpp
test_opcua_device_module.cpp
test_app.cpp
)

Expand All @@ -10,6 +11,7 @@ add_executable(${TEST_APP} ${TEST_SOURCES}

target_link_libraries(${TEST_APP} PRIVATE ${OPENDAQ_SDK_TARGET_NAMESPACE}::opendaq_test_utils gtest
${OPENDAQ_SDK_TARGET_NAMESPACE}::${MODULE_NAME}
${OPENDAQ_SDK_TARGET_NAMESPACE}::opendaq_mocks
)

add_test(NAME ${TEST_APP}
Expand Down
180 changes: 180 additions & 0 deletions modules/opcua_client_module/tests/test_opcua_device_module.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
#include <opcua_client_module/module_dll.h>
#include <opendaq/opendaq.h>
#include <opendaq/module_manager_ptr.h>
#include <opendaq/mock/mock_device_module.h>
#include <opendaq/mock/mock_fb_module.h>
#include <testutils/testutils.h>
#include <set>

using OpcuaDeviceModulesTest = testing::Test;

using namespace daq;

static ModulePtr CreateClientOpcUaModule(const ContextPtr& context)
{
ModulePtr module;
createOpcUaClientModule(&module, context);
return module;
}

static InstancePtr CreateServerInstance()
{
auto instance = InstanceBuilder().setDefaultRootDeviceLocalId("local").build();
ContextPtr context = instance.getContext();
ModuleManagerPtr manager = instance.getModuleManager();

manager.addModule(ModulePtr(MockDeviceModule_Create(context)));
manager.addModule(ModulePtr(MockFunctionBlockModule_Create(context)));

instance.addDevice("daqmock://phys_device");
instance.addFunctionBlock("mock_fb_uid");
instance.addServer("OpenDAQOPCUA", nullptr);
return instance;
}

static InstancePtr CreateClientInstance()
{
auto instance = InstanceBuilder().setModulePath("[[none]]").build();
ContextPtr context = instance.getContext();
ModuleManagerPtr manager = instance.getModuleManager();

manager.addModule(CreateClientOpcUaModule(context));

auto config = instance.createDefaultAddDeviceConfig();
PropertyObjectPtr general = config.getPropertyValue("General");
general.setPropertyValue("StreamingConnectionHeuristic", 2);

instance.addDevice("daq.opcua://127.0.0.1", config);
return instance;
}

const std::set<std::string>& getDefaultComponentsIds()
{
static const std::set<std::string> ids = []
{
std::set<std::string> result;
result.insert("Sig");
result.insert("FB");
result.insert("IP");
result.insert("Dev");
result.insert("IO");
result.insert("Synchronization");
result.insert("Srv");
return result;
}();
return ids;
}

TEST_F(OpcuaDeviceModulesTest, ComponentActiveChangedRecursive)
{
auto server = CreateServerInstance();
auto client = CreateClientInstance();

// Get the client's mirror of server device (this is a root device)
auto clientDevice = client.getDevices()[0];

// Get all components in clientDevice subtree
auto clientDeviceComponents = clientDevice.getItems(search::Recursive(search::Any()));

// checking that the local active change is not propogating to the remote
{
// Set client (Instance) active to false
client.setActive(false);

// client itself should be inactive
ASSERT_FALSE(client.getActive()) << "client should be inactive";

// clientDevice should still be active (it's a root device, doesn't receive parentActive)
ASSERT_TRUE(clientDevice.getActive()) << "clientDevice should remain active as it's a root device";

// All clientDevice subtree components should still be active
for (const auto& comp : clientDeviceComponents)
{
ASSERT_TRUE(comp.getLocalActive()) << "Component should be local active: " << comp.getGlobalId();
ASSERT_TRUE(comp.getParentActive()) << "Component parent should be inactive: " << comp.getGlobalId();
ASSERT_TRUE(comp.getActive()) << "Component should be inactive: " << comp.getGlobalId();
}

// undo changaings
client.setActive(true);
}

// checking that the local active change is not propogating to the remote
{
// Set client (Instance) active to false
clientDevice.setActive(false);

// client itself should be inactive
ASSERT_FALSE(clientDevice.getActive()) << "client clientDevice be inactive";

// clientDevice should still be active (it's a root device, doesn't receive parentActive)
ASSERT_TRUE(client.getActive()) << "client should remain active as its not child of client";

// All clientDevice subtree components should still be active
for (const auto& comp : clientDeviceComponents)
{
if (comp == clientDevice)
{
ASSERT_FALSE(comp.getLocalActive()) << "Component should be local inactive: " << comp.getGlobalId();
ASSERT_TRUE(comp.getParentActive()) << "Component parent should be active: " << comp.getGlobalId();
}
else
{
if (getDefaultComponentsIds().count(comp.getLocalId()))
continue;
ASSERT_TRUE(comp.getLocalActive()) << "Component should be local active: " << comp.getGlobalId();
ASSERT_FALSE(comp.getParentActive()) << "Component parent should be inactive: " << comp.getGlobalId();
}
ASSERT_FALSE(comp.getActive()) << "Component should be inactive: " << comp.getGlobalId();

}
}
}

TEST_F(OpcuaDeviceModulesTest, ComponentActiveChangedRecursiveGateway)
{
// Create leaf server
auto leaf = InstanceBuilder().setDefaultRootDeviceLocalId("leaf").build();
leaf.addServer("OpenDAQOPCUA", nullptr);

// Create gateway that connects to leaf
auto gateway = Instance();
auto gatewayServerConfig = gateway.getAvailableServerTypes().get("OpenDAQOPCUA").createDefaultConfig();
gatewayServerConfig.setPropertyValue("Port", 4841);
gateway.addDevice("daq.opcua://127.0.0.1");
gateway.addServer("OpenDAQOPCUA", gatewayServerConfig);

// Create client that connects to gateway
auto client = Instance();
auto clientGatewayDevice = client.addDevice("daq.opcua://127.0.0.1:4841");

// Get the leaf device through gateway
auto clientLeafDevice = clientGatewayDevice.getDevices()[0];

auto clientDeviceComponents = clientGatewayDevice.getItems(search::Recursive(search::InterfaceId(IDevice::Id)));

// Set clientGatewayDevice active to false (from client side)
clientGatewayDevice.setActive(false);

// clientGatewayDevice itself should be inactive
ASSERT_FALSE(clientGatewayDevice.getActive()) << "clientGatewayDevice should be inactive";

// clientLeafDevice should still be active (it's a root device)
ASSERT_TRUE(clientLeafDevice.getActive()) << "clientLeafDevice should remain active as it's a root device";

for (const auto& comp : clientDeviceComponents)
{
if (comp == clientGatewayDevice)
{
ASSERT_FALSE(comp.getLocalActive()) << "Component should be local inactive: " << comp.getGlobalId();
ASSERT_TRUE(comp.getParentActive()) << "Component parent should be active: " << comp.getGlobalId();
ASSERT_FALSE(comp.getActive()) << "Component should be inactive: " << comp.getGlobalId();
}
else if (const auto dev = comp.asPtrOrNull<IDevice>(true); dev.assigned())
{
ASSERT_TRUE(comp.getLocalActive()) << "Component should be local active: " << comp.getGlobalId();
ASSERT_TRUE(comp.getParentActive()) << "Component parent should be active: " << comp.getGlobalId();
ASSERT_TRUE(comp.getActive()) << "Component should be active: " << comp.getGlobalId();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class TmsClientComponentBaseImpl : public TmsClientPropertyObjectBaseImpl<Impl>

// Component overrides
ErrCode INTERFACE_FUNC getActive(Bool* active) override;
ErrCode INTERFACE_FUNC getLocalActive(Bool* active) override;
ErrCode INTERFACE_FUNC getParentActive(Bool* active) override;
ErrCode INTERFACE_FUNC setActive(Bool active) override;
ErrCode INTERFACE_FUNC getName(IString** name) override;
ErrCode INTERFACE_FUNC setName(IString* name) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class TmsClientDeviceImpl : public TmsClientComponentBaseImpl<MirroredDeviceBase
ErrCode INTERFACE_FUNC setOperationMode(OperationModeType modeType) override;
ErrCode INTERFACE_FUNC setOperationModeRecursive(OperationModeType modeType) override;
ErrCode INTERFACE_FUNC getOperationMode(OperationModeType* modeType) override;
ErrCode INTERFACE_FUNC getParentActive(Bool* active) override;

protected:
void findAndCreateSubdevices();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ using namespace daq::opcua;
template <class Impl>
ErrCode TmsClientComponentBaseImpl<Impl>::getActive(Bool* active)
{
OPENDAQ_PARAM_NOT_NULL(active);

try
{
*active = this->template readValue<IBoolean>("Active");
Expand All @@ -28,6 +30,53 @@ ErrCode TmsClientComponentBaseImpl<Impl>::getActive(Bool* active)
return OPENDAQ_SUCCESS;
}

template <class Impl>
ErrCode TmsClientComponentBaseImpl<Impl>::getLocalActive(Bool* active)
{
OPENDAQ_PARAM_NOT_NULL(active);

if (!this->hasReference("LocalActive"))
return this->getActive(active);

try
{
*active = this->template readValue<IBoolean>("LocalActive");
}
catch(...)
{
*active = true;
auto loggerComponent = getLoggerComponent();
LOG_D("Failed to get local active of component \"{}\". The default value was returned \"true\"", this->globalId);
}

return OPENDAQ_SUCCESS;
}

template <class Impl>
ErrCode TmsClientComponentBaseImpl<Impl>::getParentActive(Bool* active)
{
OPENDAQ_PARAM_NOT_NULL(active);

if (!this->hasReference("ParentActive"))
{
*active = True;
return OPENDAQ_SUCCESS;
}

try
{
*active = this->template readValue<IBoolean>("ParentActive");
}
catch(...)
{
*active = true;
auto loggerComponent = getLoggerComponent();
LOG_D("Failed to get parent active of component \"{}\". The default value was returned \"true\"", this->globalId);
}

return OPENDAQ_SUCCESS;
}

template <class Impl>
ErrCode TmsClientComponentBaseImpl<Impl>::setActive(Bool active)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,19 @@ PropertyObjectPtr TmsClientDeviceImpl::onCreateDefaultAddDeviceConfig()
return PropertyObject();
}

ErrCode TmsClientDeviceImpl::getParentActive(Bool* active)
{
OPENDAQ_PARAM_NOT_NULL(active);

if (this->isRootDevice)
{
*active = True;
return OPENDAQ_SUCCESS;
}

return Super::getParentActive(active);
}

void TmsClientDeviceImpl::findAndCreateFunctionBlocks()
{
std::map<uint32_t, FunctionBlockPtr> orderedFunctionBlocks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ void TmsServerComponent<Ptr>::bindCallbacks()
});
}

this->addReadCallback("LocalActive", [this] { return VariantConverter<IBoolean>::ToVariant( this->object.getLocalActive()); });

this->addReadCallback("ParentActive", [this] { return VariantConverter<IBoolean>::ToVariant( this->object.getParentActive()); });

this->addReadCallback("Visible", [this] { return VariantConverter<IBoolean>::ToVariant( this->object.getVisible()); });

if (!this->object.template supportsInterface<IFreezable>() || !this->object.isFrozen())
Expand Down Expand Up @@ -230,17 +234,47 @@ void TmsServerComponent<Ptr>::registerToTmsServerContext()
template <typename Ptr>
void TmsServerComponent<Ptr>::addChildNodes()
{
OpcUaNodeId newNodeId(0);
AddVariableNodeParams params(newNodeId, this->nodeId);
params.setBrowseName("Visible");
params.setDataType(OpcUaNodeId(UA_TYPES[UA_TYPES_BOOLEAN].typeId));
params.typeDefinition = OpcUaNodeId(UA_NODEID_NUMERIC(0, UA_NS0ID_PROPERTYTYPE));

OpcUaObject<UA_VariableAttributes> attr = UA_VariableAttributes_default;
attr->accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
params.attr = attr;

this->server->addVariableNode(params);
{
OpcUaNodeId newNodeId(0);
AddVariableNodeParams params(newNodeId, this->nodeId);
params.setBrowseName("Visible");
params.setDataType(OpcUaNodeId(UA_TYPES[UA_TYPES_BOOLEAN].typeId));
params.typeDefinition = OpcUaNodeId(UA_NODEID_NUMERIC(0, UA_NS0ID_PROPERTYTYPE));

OpcUaObject<UA_VariableAttributes> attr = UA_VariableAttributes_default;
attr->accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
params.attr = attr;

this->server->addVariableNode(params);
}

{
OpcUaNodeId newNodeId(0);
AddVariableNodeParams params(newNodeId, this->nodeId);
params.setBrowseName("LocalActive");
params.setDataType(OpcUaNodeId(UA_TYPES[UA_TYPES_BOOLEAN].typeId));
params.typeDefinition = OpcUaNodeId(UA_NODEID_NUMERIC(0, UA_NS0ID_PROPERTYTYPE));

OpcUaObject<UA_VariableAttributes> attr = UA_VariableAttributes_default;
attr->accessLevel = UA_ACCESSLEVELMASK_READ;
params.attr = attr;

this->server->addVariableNode(params);
}

{
OpcUaNodeId newNodeId(0);
AddVariableNodeParams params(newNodeId, this->nodeId);
params.setBrowseName("ParentActive");
params.setDataType(OpcUaNodeId(UA_TYPES[UA_TYPES_BOOLEAN].typeId));
params.typeDefinition = OpcUaNodeId(UA_NODEID_NUMERIC(0, UA_NS0ID_PROPERTYTYPE));

OpcUaObject<UA_VariableAttributes> attr = UA_VariableAttributes_default;
attr->accessLevel = UA_ACCESSLEVELMASK_READ;
params.attr = attr;

this->server->addVariableNode(params);
}

tmsPropertyObject->registerToExistingOpcUaNode(this->nodeId);
if (tmsComponentConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ target_link_libraries(${TEST_APP} PRIVATE ${OPENDAQ_SDK_TARGET_NAMESPACE}::opcua
${OPENDAQ_SDK_TARGET_NAMESPACE}::opcuatms_server
${OPENDAQ_SDK_TARGET_NAMESPACE}::opcuatms_client
${OPENDAQ_SDK_TARGET_NAMESPACE}::opendaq_mocks
${BCRYPT_LIB}
${BCRYPT_LIB}
)

if(SUPPORTS_ASAN)
Expand Down
Loading