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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// CPageGraph2.cpp: 实现文件
//
 
#include "stdafx.h"
#include "Servo.h"
#include "CPageGraph2.h"
#include "afxdialogex.h"
#include "CHMPropertyDlg.h"
#include "CEquipmentPage1.h"
#include "CEquipmentPage2.h"
#include "CEquipmentPage3.h"
#include "CPagePortProperty.h"
#include "CPagePortStatus.h"
#include "CPageCassetteCtrlCmd.h"
#include "CJobDataB.h"
 
 
// CPageGraph2 对话框
 
IMPLEMENT_DYNAMIC(CPageGraph2, CDialogEx)
 
CPageGraph2::CPageGraph2(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_PAGE_GRAPH2, pParent)
{
    m_pEqsGraphWnd = nullptr;
    m_crBkgnd = PAGE_GRPAH2_BACKGROUND_COLOR;
    m_hbrBkgnd = nullptr;
    m_pObserver = nullptr;
}
 
CPageGraph2::~CPageGraph2()
{
}
 
void CPageGraph2::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}
 
 
BEGIN_MESSAGE_MAP(CPageGraph2, CDialogEx)
    ON_WM_CTLCOLOR()
    ON_WM_DESTROY()
    ON_WM_SIZE()
    ON_WM_TIMER()
END_MESSAGE_MAP()
 
 
// CPageGraph2 消息处理程序
 
 
void CPageGraph2::InitRxWindows()
{
    /* code */
    // 订阅数据
    IRxWindows* pRxWindows = RX_GetRxWindows();
    pRxWindows->enableLog(5);
    if (m_pObserver == NULL) {
        m_pObserver = pRxWindows->allocObserver([&](IAny* pAny) -> void {
            // onNext
            pAny->addRef();
            int code = pAny->getCode();
            if (RX_CODE_EQ_DATA_CHANGED == code) {
                // 通知设备状态
                SERVO::CEquipment* pEquipment = nullptr;
                if (pAny->getPtrValue("ptr", (void*&)pEquipment)) {
                    if (pEquipment != nullptr) {
                        m_pEqsGraphWnd->ShowItemIndicator((DWORD_PTR)pEquipment, pEquipment->hasGlass());
                    }
                }
            }
 
            pAny->release();
            }, [&]() -> void {
                // onComplete
            }, [&](IThrowable* pThrowable) -> void {
                // onErrorm
                pThrowable->printf();
            });
 
        theApp.m_model.getObservable()->observeOn(pRxWindows->mainThread())
            ->subscribe(m_pObserver);
    }
}
 
