From d256f84735b75529a640ca61384c3c55b557fce9 Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期一, 12 五月 2025 16:22:57 +0800
Subject: [PATCH] 1. 修复日志时顺问题 2. 解决远程路径不存在编译报错的问题

---
 SourceCode/Bond/Servo/CMaster.cpp |  507 +++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 393 insertions(+), 114 deletions(-)

diff --git a/SourceCode/Bond/Servo/CMaster.cpp b/SourceCode/Bond/Servo/CMaster.cpp
index d0bd080..40f4705 100644
--- a/SourceCode/Bond/Servo/CMaster.cpp
+++ b/SourceCode/Bond/Servo/CMaster.cpp
@@ -2,8 +2,19 @@
 #include "Common.h"
 #include "CMaster.h"
 
+
 namespace SERVO {
 	CMaster* g_pMaster = NULL;
+
+	unsigned __stdcall ReadBitsThreadFunction(LPVOID lpParam)
+	{
+		if (g_pMaster != NULL) {
+			return g_pMaster->ReadBitsProc();
+		}
+
+		return 0;
+	}
+
 	void CALLBACK MasterTimerProc(HWND hWnd, UINT nMsg, UINT nTimerid, DWORD dwTime)
 	{
 		if (g_pMaster != NULL) {
@@ -13,7 +24,12 @@
 
 	CMaster::CMaster()
 	{
-		m_listener = {nullptr, nullptr, nullptr};
+		m_listener = {nullptr, nullptr, nullptr, nullptr};
+		m_bDataModify = FALSE;
+		m_hEventReadBitsThreadExit[0] = ::CreateEvent(NULL, TRUE, FALSE, NULL);
+		m_hEventReadBitsThreadExit[1] = ::CreateEvent(NULL, TRUE, FALSE, NULL);
+		m_hReadBitsThreadHandle = nullptr;
+		m_nReadBitsThreadAddr = 0;
 	}
 
 	CMaster::~CMaster()
@@ -22,27 +38,29 @@
 			delete item;
 		}
 		m_listEquipment.clear();
+
+		if (m_hEventReadBitsThreadExit[0] != nullptr) {
+			::CloseHandle(m_hEventReadBitsThreadExit[0]);
+			m_hEventReadBitsThreadExit[0] = nullptr;
+		}
+
+		if (m_hEventReadBitsThreadExit[1] != nullptr) {
+			::CloseHandle(m_hEventReadBitsThreadExit[1]);
+			m_hEventReadBitsThreadExit[1] = nullptr;
+		}
 	}
 
 	void CMaster::setListener(MasterListener listener)
 	{
 		m_listener.onEqAlive = listener.onEqAlive;
 		m_listener.onEqCimStateChanged = listener.onEqCimStateChanged;
-		m_listener.onEqStepEvent = listener.onEqStepEvent;
+		m_listener.onEqAlarm = listener.onEqAlarm;
+		m_listener.onEqVcrEventReport = listener.onEqVcrEventReport;
 	}
 
 	int CMaster::init()
 	{
 		LOGI("<Master>正在初始化...");
-
-
-		StepListener listener;
-		listener.onEvent = [&](void* pStep, int code, void* pData) -> void {
-			TRACE("<CEquipment>onEvent<%d, 0x%x>.\n", code, pData);
-			if (m_listener.onEqStepEvent != nullptr) {
-				m_listener.onEqStepEvent((CStep*)pStep, code, pData);
-			}
-		};
 
 
 		// 	cclink
@@ -72,23 +90,38 @@
 
 
 		// 初始化添加各子设备
-		addEFEM(listener);
+		CLoadPort* pPort1, * pPort2, * pPort3, * pPort4;
+		CEFEM* pEfem;
+		pPort1 = addLoadPort(0);
+		pPort2 = addLoadPort(1);
+		pPort3 = addLoadPort(2);
+		pPort4 = addLoadPort(3);
+		pEfem = addEFEM();
+		pEfem->setPort(0, pPort1);
+		pEfem->setPort(1, pPort1);
+		pEfem->setPort(2, pPort1);
+		pEfem->setPort(3, pPort1);
+
+		addFliper();
+		addVacuumBake();
+		addAligner();
+		addBonder(0);
+		addBonder(1);
+		addBakeCooling();
+		connectEquipments();
 
 		
-		{
-			CBonder* pBonder = new CBonder();
-			pBonder->setName("Bonder 1");
-			pBonder->setDescription("Bonder 1.");
-			pBonder->setReadBitBlock(0x4600, 0x4bff);
-			pBonder->setStation(1, 3);
-			addEquipment(pBonder);
-			LOGE("已添加“Bonder 1”.");
-		}
-		
+		// 读缓存数据
+		readCache();
+
 
 		// 定时器
 		g_pMaster = this;
 		SetTimer(NULL, 1, 250, (TIMERPROC)MasterTimerProc);
+
+
+		m_hReadBitsThreadHandle = (HANDLE)_beginthreadex(NULL, 0, SERVO::ReadBitsThreadFunction, this,
+			0, &m_nReadBitsThreadAddr);
 
 
 		LOGI("<Master>初始化完成.");
@@ -97,15 +130,53 @@
 
 	int CMaster::term()
 	{
+		SetEvent(m_hEventReadBitsThreadExit[0]);
+		::WaitForSingleObject(m_hEventReadBitsThreadExit[1], INFINITE);
+
 		LOGI("<Master>正在结束程序.");
 		for (auto item : m_listEquipment) {
 			item->term();
 		}
+		saveCache();
+
+
 
 		return 0;
 	}
 
-	int CMaster::addEquipment(CEquipment* pEquipment)
+	unsigned CMaster::ReadBitsProc()
+	{
+		while (1) {
+			// 待退出信号或时间到
+			int nRet = ::WaitForSingleObject(m_hEventReadBitsThreadExit[0], 1000);
+			if (nRet == WAIT_OBJECT_0) {
+				break;
+			}
+
+			for (auto item : m_listEquipment) {
+				if (item->getID() == EQ_ID_Bonder1 ||
+					item->getID() == EQ_ID_Bonder2) {
+					const StationIdentifier& station = item->getStation();
+					MemoryBlock& block = item->getReadBitBlock();
+
+					int nRet = m_cclink.ReadData2(station, (DeviceType)block.type,
+						block.start, block.size, block.buffer);
+					if (0 == nRet) {
+						item->onReceiveLBData(block.buffer, block.size);
+					}
+				}
+			}
+		}
+
+		SetEvent(m_hEventReadBitsThreadExit[1]);
+
+
+		// _endthreadex(0);
+		TRACE("CMaster::ReadBitsProc 线程退出\n");
+		return 0;
+	}
+
+	int CMaster::addToEquipmentList(CEquipment* pEquipment)
 	{
 		EquipmentListener listener;
 		listener.onAlive = [&](void* pEquipment, BOOL bAlive) -> void {
@@ -120,6 +191,23 @@
 				m_listener.onEqCimStateChanged(this, p, bOn);
 			}
 		};
+		listener.onAlarm = [&](void* pEquipment, int state, int alarmId, int unitId, int level) -> void {
+			CEquipment* p = (CEquipment*)pEquipment;
+			if (m_listener.onEqAlarm != nullptr) {
+				m_listener.onEqAlarm(this, p, state, alarmId, unitId, level);
+			}
+		};
+		listener.onVcrEventReport = [&](void* pEquipment, void* pReport) -> void {
+			CEquipment* p = (CEquipment*)pEquipment;
+			CVcrEventReport* p2 = (CVcrEventReport*)pReport;
+			if (m_listener.onEqVcrEventReport != nullptr) {
+				m_listener.onEqVcrEventReport(this, p, p2);
+			}
+		};
+		listener.onDataChanged = [&](void* pEquipment, int code) -> void {
+			m_bDataModify = TRUE;
+		};
+
 		pEquipment->setListener(listener);
 		pEquipment->setCcLink(&m_cclink);
 		m_listEquipment.push_back(pEquipment);
@@ -141,102 +229,140 @@
 		return nullptr;
 	}
 
-	int CMaster::addEFEM(StepListener& listener)
+	/*
+	 * 添加LoadPort1
+	 * index -- 0~3
+	 */
+	CLoadPort* CMaster::addLoadPort(int index)
+	{
+		ASSERT(index == 0 || index == 1 || index == 2 || index == 3);
+
+
+		char szName[64];
+		sprintf_s(szName, 64, "LoadPort %d", index + 1);
+		CLoadPort* pEquipment = new CLoadPort();
+		pEquipment->setIndex(index);
+		pEquipment->setID(EQ_ID_LOADPORT1 + index);
+		pEquipment->setName(szName);
+		pEquipment->setDescription(szName);
+		addToEquipmentList(pEquipment);
+
+
+		pEquipment->init();
+		LOGE("已添加“%s”.", pEquipment->getName().c_str());
+
+
+		return pEquipment;
+	}
+
+	int CMaster::addFliper()
+	{
+		CFliper* pEquipment = new CFliper();
+		pEquipment->setID(EQ_ID_FLIPER);
+		pEquipment->setBaseAlarmId(BASE_ALARM_EFEM);
+		pEquipment->setName("Fliper");
+		pEquipment->setDescription("Fliper.");
+		pEquipment->setReadBitBlock(0x4000, 0x45ff);
+		pEquipment->setStation(0, 255);
+		addToEquipmentList(pEquipment);
+
+
+		pEquipment->init();
+		LOGE("已添加“Fliper”.");
+		return 0;
+	}
+
+	int CMaster::addVacuumBake()
+	{
+		CVacuumBake* pEquipment = new CVacuumBake();
+		pEquipment->setID(EQ_ID_VACUUMBAKE);
+		pEquipment->setBaseAlarmId(BASE_ALARM_EFEM);
+		pEquipment->setName("VacuumBake");
+		pEquipment->setDescription("VacuumBake.");
+		pEquipment->setReadBitBlock(0x4000, 0x45ff);
+		pEquipment->setStation(0, 255);
+		addToEquipmentList(pEquipment);
+
+
+		pEquipment->init();
+		LOGE("已添加“VacuumBake”.");
+		return 0;
+	}
+
+	int CMaster::addAligner()
+	{
+		CAligner* pEquipment = new CAligner();
+		pEquipment->setID(EQ_ID_ALIGNER);
+		pEquipment->setBaseAlarmId(BASE_ALARM_EFEM);
+		pEquipment->setName("Aligner");
+		pEquipment->setDescription("Aligner.");
+		pEquipment->setReadBitBlock(0x4000, 0x45ff);
+		pEquipment->setStation(0, 255);
+		addToEquipmentList(pEquipment);
+
+
+		pEquipment->init();
+		LOGE("已添加“Aligner”.");
+		return 0;
+	}
+
+	CEFEM* CMaster::addEFEM()
 	{
 		CEFEM* pEquipment = new CEFEM();
 		pEquipment->setID(EQ_ID_EFEM);
 		pEquipment->setBaseAlarmId(BASE_ALARM_EFEM);
 		pEquipment->setName("EFEM(ROBOT)");
 		pEquipment->setDescription("EFEM(ROBOT).");
-		pEquipment->setReadBitBlock(0x4000, 0x45ff);
+		pEquipment->setReadBitBlock(0x3000, 0x3aff);
 		pEquipment->setStation(0, 255);
-		addEquipment(pEquipment);
+		addToEquipmentList(pEquipment);
 
-
-		// 添加 step
-		{
-			CEqModeStep* pStep = new CEqModeStep();
-			pStep->setName(STEP_MODE);
-			pStep->setListener(listener);
-			pStep->setWriteSignalDev(0x30);
-			pStep->setModeDev(0x4a8c);
-			if (pEquipment->addStep(0x360, pStep) != 0) {
-				delete pStep;
-			}
-		}
-		{
-			CEqStatusStep* pStep = new CEqStatusStep();
-			pStep->setName(STEP_STATUS);
-			pStep->setListener(listener);
-			pStep->setWriteSignalDev(0x31);
-			pStep->setStatusDev(0x4a68);
-			if (pEquipment->addStep(0x361, pStep) != 0) {
-				delete pStep;
-			}
-		}
-		{
-			CEqAlarmStep* pStep = new CEqAlarmStep();
-			pStep->setName(STEP_ALARM_BLOCK1);
-			pStep->setListener(listener);
-			pStep->setWriteSignalDev(0x32);
-			pStep->setAlarmDev(0x4c1d);
-			if (pEquipment->addStep(0x362, pStep) != 0) {
-				delete pStep;
-			}
-		}
-		{
-			CEqAlarmStep* pStep = new CEqAlarmStep();
-			pStep->setName(STEP_ALARM_BLOCK2);
-			pStep->setListener(listener);
-			pStep->setWriteSignalDev(0x33);
-			pStep->setAlarmDev(0x4c4a);
-			if (pEquipment->addStep(0x363, pStep) != 0) {
-				delete pStep;
-			}
-		}
-		{
-			CEqAlarmStep* pStep = new CEqAlarmStep();
-			pStep->setName(STEP_ALARM_BLOCK3);
-			pStep->setListener(listener);
-			pStep->setWriteSignalDev(0x34);
-			pStep->setAlarmDev(0x4c77);
-			if (pEquipment->addStep(0x364, pStep) != 0) {
-				delete pStep;
-			}
-		}
-		{
-			CEqAlarmStep* pStep = new CEqAlarmStep();
-			pStep->setName(STEP_ALARM_BLOCK4);
-			pStep->setListener(listener);
-			pStep->setWriteSignalDev(0x35);
-			pStep->setAlarmDev(0x4ca4);
-			if (pEquipment->addStep(0x365, pStep) != 0) {
-				delete pStep;
-			}
-		}
-		{
-			CEqAlarmStep* pStep = new CEqAlarmStep();
-			pStep->setName(STEP_ALARM_BLOCK5);
-			pStep->setListener(listener);
-			pStep->setWriteSignalDev(0x36);
-			pStep->setAlarmDev(0x4cd1);
-			if (pEquipment->addStep(0x366, pStep) != 0) {
-				delete pStep;
-			}
-		}
-		{
-			CEqProcessStep* pStep = new CEqProcessStep();
-			pStep->setName(STEP_PROCESS);
-			pStep->setListener(listener);
-			pStep->setWriteSignalDev(0x37);
-			pStep->setProcessDev(0x1864);
-			if (pEquipment->addStep(0x367, pStep) != 0) {
-				delete pStep;
-			}
-		}
 
 		pEquipment->init();
 		LOGE("已添加“EFEM(ROBOT)”.");
+
+		return pEquipment;
+	}
+
+	/* 添加bonder1 或 bonder2 
+	 * index -- 0, bonder1
+	 * index -- 1, bonder2
+	 */
+	int CMaster::addBonder(int index)
+	{
+		ASSERT(index == 0 || index == 1);
+		CBonder* pEquipment = new CBonder();
+		pEquipment->setID(EQ_ID_Bonder1 + index);
+		pEquipment->setName(index == 0 ? "Bonder 1" : "Bonder 2");
+		pEquipment->setDescription(index == 0 ? "Bonder 1." : "Bonder 2.");
+		pEquipment->setStation(0, 255);
+		pEquipment->setReadBitBlock(index == 0 ? 0x3b00 : 0x4600,
+			index == 0 ? 0x5600 : 0x6100);
+		pEquipment->setIndex(index);
+		addToEquipmentList(pEquipment);
+
+
+		pEquipment->init();
+		LOGE("已添加“%s”.", pEquipment->getName().c_str());
+
+
+		return 0;
+	}
+
+	int CMaster::addBakeCooling()
+	{
+		CBakeCooling* pEquipment = new CBakeCooling();
+		pEquipment->setID(EQ_ID_BAKE_COOLING);
+		pEquipment->setBaseAlarmId(BASE_ALARM_EFEM);
+		pEquipment->setName("BakeCooling");
+		pEquipment->setDescription("BakeCooling.");
+		pEquipment->setReadBitBlock(0x4000, 0x45ff);
+		pEquipment->setStation(0, 255);
+		addToEquipmentList(pEquipment);
+
+
+		pEquipment->init();
+		LOGE("已添加“Aligner”.");
 		return 0;
 	}
 
@@ -250,18 +376,171 @@
 		// 按一定频率扫描LB数据
 		static int i = 0;
 		i++;
+		/*
 		if (i % (4 * 1) == 0) {
 
 			for (auto item : m_listEquipment) {
-				const StationIdentifier& station = item->getStation();
-				MemoryBlock& block = item->getReadBitBlock();
+				if (item->getID() == EQ_ID_Bonder1) {
+					const StationIdentifier& station = item->getStation();
+					MemoryBlock& block = item->getReadBitBlock();
 
-				int nRet = m_cclink.ReadData2(station, (DeviceType)block.type,
-					block.start, block.size, block.buffer);
-				if (0 == nRet) {
-					item->onReceiveLBData(block.buffer, block.size);
+					int nRet = m_cclink.ReadData2(station, (DeviceType)block.type,
+						block.start, block.size, block.buffer);
+					if (0 == nRet) {
+						item->onReceiveLBData(block.buffer, block.size);
+					}
 				}
 			}
 		}
+		*/
+
+
+		// 自动保存缓存
+		if (i % (4 * 2) == 0) {
+			if (m_bDataModify) {
+				saveCacheAndBackups();
+				m_bDataModify = FALSE;
+			}
+		}
+
+	}
+
+	void CMaster::connectEquipments()
+	{
+		int nRet;
+		CLoadPort* pLoadPort1 = (CLoadPort*)getEquipment(EQ_ID_LOADPORT1);
+		CLoadPort* pLoadPort2 = (CLoadPort*)getEquipment(EQ_ID_LOADPORT2);
+		CLoadPort* pLoadPort3 = (CLoadPort*)getEquipment(EQ_ID_LOADPORT3);
+		CLoadPort* pLoadPort4 = (CLoadPort*)getEquipment(EQ_ID_LOADPORT4);
+		CFliper* pFliper = (CFliper*)getEquipment(EQ_ID_FLIPER);
+		CVacuumBake* pVacuumBake = (CVacuumBake*)getEquipment(EQ_ID_VACUUMBAKE);
+		CAligner* pAligner = (CAligner*)getEquipment(EQ_ID_ALIGNER);
+		CBonder* pBonder1 = (CBonder*)getEquipment(EQ_ID_Bonder1);
+		CBonder* pBonder2 = (CBonder*)getEquipment(EQ_ID_Bonder2);
+		CBakeCooling* pBakeCooling = (CBakeCooling*)getEquipment(EQ_ID_BAKE_COOLING);
+
+		nRet = pLoadPort1->getPin("Out1")->connectPin(pFliper->getPin("In1"));
+		if (nRet < 0) {
+			LOGE("连接LoadPort1-Fliper失败");
+		}
+		nRet = pLoadPort2->getPin("Out1")->connectPin(pFliper->getPin("In2"));
+		if (nRet < 0) {
+			LOGE("连接LoadPort1-Fliper失败");
+		}
+
+		nRet = pLoadPort1->getPin("Out2")->connectPin(pVacuumBake->getPin("In1"));
+		if (nRet < 0) {
+			LOGE("连接LoadPort1-VacuumBake失败");
+		}
+		nRet = pLoadPort2->getPin("Out2")->connectPin(pVacuumBake->getPin("In2"));
+		if (nRet < 0) {
+			LOGE("连接LoadPort1-VacuumBake失败");
+		}
+
+		nRet = pFliper->getPin("Out")->connectPin(pAligner->getPin("In1"));
+		if (nRet < 0) {
+			LOGE("连接Fliper-Aligner失败");
+		}
+
+		nRet = pVacuumBake->getPin("Out")->connectPin(pAligner->getPin("In2"));
+		if (nRet < 0) {
+			LOGE("连接VacuumBake-Aligner失败");
+		}
+
+		nRet = pAligner->getPin("Out1")->connectPin(pBonder1->getPin("In"));
+		if (nRet < 0) {
+			LOGE("连接Aligner-Bondere1失败");
+		}
+
+		nRet = pAligner->getPin("Out2")->connectPin(pBonder2->getPin("In"));
+		if (nRet < 0) {
+			LOGE("连接Aligner-Bondere2失败");
+		}
+
+		nRet = pBonder1->getPin("Out")->connectPin(pBakeCooling->getPin("In1"));
+		if (nRet < 0) {
+			LOGE("连接Bonder1-BakeCooling失败");
+		}
+
+		nRet = pBonder2->getPin("Out")->connectPin(pBakeCooling->getPin("In2"));
+		if (nRet < 0) {
+			LOGE("连接Bonder2-BakeCooling失败");
+		}
+
+		nRet = pBakeCooling->getPin("Out1")->connectPin(pLoadPort3->getPin("In"));
+		if (nRet < 0) {
+			LOGE("连接BakeCooling-LoadPort3失败");
+		}
+
+		nRet = pBakeCooling->getPin("Out2")->connectPin(pLoadPort4->getPin("In"));
+		if (nRet < 0) {
+			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