| | |
| | | #include <future> |
| | | #include <vector> |
| | | #include "RecipeManager.h" |
| | | #include <fstream> |
| | | #include "SerializeUtil.h" |
| | | |
| | | |
| | | namespace SERVO { |
| | |
| | | } |
| | | |
| | | m_processJobs = temp; |
| | | this->saveState(); |
| | | |
| | | return m_processJobs.size(); |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | m_pControlJob->setPJs(temps); |
| | | this->saveState(); |
| | | |
| | | |
| | | return 0; |
| | | } |
| | | |
| | |
| | | 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); |
| | | } |
| | | |
| | | // 如果版本升级,可在这里判断 version 来加载新字段 |
| | | |
| | | |
| | | return true; |
| | | } |
| | | } |