chenluhua1980
2025-12-10 db9d120efcfe76bb73df089dca8986eca9ee0e6f
SourceCode/Bond/Servo/CPageGlassList.cpp
@@ -13,6 +13,7 @@
#include <unordered_map>
#include <vector>
#include <string>
#include <algorithm>
#include "CProcessDataListDlg.h"
#define PAGE_SIZE                       50
@@ -99,6 +100,9 @@
// ====== 开关:1=启用假数据(只替换 DB 查询);0=用真实 DB ======
#define USE_FAKE_DB_DEMO 0
// ====== 开关:1=启用模拟传感器数据生成;0=使用真实数据 ======
#define USE_MOCK_SENSOR_DATA 0
#if USE_FAKE_DB_DEMO
#include <ctime>
@@ -1334,12 +1338,12 @@
    strSanitizedGlassId.Remove('>');
    strSanitizedGlassId.Remove('|');
    strDefaultFileName.Format(_T("Glass_%s.json"), strSanitizedGlassId);
    strDefaultFileName.Format(_T("Glass_%s.csv"), strSanitizedGlassId);
    // 文件保存对话框,设置默认文件名
    CFileDialog fileDialog(FALSE, _T("json"), strDefaultFileName,
    CFileDialog fileDialog(FALSE, _T("csv"), strDefaultFileName,
        OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
        _T("JSON Files (*.json)|*.json|CSV Files (*.csv)|*.csv||"));
        _T("CSV Files (*.csv)|*.csv|JSON Files (*.json)|*.json||"));
    if (fileDialog.DoModal() != IDOK) return;
@@ -1347,98 +1351,204 @@
    CString fileExt = fileDialog.GetFileExt();
    if (fileExt.CompareNoCase(_T("json")) == 0) {
        // 保存为 JSON
        if (!row->pretty.empty()) {
            CFile file;
            if (file.Open(filePath, CFile::modeCreate | CFile::modeWrite)) {
                file.Write(row->pretty.c_str(), row->pretty.length());
                file.Close();
                CString strSuccess;
                strSuccess.Format(_T("记录已保存为JSON文件:\n%s"), filePath);
                AfxMessageBox(strSuccess);
            }
            else {
                AfxMessageBox(_T("保存文件失败"));
            }
        }
        else {
            AfxMessageBox(_T("该记录没有JSON数据"));
        }
        ExportToJson(*row, filePath);
    }
    else {
        // 保存为 CSV 格式 - 分段式
        CString csvContent;
        ExportToCsv(*row, filePath);
    }
}
        // === 第一部分:基础信息 ===
        csvContent += _T("=== 基础信息 ===\n");
        csvContent += _T("ID,Cassette序列号,Job序列号,Glass ID,物料类型,状态,开始时间,结束时间,绑定Glass ID,AOI结果,路径\n");
