LAPTOP-SNT8I5JK\Boounion
2025-09-19 81df82be1f5f401b80780f65e5811685d9a8e4c4
SourceCode/Bond/Servo/CMaster.cpp
@@ -213,10 +213,9 @@
      // 读缓存数据
      readCache();
      loadState();
      // 定时器
@@ -2409,8 +2408,31 @@
            temp.push_back(p);
         }
      }
      m_processJobs = temp;
      // 更新context
      std::vector<uint8_t> newSlots;
      std::vector<void*> newContexts;
      for (auto pj : m_processJobs) {
         for (auto& c : pj->carriers()) {
            auto pPort = getPortWithCarrierId(c.carrierId);
            if (pPort == nullptr) continue;
            for (auto s : c.slots) {
               auto pGlass = pPort->getGlassFromSlot(s);
               if (pGlass == nullptr) continue;
               newSlots.push_back(s);
               newContexts.push_back(pGlass);
            }
            pj->setCarrierSlotsAndContexts(c.carrierId, newSlots, newContexts);
         }
      }
      this->saveState();
      return (int)m_processJobs.size();
@@ -2553,13 +2575,9 @@
      return true;
   }
   bool CMaster::loadState(const std::string& path)
   bool CMaster::loadState()
   {
      // 保存文件路径
      m_strStatePath = path;
      std::ifstream ifs(path, std::ios::binary);
      std::ifstream ifs(m_strStatePath, std::ios::binary);
      if (!ifs) return false;
      // 文件头
@@ -2612,10 +2630,33 @@
      m_pControlJob->setPJs(tempPjs);
      // 更新contexts
      auto pjs = m_pControlJob->getPjs();
      for (auto pj : pjs) {
         for (auto& c : pj->carriers()) {
            auto p = getPortWithCarrierId(c.carrierId);
            if (p == nullptr) continue;
            std::vector<void*> contexts;
            for (auto s : c.slots) {
               auto g = getGlass(p->getIndex(), s - 1);
               if (g == nullptr) continue;
               contexts.push_back(g);
            }
            pj->setCarrierContexts(c.carrierId, contexts);
         }
      }
      // 如果版本升级,可在这里判断 version 来加载新字段
      return true;
   }
   void CMaster::setStateFile(const std::string& path)
   {
      m_strStatePath = path;
   }
   CProcessJob* CMaster::acquireNextProcessJob()
@@ -2758,6 +2799,33 @@
      return nullptr;
   }
   bool CMaster::completeControlJob(std::string description)
   {
      if (m_pControlJob == nullptr || m_state != SERVO::MASTERSTATE::READY) {
         return false;
      }
      for (auto item : m_processJobs) {
         item->abort(description);
      }
      m_pControlJob->abort(description);
      // 释放Job相关
      for (auto item : m_processJobs) {
         delete item;
      }
      m_processJobs.clear();
      if (m_pControlJob != nullptr) {
         delete m_pControlJob;
         m_pControlJob = nullptr;
      }
      saveState();
      return true;
   }
   bool CMaster::canCreateControlJob()
   {
      return m_pControlJob == nullptr;
@@ -2849,4 +2917,21 @@
      return true;
   }
   CGlass* CMaster::getGlass(int scrPort, int scrSlot)
   {
      for (auto eq : m_listEquipment) {
         std::vector<CGlass*> glasses;
         eq->getAllGlass(glasses);
         for (auto g : glasses) {
            int p, s;
            g->getOrginPort(p, s);
            if (p == scrPort && s == scrSlot) {
               return g;
            }
         }
      }
      return nullptr;
   }
}