mrDarker
2025-10-22 e8a27bb203fe2aff70390a5eca002d7438da9b0f
SourceCode/Bond/Servo/GlassJson.cpp
@@ -178,6 +178,7 @@
                Json::Value n(Json::objectValue);
                n["eq_id"] = p->getEqID();
                n["unit"] = p->getUnit();
                n["slot"] = p->getUnit();
                put_ull_as_str(n, "time_in", static_cast<unsigned long long>(p->getInTime()));
                put_ull_as_str(n, "time_out", static_cast<unsigned long long>(p->getOutTime()));
                n["processed"] = p->isProcessEnd() ? true : false;
@@ -189,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;
}
@@ -199,7 +231,7 @@
    // 基本
    g.setID(JStr(root, "id").c_str());
    g.setType(static_cast<MaterialsType>(JInt(root, "materials", 0)));
    g.getBuddyId() = JStr(root, "buddy_id");
    g.setBuddyId(JStr(root, "buddy_id"));
    g.setScheduledForProcessing(JBool(root, "scheduled") ? TRUE : FALSE);
    g.m_failReason = JStr(root, "fail_reason");
    g.setOriginPort(JInt(root, "origin_port", 0), JInt(root, "origin_slot", 0));
@@ -293,7 +325,8 @@
        for (const auto& n : root["path"]) {
            unsigned eq = JUInt(n, "eq_id", 0);
            unsigned unit = JUInt(n, "unit", 0);
            g.addPath(eq, unit);
            unsigned slot = JUInt(n, "slot", 0);
            g.addPath(eq, unit, slot);
            CPath* tail = nullptr;
            if (auto head = g.getPath()) tail = head->getTailPath();
@@ -304,6 +337,36 @@
            if (get_ll_from_json(n, "time_out", tout)) tail->setOutTime(static_cast<ULONGLONG>(tout));
            tail->setInspResult(static_cast<InspResult>(JInt(n, "insp_result", 0)));
            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);
                        }
                    }
                }
            }
        }
    }
}
@@ -331,4 +394,4 @@
    }
    FromJson(j, g);
    return true;
}
}