From b31770cfb61272f0bcf148198d1b95ef445fe079 Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期四, 21 八月 2025 10:56:53 +0800
Subject: [PATCH] 1.完善可折叠ListCtrl; 2.l加载Job时,根据ProcessID设置ControlJob的PJ列表 3.ControlJob对话框完善;
---
SourceCode/Bond/Servo/CMaster.cpp | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 162 insertions(+), 2 deletions(-)
diff --git a/SourceCode/Bond/Servo/CMaster.cpp b/SourceCode/Bond/Servo/CMaster.cpp
index ccd1f5d..1f38e9f 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 {
@@ -58,15 +60,21 @@
m_bContinuousTransfer = false;
m_nContinuousTransferCount = 0;
m_nContinuousTransferStep = CTStep_Unknow;
+ m_pControlJob = nullptr;
InitializeCriticalSection(&m_criticalSection);
}
CMaster::~CMaster()
{
+ // 释放Job相关
for (auto item : m_processJobs) {
delete item;
}
m_processJobs.clear();
+ if (m_pControlJob != nullptr) {
+ delete m_pControlJob;
+ m_pControlJob = nullptr;
+ }
if (m_hEventReadBitsThreadExit[0] != nullptr) {
::CloseHandle(m_hEventReadBitsThreadExit[0]);
@@ -1847,22 +1855,83 @@
m_nContinuousTransferCount = round;
}
- int CMaster::setProcessJobs(std::vector<SERVO::CProcessJob*>& pjs)
+ int CMaster::setProcessJobs(std::vector<CProcessJob*>& pjs)
{
std::vector<SERVO::CProcessJob*> temp;
for (auto p : pjs) {
if (p->validate(*this)) {
+ p->queue();
temp.push_back(p);
}
}
m_processJobs = temp;
+ this->saveState();
+
return m_processJobs.size();
}
- std::vector<SERVO::CProcessJob*>& CMaster::getProcessJobs()
+ std::vector<CProcessJob*>& CMaster::getProcessJobs()
{
return m_processJobs;
+ }
+
+ CProcessJob* CMaster::getProcessJob(const std::string& id)
+ {
+ for (auto item : m_processJobs) {
+ if (item->id().compare(id) == 0) return item;
+ }
+
+ return nullptr;
+ }
+
+ int CMaster::setControlJob(CControlJob& controlJob)
+ {
+ // 回调:是否参创建ControlJob
+ auto canCreateCjFn = [&](uint32_t& cc, std::string& mm) -> bool {
+ if (m_pControlJob != nullptr) {
+ cc = 1100;
+ mm = "当前ControlJob未结批,不能创建新的ControlJob";
+ return false;
+ }
+ return true;
+ };
+
+
+ // 回调:是否存在
+ auto pjExists = [&](const std::string& id) -> bool {
+ return getProcessJob(id) != nullptr;
+ };
+
+ // 回调:是否可加入 CJ(这里定义:必须是 Queued)
+ auto pjJoinable = [&](const std::string& id) -> bool {
+ auto pj = getProcessJob(id);
+ if (pj == nullptr) return false;
+ return pj->state() == PJState::Queued;
+ };
+
+ bool bRet = controlJob.validateForCreate(canCreateCjFn, pjExists, pjJoinable);
+ if (!bRet) return -1;
+
+ std::vector<CProcessJob*> temps;
+ m_pControlJob = new CControlJob(controlJob);
+ auto pjIds = controlJob.pjIds();
+ for (auto id : pjIds) {
+ auto pj = getProcessJob(id);
+ if (pj != nullptr) {
+ temps.push_back(pj);
+ }
+ }
+ m_pControlJob->setPJs(temps);
+ this->saveState();
+
+
+ return 0;
+ }
+
+ CControlJob* CMaster::getControlJob()
+ {
+ return m_pControlJob;
}
CLoadPort* CMaster::getPortWithCarrierId(const std::string& carrierId) const
@@ -1910,4 +1979,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