You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TwoLaserHead-PushJig/LaiPuLaser/ClientMgr.cpp

393 lines
7.5 KiB
C++

// CientMgr.cpp : ʵ<><CAB5><EFBFBD>ļ<EFBFBD>
//
#include "stdafx.h"
#include "ClientMgr.h"
#include "Program_SZ_XL.h"
#include "CameraHawkvis.h"
#include "FileMgr.h"
#include "LogMgr.h"
#include "ObjComponentMgr.h"
// CCientMgr
CClientMgr * gClientMgr = new CClientMgr;
CClientMgr::CClientMgr()
{
}
CClientMgr::~CClientMgr()
{
DisConnectServer();
}
bool CClientMgr::Connect2Server()
{
if (m_bDisableServer)//<2F><><EFBFBD>θ<EFBFBD><CEB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӳ<EFBFBD><D3B2><EFBFBD>ָ<EFBFBD><D6B8>
{
return false;
}
m_ClientIP = gProgram_SZ_XL->m_ServerIP;
m_ClientPort = gProgram_SZ_XL->m_ServerPort;
if (m_hSocket == INVALID_SOCKET)
{
if (!Socket())
{
AfxMessageBox("Socket<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ʧ<EFBFBD>ܡ<EFBFBD>");
return false;
}
}
if (!m_bOnline)
{
if (!Connect(m_ClientIP, m_ClientPort))
{
return false;
}
m_bOnline = true;
}
return true;
}
void CClientMgr::DisConnectServer()
{
if (m_hSocket!=INVALID_SOCKET)
{
Close();
m_bOnline = false;
}
}
void CClientMgr::SendCmd_TransFile(CString filePath)
{
int sPos = filePath.ReverseFind('\\');
int ePos1 = filePath.ReverseFind('.');
int ePos2 = filePath.GetLength();
CString fileName = filePath.Mid(sPos, ePos1 - sPos);
CString fileType = filePath.Mid(ePos1, ePos2 - ePos1); //.DXF OR .MAK
CFileMgr fg;
CString parFileDir = fg.GetWorkPath() + "\\Cutpar";
CString parFile = parFileDir + fileName + ".par";
CString areFile = parFileDir + fileName + ".are";
CString PIFileDir = fg.GetWorkPath() + "\\WorkPar";
CString PIFile = PIFileDir + fileName + ".txt";
CString DestFolder = fg.GetWorkPath() + "\\TempMarkData";
if (!PathIsDirectory(DestFolder))//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򴴽<EFBFBD><F2B4B4BD>ļ<EFBFBD><C4BC><EFBFBD>
{
CreateDirectory(DestFolder, 0);
}
CString NewFilePath = DestFolder + fileName + fileType;
CString NewParFilePath = DestFolder + fileName + ".par";
CString NewAreFilePath = DestFolder + fileName + ".are";
CString NewPIFilePath = DestFolder + fileName + ".txt";
CopyFile(filePath, NewFilePath, false);
CopyFile(parFile, NewParFilePath, false);
CopyFile(areFile, NewAreFilePath, false);
CopyFile(PIFile, NewPIFilePath, false);
}
void CClientMgr::SendCmd_OpenFile(CString filePath)
{
SendCmd_TransFile(filePath);
Sleep(100);
if (Connect2Server())
{
CString str = "OPEN_FILE;" + filePath;
Send(str, str.GetLength());
}
}
void CClientMgr::SendCmd_CollectData()
{
if (Connect2Server())
{
bool b = gProgram_SZ_XL->IsbSelMarkMode();
gClientMgr->SendCmd_bMarkSelMode(b);
if (b)
{
gClientMgr->SendCmd_SelObj();
Sleep(200);
}
CString str = ";COLLECT_DATA;";
Send(str, str.GetLength());
}
}
void CClientMgr::SendCmd_ProductInfo(CProduct product)
{
if (Connect2Server())
{
m_bServerReady = false;
CString str = ACTION_TANSPRODUCTINFO;
if (gCameraHawkvis->GetCamIndex() == 0)
{
str += TRACK_1;
}
if (gCameraHawkvis->GetCamIndex() == 1)
{
str += TRACK_2;
}
CString temp;
temp.Format("[%.6f][%.6f][%.6f][%.6f][%.6f][%.6f]", product.m_p00, product.m_p01, product.m_p02, \
product.m_p10, product.m_p11, product.m_p12);
str += temp;
Send(str, str.GetLength());
//m_bServerMarkReady = false;
gLogMgr->WriteDebugLog("Send: "+str);
/*Send(str, str.GetLength());
Sleep(50);
Send(&product, sizeof(product));*/
}
}
void CClientMgr::SendCmd_SetSpecialObj()
{
if (Connect2Server())
{
int SelObjCnt = m_SelObjIndexVec.size();
if (SelObjCnt > 0)
{
CString str;
str.Format("%s%d", ACTION_SETSPECAILOBJ, SelObjCnt);
gLogMgr->WriteDebugLog(str);
Send(str, str.GetLength());
Sleep(50);
int* IdxArry = new int[SelObjCnt]();
for (int i = 0; i < SelObjCnt; i++)
{
IdxArry[i] = m_SelObjIndexVec[i];
}
Send(IdxArry, SelObjCnt*(sizeof(int)));
delete[]IdxArry;
IdxArry = NULL;
}
}
}
void CClientMgr::SendCmd_SelObj()
{
if (Connect2Server())
{
auto & ComponentVec = gObjComponentMgr->GetComponentVec();
auto & SelIndexVec = gClientMgr->m_SelObjIndexVec;
SelIndexVec.clear();
int cnt = ComponentVec.size();
if (cnt == 0)//û<>д<EFBFBD><D0B4><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD>
return;
for (int idx = 0; idx < cnt; idx++)
{
if ((ComponentVec[idx]).IsSelected())
{
SelIndexVec.push_back(idx);
}
}
int SelObjCnt = m_SelObjIndexVec.size();
CString str;
str.Format("%s%d", ACTION_SELECTOBJ, SelObjCnt);
gLogMgr->WriteDebugLog(str);
Send(str, str.GetLength());
if (SelObjCnt == 0)//<2F><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٷ<EFBFBD><D9B7><EFBFBD><EFBFBD><EFBFBD>
return;
int* IdxArry = new int[SelObjCnt]();
for (int i = 0; i < SelObjCnt; i++)
{
IdxArry[i] = m_SelObjIndexVec[i];
}
Send(IdxArry, SelObjCnt*(sizeof(int)));
delete[]IdxArry;
IdxArry = NULL;
}
}
void CClientMgr::SendCmd_bMarkSelMode(int bMarkSel)
{
if (Connect2Server())
{
CString str;
str.Format("%s%d", ACTION_BMARKSELOBJMODE, bMarkSel);
Send(str, str.GetLength());
}
}
void CClientMgr::SendCmd_StartLaserMark(int AreaIndex)
{
if (Connect2Server())
{
int cnt = 0;
while (!m_bServerReady&&cnt<100)
{
Sleep(200);
cnt++;
}
if (cnt==15)
{
AfxMessageBox("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>׼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD>ʱ!");
return;
}
CString str;
str.Format("%s%d", ACTION_STARTWORK, AreaIndex);
Send(str, str.GetLength());
m_bAreaMarkDone = false;
}
}
// CCientMgr <20><>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD>
int CClientMgr::Send(const void * lpBuf, int nBufLen, int nFlags)
{
if (!CheckServerDoneCmd())
{
return -1;
}
int sendcnt = CSocket::Send(lpBuf, nBufLen, nFlags);
m_bLastCmdFinished = false;
return sendcnt;
}
void CClientMgr::OnReceive(int nErrorCode)
{
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>ר<EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>
if (0 == nErrorCode)
{
char buf[512] = { 0 };
int nRcved = Receive(buf, sizeof(buf));
if (SOCKET_ERROR != nRcved)
{
CString RcvStr = (CString)buf;
if (RcvStr.Find("OK") != -1)
{
m_bLastCmdFinished = true;
}
if (RcvStr.Find("AREA_FINISHED") != -1)
{
m_bAreaMarkDone = true;
}
if (RcvStr.Find("READY") != -1)
{
m_bServerReady = true;
}
}
}
CSocket::OnReceive(nErrorCode);
}
void CClientMgr::OnClose(int nErrorCode)
{
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>ר<EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>
Close();
m_bOnline = false;
m_bLastCmdFinished = true;
m_bAreaMarkDone = true;
AfxMessageBox("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѶϿ<EFBFBD>!");
CSocket::OnClose(nErrorCode);
}
void CClientMgr::OnConnect(int nErrorCode)
{
// TODO: <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>ר<EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>
if (nErrorCode == 0)
{
m_bOnline = true;
}
CSocket::OnConnect(nErrorCode);
}
BOOL CClientMgr::Connect(LPCTSTR lpszHostAddress, UINT nHostPort)
{
SetTimeOut();
auto ret = CSocket::Connect(lpszHostAddress, nHostPort);
KillTimeOut();
return ret;
}
BOOL CClientMgr::SetTimeOut(UINT uTimeOut)
{
LARGE_INTEGER llCnt;
::QueryPerformanceCounter(&llCnt);
m_llDtStart = llCnt.QuadPart;
m_uTimeOut = uTimeOut;
return TRUE;
}
BOOL CClientMgr::KillTimeOut()
{
m_llDtStart = 0;//<2F><><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ʱ
return TRUE;
}
BOOL CClientMgr::OnMessagePending()
{
if (m_llDtStart)
{
LARGE_INTEGER lldtEnd;
::QueryPerformanceCounter(&lldtEnd);
LARGE_INTEGER llFrq;
::QueryPerformanceFrequency(&llFrq);
double dbDealy = (double)(lldtEnd.QuadPart - m_llDtStart) * 1000 / llFrq.QuadPart;
if (dbDealy > m_uTimeOut)
{
CancelBlockingCall();//<2F>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD>
AfxMessageBox("<EFBFBD><EFBFBD><EFBFBD>糬ʱ!");
return FALSE; // No need for idle time processing.
}
}
return CSocket::OnMessagePending();
}
bool CClientMgr::CheckServerDoneCmd()
{
int waitCnt = 0;
while ((!m_bLastCmdFinished)||(!m_bAreaMarkDone) && waitCnt < 12000)
{
DoEvents();
Sleep(10);
waitCnt++;
}
if (waitCnt == 12000)
{
return false;
}
return true;
}
void CClientMgr::DoEvents()
{
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9BFB4>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD> //<2F><><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
TranslateMessage(&msg); //<2F><><EFBFBD><EFBFBD>Ϣʱ<CFA2><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣת<CFA2><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>Ϣ
DispatchMessage(&msg);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD>ڳ<EFBFBD><DAB3><EFBFBD>
}
}