SourceCode/Bond/Servo/CEquipment.cpp
@@ -27,7 +27,7 @@
   CEquipment::CEquipment() : m_nID(0), m_strName(""), m_strDescription(""), m_station(0, 255)
   {
      m_listener = { nullptr, nullptr, nullptr, nullptr, nullptr };
      m_listener = { };
      m_alive = { FALSE, 0, FALSE };
      m_bCimState = FALSE;
      m_bUpstreamInline = FALSE;
@@ -35,6 +35,7 @@
      m_bLocalAlarm = FALSE;
      m_bAutoRecipeChange = FALSE;
      m_bVCREnable[0] = FALSE;
      memset(m_bLinkSignal, 0, sizeof(m_bLinkSignal));
      m_pCclink = nullptr;
      m_nBaseAlarmId = 0;
      m_pArm = nullptr;
@@ -46,7 +47,6 @@
      for (int i = 0; i < SLOT_MAX; i++) {
         CContext* pContext = m_slot[i].getContext();
         if (pContext != nullptr) {
            pContext->release();
            m_slot[i].setContext(nullptr);
         }
      }
@@ -71,11 +71,7 @@
   void CEquipment::setListener(EquipmentListener listener)
   {
      m_listener.onAlive = listener.onAlive;
      m_listener.onCimStateChanged = listener.onCimStateChanged;
      m_listener.onAlarm = listener.onAlarm;
      m_listener.onDataChanged = listener.onDataChanged;
      m_listener.onVcrEventReport = listener.onVcrEventReport;
      m_listener = listener;
   }
   void CEquipment::setCcLink(CCCLinkIEControl* pCcLink)
@@ -259,13 +255,21 @@
         if (!m_slot[i].isEnable()) continue;
         CGlass* pGlass = (CGlass*)m_slot[i].getContext();
         CGlass* pBuddy = nullptr;
         if (pGlass == nullptr) {
            attrubutes.addAttribute(new CAttribute(m_slot[i].getName().c_str(),
               "", "", weight++));
         }
         else {
            attrubutes.addAttribute(new CAttribute(m_slot[i].getName().c_str(),
               pGlass->getID().c_str(), "", weight++));
            pBuddy = pGlass->getBuddy();
            if (pBuddy == nullptr) {
               attrubutes.addAttribute(new CAttribute(m_slot[i].getName().c_str(),
                  pGlass->getID().c_str(), "", weight++));
            }
            else {
               attrubutes.addAttribute(new CAttribute(m_slot[i].getName().c_str(),
                  (pGlass->getID() + " -> " + pBuddy->getID()).c_str(), "", weight++));
            }
         }
      }
