LAPTOP-SNT8I5JK\Boounion
2025-03-28 8322172ad4e6d5636b8c32b94d95f8abbce1ed54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// BEQMain.cpp : ¶¨Òå DLL µÄ³õʼ»¯Àý³Ì¡£
//
 
 
#include "stdafx.h"
#include "BEQCommon.h"
#include "Servo.h"
#include "Equipment.h"
#include <map>
#include <string>
 
 
CString            g_strLogDir;                // ÈÕ־Ŀ¼
CStdioFile        g_logFile;                    // ÈÕÖ¾Îļþ
int                g_nActionTimeout = 5000;    // ³¬Ê±Ê±¼ä        
 
std::map<std::string, BEQ::CServo*>        g_mapServos;        // ServoÓ³Éí±í
std::map<std::string, BEQ::CEquipment*>    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;
}