// BEQMain.cpp : ¶¨Òå DLL µÄ³õʼ»¯Àý³Ì¡£ // #include "stdafx.h" #include "BEQCommon.h" #include "Servo.h" #include "Equipment.h" #include #include CString g_strLogDir; // ÈÕ־Ŀ¼ CStdioFile g_logFile; // ÈÕÖ¾Îļþ int g_nActionTimeout = 5000; // ³¬Ê±Ê±¼ä std::map g_mapServos; // ServoÓ³Éí±í std::map g_mapEquipments; // EquipmentÓ³Éí±í CString& GetCurTime(CString& strTime) { _SYSTEMTIME sysTime; GetLocalTime(&sysTime); strTime.Format(_T("%d/%02d/%02d %02d:%02d:%02d.%03d"), sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds); return strTime; } void log(int level, const char* pszMessage) { if (g_logFile.m_hFile != CFile::hFileNull) { CTime time = CTime::GetCurrentTime(); CString strTime, strText; strText.Format(_T("%s > %s\n"), GetCurTime(strTime), pszMessage); g_logFile.WriteString(strText); // Îļþ³¬¹ý50M£¬ÖØÐ´´½¨ if (g_logFile.GetLength() > 50 * 1024 * 1024) { g_logFile.Close(); CString strFilepath; strFilepath.Format(_T("%s\\%d_%02d_%02d_%02d_%02d_%02d.log"), g_strLogDir, time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute(), time.GetSecond()); if (g_logFile.Open(strFilepath, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::shareDenyWrite)) { g_logFile.SeekToEnd(); } } } } // // ³õʼ»¯ÍøÂç // extern "C" __declspec(dllexport) int BEQ_Initialize() { // ³õʼ»¯ÍøÂç WSADATA wsaData; WORD version = MAKEWORD(2, 2); int ret = WSAStartup(version, &wsaData); if (ret != 0) { log(LERROR, "³õʼ»¯ÍøÂçʧ°Ü¡£"); return -1; } log(LDEBUG, "³õʼ»¯ÍøÂç³É¹¦ ¡£"); // ±¾³ÌÐòÎļþĿ¼ TCHAR sDrive[_MAX_DRIVE]; TCHAR sDir[_MAX_DIR]; TCHAR sFilename[_MAX_FNAME], sAppFilename[_MAX_FNAME]; TCHAR sExt[_MAX_EXT]; GetModuleFileName((HMODULE)&__ImageBase, sAppFilename, _MAX_FNAME); _tsplitpath_s(sAppFilename, sDrive, sDir, sFilename, sExt); g_strLogDir = CString(sDrive) + CString(sDir) + CString("\\BEQLog"); ::CreateDirectory(g_strLogDir, NULL); CTime time = CTime::GetCurrentTime(); CString strFilepath; strFilepath.Format(_T("%s\\%d_%02d_%02d_%02d_%02d_%02d.log"), g_strLogDir, time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute(), time.GetSecond()); if (!g_logFile.Open(strFilepath, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::shareDenyWrite)) { return -2; } g_logFile.SeekToEnd(); log(0, "BEQ_Initialize Íê³É."); return 0; } // // ÖÕÖ¹²¢ÇåÀí // extern "C" __declspec(dllexport) int BEQ_Term() { // ¹Ø±ÕºÍÊÍ·ÅËùÓÐServo, Equipment for (auto itor : g_mapServos) { BEQ::CServo *pServo = (BEQ::CServo *)itor.second; pServo->close(); delete pServo; } g_mapServos.clear(); for (auto itor : g_mapEquipments) { BEQ::CEquipment *pEquipment = (BEQ::CEquipment *)itor.second; delete pEquipment; } g_mapEquipments.clear(); // ÆäËüÇåÀí¹¤×÷ WSACleanup(); log(LDEBUG, "BEQ_Initialize."); if (g_logFile.m_hFile != CFile::hFileNull) { g_logFile.Close(); } return 0; } extern "C" __declspec(dllexport) int BEQ_CreateServo(BEQ::IServo*& pServo, const char* pszName) { // ¼ì²éÊÇ·ñÒѾ­´æÔÚ auto iter = g_mapServos.find(pszName); if (iter != g_mapServos.end()) { return -1; } // ²»´æÔÚÔò´´½¨Ð嵀 BEQ::CServo* pTemp = new BEQ::CServo(pszName); pServo = pTemp; g_mapServos[pszName] = pTemp; return 0; } extern "C" __declspec(dllexport) int BEQ_GetServo(BEQ::IServo*& pServo, const char* pszName) { // ¼ì²éÊÇ·ñÒѾ­´æÔÚ auto iter = g_mapServos.find(pszName); if (iter == g_mapServos.end()) { return -1; } pServo = (BEQ::IServo*)iter->second; return 0; } extern "C" __declspec(dllexport) int BEQ_DestroyServo(BEQ::IServo* pServo) { // ¼ì²éÊÇ·ñ´æÔÚ BOOL bFound = FALSE; for (auto iter = g_mapServos.begin(); iter != g_mapServos.end(); iter++) { if (iter->second == pServo) { delete iter->second; g_mapServos.erase(iter); bFound = TRUE; break; } } return bFound ? 0 : -1; } extern "C" __declspec(dllexport) int BEQ_CreateEquipment(BEQ::IEquipment*& pEquipment, const char* pszName) { // ¼ì²éÊÇ·ñÒѾ­´æÔÚ auto iter = g_mapEquipments.find(pszName); if (iter != g_mapEquipments.end()) { return -1; } // ²»´æÔÚÔò´´½¨Ð嵀 BEQ::CEquipment* pTemp = new BEQ::CEquipment(pszName); pEquipment = pTemp; g_mapEquipments[pszName] = pTemp; return 0; } extern "C" __declspec(dllexport) int BEQ_GetEquipment(BEQ::IEquipment*& pEquipment, const char* pszName) { // ¼ì²éÊÇ·ñÒѾ­´æÔÚ auto iter = g_mapEquipments.find(pszName); if (iter == g_mapEquipments.end()) { return -1; } pEquipment = (BEQ::IEquipment*)iter->second; return 0; } extern "C" __declspec(dllexport) int BEQ_DestroyEquipment(BEQ::IEquipment*& pEquipment) { // ¼ì²éÊÇ·ñ´æÔÚ BOOL bFound = FALSE; for (auto iter = g_mapEquipments.begin(); iter != g_mapEquipments.end(); iter++) { if (iter->second == pEquipment) { delete iter->second; g_mapEquipments.erase(iter); bFound = TRUE; break; } } return bFound ? 0 : -1; }