@@ -321,8 +325,13 @@
         Lock();
         for (int i = 0; i < SLOT_MAX; i++) {
            m_slot[i].serialize(ar);
            if (m_slot[i].getContext() != nullptr) {
               ((CGlass*)m_slot[i].getContext())->serialize(ar);
            CGlass* pGlass = (CGlass *)m_slot[i].getContext();
            if (pGlass != nullptr) {
               pGlass->serialize(ar);
               CGlass* pBuddy = pGlass->getBuddy();
               if (pBuddy != nullptr) {
                  pBuddy->serialize(ar);
               }
            }
         }
         Unlock();
@@ -330,24 +339,32 @@
      else {
         for (int i = 0; i < SLOT_MAX; i++) {
            m_slot[i].serialize(ar);
            if (m_slot[i].getContext() != nullptr) {
            if (m_slot[i].getTempContext() != nullptr) {
               CGlass* pGlass = theApp.m_model.m_glassPool.allocaGlass();
               pGlass->serialize(ar);
               m_slot[i].setContext(pGlass);
               if (pGlass->getBuddy() != nullptr) {
                  CGlass* pBuddy = theApp.m_model.m_glassPool.allocaGlass();
                  pBuddy->serialize(ar);
                  pGlass->forceSetBuddy(pBuddy);
               }
            }
         }
         
         // 梳理各玻璃之间的绑定关系
         /*
         Lock();
         std::list<CGlass*> list = m_glassList;
         for (auto item : list) {
            std::string& strBuddyId = item->getBuddyId();
            if (!strBuddyId.empty()) {
               for (auto item2 : m_glassList) {
                  if (strBuddyId.compare(item2->getID()) == 0) {
                     item->setBuddy(item2);
                     TRACE("绑定关系: %s <- %s\n", item->getID().c_str(), item2->getID().c_str());
         for (int i = 0; i < SLOT_MAX; i++) {
            CGlass* pGlass = (CGlass*)m_slot[i].getContext();
            if (pGlass != nullptr) {
               std::string& strBuddyId = pGlass->getBuddyId();
               if (!strBuddyId.empty()) {
                  for (int j = 0; j < SLOT_MAX; j++) {
                     CGlass* pBudy = (CGlass*)m_slot[j].getContext();
                     if (pBudy != nullptr && strBuddyId.compare(pBudy->getID()) == 0) {
                        pGlass->setBuddy(pBudy);
                        TRACE("绑定关系: %s <- %s\n", pGlass->getID().c_str(), pBudy->getID().c_str());
                     }
                  }
               }
            }
@@ -367,9 +384,20 @@
      }
      */
      // 以下解释和处理数据
      // 连接信号解释和保存
      BOOL bFlag;
      int index = 0x540;
      int index = 0;
      for (int i = 0; i < 8; i++) {
         m_bLinkSignal[i][SIGNAL_UPSTREAM_INLINE] = isBitOn(pszData, size, index + 0);
         m_bLinkSignal[i][SIGNAL_UPSTREAM_TROUBLE] = isBitOn(pszData, size, index + 1);
         m_bLinkSignal[i][SIGNAL_INTERLOCK] = isBitOn(pszData, size, index + 2);
         m_bLinkSignal[i][SIGNAL_SEND_ABLE] = isBitOn(pszData, size, index + 3);
         index += 0x40;
      }
      // 其它信号及响应
      index = 0x540;
      // alive
@@ -586,6 +614,28 @@
      return m_bVCREnable[index];
   }
   BOOL CEquipment::isLinkSignalOn(unsigned int path, unsigned int signal)
   {
      if (path >= PATH_MAX) return FALSE;
      if (signal >= SIGNAL_MAX) return FALSE;
      return m_bLinkSignal[path][signal];
   }
   void CEquipment::setLinkSignal(unsigned int path, unsigned int signal, BOOL bOn)
   {
      if (path >= PATH_MAX) return;
      if (signal >= SIGNAL_MAX) return;
      m_bLinkSignal[path][signal] = bOn;
   }
   void CEquipment::setLinkSignalBlock(unsigned int path, BOOL* pSignal)
   {
      if (path >= PATH_MAX) return;
      for (int i = 0; i < SIGNAL_MAX; i++) {
         m_bLinkSignal[path][i] = pSignal[i];
      }
   }
   int CEquipment::onStepEvent(CStep* pStep, int code)
   {
      if (code == STEP_EVENT_READDATA) {
@@ -726,6 +776,7 @@
         CGlass* pGlass = (CGlass*)m_slot[i].getContext();
         if (pGlass != nullptr && compareJobDataB(pJobDataB, pGlass->getJobDataB())) {
            pContext = pGlass;
            if (pGlass != nullptr) pGlass->addRef();
            m_slot[i].setContext(nullptr);
            break;
         }
@@ -735,34 +786,20 @@
         return -3;
      }
      // 如果此玻璃已经贴合,贴合的玻璃也要从列表中移除
      CGlass* pBuddy = pContext->getBuddy();
      if (pBuddy != nullptr) {
         for (int i = 0; i < SLOT_MAX; i++) {
            CGlass* pGlass = (CGlass*)m_slot[i].getContext();
            if (pGlass != nullptr && compareJobDataB(pBuddy->getJobDataB(), pGlass->getJobDataB())) {
               pContext->release();
               m_slot[i].setContext(nullptr);
               break;
            }
         }
      }
      ((CArm*)m_pArm)->tempStore(pContext);
      pContext->release();
      Unlock();
      if (m_listener.onDataChanged != nullptr) {
         m_listener.onDataChanged(this, 0);
         m_listener.onDataChanged(this, EDCC_FETCHOUT_JOB);
      }
      return 0;
   }
   int CEquipment::storedJob(CJobDataB* pJobDataB)
   int CEquipment::storedJob(CJobDataB* pJobDataB, short putSlot)
   {
      /*
      if (m_pArm == nullptr) {
         return -1;
      }
@@ -775,28 +812,32 @@
      ASSERT(pGlass);
      Lock();
      pGlass->addPath(m_nID);
      pGlass->addRef();               // 加入list,addRef
      m_glassList.push_back(pGlass);
      pGlass->addPath(m_nID, getSlotUnit(putSlot));
      m_slot[putSlot - 1].setContext(pGlass);
      pGlass->release();            // tempFetchOut需要调用一次release
      Unlock();
      // 如果此玻璃已经贴合,贴合的玻璃也要从加入到列表中
      /*
      CGlass* pBuddy = pGlass->getBuddy();
      if (pBuddy != nullptr) {
         Lock();
         pBuddy->addPath(m_nID);
         pBuddy->addRef();               // 加入list,addRef
         m_glassList.push_back(pBuddy);
         pBuddy->addPath(m_nID, 0);
         if (putSlot % 2 == 0) {
            m_slot[putSlot - 2].setContext(pBuddy);
         }
         else {
            m_slot[putSlot].setContext(pBuddy);
         }
         Unlock();
      }
      */
      if (m_listener.onDataChanged != nullptr) {
         m_listener.onDataChanged(this, 0);
         m_listener.onDataChanged(this, EDCC_STORED_JOB);
      }
      */
      return 0;
   }
@@ -809,6 +850,20 @@
      }
      return FALSE;
   }
   CGlass* CEquipment::getGlass(const char* pszGlassId)
   {
      for (int i = 0; i < SLOT_MAX; i++) {
         if (!m_slot[i].isEnable()) continue;
         CGlass* pGlass = (CGlass*)m_slot[i].getContext();
         if (pGlass == nullptr) continue;
         if (pGlass->getID().compare(pszGlassId) == 0) {
            return pGlass;
         }
      }
      return nullptr;
   }
   bool CEquipment::isAlarmStep(SERVO::CStep* pStep)
