From cb9456e93002220b7247fadac088c7bddf6908d3 Mon Sep 17 00:00:00 2001
From: chenluhua1980 <Chenluhua@qq.com>
Date: 星期日, 01 二月 2026 21:03:23 +0800
Subject: [PATCH] 1.LoadPort1装G1,LoadPort2装G2, LoadPort3装G1, LoadPort4装G4, 加工完成回到G1的原位。按此增加生产模式的调度和连接图;

---
 SourceCode/Bond/Servo/CMaster.cpp |  479 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 434 insertions(+), 45 deletions(-)

diff --git a/SourceCode/Bond/Servo/CMaster.cpp b/SourceCode/Bond/Servo/CMaster.cpp
index ee0b951..707f808 100644
--- a/SourceCode/Bond/Servo/CMaster.cpp
+++ b/SourceCode/Bond/Servo/CMaster.cpp
@@ -3,10 +3,14 @@
 #include "CMaster.h"
 #include <future>
 #include <vector>
+#include <algorithm>
 #include "RecipeManager.h"
 #include <fstream>
 #include "SerializeUtil.h"
 #include "CServoUtilsTool.h"
+#include "AlarmManager.h"
+#include "ToolUnits.h"
+#include "Model.h"
 
 
 namespace SERVO {
@@ -58,6 +62,8 @@
 		m_ullStartTime = 0;
 		m_ullRunTime = 0;
 		m_state = MASTERSTATE::READY;
+		m_curveMode = CurveMode::Production;
+		m_schedulingMode = SchedulingMode::Production;
 		m_pActiveRobotTask = nullptr;
 		m_nLastError = ER_CODE_NOERROR;
 		m_isCompareMapsBeforeProceeding = FALSE;
@@ -71,6 +77,8 @@
 		m_nContinuousWorkingPort = 0;
 		m_nContinuousWorkingSlot = 0;
 		m_pControlJob = nullptr;
+		m_bPauseAlarmRaised = false;
+		m_pModelCtx = nullptr;
 		m_nTestFlag = 0;
 		InitializeCriticalSection(&m_criticalSection);
 	}
@@ -112,12 +120,18 @@
 			m_hEventDispatchThreadExit[1] = nullptr;
 		}
 
+
 		DeleteCriticalSection(&m_criticalSection);
 	}
 
 	void CMaster::setListener(MasterListener listener)
 	{
 		m_listener = listener;
+	}
+
+	void CMaster::setModelCtx(CModel* pModel)
+	{
+		m_pModelCtx = pModel;
 	}
 
 	CRobotTask* CMaster::getActiveRobotTask()
