| | |
| | | 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; |
| | | } |
| | | |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | // ==================== 便捷封装 ==================== |