From f0928d2abc4f3b5875d27b1beeb393cf5edf8c4a Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期一, 24 三月 2025 15:30:38 +0800
Subject: [PATCH] 1.Master数据的缓存,主动关闭程序,或程序闪退,可重启还原。

---
 SourceCode/Bond/Servo/CGlass.cpp     |   16 ++++++++
 SourceCode/Bond/Servo/CEquipment.cpp |   26 ++++++++++++
 SourceCode/Bond/Servo/CGlass.h       |    1 
 SourceCode/Bond/Servo/CMaster.cpp    |   52 ++++++++++++++++++++++++++
 SourceCode/Bond/Servo/CMaster.h      |    6 ++
 SourceCode/Bond/Servo/Model.cpp      |    8 +++
 6 files changed, 106 insertions(+), 3 deletions(-)

diff --git a/SourceCode/Bond/Servo/CEquipment.cpp b/SourceCode/Bond/Servo/CEquipment.cpp
index 4baffcc..393191b 100644
--- a/SourceCode/Bond/Servo/CEquipment.cpp
+++ b/SourceCode/Bond/Servo/CEquipment.cpp
@@ -22,6 +22,11 @@
 
 	CEquipment::~CEquipment()
 	{
+		for (auto item : m_glassList) {
+			item->release();
+		}
+		m_glassList.clear();
+
 		for (auto item : m_mapStep) {
 			delete item.second;
 		}
@@ -253,7 +258,26 @@
 
 	void CEquipment::serialize(CArchive& ar)
 	{
-
+		if (ar.IsStoring()) {
+			Lock();
+			int count = (int)m_glassList.size();
+			ar << count;
+			for (auto item : m_glassList) {
+				item->serialize(ar);
+			}
+			Unlock();
+		}
+		else {
+			Lock();
+			int count;
+			ar >> count;
+			for (int i = 0; i < count; i++) {
+				CGlass* pGlass = new CGlass();
+				pGlass->serialize(ar);
+				addGlassToList(pGlass);
+			}
+			Unlock();
+		}
 	}
 
 	void CEquipment::onReceiveLBData(const char* pszData, size_t size)
diff --git a/SourceCode/Bond/Servo/CGlass.cpp b/SourceCode/Bond/Servo/CGlass.cpp
index b7d05fe..e9847f9 100644
--- a/SourceCode/Bond/Servo/CGlass.cpp
+++ b/SourceCode/Bond/Servo/CGlass.cpp
@@ -38,4 +38,20 @@
 	{
 		return m_strID;
 	}
+
+	void CGlass::serialize(CArchive& ar)
+	{
+		if (ar.IsStoring())
+		{
+			Lock();
+			WriteString(ar, m_strID);
+			Unlock();
+		}
+		else
+		{
+			Lock();
+			ReadString(ar, m_strID);
+			Unlock();
+		}
+	}
 }
diff --git a/SourceCode/Bond/Servo/CGlass.h b/SourceCode/Bond/Servo/CGlass.h
index fc50ed5..fe5a704 100644
--- a/SourceCode/Bond/Servo/CGlass.h
+++ b/SourceCode/Bond/Servo/CGlass.h
@@ -15,6 +15,7 @@
 		virtual std::string toString();
 		void setID(const char* pszID);
 		std::string& getID();
+		void serialize(CArchive& ar);
 
 	private:
 		std::string m_strID;
diff --git a/SourceCode/Bond/Servo/CMaster.cpp b/SourceCode/Bond/Servo/CMaster.cpp
index 8c4f87d..0975cde 100644
--- a/SourceCode/Bond/Servo/CMaster.cpp
+++ b/SourceCode/Bond/Servo/CMaster.cpp
@@ -85,6 +85,10 @@
 		addBakeCooling(listener);
 		connectEquipments();
 
+		
+		// 读缓存数据
+		readCache();
+
 
 		// 定时器
 		g_pMaster = this;
@@ -101,6 +105,8 @@
 		for (auto item : m_listEquipment) {
 			item->term();
 		}
+		saveCache();
+
 
 		return 0;
 	}
@@ -569,6 +575,10 @@
 				}
 			}
 		}
+
+
+		// 自动保存缓存
+		saveCache();
 	}
 
 	void CMaster::connectEquipments()
@@ -643,4 +653,46 @@
 			LOGE("连接BakeCooling-LoadPort4失败");
 		}
 	}
+
+	int CMaster::saveCache()
+	{
+		CFile file;
+		if (!file.Open(m_strFilepath.c_str(), CFile::modeCreate | CFile::modeWrite)) {
+			return -1;
+		}
+
+		CArchive ar(&file, CArchive::store);
+		serialize(ar);
+		ar.Close();
+		file.Close();
+
+		return 0;
+	}
+
+	void CMaster::setCacheFilepath(const char* pszFilepath)
+	{
+		m_strFilepath = pszFilepath;
+	}
+
+	int CMaster::readCache()
+	{
+		CFile file;
+		if (!file.Open(m_strFilepath.c_str(), CFile::modeRead)) {
+			return -1;
+		}
+
+		CArchive ar(&file, CArchive::load);
+		serialize(ar);
+		ar.Close();
+		file.Close();
+
+		return 0;
+	}
+
+	void CMaster::serialize(CArchive& ar)
+	{
+		for (auto item : m_listEquipment) {
+			item->serialize(ar);
+		}
+	}
 }
diff --git a/SourceCode/Bond/Servo/CMaster.h b/SourceCode/Bond/Servo/CMaster.h
index 12b8de3..adf5059 100644
--- a/SourceCode/Bond/Servo/CMaster.h
+++ b/SourceCode/Bond/Servo/CMaster.h
@@ -35,6 +35,7 @@
         void onTimer(UINT nTimerid);
         std::list<CEquipment*>& getEquipmentList();
         CEquipment* getEquipment(int id);
+        void setCacheFilepath(const char* pszFilepath);
 
     private:
         int addToEquipmentList(CEquipment* pEquipment);
@@ -46,12 +47,15 @@
         int addBonder(int index, StepListener& listener);
         int addBakeCooling(StepListener& listener);
         void connectEquipments();
-
+        int saveCache();
+        int readCache();
+        void serialize(CArchive& ar);
 
     private:
         MasterListener m_listener;
         CCCLinkIEControl m_cclink;
         std::list<CEquipment*> m_listEquipment;
+        std::string m_strFilepath;
     };
 }
 
diff --git a/SourceCode/Bond/Servo/Model.cpp b/SourceCode/Bond/Servo/Model.cpp
index 114fe5d..a757adc 100644
--- a/SourceCode/Bond/Servo/Model.cpp
+++ b/SourceCode/Bond/Servo/Model.cpp
@@ -36,7 +36,7 @@
 	m_configuration.getUnitId(strUnitId);
 
 	// 机器型号和软件版本号应从配置中读取,当前先固定值
-	CString strModeType = _T("Bond2860");
+	CString strModeType = _T("Master");
 	CString strSoftRev = _T("1.0.2");
 
 
@@ -166,6 +166,12 @@
 	m_master.setListener(masterListener);
 
 
+	// master 设置缓存文件
+	CString strMasterDataFile;
+	strMasterDataFile.Format(_T("%s\\Master.dat"), (LPTSTR)(LPCTSTR)m_strWorkDir);
+	m_master.setCacheFilepath((LPTSTR)(LPCTSTR)strMasterDataFile);
+
+
 	// 加载警告信息
 	AlarmManager& alarmManager = AlarmManager::getInstance();
 	char szBuffer[MAX_PATH];

--
Gitblit v1.9.3