mrDarker
2025-10-22 e8a27bb203fe2aff70390a5eca002d7438da9b0f
SourceCode/Bond/Servo/CMaster.cpp
@@ -6,9 +6,15 @@
#include "RecipeManager.h"
#include <fstream>
#include "SerializeUtil.h"
#include "CServoUtilsTool.h"
namespace SERVO {
   static inline int64_t now_ms_epoch() {
      using namespace std::chrono;
      return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
   }
   CMaster* g_pMaster = NULL;
   unsigned __stdcall DispatchThreadFunction(LPVOID lpParam)
@@ -233,6 +239,10 @@
         0, &m_nReadBitsThreadAddr);
      // 曲线服务
      CreateDAQBridgeServer();
      LOGI("<Master>初始化完成.");
      return 0;
   }
@@ -262,6 +272,12 @@
      }
      m_listEquipment.clear();
      if (m_pCollector != nullptr) {
         m_pCollector->stopLoop();
         delete m_pCollector;
         m_pCollector = nullptr;
      }
      return 0;
   }
@@ -1481,7 +1497,7 @@
                           m_listener.onCjEnd(this, pJob);
                        }
                        completeControlJob("工艺正常完成");
                        completeControlJob();
                     }
                  }
               }
@@ -1520,8 +1536,20 @@
            unlock();
         }
      };
      listener.onProcessStateChanged = [&](void* pEquipment, PROCESS_STATE state) -> void {
      listener.onProcessStateChanged = [&](void* pEquipment, int slotNo, PROCESS_STATE state) -> void {
         ASSERT(1 <= slotNo && slotNo <= 8);
         int eqid = ((CEquipment*)pEquipment)->getID();
         CGlass* pGlass = ((CEquipment*)pEquipment)->getGlassFromSlot(slotNo);
         LOGI("<Master>onProcessStateChanged<%d>", (int)state);
         if (state == PROCESS_STATE::Processing) {
            if (pGlass != nullptr) {
               m_pCollector->batchStart(eqid,
                  pGlass->getID().c_str(), 10 * 60 * 1000ULL);
            }
         }
         else if (state == PROCESS_STATE::Complete) {
            m_pCollector->batchStop(eqid);
         }
      };
      listener.onMapMismatch = [&](void* pEquipment, short scanMap, short downMap) {
         LOGE("<Master-%s>Port InUse, map(%d!=%d)不一致,请检查。",
@@ -1535,17 +1563,58 @@
            for (auto pj : pjs) {
               auto carrier = pj->getCarrier(pPort->getCassetteId());
               if (carrier != nullptr) {
                  carrier->contexts.clear();
                  for (auto slot : carrier->slots) {
                     CGlass* pGlass = pPort->getGlassFromSlot(slot);
                     carrier->contexts.push_back((void*)pGlass);
                     if (pGlass != nullptr) {
                        pGlass->setProcessJob(pj);
                        PJWarp& jpWarp = pj->getPjWarp();
                        int nRecipeID = RecipeManager::getInstance().getIdByPPID(pj->recipeSpec());
                        RecipeInfo stRecipeInfo = RecipeManager::getInstance().getRecipeByPPID(pj->recipeSpec());
                        std::vector<DeviceRecipe> vecRecipeInfo = stRecipeInfo.vecDeviceList;
                        pGlass->setScheduledForProcessing(jpWarp.checkSlot[slot-1]);
                        pGlass->setType(static_cast<SERVO::MaterialsType>(jpWarp.material[slot-1]));
                        SERVO::CJobDataS* pJobDataS = pGlass->getJobDataS();
                        if (pJobDataS != nullptr) {
                           SERVO::CJobDataS* pJobDataS = pGlass->getJobDataS();
                           pJobDataS->setLotId(pj->getLotId().c_str());
                           pJobDataS->setProductId(pj->getProductId().c_str());
                           pJobDataS->setOperationId(pj->getOperationId().c_str());
                           pJobDataS->setMaterialsType(jpWarp.material[slot - 1]);
                           pJobDataS->setMasterRecipe(nRecipeID);
                           for (const auto& info : vecRecipeInfo) {
                              const std::string& name = info.strDeviceName;
                              short nRecipeID = (short)info.nRecipeID;
                              if (name == EQ_NAME_EFEM) {
                                 pJobDataS->setDeviceRecipeId(0, nRecipeID);
                              }
                              else if (name == EQ_NAME_BONDER1) {
                                 pJobDataS->setDeviceRecipeId(1, nRecipeID);
                              }
                              else if (name == EQ_NAME_BONDER2) {
                                 pJobDataS->setDeviceRecipeId(2, nRecipeID);
                              }
                              else if (name == EQ_NAME_BAKE_COOLING) {
                                 pJobDataS->setDeviceRecipeId(3, nRecipeID);
                              }
                              else if (name == EQ_NAME_VACUUMBAKE) {
                                 pJobDataS->setDeviceRecipeId(4, nRecipeID);
                              }
                              else if (name == EQ_NAME_MEASUREMENT) {
                                 pJobDataS->setDeviceRecipeId(5, nRecipeID);
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
         
         if (m_listener.onLoadPortStatusChanged != nullptr) {
@@ -1558,6 +1627,81 @@
         std::vector<CParam> params;
         ((CEquipment*)pEquipment)->parsingSVData((const char*)rawData.data(), rawData.size(), params);
      
         // 以下加入到曲线数据中
         const int64_t ts = now_ms_epoch();
         int eqid = ((CEquipment*)pEquipment)->getID();
         if (eqid == EQ_ID_Bonder1 || eqid == EQ_ID_Bonder2) {
            // 定义 Bonder 的特定映射
            std::vector<std::pair<int, int>> bonderMapping = {
               {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7},
               {8, 8}, {9, 9}, {10, 10}, {11, 11}, {12, 12}, {13, 13}, {14, 14}, {15, 15}, {16, 16}
            };
            CGlass* pGlass = ((CEquipment*)pEquipment)->getGlassFromSlot(0);
            auto& dataTypes = CServoUtilsTool::getEqDataTypes();
            auto& bonderTypes = dataTypes[eqid];
            for (const auto& mapping : bonderMapping) {
               int paramIndex = mapping.first;
               int channel = mapping.second;
               if (paramIndex < params.size() && channel - 1 < bonderTypes.size()) {
                  if(m_pCollector != nullptr)
                     m_pCollector->buffersPush(eqid, channel, ts, params.at(paramIndex).getDoubleValue());
                  if(pGlass != nullptr)
                     pGlass->addSVData(eqid, bonderTypes[channel], ts, params.at(paramIndex).getDoubleValue());
               }
            }
         }
         else if (eqid == EQ_ID_VACUUMBAKE) {
            // 定义 VACUUMBAKE 的特定映射
            std::vector<std::pair<int, int>> vacuumMapping = {
               {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7},
               {10, 8}, {11, 9}, {12, 10}, {13, 11}, {14, 12}, {15, 13}, {16, 14}
            };
            CGlass* pGlass = ((CEquipment*)pEquipment)->getGlassFromSlot(0);
            auto& dataTypes = CServoUtilsTool::getEqDataTypes();
            auto& vacuumbakeTypes = dataTypes[eqid];
            for (const auto& mapping : vacuumMapping) {
               int paramIndex = mapping.first;
               int channel = mapping.second;
               if (paramIndex < params.size() && channel - 1 < vacuumbakeTypes.size()) {
                  if (m_pCollector != nullptr)
                     m_pCollector->buffersPush(eqid, channel, ts, params.at(paramIndex).getDoubleValue());
                  if (pGlass != nullptr)
                     pGlass->addSVData(eqid, vacuumbakeTypes[channel], ts, params.at(paramIndex).getDoubleValue());
               }
            }
         }
         else if (eqid == EQ_ID_BAKE_COOLING) {
            // 定义 BAKE_COOLING 的特定映射
            std::vector<std::pair<int, int>> coolingMapping = {
               {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6},
               {11, 7}, {12, 8}, {13, 9}, {14, 10}, {15, 11}, {16, 12}
            };
            CGlass* pGlass = ((CEquipment*)pEquipment)->getGlassFromSlot(0);
            auto& dataTypes = CServoUtilsTool::getEqDataTypes();
            auto& coolingTypes = dataTypes[eqid];
            for (const auto& mapping : coolingMapping) {
               int paramIndex = mapping.first;
               int channel = mapping.second;
               if (paramIndex < params.size() && channel - 1 < coolingTypes.size()) {
                  if (m_pCollector != nullptr)
                     m_pCollector->buffersPush(eqid, channel, ts, params.at(paramIndex).getDoubleValue());
                  if (pGlass != nullptr)
                     pGlass->addSVData(eqid, coolingTypes[channel], ts, params.at(paramIndex).getDoubleValue());
               }
            }
         }
         // 以下是输出测试
         std::string strOut;
         char szBuffer[256];
         for (auto p : params) {
@@ -2270,11 +2414,19 @@
      return 0;
   }
   void CMaster::setPortType(unsigned int index, BOOL enable, int type, int mode,
      int cassetteType, int transferMode, BOOL autoChangeEnable)
   void CMaster::setPortType(unsigned int index, int type)
   {
      ASSERT(index < 4);
      int eqid[] = { EQ_ID_LOADPORT1, EQ_ID_LOADPORT2, EQ_ID_LOADPORT3, EQ_ID_LOADPORT4};
      CLoadPort* pPort = (CLoadPort*)getEquipment(eqid[index]);
      pPort->localSetPortType((SERVO::PortType)type);
   }
   void CMaster::setPortTypeEx(unsigned int index, BOOL enable, int type, int mode,
      int cassetteType, int transferMode, BOOL autoChangeEnable)
   {
      ASSERT(index < 4);
      int eqid[] = { EQ_ID_LOADPORT1, EQ_ID_LOADPORT2, EQ_ID_LOADPORT3, EQ_ID_LOADPORT4 };
      CLoadPort* pPort = (CLoadPort*)getEquipment(eqid[index]);
      pPort->localEanblePort(enable);
      pPort->localSetPortType((SERVO::PortType)type);
@@ -2781,7 +2933,43 @@
   }
   bool CMaster::completeControlJob(std::string description)
   bool CMaster::completeControlJob()
   {
      if (m_pControlJob == nullptr) {
         return false;
      }
      for (auto item : m_processJobs) {
         if (item->state() != PJState::Completed) return false;
      }
      if (m_pControlJob->state() != CJState::Completed)
         return false;
      // 释放Job相关
      for (auto item : m_processJobs) {
         delete item;
      }
      m_processJobs.clear();
      if (m_pControlJob != nullptr) {
         delete m_pControlJob;
         m_pControlJob = nullptr;
      }
      // 注意要释放引用
      m_inProcesJobs.clear();
      m_completeProcessJobs.clear();
      m_queueGlasses.clear();
      m_inProcesGlasses.clear();
      m_completeGlasses.clear();
      saveState();
      return true;
   }
   bool CMaster::forceCompleteControlJob(std::string description)
   {
      if (m_pControlJob == nullptr || m_state != SERVO::MASTERSTATE::READY) {
         return false;
@@ -2923,4 +3111,54 @@
      return nullptr;
   }
   void CMaster::CreateDAQBridgeServer()
   {
      auto connectionStatusCallback = [&](int code, const std::string& status) {
         LOGI("<DAQBridge>status:", status.c_str());
      };
      auto rawDataCallback = [](const std::vector<uint8_t>& bytes) {
      };
      // 事件:有人连入/断开就上日志
      auto clieintEventCallback = [](const std::string& ip, uint16_t port, bool connected) {
         LOGI("<DAQBridge>[Client %s] %s:%u", connected ? _T("JOIN") : _T("LEAVE"), ip.c_str(), port);
      };
      if (m_pCollector == nullptr) {
         m_pCollector = new Collector();
         m_pCollector->setConnectionStatusCallback(connectionStatusCallback);
         m_pCollector->setRawDataCallback(rawDataCallback);
         m_pCollector->setClientEventCallback(clieintEventCallback);
         m_pCollector->createServer(8081);
         m_pCollector->startLoop(10);
         // 1) 注册机台(推荐:先注册 id + 机器名称)
         RetentionPolicy defP; defP.mode = RetainMode::ByCount; defP.maxSamples = 200;
         m_pCollector->registryAddMachine(EQ_ID_Bonder1, "Bonder1", defP);
         m_pCollector->registryAddMachine(EQ_ID_Bonder2, "Bonder2", defP);
         m_pCollector->registryAddMachine(EQ_ID_VACUUMBAKE, "前烘烤", defP);
         m_pCollector->registryAddMachine(EQ_ID_BAKE_COOLING, "烘烤冷却", defP);
         // 2) 为通道设置“曲线名称”
         auto& dataTypes = CServoUtilsTool::getEqDataTypes();
         auto& bonderTypes = dataTypes[EQ_ID_Bonder1];
         for (size_t i = 0; i < bonderTypes.size(); ++i) {
            m_pCollector->buffersSetChannelName(EQ_ID_Bonder1, i + 1, bonderTypes[i].c_str());
            m_pCollector->buffersSetChannelName(EQ_ID_Bonder2, i + 1, bonderTypes[i].c_str());
         }
         auto& vacuumbakeTypes = dataTypes[EQ_ID_VACUUMBAKE];
         for (size_t i = 0; i < vacuumbakeTypes.size(); ++i) {
            m_pCollector->buffersSetChannelName(EQ_ID_VACUUMBAKE, i + 1, vacuumbakeTypes[i].c_str());
         }
         auto& coolingTypes = dataTypes[EQ_ID_BAKE_COOLING];
         for (size_t i = 0; i < coolingTypes.size(); ++i) {
            m_pCollector->buffersSetChannelName(EQ_ID_VACUUMBAKE, i + 1, coolingTypes[i].c_str());
         }
      }
   }
}