LAPTOP-SNT8I5JK\Boounion
2025-08-27 38f2ff1e5bc6c4434875e03b8cadc8f3f4e39bf6
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include "stdafx.h"
#include "LoadMonitor.h"
#include "Common.h"
#include "CBonder.h"
#include "Log.h"
 
#define ALARM_MAX        200
 
unsigned __stdcall LoadMonitorRunRecipeThreadFunction(LPVOID lpParam)
{
    CLoadMonitor* p = (CLoadMonitor*)lpParam;
    return p->RunRecipeThreadFunction();
}
 
CLoadMonitor::CLoadMonitor()
{
    m_nBeginAddr = 0;
    m_nLastId = 0;
    m_nLastLevel = 0;
    m_hEventRunRecipeResult = ::CreateEvent(NULL, TRUE, FALSE, NULL);
    m_nRunRecipeResult = 0;
    m_hRunRecipeWorkThreadHandle = NULL;
    m_nRunRecipeWorkThrdaddr = 0;
    m_bRunRecipeWorking = FALSE;
    m_hRunRecipeWorkEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
    m_bWaitingRecipeResult = FALSE;
}
 
 
CLoadMonitor::~CLoadMonitor()
{
    if (m_hEventRunRecipeResult != nullptr) {
        CloseHandle(m_hEventRunRecipeResult);
        m_hEventRunRecipeResult = nullptr;
    }
}
 
std::string& CLoadMonitor::getClassName()
{
    static std::string strClassName = "CLoadMonitor";
    return strClassName;
}
 
void CLoadMonitor::onRecvBroadcast(void* pSender, CIntent* pIntent)
{
 
}
 
void CLoadMonitor::onData(int id, const void* pData, int size)
{
    const char* pszData = (const char*)pData;
    if (3 != id) return;
 
 
    BOOL bPause, bReqLoad, bReqUnload;
    if (m_nIndex == 0 || m_nIndex == 1) {
        // ÈëÁÏÇëÇó
        bPause = pszData[m_nIndex * 2] & 0x04;
        if (bPause) {
            ReqNotReady(1);
        }
        else {
            bReqLoad = pszData[m_nIndex * 2] & 0x01;
            if (bReqLoad) {
                ReqLoad(1);
            }
            else {
                // ³öÁÏÇëÇó
                bReqUnload = pszData[m_nIndex * 2 + 1] & 0x01;
                if (bReqUnload) {
                    ReqUnload(1);
                }
            }
        }
    }
    else if (m_nIndex == 2) {
        // Layer1 ÈëÁÏÇëÇó
        bReqLoad = pszData[m_nIndex * 2] & 0x01;
        if (bReqLoad) {
            ReqLoad(1);
        }
        else {
            // ³öÁÏÇëÇó
            bReqUnload = pszData[m_nIndex * 2] & 0x20;
            if (bReqUnload) {
                ReqUnload(1);
            }
        }
 
        // Layer2
        bReqLoad = pszData[m_nIndex * 2] & 0x02;
        if (bReqLoad) {
            ReqLoad(2);
        }
        else {
            bReqUnload = pszData[m_nIndex * 2] & 0x40;
            if (bReqUnload) {
                ReqUnload(2);
            }
        }
 
        // Layer3
        bReqLoad = pszData[m_nIndex * 2] & 0x04;
        if (bReqLoad) {
            ReqLoad(3);
        }
        else {
            bReqUnload = pszData[m_nIndex * 2] & 0x80;
            if (bReqUnload) {
                ReqUnload(3);
            }
        }
 
        // Layer4
        bReqLoad = pszData[m_nIndex * 2] & 0x08;
        if (bReqLoad) {
            ReqLoad(4);
        }
        else {
            bReqUnload = pszData[m_nIndex * 2 + 1] & 0x01;
            if (bReqUnload) {
                ReqUnload(4);
            }
        }
    }
 
 
    // Çл»Åä·½½á¹û
    if (m_nIndex == 0 || m_nIndex == 1) {
        int nAddr = (90 + m_nIndex) * 2;
        m_nRunRecipeResult = (pszData[nAddr] & 0xff) | (pszData[nAddr + 1]) << 8;
        if (m_bWaitingRecipeResult) {
            LOGI("<CLoadMonitor>ÕýÔÚ¶ÁÈ¡PLCÇл»Åä·½µÄ½á¹û(RunRecipeResult:%d).", m_nRunRecipeResult);
        }
        if (m_nRunRecipeResult != 0) {
            SetEvent(m_hEventRunRecipeResult);
        }
    }
}
 
void CLoadMonitor::init()
{
    m_bRunRecipeWorking = TRUE;
    m_hRunRecipeWorkThreadHandle = (HANDLE)_beginthreadex(NULL, 0, ::LoadMonitorRunRecipeThreadFunction, this,
        0, &m_nRunRecipeWorkThrdaddr);
 
    CComponent::init();
}
 