void CPageGlassList::ExportToJson(const GlassLogDb::Row& row, const CString& filePath)
{
    // 保存为 JSON
    if (!row.pretty.empty()) {
        CFile file;
        if (file.Open(filePath, CFile::modeCreate | CFile::modeWrite)) {
            file.Write(row.pretty.c_str(), row.pretty.length());
            file.Close();
        CString baseInfoRow;
        baseInfoRow.Format(_T("%lld,%d,%d,%s,%d,%d,%s,%s,%s,%d,%s\n"),
            row->id, row->cassetteSeqNo, row->jobSeqNo,
            CString(row->classId.c_str()), row->materialType, row->state,
            CString(row->tStart.c_str()), CString(row->tEnd.c_str()),
            CString(row->buddyId.c_str()), row->aoiResult,
            CString(row->path.c_str()));
        csvContent += baseInfoRow;
        // === 第二部分:工艺参数 ===
        csvContent += _T("\n=== 工艺参数 ===\n");
        // 如果有 pretty 字段,解析工艺参数
        if (!row->pretty.empty()) {
            SERVO::CGlass tempGlass;
            if (GlassJson::FromString(row->pretty, tempGlass)) {
                auto& params = tempGlass.getParams();
                if (!params.empty()) {
                    // 工艺参数表头 - 调整后的列
                    csvContent += _T("参数名称,参数ID,数值,机器单元\n");
                    // 工艺参数数据 - 调整后的格式
                    for (auto& param : params) {
                        CString paramRow;
                        CString valueStr;
                        // 根据参数类型格式化数值
                        if (param.getValueType() == PVT_INT) {
                            valueStr.Format(_T("%d"), param.getIntValue());
                        }
                        else {
                            valueStr.Format(_T("%.3f"), param.getDoubleValue());
                        }
                        // 调整后的格式:去掉数值类型列
                        paramRow.Format(_T("%s,%s,%s,%s\n"),
                            CString(param.getName().c_str()),
                            CString(param.getId().c_str()),
                            valueStr,
                            CString(param.getUnit().c_str())); // 这里显示机器单元
                        csvContent += paramRow;
                    }
                }
                else {
                    csvContent += _T("无工艺参数数据\n");
                }
            }
            else {
                csvContent += _T("无法解析工艺参数\n");
            }
        }
        else {
            csvContent += _T("无工艺参数数据\n");
        }
        // 使用辅助函数保存为 UTF-8 编码
        if (WriteAnsiStringAsUtf8ToFile(csvContent, filePath)) {
            CString strSuccess;
            strSuccess.Format(_T("记录已保存为CSV文件:\n%s"), filePath);
            strSuccess.Format(_T("记录已保存为JSON文件:\n%s"), filePath);
            AfxMessageBox(strSuccess);
        }
        else {
            AfxMessageBox(_T("保存文件失败"));
        }
    }
    else {
        AfxMessageBox(_T("该记录没有JSON数据"));
    }
}
void CPageGlassList::ExportToCsv(const GlassLogDb::Row& row, const CString& filePath)
{
    CString csvContent;
    // === 第一部分:基础信息 ===
    ExportBasicInfo(csvContent, row);
    // === 第二部分:工艺参数 ===
    ExportProcessParams(csvContent, row);
    // === 第三部分:传感器数据详情 ===
    ExportSensorData(csvContent, row);
    // 使用辅助函数保存为 UTF-8 编码
    if (WriteAnsiStringAsUtf8ToFile(csvContent, filePath)) {
        CString strSuccess;
        strSuccess.Format(_T("记录已保存为CSV文件:\n%s"), filePath);
        AfxMessageBox(strSuccess);
    }
    else {
        AfxMessageBox(_T("保存文件失败"));
    }
}
void CPageGlassList::ExportBasicInfo(CString& csvContent, const GlassLogDb::Row& row)
{
    csvContent += _T("=== 基础信息 ===\n");
    csvContent += _T("ID,Cassette序列号,Job序列号,Glass ID,物料类型,状态,开始时间,结束时间,绑定Glass ID,AOI结果,路径\n");
    CString baseInfoRow;
    baseInfoRow.Format(_T("%lld,%d,%d,%s,%d,%d,%s,%s,%s,%d,%s\n"),
        row.id, row.cassetteSeqNo, row.jobSeqNo,
        CString(row.classId.c_str()), row.materialType, row.state,
        CString(row.tStart.c_str()), CString(row.tEnd.c_str()),
        CString(row.buddyId.c_str()), row.aoiResult,
        CString(row.path.c_str()));
    csvContent += baseInfoRow;
}
void CPageGlassList::ExportProcessParams(CString& csvContent, const GlassLogDb::Row& row)
{
    csvContent += _T("\n=== 工艺参数 ===\n");
    // 如果有 pretty 字段,解析工艺参数
    if (!row.pretty.empty()) {
        SERVO::CGlass tempGlass;
        if (GlassJson::FromString(row.pretty, tempGlass)) {
            auto& params = tempGlass.getParams();
            if (!params.empty()) {
                // 工艺参数表头
                csvContent += _T("参数名称,参数ID,数值,机器单元\n");
                // 工艺参数数据
                for (auto& param : params) {
                    CString paramRow;
                    CString valueStr;
                    // 根据参数类型格式化数值
                    if (param.getValueType() == PVT_INT) {
                        valueStr.Format(_T("%d"), param.getIntValue());
                    }
                    else {
                        valueStr.Format(_T("%.3f"), param.getDoubleValue());
                    }
                    paramRow.Format(_T("%s,%s,%s,%s\n"),
                        CString(param.getName().c_str()),
                        CString(param.getId().c_str()),
                        valueStr,
                        CString(param.getUnit().c_str()));
                    csvContent += paramRow;
                }
            }
            else {
                csvContent += _T("无工艺参数数据\n");
            }
        }
        else {
            csvContent += _T("无法解析工艺参数\n");
        }
    }
    else {
        csvContent += _T("无工艺参数数据\n");
    }
}
void CPageGlassList::ExportSensorData(CString& csvContent, const GlassLogDb::Row& row)
{
    csvContent += _T("\n=== 传感器数据详情 ===\n");
    // 如果有 pretty 字段,解析传感器数据
    if (!row.pretty.empty()) {
        SERVO::CGlass tempGlass;
        if (GlassJson::FromString(row.pretty, tempGlass)) {
#if USE_MOCK_SENSOR_DATA
            // 生成模拟的SVData用于测试
            GenerateMockSVData(tempGlass);
#endif
            // 对每个机器生成表格
            for (const auto& machinePair : tempGlass.getAllSVData()) {
                int machineId = machinePair.first;
                const auto& dataByType = machinePair.second;
                CString machineName = CString(SERVO::CServoUtilsTool::getEqName(machineId).c_str());
                csvContent += _T("\n[") + machineName + _T("]\n");
                if (dataByType.empty()) {
                    csvContent += _T("No sensor data\n");
                    continue;
                }
                auto columnOrder = getMachineColumnOrder(machineId, &dataByType);
                if (columnOrder.empty()) {
                    csvContent += _T("No exportable columns\n");
                    continue;
                }
                CString header = _T("Timestamp(ms),LocalTime");
                for (const auto& dataType : columnOrder) {
                    header += _T(",");
                    header += CString(dataType.c_str());
                }
                header += _T("\n");
                csvContent += header;
                auto baselineIt = std::find_if(columnOrder.begin(), columnOrder.end(),
                    [&](const std::string& type) {
                        auto dataIt = dataByType.find(type);
                        return dataIt != dataByType.end() && !dataIt->second.empty();
                    });
                if (baselineIt == columnOrder.end()) {
                    csvContent += _T("No usable time series\n");
                    continue;
                }
                const auto& timeSeries = dataByType.at(*baselineIt);
                for (size_t i = 0; i < timeSeries.size(); ++i) {
                    auto timestamp = timeSeries[i].timestamp;
                    auto ms = timePointToMs(timestamp);
                    CString row;
                    row.Format(_T("%lld,"), ms);
                    CString localTime = CString(timePointToString(timestamp).c_str());
                    row += localTime;
                    for (const auto& dataType : columnOrder) {
                        row += _T(",");
                        auto dataTypeIt = dataByType.find(dataType);
                        if (dataTypeIt != dataByType.end() && i < dataTypeIt->second.size()) {
                            CString valueStr;
                            valueStr.Format(_T("%.3f"), dataTypeIt->second[i].value);
                            row += valueStr;
                        }
                        else {
                            row += _T("N/A");
                        }
                    }
                    row += _T("\n");
                    csvContent += row;
                }
            }
        }
        else {
            csvContent += _T("无法解析传感器数据\n");
        }
    }
    else {
        csvContent += _T("无传感器数据\n");
    }
}
@@ -1463,9 +1573,14 @@
    auto* p = reinterpret_cast<NMC_ELC_SHOWFULLTEXT*>(pNMHDR);
    // 对话框显示工艺参数
    CProcessDataListDlg dlg;
    dlg.setRawText(p->text);
    dlg.DoModal();
    if (p->iSubItem == 12) {
        CProcessDataListDlg dlg;
        dlg.setRawText(p->text);
        dlg.DoModal();
    }
    else {
        AfxMessageBox(p->text);
    }
    *pResult = 0;
}
@@ -1797,4 +1912,151 @@
    }
    return CDialogEx::PreTranslateMessage(pMsg);
}
// 获取机器预定义的列顺序
std::vector<std::string> CPageGlassList::getMachineColumnOrder(int machineId,
    const std::unordered_map<std::string, std::vector<SERVO::SVDataItem>>* actualData)
{
    std::vector<std::string> columnOrder;
    auto dataTypes = SERVO::CServoUtilsTool::getEqDataTypes();
    auto it = dataTypes.find(machineId);
    if (actualData != nullptr) {
        if (it != dataTypes.end()) {
            for (const auto& name : it->second) {
                if (actualData->find(name) != actualData->end()) {
                    columnOrder.push_back(name);
                }
            }
        }
        for (const auto& kv : *actualData) {
            if (std::find(columnOrder.begin(), columnOrder.end(), kv.first) == columnOrder.end()) {
                columnOrder.push_back(kv.first);
            }
        }
        return columnOrder;
    }
    if (it != dataTypes.end()) {
        columnOrder = it->second;
    }
    return columnOrder;
}
// 时间戳转换为字符串
std::string CPageGlassList::timePointToString(const std::chrono::system_clock::time_point& tp)
{
    auto time_t = std::chrono::system_clock::to_time_t(tp);
    std::tm tm;
    localtime_s(&tm, &time_t);
    char buffer[20];
    std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tm);
    return buffer;
}
// 时间戳转换为毫秒
int64_t CPageGlassList::timePointToMs(const std::chrono::system_clock::time_point& tp)
{
    return std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch()).count();
}
// 生成模拟的SVData用于测试
void CPageGlassList::GenerateMockSVData(SERVO::CGlass& glass)
{
    // 获取设备数据类型配置
    auto& dataTypes = SERVO::CServoUtilsTool::getEqDataTypes();
    // 为每个设备生成模拟数据
    for (const auto& machinePair : dataTypes) {
        int machineId = machinePair.first;
        const auto& dataTypeList = machinePair.second;
        std::vector<std::string> filteredTypes;
        if (machineId == EQ_ID_VACUUMBAKE || machineId == EQ_ID_BAKE_COOLING) {
            const char activePrefix = 'A';
            for (const auto& dataType : dataTypeList) {
                if (!dataType.empty() && dataType[0] == activePrefix) {
                    filteredTypes.push_back(dataType);
                }
            }
        }
        const auto& typeList = filteredTypes.empty() ? dataTypeList : filteredTypes;
        // 生成时间序列:从当前时间往前推10分钟,每1秒一个数据点
        auto now = std::chrono::system_clock::now();
        auto startTime = now - std::chrono::minutes(10);
        // 为每个数据类型生成模拟数据
        for (const auto& dataType : typeList) {
            std::vector<SERVO::SVDataItem> mockData;
            // 生成600个数据点(10分钟 * 60个点/分钟)
            for (int i = 0; i < 600; ++i) {
                auto timestamp = startTime + std::chrono::seconds(i * 1);
                // 根据设备类型和数据类型生成不同的模拟值
                double value = GenerateMockValue(machineId, dataType, i);
                mockData.emplace_back(timestamp, value);
            }
            // 将模拟数据添加到glass对象中
            glass.addSVData(machineId, dataType, mockData);
        }
    }
}
// 根据设备类型和数据类型生成模拟数值
double CPageGlassList::GenerateMockValue(int machineId, const std::string& dataType, int index)
{
    // 基础值范围
    double baseValue = 0.0;
    double variation = 0.0;
    // 根据设备类型设置基础值
    switch (machineId) {
        case EQ_ID_Bonder1:
        case EQ_ID_Bonder2:
            if (dataType.find("压力") != std::string::npos) {
                baseValue = 50.0;  // 压力基础值
                variation = 10.0;  // 压力变化范围
            } else if (dataType.find("温度") != std::string::npos) {
                baseValue = 180.0; // 温度基础值
                variation = 5.0;   // 温度变化范围
            } else if (dataType.find("扩展值") != std::string::npos) {
                baseValue = 100.0; // 扩展值基础值
                variation = 15.0;  // 扩展值变化范围
            }
            break;
        case EQ_ID_VACUUMBAKE:
            if (dataType.find("扩展值") != std::string::npos) {
                baseValue = 80.0;
                variation = 12.0;
            } else if (dataType.find("温度") != std::string::npos) {
                baseValue = 200.0;
                variation = 8.0;
            }
            break;
        case EQ_ID_BAKE_COOLING:
            if (dataType.find("温度") != std::string::npos) {
                baseValue = 25.0;  // 冷却温度
                variation = 3.0;
            }
            break;
        default:
            baseValue = 50.0;
            variation = 5.0;
            break;
    }
    // 添加时间相关的趋势和随机变化
    double timeTrend = sin(index * 0.1) * 2.0;  // 正弦波趋势
    double randomNoise = (rand() % 100 - 50) / 100.0 * variation * 0.3;  // 随机噪声
    return baseValue + timeTrend + randomNoise;
}