BOOL CPageGraph2::OnInitDialog()
{
    CDialogEx::OnInitDialog();
    InitRxWindows();
    SetTimer(1, 2000, nullptr);
 
 
    // filter graph wnd
    EqsGraphListener listener;
    listener.onCheckConnectPin = [](PIN* pPin1, PIN* pPin2) -> bool {
        ASSERT(pPin1);
        ASSERT(pPin2);
        ASSERT(pPin1->pData);
        ASSERT(pPin2->pData);
 
        return false;
    };
    listener.onConnectPin = [](PIN* pPin1, PIN* pPin2) -> bool {
        ASSERT(pPin1);
        ASSERT(pPin2);
        ASSERT(pPin1->pData);
        ASSERT(pPin2->pData);
 
        return false;
    };
    listener.onDisconnectPin = [](PIN* pPin) -> bool {
        ASSERT(pPin);
        ASSERT(pPin->pData);
 
        return false;
    };
    listener.onDeleteEqItem = [&](EQITEM* pItem) -> bool {
        ASSERT(pItem);
        ASSERT(pItem->pData);
        return false;
    };
    listener.onEqItemPosChanged = [&](EQITEM* pItem, int x, int y) -> void {
        ASSERT(pItem);
    };
    listener.onDblckEqItem = [&](EQITEM* pItem) -> bool {
        ASSERT(pItem);
        SERVO::CEquipment* pEquipment = (SERVO::CEquipment*)pItem->pData;
 
        CHMPropertyDlg dlg(pEquipment->getName().c_str(), 658, 788);
 
        CEquipmentPage1* pPage1 = new CEquipmentPage1();
        pPage1->setEquipment(pEquipment);
        pPage1->Create(IDD_PAGE_EQUIPMENT1);
        dlg.addPage(pPage1, "Link Signal");
 
        if (_strcmpi(pEquipment->getClassName(), "CLoadPort") == 0) {
            CPagePortProperty* pPageA = new CPagePortProperty();
            pPageA->setLoadPort((SERVO::CLoadPort*)pEquipment);
            pPageA->Create(IDD_PAGE_PORT_PROPERTY);
            dlg.addPage(pPageA, "属性");
 
            CPagePortStatus* pPageB = new CPagePortStatus();
            pPageB->setLoadPort((SERVO::CLoadPort*)pEquipment);
            pPageB->Create(IDD_PAGE_PORT_STATUS);
            dlg.addPage(pPageB, "Status");
        }
 
        CEquipmentPage2* pPage2 = new CEquipmentPage2();
        pPage2->setEquipment(pEquipment);
        pPage2->Create(IDD_PAGE_EQUIPMENT2);
        dlg.addPage(pPage2, "Slots");
 
        if (pEquipment->getID() == EQ_ID_EFEM) {
            CEquipmentPage3* pPage3 = new CEquipmentPage3();
            pPage3->setEquipment(pEquipment);
            pPage3->Create(IDD_PAGE_EQUIPMENT3);
            dlg.addPage(pPage3, "Dispatching Mode");
        }
 
        if (pEquipment->getID() == EQ_ID_LOADPORT1
            || pEquipment->getID() == EQ_ID_LOADPORT2) {
            CPageCassetteCtrlCmd* pPage = new CPageCassetteCtrlCmd();
            pPage->setEquipment(pEquipment);
            pPage->Create(IDD_PAGE_CASSETTE_CTRL_CMD);
            dlg.addPage(pPage, "Cassette control command");
        }
 
        dlg.DoModal();
        return true;
    };
    listener.onRclickEqItem = [&](EQITEM* pItem) -> bool {
        ASSERT(pItem);
 
        CMenu menu, menuCommand;
        CMenu* pPopupMenu;
        menu.LoadMenu(IDR_MENU_EQSGRAPH_ITEM);
        pPopupMenu = menu.GetSubMenu(0);
 
 
        // 显示模式
        pPopupMenu->EnableMenuItem(ID_EQSGRAPHITEM_SMALL, MF_DISABLED | MF_GRAYED);
        pPopupMenu->EnableMenuItem(ID_EQSGRAPHITEM_NORMAL, MF_DISABLED | MF_GRAYED);
        pPopupMenu->EnableMenuItem(ID_EQSGRAPHITEM_LARGE, MF_DISABLED | MF_GRAYED);
        if (pItem->nShowType == ITEM_SMALL) {
            pPopupMenu->CheckMenuRadioItem(ID_EQSGRAPHITEM_SMALL, ID_EQSGRAPHITEM_LARGE, ID_EQSGRAPHITEM_SMALL, MF_CHECKED);
        }
        else if (pItem->nShowType == ITEM_LARGE) {
            pPopupMenu->CheckMenuRadioItem(ID_EQSGRAPHITEM_SMALL, ID_EQSGRAPHITEM_LARGE, ID_EQSGRAPHITEM_LARGE, MF_CHECKED);
        }
        else {
            pPopupMenu->CheckMenuRadioItem(ID_EQSGRAPHITEM_SMALL, ID_EQSGRAPHITEM_LARGE, ID_EQSGRAPHITEM_NORMAL, MF_CHECKED);
        }
 
 
 
        POINT pt;
        GetCursorPos(&pt);
        int nCmd = pPopupMenu->TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN | TPM_TOPALIGN, pt.x, pt.y, this, NULL);
        if (nCmd == ID_EQSGRAPHITEM_SMALL) {
            m_pEqsGraphWnd->SetItemType(pItem, ITEM_SMALL);
        }
        else if (nCmd == ID_EQSGRAPHITEM_NORMAL) {
            m_pEqsGraphWnd->SetItemType(pItem, ITEM_NORMAL);
        }
        else if (nCmd == ID_EQSGRAPHITEM_LARGE) {
            m_pEqsGraphWnd->SetItemType(pItem, ITEM_LARGE);
        }
 
 
        // 测试
        else if (nCmd == ID_EQSGRAPHITEM_TEST1) {
            BOOL bTestGenerate = FALSE;
            SERVO::CEquipment* pEquipment = (SERVO::CEquipment*)pItem->pData;
            if (pEquipment->getID() == EQ_ID_LOADPORT1 && !pEquipment->hasGlass()) {
                ((SERVO::CLoadPort*)pEquipment)->testGenerateGlassList(SERVO::MaterialsType::G1, 
                    "P20250320G1X", 1);
                bTestGenerate = TRUE;
            }
            else if (pEquipment->getID() == EQ_ID_LOADPORT2 && !pEquipment->hasGlass()) {
                ((SERVO::CLoadPort*)pEquipment)->testGenerateGlassList(SERVO::MaterialsType::G2,
                    "P20250320G2X", 1);
                bTestGenerate = TRUE;
            }
            
            if (!bTestGenerate) {
                SERVO::CRobotTask* pTask = theApp.m_model.getMaster().getActiveRobotTask();
                if (pTask != nullptr) {
                    SERVO::CGlass* pGlass = (SERVO::CGlass*)pTask->getContext();
                    SERVO::CJobDataB* pJobDataB = pGlass->getJobDataB();
                    SERVO::CJobDataS* pJobDataS = pGlass->getJobDataS();
                    if (pJobDataB != nullptr && pJobDataS != nullptr) {
                        pEquipment->onFetchedOutJob(0, pJobDataB);
                        pEquipment->onSentOutJob(0, pJobDataS);
                    }
                }
            }
        }
        else if (nCmd == ID_EQSGRAPHITEM_TEST2) {
            SERVO::CEquipment* pEquipment = (SERVO::CEquipment*)pItem->pData;
            SERVO::CArm* pArm = (SERVO::CArm*)pEquipment->getArm();
            if (pArm != nullptr) {
                SERVO::CGlass* pGlass = pArm->getAnyGlass();
                if (pGlass != nullptr) {
                    SERVO::CJobDataB* pJobDataB = pGlass->getJobDataB();
                    SERVO::CJobDataS* pJobDataS = pGlass->getJobDataS();
                    if (pJobDataB != nullptr && pJobDataS != nullptr) {
                        pEquipment->onReceivedJob(0, pJobDataS);
                        Sleep(600);
                        pEquipment->onStoredJob(0, pJobDataB);
                    }
                }
            }
        }
        else if (nCmd == ID_EQSGRAPHITEM_TEST3) {
            SERVO::CEquipment* pEquipment = (SERVO::CEquipment*)pItem->pData;
            if (pEquipment != nullptr) {
                SERVO::CGlass* pGlass = pEquipment->getAnyGlass();
                if (pGlass != nullptr) {
                    SERVO::CProcessData pd;
                    pd.setGlassId(pGlass->getID().c_str());
                    pEquipment->onProcessData(&pd);
                }
            }
        }
        else if (nCmd == ID_EQSGRAPHITEM_TEST4) {
            SERVO::CEquipment* pEquipment = (SERVO::CEquipment*)pItem->pData;
            if (pEquipment != nullptr) {
                pEquipment->setLinkSignal(0, SIGNAL_UPSTREAM_INLINE, TRUE);
                pEquipment->setLinkSignal(0, SIGNAL_UPSTREAM_TROUBLE, FALSE);
                pEquipment->setLinkSignal(0, SIGNAL_INTERLOCK, TRUE);
                pEquipment->setLinkSignal(0, SIGNAL_SEND_ABLE, TRUE);
                pEquipment->setLinkSignal(1, SIGNAL_UPSTREAM_INLINE, TRUE);
                pEquipment->setLinkSignal(1, SIGNAL_UPSTREAM_TROUBLE, FALSE);
                pEquipment->setLinkSignal(1, SIGNAL_INTERLOCK, TRUE);
                pEquipment->setLinkSignal(1, SIGNAL_SEND_ABLE, TRUE);
            }
 
            if (pEquipment != nullptr && (pEquipment->getID() == EQ_ID_Bonder1
                || pEquipment->getID() == EQ_ID_Bonder2)) {
                SERVO::CGlass* pGlass = pEquipment->getAnyGlass();
                if (pGlass != nullptr) {
                    SERVO::CProcessData pd;
                    pd.setGlassId(pGlass->getID().c_str());
                    pEquipment->onProcessData(&pd);
                }
            }
        }
        else if (nCmd == ID_EQSGRAPHITEM_TEST5) {
            SERVO::CEquipment* pEquipment = (SERVO::CEquipment*)pItem->pData;
            if (pEquipment != nullptr) {
                pEquipment->setLinkSignal(0, SIGNAL_UPSTREAM_INLINE, TRUE);
                pEquipment->setLinkSignal(0, SIGNAL_UPSTREAM_TROUBLE, TRUE);
                pEquipment->setLinkSignal(0, SIGNAL_INTERLOCK, TRUE);
                pEquipment->setLinkSignal(0, SIGNAL_SEND_ABLE, FALSE);
            }
        }
        else if (nCmd == ID_EQSGRAPHITEM_TEST6) {
            SERVO::CEquipment* pEquipment = (SERVO::CEquipment*)pItem->pData;
 
 
            // 测试下发Cim Message
            /*
            if (pEquipment->getID() == EQ_ID_Bonder1
                || pEquipment->getID() == EQ_ID_Bonder2) {
                static int msgId = 0; msgId++;
                if (msgId % 2 == 0) {
                    pEquipment->setCimMessage("Hello!", msgId, 1);
                }
                else {
                    pEquipment->setCimMessage("ABC0123456789", msgId, 2);
                }
            }
            */
 
 
            // 测试清除Cim Message
            /*
            if (pEquipment->getID() == EQ_ID_Bonder1
                || pEquipment->getID() == EQ_ID_Bonder2) {
                static int msgId = 0; msgId++;
                if (msgId % 2 == 0) {
                    pEquipment->clearCimMessage(msgId, 1);
                }
                else {
                    pEquipment->clearCimMessage(msgId, 2);
                }
            }
            */
 
            // 测试设置时间
            /*
            if (pEquipment->getID() == EQ_ID_Bonder1
                || pEquipment->getID() == EQ_ID_Bonder2) {
                CTime time = CTime::GetCurrentTime();
                pEquipment->setDateTime((short)time.GetYear(),
                    (short)time.GetMonth(),
                    (short)time.GetDay(),
                    (short)time.GetHour(),
                    (short)time.GetMinute(),
                    (short)time.GetSecond());
            }
            */
 
            
            // 测试设置cim mode
            /*
            if (pEquipment->getID() == EQ_ID_Bonder1
                || pEquipment->getID() == EQ_ID_Bonder2) {
                static int ii = 0; ii++;
                pEquipment->setCimMode(ii % 2 == 0);
            }
            */
            
            
            if (pEquipment->getID() == EQ_ID_Bonder1
                || pEquipment->getID() == EQ_ID_Bonder2) {
                static int ii = 0; ii++;
                pEquipment->setEqMode((ii % 5) + 1);
            }
            
 
            /*
            SERVO::CGlass* pGlass = pEquipment->getAnyGlass();
            if (pGlass != nullptr) {
                std::string strDescription;
                SERVO::CPath* pPath = pGlass->getPath();
                while (pPath != nullptr) {
                    pPath->getDescription(strDescription);
                    AfxMessageBox(strDescription.c_str());
                    pPath = pPath->getNext();
                }
            }
            */
 
 
            // 请求主配方列表
            if (pEquipment != nullptr) {
                pEquipment->masterRecipeListRequest(0);
            }            
        }
 
 
        return true;
    };
    listener.onSelectEqItem = [&](EQITEM* pItem) -> bool {
        ASSERT(pItem);
        SERVO::CEquipment* pEquipment = (SERVO::CEquipment*)pItem->pData;
        theApp.m_model.notifyPtr(RX_CODE_SELECT_EQUIPMENT, pEquipment);
 
        return true;
    };
 
    m_pEqsGraphWnd = CEqsGraphWnd::FromHandle(GetDlgItem(IDC_EQSGRAPHWND1)->m_hWnd);
    m_pEqsGraphWnd->SetBkgndColor(m_crBkgnd);
    m_pEqsGraphWnd->SetOnListener(listener);
 
 
    return TRUE;  // return TRUE unless you set the focus to a control
                  // 异常: OCX 属性页应返回 FALSE
}
 
 
HBRUSH CPageGraph2::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
 
    if (nCtlColor == CTLCOLOR_STATIC) {
        pDC->SetBkColor(m_crBkgnd);
        pDC->SetTextColor(RGB(0, 0, 0));
    }
 
    if (m_hbrBkgnd == nullptr) {
        m_hbrBkgnd = CreateSolidBrush(m_crBkgnd);
    }
 
    return m_hbrBkgnd;
}
 
 
void CPageGraph2::OnDestroy()
{
    CDialogEx::OnDestroy();
    SaveEqsGraphData();
 
    if (m_hbrBkgnd != nullptr) {
        ::DeleteObject(m_hbrBkgnd);
    }
 
    if (m_pObserver != nullptr) {
        m_pObserver->unsubscribe();
        m_pObserver = NULL;
    }
}
 
 
void CPageGraph2::OnSize(UINT nType, int cx, int cy)
{
    CDialogEx::OnSize(nType, cx, cy);
    if (GetDlgItem(IDC_EQSGRAPHWND1) == nullptr) return;
 
    CRect rcClient;
    GetClientRect(&rcClient);
    GetDlgItem(IDC_EQSGRAPHWND1)->MoveWindow(0, 0, rcClient.Width(), rcClient.Height());
}
 
