From 047c7cbd047e11fba8d7872e69a11a13e463aec4 Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期一, 13 十月 2025 17:40:39 +0800
Subject: [PATCH] 1.保存单条记录。
---
SourceCode/Bond/Servo/GlassJson.cpp | 65 +++++++++++++++++++++++++++++++-
1 files changed, 63 insertions(+), 2 deletions(-)
diff --git a/SourceCode/Bond/Servo/GlassJson.cpp b/SourceCode/Bond/Servo/GlassJson.cpp
index 91e7bed..5cbdd6d 100644
--- a/SourceCode/Bond/Servo/GlassJson.cpp
+++ b/SourceCode/Bond/Servo/GlassJson.cpp
@@ -190,7 +190,38 @@
root["path"] = std::move(arr);
}
- root["payload_version"] = 1;
+ // SV数据:三层结构序列化
+ {
+ Json::Value svDataObj(Json::objectValue);
+ const auto& allSVData = g.getAllSVData();
+
+ for (const auto& machinePair : allSVData) {
+ int machineId = machinePair.first;
+ const auto& dataTypes = machinePair.second;
+
+ Json::Value machineObj(Json::objectValue);
+ for (const auto& dataTypePair : dataTypes) {
+ const std::string& dataType = dataTypePair.first;
+ const auto& dataItems = dataTypePair.second;
+
+ Json::Value dataArray(Json::arrayValue);
+ for (const auto& item : dataItems) {
+ Json::Value itemObj(Json::objectValue);
+ // 时间戳转换为毫秒字符串
+ auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
+ item.timestamp.time_since_epoch()).count();
+ itemObj["t"] = std::to_string(ms);
+ itemObj["v"] = item.value;
+ dataArray.append(itemObj);
+ }
+ machineObj[dataType] = dataArray;
+ }
+ svDataObj[std::to_string(machineId)] = machineObj;
+ }
+ root["sv_datas"] = svDataObj;
+ }
+
+ root["payload_version"] = 2; // 版本升级,因为新增了sv_datas字段
return root;
}
@@ -308,6 +339,36 @@
if (JBool(n, "processed", false)) tail->processEnd();
}
}
+
+ // SV数据:反序列化三层结构
+ if (root.isMember("sv_datas") && root["sv_datas"].isObject()) {
+ g.clearAllSVData(); // 清空现有数据
+
+ const auto& svDataObj = root["sv_datas"];
+ auto memberNames = svDataObj.getMemberNames();
+
+ for (const auto& machineIdStr : memberNames) {
+ int machineId = std::stoi(machineIdStr);
+ const auto& machineObj = svDataObj[machineIdStr];
+
+ auto dataTypeNames = machineObj.getMemberNames();
+ for (const auto& dataType : dataTypeNames) {
+ const auto& dataArray = machineObj[dataType];
+ if (dataArray.isArray()) {
+ for (const auto& itemObj : dataArray) {
+ long long timestampMs = 0;
+ double value = 0.0;
+
+ if (get_ll_from_json(itemObj, "t", timestampMs)) {
+ value = JDouble(itemObj, "v", 0.0);
+ // 添加SV数据
+ g.addSVData(machineId, dataType, timestampMs, value);
+ }
+ }
+ }
+ }
+ }
+ }
}
// ==================== 便捷封装 ====================
@@ -333,4 +394,4 @@
}
FromJson(j, g);
return true;
-}
+}
\ No newline at end of file
--
Gitblit v1.9.3