mrDarker
6 天以前 829fe6c6bc33d53fda9c31fd45a37e1df87befff
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>
@@ -372,6 +376,55 @@
    return true;
}
// 辅助函数:将 ANSI CString 写入文件为 UTF-8 编码
bool CPageGlassList::WriteAnsiStringAsUtf8ToFile(const CString& ansiContent, const CString& filePath)
{
    CFile file;
    if (!file.Open(filePath, CFile::modeCreate | CFile::modeWrite)) {
        return false;
    }
    // 写入 UTF-8 BOM
    const unsigned char bom[] = { 0xEF, 0xBB, 0xBF };
    file.Write(bom, 3);
    // 将 ANSI 转换为 Unicode
    int unicodeLength = MultiByteToWideChar(CP_ACP, 0,
        ansiContent, ansiContent.GetLength(),
        NULL, 0);
    if (unicodeLength <= 0) {
        file.Close();
        return false;
    }
    wchar_t* unicodeBuffer = new wchar_t[unicodeLength + 1];
    MultiByteToWideChar(CP_ACP, 0,
        ansiContent, ansiContent.GetLength(),
        unicodeBuffer, unicodeLength);
    unicodeBuffer[unicodeLength] = 0;
    // 将 Unicode 转换为 UTF-8
    int utf8Length = WideCharToMultiByte(CP_UTF8, 0,
        unicodeBuffer, unicodeLength,
        NULL, 0, NULL, NULL);
    bool success = false;
    if (utf8Length > 0) {
        char* utf8Buffer = new char[utf8Length];
        WideCharToMultiByte(CP_UTF8, 0,
            unicodeBuffer, unicodeLength,
            utf8Buffer, utf8Length, NULL, NULL);
        file.Write(utf8Buffer, utf8Length);
        delete[] utf8Buffer;
        success = true;
    }
    delete[] unicodeBuffer;
    file.Close();
    return success;
}
// CPageGlassList 对话框
@@ -426,6 +479,7 @@
    ON_BN_CLICKED(IDC_BUTTON_PREV_PAGE, &CPageGlassList::OnBnClickedButtonPrevPage)
    ON_BN_CLICKED(IDC_BUTTON_NEXT_PAGE, &CPageGlassList::OnBnClickedButtonNextPage)
    ON_NOTIFY(ELCN_SHOWFULLTEXT, IDC_LIST_ALARM, &CPageGlassList::OnShowFullText)
    ON_BN_CLICKED(IDC_BUTTON_EXPORT_ROW, &CPageGlassList::OnBnClickedButtonExportRow)
END_MESSAGE_MAP()
// ===== 私有小工具 =====
@@ -1034,9 +1088,10 @@
{
    CDialogEx::OnInitDialog();
    // 定时器:1=初始化订阅,2=周期刷新(只增量)
    // 定时器:1=初始化订阅,2=周期刷新(只增量),3=延迟加载首屏数据
    SetTimer(1, 3000, nullptr);
    SetTimer(2, 2000, nullptr);
    SetTimer(3, 10, nullptr);
    // 下拉框控件
    InitStatusCombo();
@@ -1086,7 +1141,6 @@
    m_listCtrl.SetPopupFullTextColumns({ 11, 12 });
    Resize();
    OnBnClickedButtonSearch(); // 触发一次查询与首屏填充
    return TRUE;  // return TRUE unless you set the focus to a control
}
@@ -1146,6 +1200,10 @@
    else if (nIDEvent == 2) {
        UpdateWipData();  // 只做增量,不重建
    }
    else if (nIDEvent == 3) {
        KillTimer(3);
        OnBnClickedButtonSearch(); // 延迟首屏查询,避免卡住 OnInitDialog
    }
    CDialogEx::OnTimer(nIDEvent);
}
@@ -1175,6 +1233,8 @@
void CPageGlassList::OnBnClickedButtonSearch()
{
    CWaitCursor wait; // 显示等待光标,提示正在加载
    // 获取关键字输入框内容
    CString strKeyword;
    GetDlgItemText(IDC_EDIT_KEYWORD, strKeyword);
@@ -1240,6 +1300,264 @@
    }
}
void CPageGlassList::OnBnClickedButtonExportRow()
{
    int nSelected = m_listCtrl.GetSelectionMark();
    if (nSelected == -1) {
        AfxMessageBox(_T("请先选择一行记录!"));
        return;
    }
    // 直接从第一列获取 ID
    CString strId = m_listCtrl.GetItemText(nSelected, 1);
    if (strId.IsEmpty()) {
        AfxMessageBox(_T("WIP记录暂不支持保存"));
        return;
    }
    // 数据库记录
    long long recordId = _ttoi64(strId);
    // 从数据库查询完整记录
    auto& db = GlassLogDb::Instance();
    auto row = db.queryById(recordId);
    if (!row) {
        AfxMessageBox(_T("查询记录失败"));
        return;
    }
    // 使用 Glass ID 构建默认文件名
    CString strDefaultFileName;
    CString strGlassId = row->classId.c_str();
    // 移除文件名中的非法字符
    CString strSanitizedGlassId = strGlassId;
    strSanitizedGlassId.Remove('\\');
    strSanitizedGlassId.Remove('/');
    strSanitizedGlassId.Remove(':');
    strSanitizedGlassId.Remove('*');
    strSanitizedGlassId.Remove('?');
    strSanitizedGlassId.Remove('"');
    strSanitizedGlassId.Remove('<');
    strSanitizedGlassId.Remove('>');
    strSanitizedGlassId.Remove('|');
    strDefaultFileName.Format(_T("Glass_%s.csv"), strSanitizedGlassId);
    // 文件保存对话框,设置默认文件名
    CFileDialog fileDialog(FALSE, _T("csv"), strDefaultFileName,
        OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
        _T("CSV Files (*.csv)|*.csv|JSON Files (*.json)|*.json||"));
    if (fileDialog.DoModal() != IDOK) return;
    CString filePath = fileDialog.GetPathName();
    CString fileExt = fileDialog.GetFileExt();
    if (fileExt.CompareNoCase(_T("json")) == 0) {
        ExportToJson(*row, filePath);
    }
    else {
        ExportToCsv(*row, filePath);
    }
}
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(), (UINT)row.pretty.length());
            file.Close();
            CString strSuccess;
            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");
    }
}
void CPageGlassList::OnBnClickedButtonPrevPage()
{
    if (m_nCurPage > 1) {
@@ -1261,9 +1579,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;
}
@@ -1596,3 +1919,150 @@
    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;
}