#include "stdafx.h" #include "Equipment.h" #include "Data.h" #include "BEQCommon.h" #define DATA_INIT_LEN 1024 * 8 namespace BEQ { CEquipment::CEquipment() { m_pClientListener = nullptr; m_strVersion = _T("1.0.1"); m_pActiveClient = nullptr; m_listener.onConnected = nullptr; m_listener.onDisconnected = nullptr; m_listener.onRunRecipe = nullptr; m_listener.onGetRecipeList = nullptr; m_listener.onLoadReady = nullptr; m_listener.onLoadComplete = nullptr; m_listener.onUnloadComplete = nullptr; m_pReqRecipeListClient = nullptr; m_pReqRecipeListUnit = nullptr; } CEquipment::CEquipment(const char* pszName) { m_strName = pszName; m_strVersion = _T("1.0.1"); m_pClientListener = nullptr; m_pActiveClient = nullptr; m_listener.onConnected = nullptr; m_listener.onDisconnected = nullptr; m_listener.onRunRecipe = nullptr; m_listener.onGetRecipeList = nullptr; m_listener.onLoadReady = nullptr; m_listener.onLoadComplete = nullptr; m_listener.onUnloadComplete = nullptr; m_pReqRecipeListClient = nullptr; m_pReqRecipeListUnit = nullptr; } CEquipment::~CEquipment() { for (auto iter = m_recycleClients.begin(); iter != m_recycleClients.end(); iter++) { delete (CData*)((*iter)->m_pContext1); delete *iter; } m_recycleClients.clear(); for (auto iter = m_clients.begin(); iter != m_clients.end(); iter++) { delete (CData*)((*iter)->m_pContext1); delete *iter; } for (auto iter = m_units.begin(); iter != m_units.end(); iter++) { delete iter->second; } m_units.clear(); if (m_pClientListener != nullptr) { delete m_pClientListener; m_pClientListener = nullptr; } if (m_pServer != nullptr) { m_pServer->Close(); delete m_pServer; m_pServer = nullptr; } } void CEquipment::setEquipmentListener(EquipmentListener listener) { m_listener.onConnected = listener.onConnected; m_listener.onDisconnected = listener.onDisconnected; m_listener.onRunRecipe = listener.onRunRecipe; m_listener.onGetRecipeList = listener.onGetRecipeList; m_listener.onLoadReady = listener.onLoadReady; m_listener.onLoadComplete = listener.onLoadComplete; m_listener.onUnloadComplete = listener.onUnloadComplete; } int CEquipment::getName(char* pszBuffer, int nMaxCount) { return strcpy_s(pszBuffer, nMaxCount, m_strName.c_str()); } void CEquipment::setName(const char* pszName) { m_strName = pszName; } int CEquipment::getVersion(char* pszBuffer, int nMaxCount) { return strcpy_s(pszBuffer, nMaxCount, m_strName.c_str()); } void CEquipment::setVersion(const char* pszVersion) { m_strVersion = pszVersion; } IUnit* CEquipment::addUnit(const char* pszName, int nDoorCount) { auto iter = m_units.find(pszName); if (iter != m_units.end()) { return (IUnit*)iter->second; } CUnit* pUnit = new CUnit(pszName); m_units[pszName] = pUnit; pUnit->setDoorCount(nDoorCount); UnitListener listener; listener.onStateChanged = [&](void* pUnit, EQ_STATE nStete) -> void { if (m_pActiveClient != nullptr) { repState(m_pActiveClient, (CUnit*)pUnit, AS_MC_STATE_REP); } }; listener.onDoorStateChanged = [&](void* pUnit, int stete) -> void { if (m_pActiveClient != nullptr) { repDoorState(m_pActiveClient, (CUnit*)pUnit, AS_GET_DOOR_REP); } }; listener.onAlarm = [&](void* pUnit, int code, int level, const char* pszText) -> void { if (m_pActiveClient != nullptr) { repAlarm(m_pActiveClient, (CUnit*)pUnit, AS_SEND_ERROR_REP); } }; listener.onRemoveAlarm = [&](void* pUnit, int code, int level, const char* pszText) -> void { if (m_pActiveClient != nullptr) { repAlarm(m_pActiveClient, (CUnit*)pUnit, AS_REMOVE_ERROR_REP); } }; listener.onStepChanged = [&](void* pUnit, STEP_STATE) -> void { if (m_pActiveClient != nullptr) { repStep(m_pActiveClient, (CUnit*)pUnit, AS_SEND_STEP_REP); } }; listener.onDataChanged = [&](void* pUnit, unsigned long long time) -> void { if (m_pActiveClient != nullptr) { repData(m_pActiveClient, (CUnit*)pUnit, AS_MC_INFO_REP); } }; listener.onReqNotReady = [&](void* pUnit, int layer) -> void { if (m_pActiveClient != nullptr) { repNotReady(m_pActiveClient, (CUnit*)pUnit, layer, AS_SEND_EVENT_REP); } }; listener.onReqLoad = [&](void* pUnit, int layer) -> void { if (m_pActiveClient != nullptr) { repLoad(m_pActiveClient, (CUnit*)pUnit, layer, AS_SEND_EVENT_REP); } }; listener.onReqUnload = [&](void* pUnit, int layer) -> void { if (m_pActiveClient != nullptr) { repUnload(m_pActiveClient, (CUnit*)pUnit, layer, AS_SEND_EVENT_REP); } }; pUnit->setListener(listener); return (IUnit*)pUnit; } IUnit* CEquipment::getUnit(const char* pszName) { auto iter = m_units.find(pszName); if (iter != m_units.end()) { return (IUnit*)iter->second; } return nullptr; } int CEquipment::runOnClientMode(const char* pAddr, int port) { return 0; } int CEquipment::runOnServerMode(int port) { m_pClientListener = new AcceptClientListener; m_pClientListener->onClose = [&](void* pClient) -> int { if (m_listener.onDisconnected != nullptr) { CAcceptClient* p = (CAcceptClient*)pClient; CString strIp; UINT port; m_pActiveClient->GetPeerName(strIp, port); m_listener.onDisconnected(this, (LPTSTR)(LPCTSTR)strIp, port); } removeClient((CAcceptClient*)pClient); m_pActiveClient = nullptr; return 0; }; m_pClientListener->onRead = [&](void* pClient, const char* pData, int len) -> int { return decode((CAcceptClient*)pClient, pData, len); }; ServerListener listener; listener.onAccept = [&](void* pServer, CAcceptClient* pClient) -> int { addClient(pClient); m_pActiveClient = pClient; for (auto item : m_units) { item.second->resetLoadState(); } if (m_listener.onConnected != nullptr) { CString strIp; UINT port; m_pActiveClient->GetPeerName(strIp, port); m_listener.onConnected(this, (LPTSTR)(LPCTSTR)strIp, port); } return 0; }; listener.onClose = [&](void* pServer) -> int { return 0; }; m_pServer = new CSocketServer(port, listener); m_pServer->init(); return 0; } void CEquipment::addClient(CAcceptClient* pClient) { pClient->m_pContext1 = new CData(DATA_INIT_LEN); m_clients.push_back(pClient); pClient->setListener(*m_pClientListener); } void CEquipment::removeClient(CAcceptClient* pClient) { for (auto iter = m_clients.begin(); iter != m_clients.end(); iter++) { if (*iter == pClient) { m_recycleClients.push_back(*iter); m_clients.erase(iter); break; } } } int CEquipment::decode(CAcceptClient* pClient, const char* pData, int len) { // Ôö¼ÓÊý¾Ýµ½Î²²¿ BEQ::CData* pClsData = (BEQ::CData*) (pClient->m_pContext1); if (-1 == pClsData->append(pData, len)) { return -1; } // ÔÚδÕÒµ½@·û֮ǰ£¬ËùÓеÄ×Öĸ±ØÐëÊÇ 'A' ~ 'Z' »ò '_' int nCmdEnd = -1; BOOL bInvalid = false; for (int i = 0; i < pClsData->m_nDataLen; i++) { if ((char)'@' == pClsData->m_pBuffer[i]) { nCmdEnd = i; break; } if ( !(((char)'A' <= pClsData->m_pBuffer[i] && pClsData->m_pBuffer[i] <= (char)'Z') || (char)'_' == pClsData->m_pBuffer[i]) ) { bInvalid = true; break; } } if (bInvalid) { pClsData->clear(); return -1; } if (nCmdEnd <= 0) { return -2; } // ²éÕÒÖÕÖ¹·û int nPacketEnd = -1; bInvalid = false; for (int i = nCmdEnd; i < pClsData->m_nDataLen; i++) { if ((char)'#' == pClsData->m_pBuffer[i]) { nPacketEnd = i; break; } if ( !((char)32 <= pClsData->m_pBuffer[i] && pClsData->m_pBuffer[i] <= (char)127) ) { bInvalid = true; break; } } if (bInvalid) { pClsData->clear(); return -3; } if (nPacketEnd <= nCmdEnd) { return -4; } // µÃµ½ÃüÁÏûÏ¢Ìå // ÏûÏ¢Ìå·Ö½âΪ²ÎÊýÁбí CString strCmd = CString(pClsData->m_pBuffer, nCmdEnd); CString strBody = CString(&pClsData->m_pBuffer[nCmdEnd+1], nPacketEnd - nCmdEnd - 1); int iParam = 0; CString strParam; std::map params; do { if (!AfxExtractSubString(strParam, (LPCTSTR)strBody, iParam, '/')) break; int n = strParam.Find("="); if (n > 0) { std::string strName = (LPTSTR)(LPCTSTR)strParam.Left(n); std::string strValue = (LPTSTR)(LPCTSTR)strParam.Right(strParam.GetLength() - n - 1); params[strName] = strValue; } iParam++; } while (true); // ´¦ÀíÏûÏ¢ºó¸´Î» int nRet = executeCommand(pClient, strCmd, params); pClsData->clear(); return nRet; } int CEquipment::executeCommand(CAcceptClient* pClient, CString& strCmd, std::map& params) { // »ñÈ¡»úÆ÷Ãû CString strResponse; if (strCmd.Compare(CMD_GET_EQID_REQ) == 0) { makeResponse(CMD_GET_EQID_REP, PARAM_EQID, m_strName.c_str(), strResponse); pClient->Send((LPTSTR)(LPCTSTR)strResponse, strResponse.GetLength()); return ERR_NOERR; } // ÒÔÏÂÃüÁîÐèÒªÑéÖ¤»úÆ÷ID,²»Ò»Ö²»»Ø¸´ auto iter = params.find(PARAM_EQID); if (iter == params.end()) return -101; if (iter->second.compare(m_strName) != 0) { return ERR_MISMATCH; } // »ñÈ¡³ÌÐò°æ±¾ if (strCmd.Compare(CMD_GET_VERSION_REQ) == 0) { makeResponseEx(CMD_GET_VERSION_REP, PARAM_VERSION, m_strVersion.c_str(), strResponse); pClient->Send((LPTSTR)(LPCTSTR)strResponse, strResponse.GetLength()); return ERR_NOERR; } // È¡³ö²ÎÊýÖеĵ¥Ôª£¬Ä¬ÈÏΪ0 CUnit* pUnit = nullptr; std::string strUnit = "1"; auto iterUnit = params.find("UNIT"); if (iterUnit != params.end()) { strUnit = iterUnit->second; } // »ñÈ¡»úÆ÷״̬ if (strCmd.Compare(CMD_GET_STATE_REQ) == 0) { pUnit = checkUnitAndReply(strUnit, pClient, CMD_GET_STATE_REP); if (pUnit == nullptr) { return ERR_NO_UNIT; } repState(m_pActiveClient, (CUnit*)pUnit, CMD_GET_STATE_REP); return ERR_NOERR; } // »ñÈ¡°²È«ÃÅ״̬ if (strCmd.Compare(CMD_GET_DOOR_REQ) == 0) { pUnit = checkUnitAndReply(strUnit, pClient, CMD_GET_DOOR_REP); if (pUnit == nullptr) { return ERR_NO_UNIT; } repDoorState(pClient, pUnit, CMD_GET_DOOR_REP); return ERR_NOERR; } // »ñÈ¡µ±Ç°¸æ¾¯ÐÅÏ¢ if (strCmd.Compare(CMD_GET_ERROR_REQ) == 0) { pUnit = checkUnitAndReply(strUnit, pClient, CMD_GET_ERROR_REP); if (pUnit == nullptr) { return ERR_NO_UNIT; } repAlarm(pClient, pUnit, CMD_GET_ERROR_REP); return ERR_NOERR; } // »ñÈ¡µ±Ç°ÖÆ³Ì if (strCmd.Compare(CMD_GET_STEP_REQ) == 0) { pUnit = checkUnitAndReply(strUnit, pClient, CMD_GET_STEP_REP); if (pUnit == nullptr) { return ERR_NO_UNIT; } repStep(pClient, pUnit, CMD_GET_STEP_REP); return ERR_NOERR; } // »ñÈ¡Êý¾Ý if (strCmd.Compare(CMD_GET_DATA_REQ) == 0) { pUnit = checkUnitAndReply(strUnit, pClient, CMD_GET_DATA_REP); if (pUnit == nullptr) { return ERR_NO_UNIT; } repData(pClient, pUnit, CMD_GET_DATA_REP); return ERR_NOERR; } // »ñÈ¡Åä·½Áбí if (strCmd.Compare(CMD_GET_RECIPE_LIST_REQ) == 0) { pUnit = checkUnitAndReply(strUnit, pClient, CMD_GET_RECIPE_LIST_REP); if (pUnit == nullptr) { return ERR_NO_UNIT; } repRecipeList(pClient, pUnit, CMD_GET_RECIPE_LIST_REP); return ERR_NOERR; } // Ö´ÐÐÅä·½ if (strCmd.Compare(CMD_RUN_RECIPE_REQ) == 0) { pUnit = checkUnitAndReply(strUnit, pClient, CMD_RUN_RECIPE_REP); if (pUnit == nullptr) { return ERR_NO_UNIT; } auto iterId = params.find(PARAM_RECIPEID); auto iterName = params.find(PARAM_RECIPENAME); if (iterId == params.end() || iterName == params.end()) { makeFailResponse(CMD_RUN_RECIPE_REP, strUnit.c_str(), "ȱÉÙ²ÎÊý", strResponse); pClient->Send((LPTSTR)(LPCTSTR)strResponse, strResponse.GetLength()); return ERR_PARAM_ERROE; } repRunRecipe(pClient, pUnit, atoi(iterId->second.c_str()), iterName->second.c_str(), CMD_RUN_RECIPE_REP); return ERR_NOERR; } // ÉÏÁϾÍÐ÷ if (strCmd.Compare(CMD_LOAD_READY_REQ) == 0) { int layer = 1; pUnit = checkUnitAndReplyEx(strUnit, layer, pClient, CMD_LOAD_READY_REP); if (pUnit == nullptr) { return ERR_NO_UNIT; } auto iterMeterialId = params.find(PARAM_MATERIAL_ID); if (iterMeterialId == params.end()) { iterMeterialId = params.find(PARAM_MATERIEL_ID); } if (iterMeterialId == params.end()) { makeFailResponse(CMD_LOAD_READY_REP, strUnit.c_str(), "ȱÉÙ²ÎÊý\"MATERIAL_ID\"", strResponse); pClient->Send((LPTSTR)(LPCTSTR)strResponse, strResponse.GetLength()); return ERR_PARAM_ERROE; } auto iterRecipeId = params.find(PARAM_RECIPEID); if (iterRecipeId == params.end()) { makeFailResponse(CMD_LOAD_READY_REP, strUnit.c_str(), "ȱÉÙ²ÎÊý\"RECIPEID\"", strResponse); pClient->Send((LPTSTR)(LPCTSTR)strResponse, strResponse.GetLength()); return ERR_PARAM_ERROE; } repLoadReady(pClient, pUnit, strUnit.c_str(), iterMeterialId->second.c_str(), iterRecipeId->second.c_str(), CMD_LOAD_READY_REP); return ERR_NOERR; } // ÉÏÁÏÍê³É if (strCmd.Compare(CMD_LOAD_COMPLETE_REQ) == 0) { int layer = 1; pUnit = checkUnitAndReplyEx(strUnit, layer, pClient, CMD_LOAD_COMPLETE_REP); if (pUnit == nullptr) { return ERR_NO_UNIT; } repLoadComplete(pClient, pUnit, strUnit.c_str(), layer, CMD_LOAD_COMPLETE_REP); return ERR_NOERR; } // ÏÂÁÏÍê³É if (strCmd.Compare(CMD_UNLOAD_COMPLETE_REQ) == 0) { int layer = 1; pUnit = checkUnitAndReplyEx(strUnit, layer, pClient, CMD_UNLOAD_COMPLETE_REP); if (pUnit == nullptr) { return ERR_NO_UNIT; } repUnloadComplete(pClient, pUnit, strUnit.c_str(), layer, CMD_UNLOAD_COMPLETE_REP); return ERR_NOERR; } return ERR_UNKNOWN_CMD; } const char* CEquipment::getReply(const char* pszReply) { if (strcmp(pszReply, CMD_GET_EQID_REQ) == 0) { return CMD_GET_EQID_REP; } if (strcmp(pszReply, CMD_GET_VERSION_REQ) == 0) { return CMD_GET_VERSION_REP; } if (strcmp(pszReply, CMD_GET_STATE_REQ) == 0) { return CMD_GET_STATE_REP; } return ""; } CUnit* CEquipment::checkUnitAndReply(std::string& strUnit, CAcceptClient* pClient, const char* pszReply) { auto iter = m_units.find(strUnit); if (iter != m_units.end()) return iter->second; CString strResponse; makeFailResponse(pszReply, strUnit.c_str(), "µ¥Ôª²»´æÔÚ", strResponse); pClient->Send((LPTSTR)(LPCTSTR)strResponse, strResponse.GetLength()); return nullptr; } CUnit* CEquipment::checkUnitAndReplyEx(std::string& strUnit, int& layer, CAcceptClient* pClient, const char* pszReply) { // ²éÕÒµ½Êý×Ö£¬È¡³ö²ã std::string strUnit2, strLayer; BOOL bLeft = TRUE; for (int i = 0; i < strUnit.length(); i++) { char c = strUnit.at(i); if (0x30 <= c && c <= 0x39) { bLeft = FALSE; } if (bLeft) strUnit2.push_back(c); else strLayer.push_back(c); } layer = strLayer.empty() ? 1 : atoi(strLayer.c_str()); auto iter = m_units.find(strUnit2); if (iter != m_units.end()) return iter->second; CString strResponse; makeFailResponse(pszReply, strUnit.c_str(), "µ¥Ôª²»´æÔÚ", strResponse); pClient->Send((LPTSTR)(LPCTSTR)strResponse, strResponse.GetLength()); return nullptr; } void CEquipment::makeResponse(const char* pszReply, const char* pszParam, const char* pValue, CString& strResponse) { strResponse.Format(_T("%s@%s=%s#"), pszReply, pszParam, pValue); } void CEquipment::makeResponseEx(const char* pszReply, const char* pszParam, const char* pValue, CString& strResponse) { strResponse.Format(_T("%s@%s=%s/%s=%s#"), pszReply, PARAM_EQID, m_strName.c_str(), pszParam, pValue); } void CEquipment::makeResponseEx(const char* pszReply, const char* pszUnit, const char* pszParam, const char* pValue, CString& strResponse) { strResponse.Format(_T("%s@%s=%s/%s=%s/%s=%s#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pszUnit, pszParam, pValue); } void CEquipment::makeFailResponse(const char* pszReply, const char* pszUnit, const char* pszText, CString& strResponse) { strResponse.Format(_T("%s@%s=%s/%s=%s/RESULT=FAIL/TEXT=%s#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pszUnit, pszText); } void CEquipment::repState(CAcceptClient* pClient, CUnit* pUnit, const char* pszReply) { ASSERT(pClient != nullptr); CString strReply; strReply.Format(_T("%s@%s=%s/%s=%s/%s=%s#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str(), PARAM_STATE, pUnit->getStateText()); pClient->Send((LPTSTR)(LPCTSTR)strReply, strReply.GetLength()); } void CEquipment::repDoorState(CAcceptClient* pClient, CUnit* pUnit, const char* pszReply) { ASSERT(pClient != nullptr); std::string strDoorState; CString strReply; strReply.Format(_T("%s@%s=%s/%s=%s/%s=%s#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str(), PARAM_DOOR, pUnit->getDoorState(strDoorState).c_str()); pClient->Send((LPTSTR)(LPCTSTR)strReply, strReply.GetLength()); } void CEquipment::repAlarm(CAcceptClient* pClient, CUnit* pUnit, const char* pszReply) { ASSERT(pClient != nullptr); CString strReply; int code = pUnit->getAlarmCode(); if (code == 0) { strReply.Format(_T("%s@%s=%s/%s=%s/%s=%d#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str(), PARAM_CODE, pUnit->getAlarmCode()); } else { strReply.Format(_T("%s@%s=%s/%s=%s/%s=%d,%d,%s#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str(), PARAM_CODE, pUnit->getAlarmCode(), pUnit->getAlarmLevel(), pUnit->getAlarmText().c_str()); } pClient->Send((LPTSTR)(LPCTSTR)strReply, strReply.GetLength()); } void CEquipment::repStep(CAcceptClient* pClient, CUnit* pUnit, const char* pszReply) { ASSERT(pClient != nullptr); CString strReply; STEP_STATE stepState = pUnit->getStepState(); if (stepState == STEP_STATE::IDLE) { // idleÖ»ÓÐ״̬ strReply.Format(_T("%s@%s=%s/%s=%s/%s=%s#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str(), PARAM_STEP, pUnit->getStepStateText()); } else if (stepState == STEP_STATE::PROCESSING) { // step, id, svtime, pvtime strReply.Format(_T("%s@%s=%s/%s=%s/%s=%s/%s=%s/SVTIME=%d/PVTIME=%d#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str(), PARAM_STEP, pUnit->getStepStateText(), PARAM_MATERIAL_ID, pUnit->getStepMaterialId().c_str(), pUnit->getStepSvTime(), pUnit->getSetpPvTime()); } else { // step, id strReply.Format(_T("%s@%s=%s/%s=%s/%s=%s/%s=%s#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str(), PARAM_STEP, pUnit->getStepStateText(), PARAM_MATERIAL_ID, pUnit->getStepMaterialId().c_str()); } pClient->Send((LPTSTR)(LPCTSTR)strReply, strReply.GetLength()); } void CEquipment::repData(CAcceptClient* pClient, CUnit* pUnit, const char* pszReply) { ASSERT(pClient != nullptr); CString strReply; unsigned long long time = pUnit->getDataTime(); if (time == 0) { // ûÓÐÊý¾Ý strReply.Format(_T("%s@%s=%s/%s=%s/RESULT=FAIL/TEX=NO DATA#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str()); } else { strReply.Format(_T("%s@%s=%s/%s=%s/%s=%s"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str(), PARAM_TIME, timeToString(pUnit->getDataTime()).c_str()); std::map& datas = pUnit->getDatas(); for (auto& item : datas) { strReply.AppendFormat("/%s=%s", item.first.c_str(), item.second.c_str()); } strReply.Append("#"); } pClient->Send((LPTSTR)(LPCTSTR)strReply, strReply.GetLength()); } void CEquipment::repRecipeList(CAcceptClient* pClient, CUnit* pUnit, const char* pszReply) { ASSERT(pClient != nullptr); CString strReply; if (m_listener.onGetRecipeList != nullptr) { int nRet = m_listener.onGetRecipeList(this, pUnit); if (nRet == 1) { // ±£´æÊý¾Ý£¬µÈ´ýÉϲãÍê³É m_pReqRecipeListClient = pClient; m_pReqRecipeListUnit = pUnit; m_strReqRecipeListReply = pszReply; } else { std::map& recipes = pUnit->getRecipes(); if (recipes.empty()) { // ûÓÐÅä·½ strReply.Format(_T("%s@%s=%s/%s=%s/RESULT=FAIL/TEX=NO RECIPE#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str()); } else { strReply.Format(_T("%s@%s=%s/%s=%s/RECIPE_LIST="), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str()); for (auto& item : recipes) { strReply.AppendFormat("%d,%s,", item.first, item.second.c_str()); } strReply.SetAt(strReply.GetLength() - 1, '#'); } pClient->Send((LPTSTR)(LPCTSTR)strReply, strReply.GetLength()); } } } int CEquipment::repRecipeListComplete() { if (m_pReqRecipeListClient == nullptr) return -1; if (m_pReqRecipeListUnit == nullptr) return -1; CString strReply; std::map& recipes = m_pReqRecipeListUnit->getRecipes(); if (recipes.empty()) { // ûÓÐÅä·½ strReply.Format(_T("%s@%s=%s/%s=%s/RESULT=FAIL/TEX=NO RECIPE#"), m_strReqRecipeListReply.c_str(), PARAM_EQID, m_strName.c_str(), PARAM_UNIT, m_pReqRecipeListUnit->getName().c_str()); } else { strReply.Format(_T("%s@%s=%s/%s=%s/RECIPE_LIST="), m_strReqRecipeListReply.c_str(), PARAM_EQID, m_strName.c_str(), PARAM_UNIT, m_pReqRecipeListUnit->getName().c_str()); for (auto& item : recipes) { strReply.AppendFormat("%d,%s,", item.first, item.second.c_str()); } strReply.SetAt(strReply.GetLength() - 1, '#'); } m_pReqRecipeListClient->Send((LPTSTR)(LPCTSTR)strReply, strReply.GetLength()); return 0; } void CEquipment::repRunRecipe(CAcceptClient* pClient, CUnit* pUnit, int id, const char* pszName, const char* pszReply) { ASSERT(pClient != nullptr); CString strReply; if (!pUnit->checkRecipe(id, pszName)) { // ûÓÐÅä·½ strReply.Format(_T("%s@%s=%s/%s=%s/RESULT=FAIL/TEX=RECIPE'S ID AND NAME DO NOT MATCH#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str()); } else { // Ä£ÄâÇл»Åä·½ if (m_listener.onRunRecipe != nullptr) { int ret = m_listener.onRunRecipe(this, pUnit, id); if (ret >= 0) { strReply.Format(_T("%s@%s=%s/%s=%s/RESULT=PASS#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str()); } else { strReply.Format(_T("%s@%s=%s/%s=%s/RESULT=FAIL/TEX=ERROR%d#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str(), ret); } } } pClient->Send((LPTSTR)(LPCTSTR)strReply, strReply.GetLength()); } void CEquipment::repLoadReady(CAcceptClient* pClient, CUnit* pUnit, const char* pszUnitName, const char* pszMaterielId, const char* pszRecipeId, const char* pszReply) { ASSERT(pClient != nullptr); CString strReply; if (m_listener.onRunRecipe != nullptr) { int ret = m_listener.onLoadReady(this, pUnit, pszMaterielId, pszRecipeId); if (ret == 1) { // ±£´æÊý¾Ý£¬µÈ´ýÉϲãÍê³É m_pRunRecipeClient = pClient; m_pRunRecipeUnit = pUnit; m_strRunRecipeReply = pszReply; } else if (ret >= 0) { strReply.Format(_T("%s@%s=%s/%s=%s/RESULT=PASS#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pszUnitName); } else { strReply.Format(_T("%s@%s=%s/%s=%s/RESULT=FAIL/TEX=ERROR%d#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pszUnitName, ret); } } pClient->Send((LPTSTR)(LPCTSTR)strReply, strReply.GetLength()); } int CEquipment::repLoadReadyComplete(int errorCode) { if (m_pRunRecipeClient == nullptr) return -1; if (m_pRunRecipeUnit == nullptr) return -1; CString strReply; if (errorCode == 0) { strReply.Format(_T("%s@%s=%s/%s=%s/RESULT=PASS#"), m_strRunRecipeReply.c_str(), PARAM_EQID, m_strName.c_str(), PARAM_UNIT, m_pRunRecipeUnit->getName().c_str()); } else { strReply.Format(_T("%s@%s=%s/%s=%s/RESULT=FAIL/TEX=ERROR%d#"), m_strRunRecipeReply.c_str(), PARAM_EQID, m_strName.c_str(), PARAM_UNIT, m_pRunRecipeUnit->getName().c_str(), errorCode); } m_pRunRecipeClient->Send((LPTSTR)(LPCTSTR)strReply, strReply.GetLength()); return 0; } void CEquipment::repNotReady(CAcceptClient* pClient, CUnit* pUnit, int layer, const char* pszReply) { ASSERT(pClient != nullptr); CString strReply; if (pUnit->getLayerCount() == 1) { strReply.Format(_T("%s@%s=%s/%s=%s/%s=%s#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str(), PARAM_EVENT, EVENT_NOTREADY); } else { strReply.Format(_T("%s@%s=%s/%s=%s%d/%s=%s#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str(), layer, PARAM_EVENT, EVENT_NOTREADY); } pClient->Send((LPTSTR)(LPCTSTR)strReply, strReply.GetLength()); } void CEquipment::repLoad(CAcceptClient* pClient, CUnit* pUnit, int layer, const char* pszReply) { ASSERT(pClient != nullptr); CString strReply; if (pUnit->getLayerCount() == 1) { strReply.Format(_T("%s@%s=%s/%s=%s/%s=%s#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str(), PARAM_EVENT, EVENT_LOAD); } else { strReply.Format(_T("%s@%s=%s/%s=%s%d/%s=%s#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str(), layer, PARAM_EVENT, EVENT_LOAD); } pClient->Send((LPTSTR)(LPCTSTR)strReply, strReply.GetLength()); } void CEquipment::repUnload(CAcceptClient* pClient, CUnit* pUnit, int layer, const char* pszReply) { ASSERT(pClient != nullptr); CString strReply; if (pUnit->getLayerCount() == 1) { strReply.Format(_T("%s@%s=%s/%s=%s/%s=%s#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str(), PARAM_EVENT, EVENT_UNLOAD); } else { strReply.Format(_T("%s@%s=%s/%s=%s%d/%s=%s#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pUnit->getName().c_str(), layer, PARAM_EVENT, EVENT_UNLOAD); } pClient->Send((LPTSTR)(LPCTSTR)strReply, strReply.GetLength()); } void CEquipment::repLoadComplete(CAcceptClient* pClient, CUnit* pUnit, const char* pszUnitName, int layer, const char* pszReply) { ASSERT(pClient != nullptr); CString strReply; if (m_listener.onLoadComplete != nullptr) { int ret = m_listener.onLoadComplete(this, pUnit, layer); if (ret >= 0) { strReply.Format(_T("%s@%s=%s/%s=%s/RESULT=PASS#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pszUnitName); } else { strReply.Format(_T("%s@%s=%s/%s=%s/RESULT=FAIL/TEX=ERROR%d#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pszUnitName, ret); } } pClient->Send((LPTSTR)(LPCTSTR)strReply, strReply.GetLength()); } void CEquipment::repUnloadComplete(CAcceptClient* pClient, CUnit* pUnit, const char* pszUnitName, int layer, const char* pszReply) { ASSERT(pClient != nullptr); CString strReply; if (m_listener.onUnloadComplete != nullptr) { int ret = m_listener.onUnloadComplete(this, pUnit, layer); if (ret >= 0) { strReply.Format(_T("%s@%s=%s/%s=%s/RESULT=PASS#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pszUnitName); } else { strReply.Format(_T("%s@%s=%s/%s=%s/RESULT=FAIL/TEX=ERROR%d#"), pszReply, PARAM_EQID, m_strName.c_str(), PARAM_UNIT, pszUnitName, ret); } } pClient->Send((LPTSTR)(LPCTSTR)strReply, strReply.GetLength()); } std::string CEquipment::timeToString(ULONGLONG time) { ULONGLONG time1; time1 = time / 1000; char buffer[256]; struct tm timeinfo; time_t t = time_t(time1); localtime_s(&timeinfo, &t); strftime(buffer, 128, "%Y-%m-%d %H:%M:%S", &timeinfo); return std::string(buffer); } }