#define INPIN        1
#define OUTPIN        2
void CPageGraph2::AddEqToGraphWnd(SERVO::CEquipment* pEquipment)
{
    int x, y;
    GetItemDataFormIni(pEquipment->getName().c_str(), x, y);
 
    EQITEM* pItem = m_pEqsGraphWnd->AddItem(0, pEquipment->getName().c_str(), (DWORD_PTR)pEquipment);
    m_pEqsGraphWnd->SetItemType(pItem, ITEM_SMALL);
    if (x > 0 && y > 0) {
        m_pEqsGraphWnd->SetItemPos(pItem, x, y);
    }
 
    std::vector<SERVO::CPin*>& inPins = pEquipment->getInputPins();
    for (auto inPin : inPins) {
        m_pEqsGraphWnd->AddPin(pItem, INPIN, inPin->getName().c_str(), (DWORD_PTR)inPin);
    }
 
    std::vector<SERVO::CPin*>& outPins = pEquipment->getOutputPins();
    for (auto outPin : outPins) {
        m_pEqsGraphWnd->AddPin(pItem, OUTPIN, outPin->getName().c_str(), (DWORD_PTR)outPin);
    }
 
    m_pEqsGraphWnd->ShowItemIndicator((DWORD_PTR)pEquipment, pEquipment->hasGlass());
}
 
