diff --git a/src/mtconnect/configuration/agent_config.cpp b/src/mtconnect/configuration/agent_config.cpp index 097f25c7..eb2aad87 100644 --- a/src/mtconnect/configuration/agent_config.cpp +++ b/src/mtconnect/configuration/agent_config.cpp @@ -964,10 +964,18 @@ namespace mtconnect::configuration { { adapterOptions[configuration::Device] = *device->getUuid(); } + if (!device) { LOG(warning) << "Cannot locate device name '" << deviceName << "', assuming dynamic"; } + else if (auto uuid = GetOption(adapterOptions, configuration::UUID)) + { + // Set the UUID of the device + auto oldUuid = *device->getUuid(); + device->setUuid(*uuid); + m_agent->deviceChanged(device, oldUuid, *device->getComponentName()); + } auto additional = block.second.get_optional(configuration::AdditionalDevices); if (additional) diff --git a/test_package/config_test.cpp b/test_package/config_test.cpp index 542763b0..1a61c044 100644 --- a/test_package/config_test.cpp +++ b/test_package/config_test.cpp @@ -2257,4 +2257,45 @@ AgentDeviceUUID = SOME_UUID const auto &ad = m_config->getAgent()->getAgentDevice(); ASSERT_EQ("SOME_UUID", *(ad->getUuid())); } + + TEST_F(ConfigTest, should_set_device_uuid_when_specified_in_adapter_config) + { + string config(R"DOC( +SchemaVersion=2.3 +Adapters { + Simplest { + UUID = NEW-UUID + Port = 7878 + } +} +)DOC"); + + m_config->setDebug(true); + m_config->loadConfig(config); + + auto dev = m_config->getAgent()->getDeviceByName("Simplest"); + ASSERT_TRUE(dev); + ASSERT_EQ("NEW-UUID", *(dev->getUuid())); + } + + TEST_F(ConfigTest, should_set_default_device_uuid_when_specified_in_adapter_config) + { + string config(R"DOC( +SchemaVersion=2.3 +Adapters { + Adapter { + UUID = NEW-UUID + Port = 7878 + } +} +)DOC"); + + m_config->setDebug(true); + m_config->loadConfig(config); + + auto dev = m_config->getAgent()->getDeviceByName("Simplest"); + ASSERT_TRUE(dev); + ASSERT_EQ("NEW-UUID", *(dev->getUuid())); + } + } // namespace