void CLoadMonitor::term()
{
    // ½áÊøÏß³Ì
    m_bRunRecipeWorking = FALSE;
    SetEvent(m_hRunRecipeWorkEvent);
    if (m_hRunRecipeWorkThreadHandle != NULL) {
        WaitForSingleObject(m_hRunRecipeWorkThreadHandle, INFINITE);
        CloseHandle(m_hRunRecipeWorkThreadHandle);
        m_hRunRecipeWorkThreadHandle = NULL;
    }
 
    for (auto item : m_alarms) {
        item->release();
    }
}
 
void CLoadMonitor::setBeginAddr(int nAddr)
{
    m_nBeginAddr = nAddr;
}
 
void CLoadMonitor::OnTimer(UINT nTimerid)
{
    /*
    char szBuffer[184];
    memset(szBuffer, 0, 184);
 
    if (m_nIndex == 0) {
        static int iii = 0;
        iii++;
        if (iii > 30 && iii < 35) {
            szBuffer[0] = 0x01;
            onData(3, &szBuffer[0], 184);
        }
        else if (iii > 35 && iii < 52) {
            szBuffer[90 * 2] = 0x08;
            onData(3, &szBuffer[0], 184);
        }
        else if (iii > 60 && iii < 90) {
            szBuffer[1] = 0x01;
            onData(3, &szBuffer[0], 184);
        }
    }
    if (m_nIndex == 1) {
        static int iii = 0;
        iii++;
        if (iii > 130 && iii < 160) {
            szBuffer[2] = 0x01;
            onData(3, &szBuffer[0], 184);
        }
        else if (iii > 160 && iii < 190) {
            szBuffer[3] = 0x01;
            onData(3, &szBuffer[0], 184);
        }
    }
    if (m_nIndex == 2) {
        static int iii = 0;
        iii++;
        if (iii > 30 && iii < 60) {
            szBuffer[4] = 0x20;
            onData(3, &szBuffer[0], 184);
        }
        else if (iii > 60 && iii < 90) {
            szBuffer[4] = 0x40;
            onData(3, &szBuffer[0], 184);
        }
        else if (iii > 90 && iii < 120) {
            szBuffer[4] = 0x80;
            szBuffer[5] = 0x1;
            onData(3, &szBuffer[0], 184);
        }
    }
    */
}
 
void CLoadMonitor::ReqNotReady(int layer)
{
    BEQ::IUnit* pUnit = m_pBonder->getUnit(m_nIndex);
    ASSERT(pUnit);
 
    // ÇëÇóÉÏÁÏ
    if (0 == pUnit->reqNotReady(layer))
        LOGI("<CLoadMonitor-%d>Layer%dÔÝÍ£ÉÏÏÂÁÏ.", m_nIndex, layer);
}
 
void CLoadMonitor::ReqLoad(int layer)
{
    BEQ::IUnit* pUnit = m_pBonder->getUnit(m_nIndex);
    ASSERT(pUnit);
 
    // ÇëÇóÉÏÁÏ
    if(0 == pUnit->reqLoad(layer))
        LOGI("<CLoadMonitor-%d>Layer%dÇëÇóÉÏÁÏ.", m_nIndex, layer);
}
 
void CLoadMonitor::ReqUnload(int layer)
{
    BEQ::IUnit* pUnit = m_pBonder->getUnit(m_nIndex);
    ASSERT(pUnit);
 
    // ÇëÇóÉÏÁÏ
    if(0 == pUnit->reqUnload(layer))
        LOGI("<CLoadMonitor-%d>Layer%dÇëÇóÏÂÁÏ.", m_nIndex, layer);
}
 