@@ -127,11 +141,18 @@
 
 	int CMaster::init()
 	{
+		const ULONGLONG boot_master_begin = GetTickCount64();
 		LOGI("<Master>姝e湪鍒濆鍖�...");
+		LOGI("[BOOT][MASTER] init begin");
 
 
 		// 	cclink
-		if (m_cclink.Connect(CC_LINK_IE_CONTROL_CHANNEL(1)) != 0) {
+		const ULONGLONG boot_cclink_begin = GetTickCount64();
+		const int cc_ret = m_cclink.Connect(CC_LINK_IE_CONTROL_CHANNEL(1));
+		LOGI("[BOOT][MASTER] CC-Link connect ret=%d, cost=%llu ms",
+			cc_ret,
+			(unsigned long long)(GetTickCount64() - boot_cclink_begin));
+		if (cc_ret != 0) {
 			LOGE("杩炴帴CC-Link澶辫触.");
 		}
 		else {
@@ -220,11 +241,21 @@
 
 
 		// 璇荤紦瀛樻暟鎹�
+		const ULONGLONG boot_cache_begin = GetTickCount64();
+		const ULONGLONG boot_read_begin = GetTickCount64();
 		readCache();
+		LOGI("[BOOT][MASTER] readCache finished, cost=%llu ms", (unsigned long long)(GetTickCount64() - boot_read_begin));
+
+		const ULONGLONG boot_state_begin = GetTickCount64();
 		loadState();
+		LOGI("[BOOT][MASTER] loadState finished, cost=%llu ms", (unsigned long long)(GetTickCount64() - boot_state_begin));
 		if (m_listener.onControlJobChanged) {
-			m_listener.onControlJobChanged(this);
+			notifyControlJobChanged();
 		}
+
+		LOGI("[BOOT][MASTER] cache/state loaded, cost=%llu ms (since init %llu ms)",
+			(unsigned long long)(GetTickCount64() - boot_cache_begin),
+			(unsigned long long)(GetTickCount64() - boot_master_begin));
 
 
 		// 瀹氭椂鍣�
@@ -247,7 +278,50 @@
 
 
 		LOGI("<Master>鍒濆鍖栧畬鎴�.");
+		LOGI("[BOOT][MASTER] init finished, total cost=%llu ms",
+			(unsigned long long)(GetTickCount64() - boot_master_begin));
 		return 0;
+	}
+
+	void CMaster::setCurveMode(CurveMode mode)
+	{
+		if (m_curveMode == mode) {
+			return;
+		}
+		m_curveMode = mode;
+		if (m_pCollector != nullptr) {
+			const uint32_t mids[] = {
+				MID_Bonder1, MID_Bonder2,
+				MID_VacuumBakeA, MID_VacuumBakeB,
+				MID_BakeCoolingA, MID_BakeCoolingB
+			};
+			for (uint32_t mid : mids) {
+				if (mode == CurveMode::EmptyChamber) {
+					m_pCollector->batchStart(mid, "EMPTY_CHAMBER", 30 * 60 * 1000ULL); // 绌鸿厰妯″紡锛氬惎鍔ㄩ噰鏍锋壒娆�
+				}
+				else {
+					m_pCollector->batchStop(mid);
+					m_pCollector->buffersClear(mid); // 鍒囧洖鐢熶骇妯″紡锛屾竻鎺夌┖鑵旀暟鎹�
+				}
+			}
+		}
+		LOGI("<Master>CurveMode=%s", mode == CurveMode::EmptyChamber ? "EmptyChamber" : "Production");
+	}
+
+	CurveMode CMaster::getCurveMode() const
+	{
+		return m_curveMode;
+	}
+
+	void CMaster::setSchedulingMode(SchedulingMode mode)
+	{
+		m_schedulingMode = mode;
+		LOGI("<Master>SchedulingMode=%s", mode == SchedulingMode::Production ? "Production" : "Tuning");
+	}
+
+	SchedulingMode CMaster::getSchedulingMode() const
+	{
+		return m_schedulingMode;
 	}
 
 	int CMaster::term()
@@ -275,6 +349,13 @@
 		}
 		m_listEquipment.clear();
 
+		// release manual-remove buffer before glass pool is torn down
+		for (auto* pGlass : m_bufGlass) {
+			if (pGlass != nullptr) {
+				pGlass->release();
+			}
+		}
+		m_bufGlass.clear();
 
 		if (m_pCollector != nullptr) {
 			m_pCollector->stopLoop();
@@ -677,14 +758,22 @@
 
 
 				// Measurement -> LoadPort
-				for (int s = 0; s < 4; s++) {
-					PortType pt = pLoadPorts[s]->getPortType();
-					if (!rmd.armState[0] && pLoadPorts[s]->isEnable()
-						&& (pt == PortType::Unloading || pt == PortType::Both)
-						&& pLoadPorts[s]->getPortStatus() == PORT_INUSE) {
-						m_pActiveRobotTask = createTransferTask(pMeasurement, pLoadPorts[s], MaterialsType::G1, secondaryType);
-						if (m_pActiveRobotTask != nullptr) {
-							goto PORT_PUT;
+				if (m_schedulingMode == SchedulingMode::Production) {
+					if (!rmd.armState[0]) {
+						m_pActiveRobotTask = createTransferTask_returnOrigin(pMeasurement, pLoadPorts);
+						CHECK_RUN_ACTIVE_ROBOT_TASK(m_pActiveRobotTask);
+					}
+				}
+				else {
+					for (int s = 0; s < 4; s++) {
+						PortType pt = pLoadPorts[s]->getPortType();
+						if (!rmd.armState[0] && pLoadPorts[s]->isEnable()
+							&& (pt == PortType::Unloading || pt == PortType::Both)
+							&& pLoadPorts[s]->getPortStatus() == PORT_INUSE) {
+							m_pActiveRobotTask = createTransferTask(pMeasurement, pLoadPorts[s], MaterialsType::G1, secondaryType);
+							if (m_pActiveRobotTask != nullptr) {
+								goto PORT_PUT;
+							}
 						}
 					}
 				}
@@ -881,6 +970,33 @@
 					continue;
 				}
 
+				// 5.5) 鏆傚仠鐘舵�佹鏌ワ細鑻� CJ 鎴栧湪鍒� PJ 澶勪簬 Paused锛屾殏缂撹皟搴︽柊鐨勬惉閫�
+				bool pausedByEvent = false;
+				if (m_pControlJob != nullptr && m_pControlJob->state() == CJState::Paused) {
+					pausedByEvent = true;
+				}
+				for (auto pj : m_inProcesJobs) {
+					if (pj != nullptr && pj->state() == PJState::Paused) {
+						pausedByEvent = true;
+						break;
+					}
+				}
+				if (!pausedByEvent && m_bPauseAlarmRaised) {
+					if (m_pModelCtx != nullptr) {
+						m_pModelCtx->clearSoftAlarm(ALID_SOFTWARE_PAUSE_EVENT, 0, 0);
+					}
+					else {
+						AlarmManager& alarmManager = AlarmManager::getInstance();
+						alarmManager.clearAlarmByAttributes(ALID_SOFTWARE_PAUSE_EVENT, 0, 0, CToolUnits::getCurrentTimeString());
+					}
+					m_bPauseAlarmRaised = false;
+				}
+				if (pausedByEvent) {
+					LOGI("<Master>璋冨害鏆傚仠锛欳ontrolJob/ProcessJob 澶勪簬 Paused 鐘舵�侊紙鍙兘鐢� PauseEvent 瑙﹀彂锛�");
+					unlock();
+					continue;
+				}
+
 				// 6) 鈥斺�斿叧閿細鍏ㄥ眬缁熻 G1/G2 涓庣粍鏁伴棬闄愶紙涓庡崟鐗囧垎鏀榻愶級鈥斺��
 				auto countG1G2 = [&]() {
 					int g1 = 0, g2 = 0;
@@ -912,19 +1028,27 @@
 				// 缁勬暟闂ㄩ檺锛氣墺2 缁勬椂涓嶅啀浠� LP 涓婄墖锛岄伩鍏嶅爢绉紙涓庡崟鐗囦竴鑷达級
 				bool blockLoadFromLP = (nGlassGroup >= 2);
 
-				// 7) Measurement -> LoadPort锛堝浐瀹氾細G1 浼樺厛鍥� LP锛�
+				// 7) Measurement -> LoadPort
 				if (rmd.armState[0] || rmd.armState[1]) {
 					LOGD("Arm1 %s, Arm2 %s.",
 						rmd.armState[0] ? _T("涓嶅彲鐢�") : _T("鍙敤"),
 						rmd.armState[1] ? _T("涓嶅彲鐢�") : _T("鍙敤"));
 				}
-				for (int s = 0; s < 4; s++) {
-					PortType pt = pLoadPorts[s]->getPortType();
-					if (!rmd.armState[0] && pLoadPorts[s]->isEnable()
-						&& (pt == PortType::Unloading || pt == PortType::Both)
-						&& pLoadPorts[s]->getPortStatus() == PORT_INUSE) {
-						m_pActiveRobotTask = createTransferTask(pMeasurement, pLoadPorts[s], MaterialsType::G1, secondaryType);
-						if (m_pActiveRobotTask != nullptr) { goto BATCH_PORT_PUT; }
+				if (m_schedulingMode == SchedulingMode::Production) {
+					if (!rmd.armState[0]) {
+						m_pActiveRobotTask = createTransferTask_returnOrigin(pMeasurement, pLoadPorts);
+						CHECK_RUN_ACTIVE_ROBOT_TASK(m_pActiveRobotTask);
+					}
+				}
+				else {
+					for (int s = 0; s < 4; s++) {
+						PortType pt = pLoadPorts[s]->getPortType();
+						if (!rmd.armState[0] && pLoadPorts[s]->isEnable()
+							&& (pt == PortType::Unloading || pt == PortType::Both)
+							&& pLoadPorts[s]->getPortStatus() == PORT_INUSE) {
+							m_pActiveRobotTask = createTransferTask(pMeasurement, pLoadPorts[s], MaterialsType::G1, secondaryType);
+							if (m_pActiveRobotTask != nullptr) { goto BATCH_PORT_PUT; }
+						}
 					}
 				}
 
@@ -1637,6 +1761,15 @@
 			}
 		};
 		listener.onSVDataReport = [&](void* pEquipment, void* pData) {
+			const bool allowSvLog =
+				(m_state == MASTERSTATE::RUNNING ||
+					m_state == MASTERSTATE::RUNNING_CONTINUOUS_TRANSFER ||
+					m_state == MASTERSTATE::RUNNING_BATCH ||
+					m_state == MASTERSTATE::STARTING);
+			const bool allowCurve = allowSvLog || (m_curveMode == CurveMode::EmptyChamber);
+			if (!allowCurve) {
+				return;
+			}
 			CSVData* pSVData = (CSVData*)pData;
 			auto rawData = pSVData->getSVRawData();
 			std::vector<CParam> params;
@@ -1688,18 +1821,23 @@
 					int paramIndex = mapping.first;
 					int channel = mapping.second;
 
-					if (paramIndex < params.size() && channel - 1 < vacuumbakeTypes.size()) {
+					if (paramIndex < params.size()) {
 						auto& param = params.at(paramIndex);
 						double value = param.getDoubleValue();
-						const std::string& dataType = vacuumbakeTypes[channel - 1];
 						const std::string& paramName = param.getName();
 						const char slotTag = !paramName.empty() ? paramName[0] : '\0';
+						const int typeIndex = (slotTag == 'B') ? (channel - 8) : (channel - 1);
+						if (typeIndex < 0 || typeIndex >= (int)vacuumbakeTypes.size()) {
+							continue;
+						}
+						const int pushChannel = typeIndex + 1;
+						const std::string& dataType = vacuumbakeTypes[typeIndex];
 
 						if (m_pCollector != nullptr) {
 							if (slotTag == 'A')
-								m_pCollector->buffersPush(SlotToMid(eqid, 1), channel, ts, value);
+								m_pCollector->buffersPush(SlotToMid(eqid, 1), pushChannel, ts, value);
 							else if (slotTag == 'B')
-								m_pCollector->buffersPush(SlotToMid(eqid, 2), channel, ts, value);
+								m_pCollector->buffersPush(SlotToMid(eqid, 2), pushChannel, ts, value);
 						}
 
 						// 鏍规嵁鑵斾綋鍓嶇紑鍐欏叆瀵瑰簲 Slot 鐨勭幓鐠�
@@ -1733,20 +1871,25 @@
 					int paramIndex = mapping.first;
 					int channel = mapping.second;
 
-					if (paramIndex < params.size() && channel - 1 < coolingTypes.size()) {
+					if (paramIndex < params.size()) {
 						auto& param = params.at(paramIndex);
 						double value = param.getDoubleValue();
-						const std::string& dataType = coolingTypes[channel - 1];
 						const std::string& paramName = param.getName();
 						const char slotTag = !paramName.empty() ? paramName[0] : '\0';
 						const bool paramIsBake = paramName.find("鐑樼儰") != std::string::npos;
 						const bool paramIsCooling = paramName.find("鍐峰嵈") != std::string::npos;
+						const int typeIndex = (slotTag == 'B') ? (channel - 7) : (channel - 1);
+						if (typeIndex < 0 || typeIndex >= (int)coolingTypes.size()) {
+							continue;
+						}
+						const int pushChannel = typeIndex + 1;
+						const std::string& dataType = coolingTypes[typeIndex];
 
 						if (m_pCollector != nullptr && paramIsBake) {
 							if (slotTag == 'A')
-								m_pCollector->buffersPush(SlotToMid(eqid, 1), channel, ts, value);
+								m_pCollector->buffersPush(SlotToMid(eqid, 1), pushChannel, ts, value);
 							else if (slotTag == 'B')
-								m_pCollector->buffersPush(SlotToMid(eqid, 3), channel, ts, value);
+								m_pCollector->buffersPush(SlotToMid(eqid, 3), pushChannel, ts, value);
 						}
 
 						if (!dataType.empty()) {
@@ -1813,6 +1956,16 @@
 		listener.onReceivedJob = [&](void* pEquipment, int port, CJobDataS* pJobDataS) {
 			if (m_listener.onJobReceived != nullptr) {
 				m_listener.onJobReceived(this, (CEquipment*)pEquipment, port, pJobDataS);
+			}
+		};
+		listener.onSentOutJob = [&](void* pEquipment, int port, CJobDataS* pJobDataS) {
+			if (m_listener.onJobSentOut != nullptr) {
+				m_listener.onJobSentOut(this, (CEquipment*)pEquipment, port, pJobDataS);
+			}
+		};
+		listener.onEqStatusChanged = [&](void* pEquipment, int unitId, int status, int reason) {
+			if (m_listener.onEqStatusChanged != nullptr) {
+				m_listener.onEqStatusChanged(this, (CEquipment*)pEquipment, unitId, status, reason);
 			}
 		};
 		pEquipment->setListener(listener);
@@ -2324,11 +2477,19 @@
 
 		nRet = pLoadPort1->getPin("Out")->connectPin(pAligner->getPin("In1"));
 		if (nRet < 0) {
-			LOGE("杩炴帴LoadPort1-Fliper澶辫触");
+			LOGE("杩炴帴LoadPort1-Aligner澶辫触");
 		}
 		nRet = pLoadPort2->getPin("Out")->connectPin(pAligner->getPin("In2"));
 		if (nRet < 0) {
-			LOGE("杩炴帴LoadPort1-Fliper澶辫触");
+			LOGE("杩炴帴LoadPort2-Aligner澶辫触");
+		}
+		nRet = pLoadPort3->getPin("Out")->connectPin(pAligner->getPin("In3"));
+		if (nRet < 0) {
+			LOGE("杩炴帴LoadPort3-Aligner澶辫触");
+		}
+		nRet = pLoadPort4->getPin("Out")->connectPin(pAligner->getPin("In4"));
+		if (nRet < 0) {
+			LOGE("杩炴帴LoadPort4-Aligner澶辫触");
 		}
 
 		nRet = pAligner->getPin("Out1")->connectPin(pFliper->getPin("In"));
@@ -2373,14 +2534,29 @@
 			LOGE("杩炴帴BakeCooling-LoadPort3澶辫触");
 		}
 
-		nRet = pMeasurement->getPin("Out1")->connectPin(pLoadPort3->getPin("In"));
-		if (nRet < 0) {
-			LOGE("杩炴帴BakeCooling-LoadPort3澶辫触");
-		}
+		if (m_schedulingMode == SchedulingMode::Production) {
+			// 鐢熶骇妯″紡锛氭祴閲忚緭鍑哄洖鍒� G1 鍘熶綅锛堥粯璁� Port1 / Port3锛�
+			nRet = pMeasurement->getPin("Out1")->connectPin(pLoadPort1->getPin("In"));
+			if (nRet < 0) {
+				LOGE("杩炴帴Measurement-LoadPort1澶辫触");
+			}
 
-		nRet = pMeasurement->getPin("Out2")->connectPin(pLoadPort4->getPin("In"));
-		if (nRet < 0) {
-			LOGE("杩炴帴BakeCooling-LoadPort4澶辫触");
+			nRet = pMeasurement->getPin("Out2")->connectPin(pLoadPort3->getPin("In"));
+			if (nRet < 0) {
+				LOGE("杩炴帴Measurement-LoadPort3澶辫触");
+			}
+		}
+		else {
+			// 璋冩満妯″紡锛氱淮鎸佸師杩炴帴锛圤ut1->Port3, Out2->Port4锛�
+			nRet = pMeasurement->getPin("Out1")->connectPin(pLoadPort3->getPin("In"));
+			if (nRet < 0) {
+				LOGE("杩炴帴Measurement-LoadPort3澶辫触");
+			}
+
+			nRet = pMeasurement->getPin("Out2")->connectPin(pLoadPort4->getPin("In"));
+			if (nRet < 0) {
+				LOGE("杩炴帴Measurement-LoadPort4澶辫触");
+			}
 		}
 	}
 
@@ -2610,6 +2786,57 @@
 		return pTask;
 	}
 
+	CRobotTask* CMaster::createTransferTask_returnOrigin(CEquipment* pEqSrc, CLoadPort** pPorts)
+	{
+		if (!pEqSrc->IsEnabled()) {
+			return nullptr;
+		}
+
+		CSlot* pSrcSlot = pEqSrc->getProcessedSlot(MaterialsType::G1, m_bJobMode);
+		if (pSrcSlot == nullptr) {
+			return nullptr;
+		}
+
+		CGlass* pGlass = (CGlass*)pSrcSlot->getContext();
+		if (pGlass == nullptr) {
+			return nullptr;
+		}
+
+		int port = 0, slot = 0;
+		pGlass->getOrginPort(port, slot);
+		if (port < 0 || port >= 4 || slot < 0 || slot >= SLOT_MAX) {
+			return nullptr;
+		}
+
+		CLoadPort* pPort = pPorts[port];
+		if (pPort == nullptr || !pPort->isEnable()) {
+			return nullptr;
+		}
+		PortType pt = pPort->getPortType();
+		if (!(pt == PortType::Unloading || pt == PortType::Both)) {
+			return nullptr;
+		}
+		if (pPort->getPortStatus() != PORT_INUSE) {
+			return nullptr;
+		}
+
+		CSlot* pTarSlot = pPort->getSlot(slot);
+		if (pTarSlot == nullptr) {
+			return nullptr;
+		}
+		if (!pTarSlot->isEnable() || pTarSlot->isLock() || pTarSlot->getContext() != nullptr) {
+			return nullptr;
+		}
+
+		CRobotTask* pTask = new CRobotTask();
+		pTask->setContext(pSrcSlot->getContext());
+		pTask->setEFEM((CEFEM*)getEquipment(EQ_ID_EFEM));
+		taskSeqNo = pTask->setRobotTransferParam(taskSeqNo, 1, pSrcSlot->getPosition(),
+			pTarSlot->getPosition(), pSrcSlot->getNo(), pTarSlot->getNo());
+
+		return pTask;
+	}
+
 	CRobotTask* CMaster::createTransferTask_continuous_transfer(CEquipment* pSrcEq, int nSrcSlot,
 		CEquipment* pTarEq, int nTarSlot, int armNo/* = 1*/)
 	{
@@ -2719,6 +2946,19 @@
 		pPort->localSetCessetteType(type);
 	}
 
+	void CMaster::applySchedulingModePortMapping()
+	{
+		// 鐢熶骇妯″紡锛氬浐瀹� Port1/Port3 涓� G1锛孭ort2/Port4 涓� G2锛圙4 鏈畾涔夛紝鎸� G2 澶勭悊锛�
+		if (m_schedulingMode != SchedulingMode::Production) {
+			return;
+		}
+
+		setPortCassetteType(0, SERVO::CassetteType::G1);
+		setPortCassetteType(1, SERVO::CassetteType::G2);
+		setPortCassetteType(2, SERVO::CassetteType::G1);
+		setPortCassetteType(3, SERVO::CassetteType::G2);
+	}
+
 	void CMaster::setPortEnable(unsigned int index, BOOL bEnable)
 	{
 		ASSERT(index < 4);
@@ -2782,7 +3022,40 @@
 
 		static int pid[] = { EQ_ID_LOADPORT1, EQ_ID_LOADPORT2, EQ_ID_LOADPORT3, EQ_ID_LOADPORT4};
 		CLoadPort* pPort = (CLoadPort*)getEquipment(pid[port]);
-		pPort->sendCassetteCtrlCmd(CCC_PROCESS_START, nullptr, 0, 0, 0, nullptr, nullptr);
+		if (pPort == nullptr) return -1;
+
+		short jobExistence[12] = { 0 };
+		short slotProcess = 0;
+		short jobCount = 0; // 0 = Process All Glass
+		bool anyScheduled = false;
+
+		// Prefer hardware scan map for job existence (first 16 slots).
+		const short scanMap = pPort->getScanCassetteMap();
+		if (scanMap != 0) {
+			jobExistence[0] = scanMap;
+		}
+
+		const int maxSlots = 12 * 16;
+		const int totalSlots = (SLOT_MAX < maxSlots) ? SLOT_MAX : maxSlots;
+		for (int slot = 1; slot <= totalSlots; ++slot) {
+			CGlass* pGlass = pPort->getGlassFromSlot(slot);
+			if (pGlass == nullptr) continue;
+
+			const int wordIndex = (slot - 1) / 16;
+			const int bitIndex = (slot - 1) % 16;
+			jobExistence[wordIndex] = (short)(jobExistence[wordIndex] | (1 << bitIndex));
+
+			if (slot <= 16 && pGlass->isScheduledForProcessing()) {
+				slotProcess = (short)(slotProcess | (1 << bitIndex));
+				anyScheduled = true;
+			}
+		}
+
+		if (!anyScheduled) {
+			slotProcess = jobExistence[0];
+		}
+
+		pPort->sendCassetteCtrlCmd(CCC_PROCESS_START, jobExistence, 12, slotProcess, jobCount, nullptr, nullptr);
 		return 0;
 	}
 
@@ -2858,7 +3131,7 @@
 
 		this->saveState();
 		if (m_listener.onControlJobChanged) {
-			m_listener.onControlJobChanged(this);
+			notifyControlJobChanged();
 		}
 
 		return (int)m_processJobs.size();
@@ -2918,7 +3191,7 @@
 		m_pControlJob->setPJs(temps);
 		this->saveState();
 		if (m_listener.onControlJobChanged) {
-			m_listener.onControlJobChanged(this);
+			notifyControlJobChanged();
 		}
 
 
@@ -2972,7 +3245,98 @@
 
 	bool CMaster::ceidDefined(uint32_t ceid) const
 	{
-		return true;
+		if (m_allowedCeids.empty()) return true; // backward compatible: treat as all allowed when not configured
+		return m_allowedCeids.find(ceid) != m_allowedCeids.end();
+	}
+
+	bool CMaster::raiseSoftAlarm(int alarmId,
+		const std::string& desc,
+		int level/* = -1*/,
+		int deviceId/* = 0*/,
+		int unitId/* = 0*/,
+		const char* deviceName/* = "Software"*/,
+		const char* unitName/* = "App"*/)
+	{
+		AlarmManager& alarmManager = AlarmManager::getInstance();
+		const AlarmInfo* info = alarmManager.getAlarmInfoByID(alarmId);
+
+		int severity = level;
+		if (severity < 0 && info != nullptr) {
+			severity = info->nAlarmLevel;
+		}
+		if (severity < 0) severity = 0; // fallback
+
+		std::string descText = desc;
+		if (descText.empty() && info != nullptr) {
+			descText = !info->strDescription.empty() ? info->strDescription : info->strAlarmText;
+		}
+		if (descText.empty()) {
+			descText = CToolUnits::formatString("Alarm %d", alarmId);
+		}
+
+		AlarmData alarmData;
+		alarmData.nId = alarmId;
+		alarmData.nSeverityLevel = severity;
+		alarmData.nDeviceId = deviceId;
+		alarmData.nUnitId = unitId;
+		alarmData.strDeviceName = deviceName;
+		alarmData.strUnitName = unitName;
+		alarmData.strStartTime = CToolUnits::timeToString2(CToolUnits::getTimestamp());
+		alarmData.strEndTime = "";
+		alarmData.strDescription = descText;
+
+		int nAlarmEventId = 0;
+		return alarmManager.addAlarm(alarmData, nAlarmEventId);
+	}
+
+	void CMaster::handleCollectionEvent(uint32_t ceid)
+	{
+		// 閬嶅巻褰撳墠 PJ锛屽懡涓� pauseEvents 鏃跺彲鍦ㄦ鎵╁睍鏆傚仠鍔ㄤ綔
+		bool pausedAny = false;
+		for (auto pj : m_processJobs) {
+			if (pj == nullptr) continue;
+			const auto& pauseList = pj->pauseEvents();
+			if (std::find(pauseList.begin(), pauseList.end(), ceid) != pauseList.end()) {
+				LOGW("<Master>PauseEvent hit: CEID=%u, PJ=%s, state=%d", ceid, pj->id().c_str(), (int)pj->state());
+				if (pj->pause()) {
+					LOGI("<Master>PJ paused by CEID=%u", ceid);
+					pausedAny = true;
+				}
+				if (m_pControlJob != nullptr && m_pControlJob->state() == CJState::Executing) {
+					if (m_pControlJob->pause()) {
+						LOGI("<Master>ControlJob paused by CEID=%u", ceid);
+						pausedAny = true;
+					}
+				}
+			}
+		}
+		if (pausedAny && m_listener.onControlJobChanged) {
+			// 閫氱煡搴旂敤灞傚埛鏂� UI/鎸夐挳鐘舵��
+			notifyControlJobChanged();
+		}
+		if (pausedAny && !m_bPauseAlarmRaised) {
+			std::string desc = CToolUnits::formatString("<PauseEvent CEID=%u>", ceid);
+			bool raised = false;
+			if (m_pModelCtx != nullptr) {
+				raised = m_pModelCtx->raiseSoftAlarm(ALID_SOFTWARE_PAUSE_EVENT, desc);
+			}
+			else {
+				raised = raiseSoftAlarm(ALID_SOFTWARE_PAUSE_EVENT, desc);
+			}
+			if (raised) {
+				LOGI("<Master>PauseEvent soft alarm raised, CEID=%u", ceid);
+				m_bPauseAlarmRaised = true;
+			}
+		}
+	}
+
+	void CMaster::setAllowedCeids(const std::vector<unsigned int>& ceids)
+	{
+		m_allowedCeids.clear();
+		m_allowedCeids.reserve(ceids.size());
+		for (auto id : ceids) {
+			m_allowedCeids.insert(id);
+		}
 	}
 
 	bool CMaster::saveState() const
@@ -3263,7 +3627,7 @@
 
 		saveState();
 		if (m_listener.onControlJobChanged) {
-			m_listener.onControlJobChanged(this);
+			notifyControlJobChanged();
 		}
 
 		return true;
@@ -3278,6 +3642,11 @@
 			item->abort(description);
 		}
 		m_pControlJob->abort(description);
+
+		// 鍏堜笂鎶ヤ竴娆$姸鎬佸彉鍖栵紙渚夸簬 PrJobAbort 瑙﹀彂锛�
+		if (m_listener.onControlJobChanged) {
+			notifyControlJobChanged();
+		}
 
 
 		// 閲婃斁Job鐩稿叧
@@ -3300,7 +3669,7 @@
 
 		saveState();
 		if (m_listener.onControlJobChanged) {
-			m_listener.onControlJobChanged(this);
+			notifyControlJobChanged();
 		}
 
 		return true;
@@ -3363,6 +3732,15 @@
 		if (pSlot == nullptr) return false;
 
 		CGlass* pGlass = (CGlass*)pSlot->getContext();
+		if (pGlass == nullptr) return false;
+
+		// Buffer 涓婇檺涓� 1锛氭柊鎼嚭鏃朵涪寮冩棫鐨�
+		if (!m_bufGlass.empty()) {
+			for (auto* oldGlass : m_bufGlass) {
+				if (oldGlass != nullptr) oldGlass->release();
+			}
+			m_bufGlass.clear();
+		}
 		m_bufGlass.push_back(pGlass);
 		pGlass->addRef();
 		pSlot->setContext(nullptr);
@@ -3451,14 +3829,14 @@
 			auto& dataTypes = CServoUtilsTool::getEqDataTypes();
 			auto& bonderTypes = dataTypes[MID_Bonder1];
 			for (size_t i = 0; i < bonderTypes.size(); ++i) {
-				m_pCollector->buffersSetChannelName(MID_Bonder1, i + 1, bonderTypes[i].c_str());
-				m_pCollector->buffersSetChannelName(MID_Bonder2, i + 1, bonderTypes[i].c_str());
+				m_pCollector->buffersSetChannelName(MID_Bonder1, (UINT)i + 1, bonderTypes[(UINT)i].c_str());
+				m_pCollector->buffersSetChannelName(MID_Bonder2, (UINT)i + 1, bonderTypes[(UINT)i].c_str());
 			}
 
 			auto& vacuumbakeTypes = dataTypes[MID_VacuumBakeA];
 			for (size_t i = 0; i < vacuumbakeTypes.size(); ++i) {
-				m_pCollector->buffersSetChannelName(MID_VacuumBakeA, i + 1, vacuumbakeTypes[i].c_str());
-				m_pCollector->buffersSetChannelName(MID_VacuumBakeB, i + 1, vacuumbakeTypes[i].c_str());
+				m_pCollector->buffersSetChannelName(MID_VacuumBakeA, (UINT)i + 1, vacuumbakeTypes[(UINT)i].c_str());
+				m_pCollector->buffersSetChannelName(MID_VacuumBakeB, (UINT)i + 1, vacuumbakeTypes[(UINT)i].c_str());
 			}
 
 			auto& coolingTypes = dataTypes[MID_BakeCoolingA];
@@ -3466,6 +3844,17 @@
 				m_pCollector->buffersSetChannelName(MID_BakeCoolingA, i + 1, coolingTypes[i].c_str());
 				m_pCollector->buffersSetChannelName(MID_BakeCoolingB, i + 1, coolingTypes[i].c_str());
 			}
+
+			if (m_curveMode == CurveMode::EmptyChamber) {
+				const uint32_t mids[] = {
+					MID_Bonder1, MID_Bonder2,
+					MID_VacuumBakeA, MID_VacuumBakeB,
+					MID_BakeCoolingA, MID_BakeCoolingB
+				};
+				for (uint32_t mid : mids) {
+					m_pCollector->batchStart(mid, "EMPTY_CHAMBER", 10 * 60 * 1000ULL);
+				}
+			}
 		}
 	}
 

--
Gitblit v1.9.3