From 594cf28244b9420e5a8543883f3df183368dfbdd Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期二, 13 五月 2025 15:06:12 +0800
Subject: [PATCH] 1. 修复AlarmList.csv文件异常时导致闪退的问题 2. 修复Master.dat异常时进程无法退出的问题

---
 SourceCode/Bond/Servo/AlarmManager.cpp |   39 ++++++++++++-------
 SourceCode/Bond/Servo/CMaster.cpp      |   24 ++++++++----
 2 files changed, 40 insertions(+), 23 deletions(-)

diff --git a/SourceCode/Bond/Servo/AlarmManager.cpp b/SourceCode/Bond/Servo/AlarmManager.cpp
index 0a4acf9..0ed76e5 100644
--- a/SourceCode/Bond/Servo/AlarmManager.cpp
+++ b/SourceCode/Bond/Servo/AlarmManager.cpp
@@ -781,23 +781,32 @@
         std::string cell;
         AlarmInfo alarm;
 
-        std::getline(ss, cell, ',');
-        std::getline(ss, alarm.strUnitID, ',');
-        std::getline(ss, alarm.strUnitNo, ',');
-        std::getline(ss, cell, ',');
-        alarm.nAlarmLevel = std::stoi(cell);
-        std::getline(ss, cell, ',');
-        alarm.nAlarmCode = std::stoi(cell);
-        std::getline(ss, cell, ',');
-        alarm.nAlarmID = std::stoi(cell);
-        std::getline(ss, alarm.strAlarmText, ',');
-        std::getline(ss, alarm.strDescription, ',');
+        try {
+            // 逐字段解析并验证
+            if (!std::getline(ss, cell, ',')) throw std::runtime_error("Missing field: No");
+            if (!std::getline(ss, alarm.strUnitID, ',')) throw std::runtime_error("Missing field: UnitID");
+            if (!std::getline(ss, alarm.strUnitNo, ',')) throw std::runtime_error("Missing field: UnitNo");
+            if (!std::getline(ss, cell, ',')) throw std::runtime_error("Missing field: AlarmLevel");
+            alarm.nAlarmLevel = std::stoi(cell);
+            if (!std::getline(ss, cell, ',')) throw std::runtime_error("Missing field: AlarmCode");
+            alarm.nAlarmCode = std::stoi(cell);
+            if (!std::getline(ss, cell, ',')) throw std::runtime_error("Missing field: AlarmID");
+            alarm.nAlarmID = std::stoi(cell);
+            if (!std::getline(ss, alarm.strAlarmText, ',')) throw std::runtime_error("Missing field: AlarmText");
+            if (!std::getline(ss, alarm.strDescription, ',')) throw std::runtime_error("Missing field: Description");
 
-        if (m_mapAlarm.find(alarm.nAlarmID) == m_mapAlarm.end()) {
-            m_mapAlarm[alarm.nAlarmID] = alarm;
+            // 检查是否重复
+            if (m_mapAlarm.find(alarm.nAlarmID) == m_mapAlarm.end()) {
+                m_mapAlarm[alarm.nAlarmID] = alarm;
+            }
+            else {
+                std::cerr << "Duplicate AlarmID: " << alarm.nAlarmID << std::endl;
+            }
         }
-        else {
-            std::cerr << "Duplicate AlarmID: " << alarm.nAlarmID << std::endl;
+        catch (const std::exception& e) {
+            // 捕获并记录解析错误
+            std::cerr << "Error parsing line: " << line << " - " << e.what() << std::endl;
+            continue;
         }
     }
 
diff --git a/SourceCode/Bond/Servo/CMaster.cpp b/SourceCode/Bond/Servo/CMaster.cpp
index 6394380..7331415 100644
--- a/SourceCode/Bond/Servo/CMaster.cpp
+++ b/SourceCode/Bond/Servo/CMaster.cpp
@@ -602,15 +602,23 @@
 
 	int CMaster::readCache()
 	{
-		CFile file;
-		if (!file.Open(m_strFilepath.c_str(), CFile::modeRead)) {
-			return -1;
-		}
+		try {
+			CFile file;
+			if (!file.Open(m_strFilepath.c_str(), CFile::modeRead)) {
+				return -1;
+			}
 
-		CArchive ar(&file, CArchive::load);
-		serialize(ar);
-		ar.Close();
-		file.Close();
+			CArchive ar(&file, CArchive::load);
+			serialize(ar);
+			ar.Close();
+			file.Close();
+		}
+		catch (CFileException* e) {
+			TCHAR szErr[512];
+			e->GetErrorMessage(szErr, 512);
+			AfxMessageBox(szErr);
+			e->Delete();
+		}
 
 		return 0;
 	}

--
Gitblit v1.9.3