From b5632ef8da8214a8d9595753cc407458354c64ea Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期三, 15 十月 2025 11:24:30 +0800
Subject: [PATCH] 1.修复数据采集服务端问题;
---
SourceCode/Bond/Servo/CPageGlassList.cpp | 434 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 434 insertions(+), 0 deletions(-)
diff --git a/SourceCode/Bond/Servo/CPageGlassList.cpp b/SourceCode/Bond/Servo/CPageGlassList.cpp
index eabdf81..cbe79ed 100644
--- a/SourceCode/Bond/Servo/CPageGlassList.cpp
+++ b/SourceCode/Bond/Servo/CPageGlassList.cpp
@@ -100,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>
#include <atlconv.h> // CStringA
@@ -372,6 +375,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 +478,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()
// ===== 绉佹湁灏忓伐鍏� =====
@@ -1240,6 +1293,274 @@
}
}
+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(), row.pretty.length());
+ file.Close();
+
+ CString strSuccess;
+ strSuccess.Format(_T("璁板綍宸蹭繚瀛樹负JSON鏂囦欢锛歕n%s"), filePath);
+ AfxMessageBox(strSuccess);
+ }
+ else {
+ AfxMessageBox(_T("淇濆瓨鏂囦欢澶辫触"));
+ }
+ }
+ else {
+ AfxMessageBox(_T("璇ヨ褰曟病鏈塉SON鏁版嵁"));
+ }
+}
+
+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("鏃犳硶瑙f瀽宸ヨ壓鍙傛暟\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
+ // 鐢熸垚妯℃嫙鐨凷VData鐢ㄤ簬娴嬭瘯
+ GenerateMockSVData(tempGlass);
+#endif
+ // 瀵规瘡涓満鍣ㄧ敓鎴愯〃鏍�
+ 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("鏃犳硶瑙f瀽浼犳劅鍣ㄦ暟鎹甛n");
+ }
+ }
+ else {
+ csvContent += _T("鏃犱紶鎰熷櫒鏁版嵁\n");
+ }
+}
+
void CPageGlassList::OnBnClickedButtonPrevPage()
{
if (m_nCurPage > 1) {
@@ -1596,3 +1917,116 @@
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();
+}
+
+// 鐢熸垚妯℃嫙鐨凷VData鐢ㄤ簬娴嬭瘯
+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;
+
+ // 鐢熸垚鏃堕棿搴忓垪锛氫粠褰撳墠鏃堕棿寰�鍓嶆帹10鍒嗛挓锛屾瘡1绉掍竴涓暟鎹偣
+ auto now = std::chrono::system_clock::now();
+ auto startTime = now - std::chrono::minutes(10);
+
+ // 涓烘瘡涓暟鎹被鍨嬬敓鎴愭ā鎷熸暟鎹�
+ for (const auto& dataType : dataTypeList) {
+ 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; // 姝e鸡娉㈣秼鍔�
+ double randomNoise = (rand() % 100 - 50) / 100.0 * variation * 0.3; // 闅忔満鍣0
+
+ return baseValue + timeTrend + randomNoise;
+}
\ No newline at end of file
--
Gitblit v1.9.3