@@ -1056,6 +1111,27 @@
      return nullptr;
   }
   CSlot* CEquipment::getAvailableSlotForGlass2(MaterialsType type, const std::vector<int>& candidates)
   {
      for (auto item : candidates) {
         for (int i = 0; i < SLOT_MAX; i++) {
            if (item == i + 1) {
               if (!m_slot[i].isEnable()) continue;
               if (m_slot[i].isLock()) continue;
               if (!m_slot[i].isEmpty()) continue;
               MaterialsType slotType = m_slot[i].getType();
               if (type == MaterialsType::G1 && slotType == MaterialsType::G2) continue;
               if (type == MaterialsType::G2 && slotType == MaterialsType::G1) continue;
               return &m_slot[i];
            }
         }
      }
      return nullptr;
   }
   CSlot* CEquipment::getNonEmptySlot(MaterialsType putSlotType)
   {
      for (int i = 0; i < SLOT_MAX; i++) {
@@ -1072,6 +1148,62 @@
      }
      return nullptr;
   }
   CSlot* CEquipment::getProcessedSlot(MaterialsType putSlotType)
   {
      for (int i = 0; i < SLOT_MAX; i++) {
         if (!m_slot[i].isEnable()) continue;
         if (m_slot[i].isLock()) continue;
         CGlass* pGlass = (CGlass*)m_slot[i].getContext();
         if (pGlass == nullptr) continue;
         int lsPath = m_slot[i].getLinkSignalPath();
         if(!m_bLinkSignal[lsPath][SIGNAL_UPSTREAM_INLINE]
            || m_bLinkSignal[lsPath][SIGNAL_UPSTREAM_TROUBLE]
            || !m_bLinkSignal[lsPath][SIGNAL_INTERLOCK]
            || !m_bLinkSignal[lsPath][SIGNAL_SEND_ABLE] ) continue;
         MaterialsType glassType = pGlass->getType();
         if (glassType == MaterialsType::G1 && putSlotType == MaterialsType::G2) continue;
         if (glassType == MaterialsType::G2 && putSlotType == MaterialsType::G1) continue;
         return &m_slot[i];
      }
      return nullptr;
   }
   CSlot* CEquipment::getProcessedSlot2(MaterialsType putSlotType, const std::vector<int>& candidates)
   {
      for (auto item : candidates) {
         for (int i = 0; i < SLOT_MAX; i++) {
            if (item == i + 1) {
               if (!m_slot[i].isEnable()) continue;
               if (m_slot[i].isLock()) continue;
               CGlass* pGlass = (CGlass*)m_slot[i].getContext();
               if (pGlass == nullptr) continue;
               int lsPath = m_slot[i].getLinkSignalPath();
               if (!m_bLinkSignal[lsPath][SIGNAL_UPSTREAM_INLINE]
                  || m_bLinkSignal[lsPath][SIGNAL_UPSTREAM_TROUBLE]
                  || !m_bLinkSignal[lsPath][SIGNAL_INTERLOCK]
                  || !m_bLinkSignal[lsPath][SIGNAL_SEND_ABLE]) continue;
               MaterialsType glassType = pGlass->getType();
               if (glassType == MaterialsType::G1 && putSlotType == MaterialsType::G2) continue;
               if (glassType == MaterialsType::G2 && putSlotType == MaterialsType::G1) continue;
               return &m_slot[i];
            }
         }
      }
      return nullptr;
   }
   CSlot* CEquipment::getSlot(int index)
   {
      if (index >= SLOT_MAX) return nullptr;
      return &m_slot[index];
   }
   CGlass* CEquipment::getGlassFromSlot(int slotNo)
