| | |
| | | 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 对话框 |
| | | |
| | |
| | | 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() |
| | | |
| | | // ===== 私有小工具 ===== |
| | |
| | | } |
| | | } |
| | | |
| | | 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.json"), strSanitizedGlassId); |
| | | |
| | | // 文件保存对话框,设置默认文件名 |
| | | CFileDialog fileDialog(FALSE, _T("json"), strDefaultFileName, |
| | | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, |
| | | _T("JSON Files (*.json)|*.json|CSV Files (*.csv)|*.csv||")); |
| | | |
| | | 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(), 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)) { |
| | | // 对每个机器生成表格 |
| | | for (const auto& machinePair : tempGlass.getAllSVData()) { |
| | | int machineId = machinePair.first; |
| | | CString machineName = CString(SERVO::CServoUtilsTool::getEqName(machineId).c_str()); |
| | | |
| | | csvContent += _T("\n[") + machineName + _T("]\n"); |
| | | |
| | | // 获取该机器的预定义列顺序 |
| | | auto columnOrder = getMachineColumnOrder(machineId); |
| | | |
| | | if (columnOrder.empty()) { |
| | | csvContent += _T("无预定义列配置\n"); |
| | | continue; |
| | | } |
| | | |
| | | // 构建表头 - 直接使用中文列名 |
| | | CString header = _T("时间戳(ms),本地时间"); |
| | | for (const auto& dataType : columnOrder) { |
| | | header += _T(","); |
| | | header += CString(dataType.c_str()); // 直接使用中文列名 |
| | | } |
| | | header += _T("\n"); |
| | | csvContent += header; |
| | | |
| | | // 检查是否有数据 |
| | | if (machinePair.second.empty()) { |
| | | csvContent += _T("无传感器数据\n"); |
| | | continue; |
| | | } |
| | | |
| | | // 使用第一个数据类型的时间序列作为基准 |
| | | const std::string& firstDataType = columnOrder[0]; |
| | | auto firstDataTypeIt = machinePair.second.find(firstDataType); |
| | | if (firstDataTypeIt == machinePair.second.end() || firstDataTypeIt->second.empty()) { |
| | | csvContent += _T("无基准数据类型数据\n"); |
| | | continue; |
| | | } |
| | | |
| | | const auto& timeSeries = firstDataTypeIt->second; |
| | | |
| | | // 对于每个时间点,输出一行数据 |
| | | 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 = machinePair.second.find(dataType); |
| | | if (dataTypeIt != machinePair.second.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) { |
| | |
| | | |
| | | return CDialogEx::PreTranslateMessage(pMsg); |
| | | } |
| | | |
| | | // 获取机器预定义的列顺序 |
| | | std::vector<std::string> CPageGlassList::getMachineColumnOrder(int machineId) |
| | | { |
| | | auto dataTypes = SERVO::CServoUtilsTool::getEqDataTypes(); |
| | | auto it = dataTypes.find(machineId); |
| | | return it != dataTypes.end() ? it->second : std::vector<std::string>(); |
| | | } |
| | | |
| | | // 时间戳转换为字符串 |
| | | 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(); |
| | | } |