2025-05-21 12:17:50 +08:00
|
|
|
#include "XNDDSInterface.h"
|
|
|
|
#include "XNObject_p.h"
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
2025-05-22 08:53:29 +08:00
|
|
|
XNByteArray XNDDSInterface::getUDPPackage()
|
2025-05-21 12:17:50 +08:00
|
|
|
{
|
2025-05-22 08:53:29 +08:00
|
|
|
XNByteArray result;
|
2025-05-21 12:17:50 +08:00
|
|
|
|
|
|
|
size_t currentPos = 0;
|
|
|
|
|
|
|
|
// 复制头部
|
|
|
|
if (headerSize >= 5) {
|
2025-05-22 08:53:29 +08:00
|
|
|
result.append(header, 5);
|
2025-05-21 12:17:50 +08:00
|
|
|
currentPos = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 复制数据
|
|
|
|
for (auto func : getByteArrayFunction) {
|
|
|
|
if (currentPos + func.size <= MAX_UDP_PACKET_SIZE) {
|
2025-05-22 08:53:29 +08:00
|
|
|
result.append(func.func());
|
2025-05-21 12:17:50 +08:00
|
|
|
currentPos += func.size;
|
|
|
|
} else {
|
|
|
|
break; // 超出最大包大小
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 更新包大小
|
|
|
|
if (currentPos >= 5) {
|
2025-05-22 08:53:29 +08:00
|
|
|
result[4] = static_cast<uint8_t>(currentPos);
|
2025-05-21 12:17:50 +08:00
|
|
|
}
|
2025-05-22 08:53:29 +08:00
|
|
|
|
|
|
|
return result;
|
2025-05-21 12:17:50 +08:00
|
|
|
}
|
|
|
|
|
2025-05-22 08:53:29 +08:00
|
|
|
std::unordered_map<std::string, std::string>
|
|
|
|
XNDDSInterface::getStringData(std::vector<std::string> varNames)
|
2025-05-21 12:17:50 +08:00
|
|
|
{
|
2025-05-22 08:53:29 +08:00
|
|
|
std::unordered_map<std::string, std::string> result;
|
2025-05-21 12:17:50 +08:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2025-05-22 08:53:29 +08:00
|
|
|
for (const auto &varName : varNames) {
|
|
|
|
auto it = getDataFunction.find(varName);
|
|
|
|
if (it == getDataFunction.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result[varName] = it->second();
|
2025-05-21 12:17:50 +08:00
|
|
|
}
|
|
|
|
|
2025-05-22 08:53:29 +08:00
|
|
|
return result;
|
|
|
|
}
|
2025-05-21 12:17:50 +08:00
|
|
|
|
2025-05-22 08:53:29 +08:00
|
|
|
void XNDDSInterface::setDataByString(std::unordered_map<std::string, std::string> data)
|
|
|
|
{
|
|
|
|
clearOutData();
|
|
|
|
for (const auto &[varName, value] : data) {
|
|
|
|
auto it = setDataFunction.find(varName);
|
|
|
|
if (it == setDataFunction.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
it->second(value);
|
2025-05-21 12:17:50 +08:00
|
|
|
}
|
2025-05-22 08:53:29 +08:00
|
|
|
sendOutData();
|
|
|
|
}
|