@@ -1102,6 +1234,30 @@
      }
      return nullptr;
   }
   BOOL CEquipment::canPlaceGlassInSlot(const short slotIndex)
   {
      if (slotIndex >= SLOT_MAX) return FALSE;
      if (!m_slot[slotIndex].isEnable()) return FALSE;
      if (m_slot[slotIndex].getContext() != nullptr) return FALSE;
      return TRUE;
   }
   int CEquipment::removeGlass(int slotNo)
   {
      CSlot* pSlot = nullptr;
      for (int i = 0; i < SLOT_MAX; i++) {
         if (!m_slot[i].isEnable()) continue;
         if (m_slot[i].getNo() != slotNo) continue;
         pSlot = &m_slot[i];
         break;
      }
      if (pSlot == nullptr) return -1;
      pSlot->setContext(nullptr);
      return 0;
   }
   short CEquipment::decodeRecipeListReport(const char* pszData, size_t size)
@@ -1233,10 +1389,14 @@
      return index;
   }
   int CEquipment::onPreFetchedOutJob(int port, CJobDataB* pJobDataB)
   BOOL CEquipment::onPreFetchedOutJob(int port, CJobDataB* pJobDataB)
   {
      LOGI("<CEquipment-%s>onPreFetchedOutJob:port:%d|GlassId:%s",
         m_strName.c_str(), port, pJobDataB->getGlassId().c_str());
      if (m_listener.onPreFethedOutJob != nullptr) {
         return m_listener.onPreFethedOutJob(this, pJobDataB);
      }
      return TRUE;
   }
