diff --git a/commonLibs/livoxProto1/device.cpp b/commonLibs/livoxProto1/device.cpp index e2788b4..21dea57 100644 --- a/commonLibs/livoxProto1/device.cpp +++ b/commonLibs/livoxProto1/device.cpp @@ -1896,12 +1896,25 @@ void Device::registerUdpCommandHandler( */ if (!deviceIP.empty()) { - // Add command-specific handler to the list for this device IP + auto& handlers = devicesUnderConstruction[deviceIP]; + auto it = std::find_if(handlers.begin(), handlers.end(), + [cmd_set, cmd_id](const CommandHandler& existing) { + return existing.cmd_set == cmd_set && existing.cmd_id == cmd_id; + }); + + // Create the new command handler CommandHandler cmdHandler; cmdHandler.cmd_set = cmd_set; cmdHandler.cmd_id = cmd_id; cmdHandler.handler = std::move(handler); - devicesUnderConstruction[deviceIP].push_back(std::move(cmdHandler)); + + if (it != handlers.end()) { + // Replace existing handler + *it = std::move(cmdHandler); + } else { + // Add new handler + handlers.push_back(std::move(cmdHandler)); + } } }