void CPageGraph2::OnTimer(UINT_PTR nIDEvent)
{
    if (1 == nIDEvent) {
        KillTimer(1);
        std::list<SERVO::CEquipment*>& eqs = theApp.m_model.m_master.getEquipmentList();
        for (auto eq : eqs) {
            AddEqToGraphWnd(eq);
        }
 
        for (auto eq : eqs) {
            auto& pins = eq->getOutputPins();
            for (auto pin : pins) {
                SERVO::CPin* pPin2 = pin->getConnectedPin();
                if (pPin2 != nullptr) {
                    m_pEqsGraphWnd->ConnectPin((DWORD_PTR)eq, (DWORD_PTR)pin, (DWORD_PTR)pPin2->getEquipment(), (DWORD_PTR)pPin2);
                }
            }
        }
    }
 
    CDialogEx::OnTimer(nIDEvent);
}
 
void CPageGraph2::SaveEqsGraphData()
{
    char szFilepath[MAX_PATH];
    CString strValue;
    sprintf_s(szFilepath, MAX_PATH, "%s\\EqsGraph.ini", (LPTSTR)(LPCTSTR)theApp.m_strAppDir);
    std::list<SERVO::CEquipment*>& eqs = theApp.m_model.m_master.getEquipmentList();
    for (auto item : eqs) {
        EQITEM* pEqItem = m_pEqsGraphWnd->GetItem((DWORD_PTR)item);
        if (nullptr == pEqItem) {
            continue;
        }
 
        strValue.Format(_T("%d"), pEqItem->rect.left);
        WritePrivateProfileString(item->getName().c_str(), _T("Left"),
            (LPTSTR)(LPCTSTR)strValue, szFilepath);
        strValue.Format(_T("%d"), pEqItem->rect.top);
        WritePrivateProfileString(item->getName().c_str(), _T("Top"),
            (LPTSTR)(LPCTSTR)strValue, szFilepath);
    }
}
 
void CPageGraph2::GetItemDataFormIni(const char* pszItemName, int& left, int& top)
{
    char szFilepath[MAX_PATH];
    CString strValue;
    sprintf_s(szFilepath, MAX_PATH, "%s\\EqsGraph.ini", (LPTSTR)(LPCTSTR)theApp.m_strAppDir);
    left = GetPrivateProfileInt(pszItemName, _T("Left"), 0, szFilepath);
    top = GetPrivateProfileInt(pszItemName, _T("Top"), 0, szFilepath);
}