| | |
| | | #include "stdafx.h" |
| | | #include "ProcessJob.h" |
| | | #include <cctype> |
| | | #include <fstream> |
| | | #include "SerializeUtil.h" |
| | | |
| | | |
| | | namespace SERVO { |
| | | static inline std::string trimCopy(std::string s) { |
| | |
| | | s.erase(s.begin(), std::find_if(s.begin(), s.end(), notspace)); |
| | | s.erase(std::find_if(s.rbegin(), s.rend(), notspace).base(), s.end()); |
| | | return s; |
| | | } |
| | | |
| | | CProcessJob::CProcessJob() |
| | | { |
| | | |
| | | } |
| | | |
| | | CProcessJob::CProcessJob(std::string pjId) |
| | |
| | | m_carriers.emplace_back(std::move(cs)); |
| | | } |
| | | } |
| | | |
| | | // --------- 核心:serialize/deserialize --------- |
| | | void CProcessJob::serialize(std::ostream& os) const { |
| | | // ͷ |
| | | write_pod(os, PJ_FILE_MAGIC); |
| | | write_pod(os, PJ_FILE_VERSION); |
| | | |
| | | // 基本 |
| | | write_string(os, m_pjId); |
| | | write_string(os, m_parentCjId); |
| | | |
| | | // 配方 |
| | | uint8_t recipeType = static_cast<uint8_t>(m_recipeMethod); |
| | | write_pod(os, m_recipeMethod); |
| | | write_string(os, m_recipeSpec); |
| | | |
| | | // 物料(多 Carrier & Slot) |
| | | { |
| | | uint32_t n = static_cast<uint32_t>(m_carriers.size()); |
| | | write_pod(os, n); |
| | | for (const auto& cs : m_carriers) { |
| | | write_string(os, cs.carrierId); |
| | | write_vec<uint8_t>(os, cs.slots); |
| | | } |
| | | } |
| | | |
| | | // 参数 |
| | | { |
| | | uint32_t n = static_cast<uint32_t>(m_params.size()); |
| | | write_pod(os, n); |
| | | for (const auto& p : m_params) { |
| | | write_string(os, p.name); |
| | | write_string(os, p.value); |
| | | } |
| | | } |
| | | |
| | | // 暂停事件 |
| | | write_vec<uint32_t>(os, m_pauseEvents); |
| | | |
| | | // 启动策略 & 状态 |
| | | uint8_t startPolicy = static_cast<uint8_t>(m_startPolicy); |
| | | uint8_t st = static_cast<uint8_t>(m_state); |
| | | write_pod(os, startPolicy); |
| | | write_pod(os, st); |
| | | |
| | | // 失败原因 |
| | | write_string(os, m_failReason); |
| | | |
| | | // 时间戳 |
| | | write_opt_time(os, m_tQueued); |
| | | write_opt_time(os, m_tStart); |
| | | write_opt_time(os, m_tEnd); |
| | | } |
| | | |
| | | bool CProcessJob::deserialize(std::istream& is, CProcessJob& out, std::string* err) { |
| | | auto fail = [&](const char* msg) { if (err) *err = msg; return false; }; |
| | | |
| | | uint32_t magic = 0; if (!read_pod(is, magic)) return fail("read magic failed"); |
| | | if (magic != PJ_FILE_MAGIC) return fail("bad magic"); |
| | | |
| | | uint16_t ver = 0; if (!read_pod(is, ver)) return fail("read version failed"); |
| | | if (ver != PJ_FILE_VERSION) return fail("unsupported version"); |
| | | |
| | | // 基本 |
| | | if (!read_string(is, out.m_pjId)) return fail("read pjId"); |
| | | if (!read_string(is, out.m_parentCjId)) return fail("read parentCjId"); |
| | | |
| | | // 配方 |
| | | uint8_t recipeType = 0; if (!read_pod(is, recipeType)) return fail("read recipeType"); |
| | | out.m_recipeMethod = static_cast<RecipeMethod>(recipeType); |
| | | if (!read_string(is, out.m_recipeSpec)) return fail("read recipeSpec"); |
| | | |
| | | // 物料 |
| | | { |
| | | uint32_t n = 0; if (!read_pod(is, n)) return fail("read carriers count"); |
| | | out.m_carriers.clear(); out.m_carriers.reserve(n); |
| | | for (uint32_t i = 0; i < n; ++i) { |
| | | CarrierSlotInfo cs; |
| | | if (!read_string(is, cs.carrierId)) return fail("read carrierId"); |
| | | if (!read_vec<uint8_t>(is, cs.slots)) return fail("read slots"); |
| | | out.m_carriers.emplace_back(std::move(cs)); |
| | | } |
| | | } |
| | | |
| | | // 参数 |
| | | { |
| | | uint32_t n = 0; if (!read_pod(is, n)) return fail("read params count"); |
| | | out.m_params.clear(); out.m_params.reserve(n); |
| | | for (uint32_t i = 0; i < n; ++i) { |
| | | PJParam p; |
| | | if (!read_string(is, p.name)) return fail("read param name"); |
| | | if (!read_string(is, p.value)) return fail("read param value"); |
| | | out.m_params.emplace_back(std::move(p)); |
| | | } |
| | | } |
| | | |
| | | // 暂停事件 |
| | | if (!read_vec<uint32_t>(is, out.m_pauseEvents)) return fail("read pauseEvents"); |
| | | |
| | | // 启动策略 & 状态 |
| | | uint8_t startPolicy = 0, st = 0; |
| | | if (!read_pod(is, startPolicy)) return fail("read startPolicy"); |
| | | if (!read_pod(is, st)) return fail("read state"); |
| | | out.m_startPolicy = static_cast<StartPolicy>(startPolicy); |
| | | out.m_state = static_cast<PJState>(st); |
| | | |
| | | // 失败原因 |
| | | if (!read_string(is, out.m_failReason)) return fail("read failReason"); |
| | | |
| | | // 时间戳 |
| | | if (!read_opt_time(is, out.m_tQueued)) return fail("read tQueued"); |
| | | if (!read_opt_time(is, out.m_tStart)) return fail("read tStart"); |
| | | if (!read_opt_time(is, out.m_tEnd)) return fail("read tEnd"); |
| | | |
| | | return true; |
| | | } |
| | | |
| | | std::string CProcessJob::getStateText() |
| | | { |
| | | switch (m_state) |
| | | { |
| | | case SERVO::PJState::NoState: |
| | | return "NoState"; |
| | | break; |
| | | case SERVO::PJState::Queued: |
| | | return "Queued"; |
| | | break; |
| | | case SERVO::PJState::SettingUp: |
| | | return "SettingUp"; |
| | | break; |
| | | case SERVO::PJState::InProcess: |
| | | return "InProcess"; |
| | | break; |
| | | case SERVO::PJState::Paused: |
| | | return "Queued"; |
| | | break; |
| | | case SERVO::PJState::Aborting: |
| | | return "Aborting"; |
| | | break; |
| | | case SERVO::PJState::Completed: |
| | | return "Queued"; |
| | | break; |
| | | case SERVO::PJState::Aborted: |
| | | return "Aborted"; |
| | | break; |
| | | case SERVO::PJState::Failed: |
| | | return "Failed"; |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | |
| | | return ""; |
| | | } |
| | | |
| | | CarrierSlotInfo* CProcessJob::getCarrier(std::string& strId) |
| | | { |
| | | for (auto& item : m_carriers) { |
| | | if (item.carrierId.compare(strId) == 0) { |
| | | return &item; |
| | | } |
| | | } |
| | | |
| | | return nullptr; |
| | | } |
| | | } |