From 2a21061d88d5533065dc57cfae0b1f2c1952e06f Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期五, 22 八月 2025 16:01:32 +0800
Subject: [PATCH] 1.PorcessJob和Glass关系绑定; 2.对话框显示ProcessJob、Glass等数据;

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

diff --git a/SourceCode/Bond/Servo/CMaster.cpp b/SourceCode/Bond/Servo/CMaster.cpp
index 81a0115..e01b180 100644
--- a/SourceCode/Bond/Servo/CMaster.cpp
+++ b/SourceCode/Bond/Servo/CMaster.cpp
@@ -4,6 +4,8 @@
 #include <future>
 #include <vector>
 #include "RecipeManager.h"
+#include <fstream>
+#include "SerializeUtil.h"
 
 
 namespace SERVO {
@@ -1141,6 +1143,25 @@
 		};
 		listener.onPortStatusChanged = [&](void* pEquipment, short status, __int64 data) {
 			LOGE("<Master-%s>onPortStatusChanged。status=%d, data=%lld", ((CEquipment*)pEquipment)->getName().c_str(), status);
+			if (status == PORT_INUSE && m_pControlJob != nullptr) {
+				CLoadPort* pPort = (CLoadPort*)pEquipment;
+				auto pjs = m_pControlJob->getPjs();
+				for (auto pj : pjs) {
+					auto carrier = pj->getCarrier(pPort->getCassetteId());
+					if (carrier != nullptr) {
+						for (auto slot : carrier->slots) {
+							CGlass* pGlass = pPort->getGlassFromSlot(slot);
+							carrier->contexts.push_back((void*)pGlass);
+							if (pGlass != nullptr) {
+								pGlass->setProcessJob(pj);
+							}
+						}
+					}
+				}
+
+
+			}
+			
 			if (m_listener.onLoadPortStatusChanged != nullptr) {
 				m_listener.onLoadPortStatusChanged(this, (CEquipment*)pEquipment, status, data);
 			}
@@ -1864,7 +1885,9 @@
 		}
 
 		m_processJobs = temp;
-		return m_processJobs.size();
+		this->saveState();
+
+		return (int)m_processJobs.size();
 	}
 
 	std::vector<CProcessJob*>& CMaster::getProcessJobs()
@@ -1919,7 +1942,15 @@
 			}
 		}
 		m_pControlJob->setPJs(temps);
+		this->saveState();
+
+
 		return 0;
+	}
+
+	CControlJob* CMaster::getControlJob()
+	{
+		return m_pControlJob;
 	}
 
 	CLoadPort* CMaster::getPortWithCarrierId(const std::string& carrierId) const
@@ -1967,4 +1998,95 @@
 		return true;
 	}
 
+	bool CMaster::saveState() const
+	{
+		std::ofstream ofs(m_strStatePath, std::ios::binary);
+		if (!ofs) return false;
+
+		// 文件头
+		uint32_t magic = 0x4D415354; // 'MAST'
+		uint16_t version = 1;
+		ofs.write(reinterpret_cast<const char*>(&magic), sizeof(magic));
+		ofs.write(reinterpret_cast<const char*>(&version), sizeof(version));
+
+		// 保存 ControlJob
+		bool hasCJ = (m_pControlJob != nullptr);
+		ofs.write(reinterpret_cast<const char*>(&hasCJ), sizeof(hasCJ));
+		if (hasCJ) {
+			m_pControlJob->serialize(ofs);
+		}
+
+		// 保存 ProcessJob 列表
+		uint32_t count = static_cast<uint32_t>(m_processJobs.size());
+		ofs.write(reinterpret_cast<const char*>(&count), sizeof(count));
+		for (const auto& job : m_processJobs) {
+			job->serialize(ofs);
+		}
+
+		// 以后可以在这里追加新字段
+		return true;
+	}
+
+	bool CMaster::loadState(const std::string& path)
+	{
+		// 保存文件路径
+		m_strStatePath = path;
+
+
+		std::ifstream ifs(path, std::ios::binary);
+		if (!ifs) return false;
+
+		// 文件头
+		uint32_t magic = 0;
+		uint16_t version = 0;
+		ifs.read(reinterpret_cast<char*>(&magic), sizeof(magic));
+		ifs.read(reinterpret_cast<char*>(&version), sizeof(version));
+
+		if (magic != 0x4D415354) {
+			// 文件不合法
+			return false;
+		}
+
+		if (m_pControlJob != nullptr) {
+			delete m_pControlJob;
+			m_pControlJob = nullptr;
+		}
+
+		// 读取 ControlJob
+		bool hasCJ = false;
+		ifs.read(reinterpret_cast<char*>(&hasCJ), sizeof(hasCJ));
+		if (hasCJ) {
+			m_pControlJob = new CControlJob();
+			if (!CControlJob::deserialize(ifs, *m_pControlJob)) return false;
+		}
+
+
+		// 读取 ProcessJob 列表
+		uint32_t count = 0;
+		ifs.read(reinterpret_cast<char*>(&count), sizeof(count));
+		m_processJobs.clear();
+		for (uint32_t i = 0; i < count; i++) {
+			CProcessJob* pProcessJob = new CProcessJob();
+			if (!CProcessJob::deserialize(ifs, *pProcessJob)) return false;
+			m_processJobs.push_back(pProcessJob);
+		}
+
+
+		// 找到CProcessJob指针加入列表中
+		std::vector<CProcessJob*> tempPjs;
+		auto ids = m_pControlJob->pjIds();
+		for (auto id : ids) {
+			auto pj = getProcessJob(id);
+			if (pj != nullptr) {
+				tempPjs.push_back(pj);
+			}
+		}
+		m_pControlJob->setPJs(tempPjs);
+
+
+		// 如果版本升级,可在这里判断 version 来加载新字段
+
+
+		return true;
+	}
 }

--
Gitblit v1.9.3