From cfa0f3595c53e3567bb12cb194a37617a7b8a662 Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期一, 24 三月 2025 17:39:35 +0800
Subject: [PATCH] 1.缓存文件的备份。

---
 SourceCode/Bond/Servo/CMaster.cpp |   87 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 87 insertions(+), 0 deletions(-)

diff --git a/SourceCode/Bond/Servo/CMaster.cpp b/SourceCode/Bond/Servo/CMaster.cpp
index 8c4f87d..145531f 100644
--- a/SourceCode/Bond/Servo/CMaster.cpp
+++ b/SourceCode/Bond/Servo/CMaster.cpp
@@ -14,6 +14,7 @@
 	CMaster::CMaster()
 	{
 		m_listener = {nullptr, nullptr, nullptr};
+		m_bDataModify = FALSE;
 	}
 
 	CMaster::~CMaster()
@@ -85,6 +86,10 @@
 		addBakeCooling(listener);
 		connectEquipments();
 
+		
+		// 读缓存数据
+		readCache();
+
 
 		// 定时器
 		g_pMaster = this;
@@ -101,6 +106,8 @@
 		for (auto item : m_listEquipment) {
 			item->term();
 		}
+		saveCache();
+
 
 		return 0;
 	}
@@ -120,6 +127,10 @@
 				m_listener.onEqCimStateChanged(this, p, bOn);
 			}
 		};
+		listener.onDataChanged = [&](void* pEquipment, int code) -> void {
+			m_bDataModify = TRUE;
+		};
+
 		pEquipment->setListener(listener);
 		pEquipment->setCcLink(&m_cclink);
 		m_listEquipment.push_back(pEquipment);
@@ -569,6 +580,16 @@
 				}
 			}
 		}
+
+
+		// 自动保存缓存
+		if (i % (4 * 2) == 0) {
+			if (m_bDataModify) {
+				saveCacheAndBackups();
+				m_bDataModify = FALSE;
+			}
+		}
+
 	}
 
 	void CMaster::connectEquipments()
@@ -643,4 +664,70 @@
 			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;
+	}
+
+	int CMaster::saveCacheAndBackups()
+	{
+		saveCache();
+
+
+		// 创建备份目录
+		CString strNewFile;
+		CString strFileDir = m_strFilepath.c_str();
+		int index = strFileDir.ReverseFind('\\');
+		ASSERT(index > 0);
+		strFileDir = strFileDir.Left(index);
+		strFileDir = strFileDir + _T("Backups");
+		::CreateDirectory(strFileDir, nullptr);
+
+		CTime time = CTime::GetCurrentTime();
+		strNewFile.Format(_T("%s//Master_%d_%02d_%02d_%02d_%02d_%02d.dat"),
+			(LPTSTR)(LPCTSTR)strFileDir,
+			time.GetYear(), time.GetMonth(), time.GetDay(),
+			time.GetHour(), time.GetMinute(), time.GetSecond());
+		::CopyFile(m_strFilepath.c_str(), strNewFile, FALSE);
+
+		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);
+		}
+	}
 }

--
Gitblit v1.9.3