int CLoadMonitor::loadReady(const char* pszMaterielId, const char* pszRecipeId)
{
    // ¸ù¾Ýµ¥ÔªºÅÈ¡µÃPLCµØÖ·
    int nMaterielIdAddr, nRecipeIdAddr;
    if (m_nIndex == 0) {
        nMaterielIdAddr = 4720;
        nRecipeIdAddr = 4780;
    }
    else if (m_nIndex == 1) {
        nMaterielIdAddr = 4750;
        nRecipeIdAddr = 4781;
    }
    else {
        return 0;
    }
 
 
    // Ð´ÈëÎïÁÏID
    HANDLE hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
    BOOL bWriteOk = TRUE;
    char szBuffer[40];
    memset(szBuffer, 0, 40);
    memcpy(szBuffer, pszMaterielId, min(40, strlen(pszMaterielId)));
    WriteData(MC::SOFT_COMPONENT::D, nMaterielIdAddr, szBuffer, 40, [&](IMcChannel* pChannel, int addr, DWORD value, int flag) -> void {
        if (flag != 0) {
            LOGE("<CBonder::loadReady-%d>PLC1дÊý¾Ý³¬Ê±.flag=%d", __LINE__, flag);
            bWriteOk = FALSE;
        }
        SetEvent(hEvent);
    });
    WaitForSingleObject(hEvent, INFINITE);
    ResetEvent(hEvent);
    if (!bWriteOk) {
        LOGE("<CBonder>%sÉÏÁÏ×¼±¸¾ÍÐ÷, PLCÒÑдÈëÎïÁÏidʧ°Ü", m_strName.c_str());
        CloseHandle(hEvent);
        return -3;
    }
    LOGI("<CBonder>%sÉÏÁÏ×¼±¸¾ÍÐ÷£¬PLCÒÑдÈëÎïÁÏid", m_strName.c_str());
 
    
    // Ð´ÈëÅä·½ID
    bWriteOk = TRUE;
    int nRecipeId = atoi(pszRecipeId);
    szBuffer[0] = (nRecipeId & 0xff);
    szBuffer[1] = (nRecipeId & 0xff00) >> 8;
    WriteData(MC::SOFT_COMPONENT::D, nRecipeIdAddr, szBuffer, 2, [&](IMcChannel* pChannel, int addr, DWORD value, int flag) -> void {
        if (flag != 0) {
            LOGE("<CBonder::loadReady-%d>PLC1дÊý¾Ý³¬Ê±.flag=%d", __LINE__, flag);
            bWriteOk = FALSE;
        }
        SetEvent(hEvent);
    });
    WaitForSingleObject(hEvent, INFINITE);
    ResetEvent(hEvent);
    CloseHandle(hEvent);
    if (!bWriteOk) {
        LOGE("<CBonder>%sÉÏÁÏ×¼±¸¾ÍÐ÷, PLCдÅä·½IDÎÞÏìÓ¦", m_strName.c_str());
        return -4;
    }
    LOGI("<CBonder>%sÉÏÁÏ×¼±¸¾ÍÐ÷£¬PLCÒÑдÈëÅä·½id", m_strName.c_str());
 
 
    // µÈ´ýPLCÏìÓ¦£¬PLC D4790(D4791)Çл»Åä·½½á¹û£º1³É¹¦£¬2ÉÔºòÖØÊÔ£¬3±¨¾¯.txt
    // ÕâÀïÒª·Åµ½Ïß³ÌÈ¥Ö´ÐÐÁË
    SetEvent(m_hRunRecipeWorkEvent);
    return 1;
}
 
void CLoadMonitor::Serialize(CArchive& ar)
{
    if (ar.IsStoring())
    {
        Lock();
        int count = (int)m_alarms.size();
        ar << count;
        for (auto item : m_alarms) {
            item->Serialize(ar);
        }
        Unlock();
    }
    else
    {
        Lock();
        int count;
        ar >> count;
        for (int i = 0; i < count; i++) {
            CAlarm* pAlarm = new CAlarm();
            pAlarm->addRef();
            pAlarm->Serialize(ar);
            AddAlarm(pAlarm);
            pAlarm->release();
 
        }
        Unlock();
    }
}
 
void CLoadMonitor::AddAlarm(CAlarm* pAlarm)
{
    Lock();
    pAlarm->addRef();
    m_alarms.push_back(pAlarm);
    if (m_alarms.size() > ALARM_MAX) {
        CAlarm* pTemp = m_alarms.front();
        pTemp->release();
        m_alarms.pop_front();
    }
    Unlock();
}
 
DWORD CLoadMonitor::WaitForGetRunRecipeResult(int timeout, int& result)
{
    m_bWaitingRecipeResult = TRUE;
    ResetEvent(m_hEventRunRecipeResult);
    m_nRunRecipeResult = 0;
    DWORD nRet = WaitForSingleObject(m_hEventRunRecipeResult, timeout);
    result = m_nRunRecipeResult;
    m_bWaitingRecipeResult = FALSE;
 
    return nRet;
}
 
unsigned CLoadMonitor::RunRecipeThreadFunction()
{
    while (m_bRunRecipeWorking) {
 
        // ´ýÍ˳öÐźŻòʱ¼äµ½
        int nRet = WaitForSingleObject(m_hRunRecipeWorkEvent, INFINITE);
        ResetEvent(m_hRunRecipeWorkEvent);
        if (!m_bRunRecipeWorking) break;
 
 
        // µÈ´ýPLCÇл»Åä·½½á¹û
        int nError = 0;
        int nRunRecipeResult = 0;
        nRet = WaitForGetRunRecipeResult(3000, nRunRecipeResult);
        if (nRet != WAIT_OBJECT_0) {
            LOGE("<CBonder>%s, ¶ÁÈ¡PLCÇл»Åä·½½á¹û³¬Ê±", m_strName.c_str());
            nError = -5;
        }
        else {
            if (nRunRecipeResult == 1) {
                LOGI("<CBonder>%s, PLCÇл»Åä·½³É¹¦", m_strName.c_str());
                nError = 0;
            }
            else if (nRunRecipeResult == 2) {
                LOGE("<CBonder>%s, PLCæµ£¬ÇëÉÔºòÖØÊÔ¡£", m_strName.c_str());
                nError = -6;
            }
            else {
                LOGE("<CBonder>%s, PLCÇл»Å䷽ʧ°Ü", m_strName.c_str());
                nError = -7;
            }
        }
        SendBroadcast(&CIntent(BC_CODE_RUN_RECIPE_RESULT, "", (CContext*)(__int64)nError));
 
        
 
        TRACE("RunRecipeThreadFunction...... \n");
    }
 
    // _endthreadex(0);
    TRACE("RunRecipeThreadFunction exit\n");
    return 0;
}