@@ -1319,7 +1479,58 @@
      return 0;
   }
   int CEquipment::onPreStoredJob(int port, CJobDataB* pJobDataB)
   int CEquipment::decodePanelDataReport(CStep* pStep, const char* pszData, size_t size)
   {
      short cassetteNo, jobSequenceNo;
      int index = 0;
      std::string strPanelJudgeData, strPanelGradeData;
      memcpy(&cassetteNo, &pszData[index], sizeof(short));
      index += sizeof(short);
      memcpy(&jobSequenceNo, &pszData[index], sizeof(short));
      index += sizeof(short);
      CToolUnits::convertString(&pszData[index], 128 * 2, strPanelJudgeData);
      index += 128 * 2;
      CToolUnits::convertString(&pszData[index], 256 * 2, strPanelJudgeData);
      index += 256 * 2;
      // 缓存Attribute,用于调试时显示信息
      unsigned int weight = 201;
      pStep->addAttribute(new CAttribute("CassetteNo",
         std::to_string(cassetteNo).c_str(), "", weight++));
      pStep->addAttribute(new CAttribute("JobSequenceNo",
         std::to_string(jobSequenceNo).c_str(), "", weight++));
      pStep->addAttribute(new CAttribute("PanelJudgeData",
         strPanelJudgeData.c_str(), "", weight++));
      pStep->addAttribute(new CAttribute("PanelGradeData",
         strPanelGradeData.c_str(), "", weight++));
      return 0;
   }
   int CEquipment::decodeFacDataReport(CStep* pStep, const char* pszData, size_t size)
   {
      int index = 0;
      std::string strSvTimeRecord, strSvData;
      CToolUnits::convertString(&pszData[index], 8 * 2, strSvTimeRecord);
      index += 128 * 2;
      CToolUnits::convertString(&pszData[index], 640 * 2, strSvData);
      index += 256 * 2;
      // 缓存Attribute,用于调试时显示信息
      unsigned int weight = 201;
      pStep->addAttribute(new CAttribute("SV Time Record",
         strSvTimeRecord.c_str(), "", weight++));
      pStep->addAttribute(new CAttribute("SV Data",
         strSvData.c_str(), "", weight++));
      return 0;
   }
   int CEquipment::onPreStoredJob(int port, CJobDataB* pJobDataB, short& putSlot)
   {
      LOGI("<CEquipment-%s>onPreStoredJob:port:%d|GlassId:%s",
         m_strName.c_str(), port, pJobDataB->getGlassId().c_str());
@@ -1331,7 +1542,7 @@
         return FALSE;
      }
      // 当前不能有任何玻璃,且当前准备进的片是G2
      // 如果没有可用位置,报错
      Lock();
      CSlot* pSlot = getAvailableSlotForGlass((MaterialsType)pJobDataS->getMaterialsType());
      if (pSlot == nullptr) {
@@ -1341,6 +1552,17 @@
      }
      Unlock();
      if (m_listener.onPreStoredJob != nullptr) {
         if (!m_listener.onPreStoredJob(this, pJobDataB, putSlot)) {
            return FALSE;
         }
         if(!canPlaceGlassInSlot(putSlot - 1)) {
            return FALSE;
         }
      }
      return TRUE;
   }
@@ -1349,10 +1571,11 @@
      LOGI("<CEquipment-%s>onStore:port:%d|GlassId:%s",
         m_strName.c_str(), port, pJobDataB->getGlassId().c_str());
      BOOL bCheck = onPreStoredJob(port, pJobDataB);
      short putSlot = 0;
      BOOL bCheck = onPreStoredJob(port, pJobDataB, putSlot);
      if (bCheck) {
         addJobDataB(pJobDataB);
         return storedJob(pJobDataB);
         return storedJob(pJobDataB, putSlot);
      }
      // 数据异常,处理或显示
@@ -1364,6 +1587,7 @@
   int CEquipment::onProcessData(CProcessData* pProcessData)
   {
      LOGI("<CEquipment-%s>onProcessData.", m_strName.c_str());
      return 0;
   }