SDC C-Project CF Review 프로그램
LYW
2022-07-05 63439977901d54a01924ed76290931aeddbce66c
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
 
// MainFrm.cpp : CMainFrame 클래스의 구현
//
 
#include "stdafx.h"
#include "ReviewSystem.h"
#include "MainFrm.h"
#include "ProcessController.h"
 
//#include "CHReviewSetting/SystemInfo.h"
 
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
 
// CMainFrame
 
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWndEx)
 
const int  iMaxUserToolbars = 10;
const UINT uiFirstUserToolBarId = AFX_IDW_CONTROLBAR_FIRST + 40;
const UINT uiLastUserToolBarId = uiFirstUserToolBarId + iMaxUserToolbars - 1;
 
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWndEx)
    ON_WM_CREATE()
    ON_COMMAND(ID_VIEW_CUSTOMIZE, &CMainFrame::OnViewCustomize)
    ON_REGISTERED_MESSAGE(AFX_WM_CREATETOOLBAR, &CMainFrame::OnToolbarCreateNew)
    ON_WM_GETMINMAXINFO()
    ON_WM_TIMER()
    ON_WM_CLOSE()
    ON_WM_DESTROY()
    ON_WM_COPYDATA()    
    ON_WM_SHOWWINDOW()
END_MESSAGE_MAP()
 
static UINT indicators[] =
{
    ID_SEPARATOR,           // 상태 줄 표시기
    ID_INDICATOR_CAPS,
    ID_INDICATOR_NUM,
    ID_INDICATOR_SCRL,
};
 
// 정의 
 
// CMainFrame 생성/소멸
 
CMainFrame::CMainFrame()
{
    // TODO: 여기에 멤버 초기화 코드를 추가합니다.
    m_ResetComm.bSignal = 0;    
    m_ResetComm.iIdentMessage = DEFAULT_MESSAGE;
    m_ResetComm.iIntaval = 2000;
    m_bAlive = FALSE;
    m_nSameTimeCount[0] = 0;
    m_nSameTimeCount[1] = 0;
    m_wsiLongRunTestFlag = 0;
    m_nAfmAliveCount = 5;
    m_nSendAFMRestart = 0;
    m_nSendSignalTimeCount = 0;
}
 
CMainFrame::~CMainFrame()
{
    
}
 
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CFrameWndEx::OnCreate(lpCreateStruct) == -1)
        return -1;
 
    SetUnhandledExceptionFilter(TopLevelExceptionFilterCallBack);
 
    ModifyStyle(WS_CAPTION, NULL);
 
    SetMenu(NULL);
 
    //SetTimer(ID_TIMER_RESET_COMM, 2000, NULL);
 
    SetTimer(ID_TIMER_ALIVE, 1813, NULL); //->> 기존 SetTimer(ID_TIMER_ALIVE, 1000, NULL); 
 
    //SetTimer(ID_TIMER_AFM_ALIVE, 5000, NULL);
 
    //SetTimer(ID_TIMER_WSI_READY_ACK_TIME, 100, NULL);
 
    //SetTimer(ID_TIMER_WSI_START_ACK_TIME, 100, NULL);
 
    SetTimer(ID_TIMER_SEND_SIGNAL_ALIVE, 100, NULL);
 
    //LYW Align Reconnect
    //업데이트시
    system("netsh interface set interface name = \"Align #1\" admin = disable"
        "&& netsh interface set interface name = \"Align #1\" admin = enable"
        "&& netsh interface set interface name = \"Align #2\" admin = disable"
        "&& netsh interface set interface name = \"Align #2\" admin = enable"
        "&& net use Y : \\\\126.100.100.1\\d\\DIT_ResultData pw : dit / user : Server / persistent : yes");
 
    Sleep(2000);
 
//    SetTimer(ID_TIMER_WSI_LONGRUN_TEST_TIME, 10000, NULL);
 
    return 0;
}
 
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
    if( !CFrameWndEx::PreCreateWindow(cs) )
        return FALSE;
 
    cs.style = cs.style & ~WS_THICKFRAME & ~WS_MINIMIZEBOX & ~WS_CAPTION & ~WS_SYSMENU & ~FWS_ADDTOTITLE;
    cs.lpszName = _T("ReviewSystem");
    this->SetTitle(_T("ReviewSystem"));
 
    return TRUE;
}
 
// CMainFrame 진단
 
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
    CFrameWndEx::AssertValid();
}
 
void CMainFrame::Dump(CDumpContext& dc) const
{
    CFrameWndEx::Dump(dc);
}
#endif //_DEBUG
 
 
// CMainFrame 메시지 처리기
void CMainFrame::OnViewCustomize()
{
    CMFCToolBarsCustomizeDialog* pDlgCust = new CMFCToolBarsCustomizeDialog(this, TRUE /* 메뉴를 검색합니다. */);
    pDlgCust->EnableUserDefinedToolbars();
    pDlgCust->Create();
}
 
LRESULT CMainFrame::OnToolbarCreateNew(WPARAM wp,LPARAM lp)
{
    LRESULT lres = CFrameWndEx::OnToolbarCreateNew(wp,lp);
    if (lres == 0)
    {
        return 0;
    }
 
    CMFCToolBar* pUserToolbar = (CMFCToolBar*)lres;
    ASSERT_VALID(pUserToolbar);
 
    BOOL bNameValid;
    CString strCustomize;
    bNameValid = strCustomize.LoadString(IDS_TOOLBAR_CUSTOMIZE);
    ASSERT(bNameValid);
 
    pUserToolbar->EnableCustomizeButton(TRUE, ID_VIEW_CUSTOMIZE, strCustomize);
    return lres;
}
 
BOOL CMainFrame::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle, CWnd* pParentWnd, CCreateContext* pContext) 
{
    // 기본 클래스가 실제 작업을 수행합니다.
    if (!CFrameWndEx::LoadFrame(nIDResource, dwDefaultStyle, pParentWnd, pContext))
    {
        return FALSE;
    }
    
    // 모든 사용자 도구 모음에 사용자 지정 단추를 활성화합니다.
    BOOL bNameValid;
    CString strCustomize;
    bNameValid = strCustomize.LoadString(IDS_TOOLBAR_CUSTOMIZE);
    ASSERT(bNameValid);
 
    for (int i = 0; i < iMaxUserToolbars; i ++)
    {
        CMFCToolBar* pUserToolbar = GetUserToolBarByIndex(i);
        if (pUserToolbar != NULL)
        {
            pUserToolbar->EnableCustomizeButton(TRUE, ID_VIEW_CUSTOMIZE, strCustomize);
        }
    }
 
    return TRUE;
}
 
 
void CMainFrame::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
    // TODO: 여기에 메시지 처리기 코드를 추가 및/또는 기본값을 호출합니다.
 
    int nMonitorCount = GetSystemMetrics(80 /*SM_CMONITORS*/);            // Get Monitor Count.
 
    if(nMonitorCount>=2)
    {
        lpMMI->ptMinTrackSize.x = TOTAL_TWO_MONITOR_WINDOW_SIZE_X;
        lpMMI->ptMinTrackSize.y = TOTAL_TWO_MONITOR_WINDOW_SIZE_Y;
 
        lpMMI->ptMaxTrackSize.x = TOTAL_TWO_MONITOR_WINDOW_SIZE_X;
        lpMMI->ptMaxTrackSize.y = TOTAL_TWO_MONITOR_WINDOW_SIZE_Y;
    }
    else
    {
      //lpMMI->ptMinTrackSize.x = TOTAL_WINDOW_SIZE_X;
        lpMMI->ptMinTrackSize.x = 500;
        lpMMI->ptMinTrackSize.y = TOTAL_WINDOW_SIZE_Y;
 
        lpMMI->ptMaxTrackSize.x = TOTAL_WINDOW_SIZE_X;
        lpMMI->ptMaxTrackSize.y = TOTAL_WINDOW_SIZE_Y;
    }
 
    CFrameWndEx::OnGetMinMaxInfo(lpMMI);
}
 
void CMainFrame::OnShowWindow(BOOL bShow, UINT nStatus)
{
    __super::OnShowWindow(bShow, nStatus);
 
    static bool once = true;   
 
    if (bShow && !IsWindowVisible() && once)  
    {
        once = false;
        POINT min, max;
 
        int nMonitorCount = GetSystemMetrics(80 /*SM_CMONITORS*/);            // Get Monitor Count.
 
        if(nMonitorCount > 2)
        {
            min.x = TOTAL_TWO_MONITOR_WINDOW_SIZE_X;
            min.y = 0;
            max.x = TOTAL_TWO_MONITOR_WINDOW_SIZE_X;
            max.y = 0;
        }
        else
        {
            min.x = TOTAL_WINDOW_SIZE_X;
            min.y = 0;
            max.x = TOTAL_WINDOW_SIZE_X;
            max.y = 0;
        }
 
        WINDOWPLACEMENT lwp;
        lwp.length = sizeof(WINDOWPLACEMENT);
        lwp.flags = SW_SHOWMINIMIZED | WPF_SETMINPOSITION;
        lwp.showCmd = SW_SHOW;
        lwp.ptMinPosition = min;
        lwp.ptMaxPosition = max;
        lwp.rcNormalPosition = CRect(min, max);
 
        SetWindowPlacement(&lwp);
    }
}
 
 
void CMainFrame::OnTimer(UINT_PTR nIDEvent)
{
    // TODO: 여기에 메시지 처리기 코드를 추가 및/또는 기본값을 호출합니다.
    switch(nIDEvent)
    {    
    case ID_TIMER_LONG_RUN_GLASS_LOADING:
        KillTimer(ID_TIMER_LONG_RUN_GLASS_LOADING);
        //this->PostMessage(UM_PCControl_MESSAGE, SIGNAL_COMMAND_GLASS_LOADING, NULL);
        break;
 
    case ID_TIMER_LONG_RUN_GLASS_LOADING_END:
        KillTimer(ID_TIMER_LONG_RUN_GLASS_LOADING_END);
        //this->PostMessage(UM_PCControl_MESSAGE, SIGNAL_COMMAND_GLASS_LOADING_END, NULL);
        break;
 
    case ID_TIMER_LONG_RUN_ALIGN_START:
        KillTimer(ID_TIMER_LONG_RUN_ALIGN_START);
        //this->PostMessage(UM_PCControl_MESSAGE, SIGNAL_COMMAND_ALIGN_START, NULL);
        break;
 
    case ID_TIMER_LONG_RUN_ALIGN_END:
        KillTimer(ID_TIMER_LONG_RUN_ALIGN_END);
        //this->PostMessage(UM_PCControl_MESSAGE, SIGNAL_COMMAND_ALIGN_END, NULL);
        break;
 
    case ID_TIMER_LONG_RUN_REVIEW_START:
        KillTimer(ID_TIMER_LONG_RUN_REVIEW_START);
        //this->PostMessage(UM_PCControl_MESSAGE, SIGNAL_COMMAND_REVIEW_START, NULL);
        break;
    case ID_TIMER_LONG_RUN_REVIEW_END:
        KillTimer(ID_TIMER_LONG_RUN_REVIEW_END);
        //this->PostMessage(UM_PCControl_MESSAGE, SIGNAL_COMMAND_REVIEW_END, NULL);
        break;
 
    case ID_TIMER_LONG_RUN_GLASS_UNLOADING:
        KillTimer(ID_TIMER_LONG_RUN_GLASS_UNLOADING);
        //this->PostMessage(UM_PCControl_MESSAGE, SIGNAL_COMMAND_GLASS_UNLOADING, NULL);
        break;
 
    case ID_TIMER_RESET_COMM:
        {
            m_ResetComm.bSignal = !m_ResetComm.bSignal;
 
            HWND hWnd = NULL;
            COPYDATASTRUCT cds;
            cds.dwData = COPYDATA_RESETCOMM;
            cds.cbData = sizeof(RESETCOMM);
            cds.lpData = &m_ResetComm;
 
            CString    strCaption = _T("Montering Inspection System");
            hWnd = ::FindWindow(NULL, strCaption);
 
            if (hWnd) ::SendMessage(hWnd, WM_COPYDATA, 0, (LPARAM)(LPVOID)&cds);
        }
        break;
    case ID_TIMER_ALIVE:
        SendAlive();
        break;
 
    case ID_TIMER_AFM_ALIVE:
        SendAFMAlive();
        CheckAFMAlive();
        break;
 
    case ID_TIMER_WSI_READY_ACK_TIME:
        CheckWsiReady();
        break;
 
    case ID_TIMER_WSI_START_ACK_TIME:
        CheckWsiStart();
        break;
 
    case ID_TIMER_WSI_LONGRUN_TEST_TIME:
        WSILongRunTest();
        break;
 
    case ID_TIMER_SEND_SIGNAL_ALIVE:
        SendSignalCheck();
        break;
 
    default:
        break;
    }
 
    CFrameWndEx::OnTimer(nIDEvent);
}
 
void CMainFrame::SendAlive()
{
        m_bAlive = !m_bAlive;
        CMainFrame* pMain = static_cast<CMainFrame*>(AfxGetMainWnd());
        CSequenceProcessor* pSequenceProcessor = pMain->IRP2P_GetSequenceProcessor();
        if (pSequenceProcessor == NULL)    return;
 
    pSequenceProcessor->SendSignalReviewAlive((long)m_bAlive);
 
    //#3358 KYH FDC 데이터 전송 시점 개선 ADD START
    pSequenceProcessor->MakeDVData2(); // 210325 kyh
    //#3358 KYH FDC 데이터 전송 시점 개선 ADD END
}
void CMainFrame::SendAFMAlive()
{
    CMainFrame* pMain = static_cast<CMainFrame*>(AfxGetMainWnd());
    int nModuleCount = pMain->Module_GetModuleStatusCount(0);
    for (int nModuleNum = 0; nModuleNum < nModuleCount; nModuleNum++)
    {
//         CModuleStatus *pModuleStatus = pMain->Module_GetModuleStatus(nModuleNum);//nModuleIndex); 나중에 확인
//         if (pModuleStatus==NULL) 
//         {
//             return;
//         }
// 
//         SExternalIndex* pExternalIndex = pModuleStatus->GetExternalIndex();
//         if(pExternalIndex == NULL) return;
 
        CAfmControl* pAFMControl = ISP2P_GetAFMControl(nModuleNum);
        if (pAFMControl==NULL) return;
 
        pAFMControl->NetProcess_FC_SEND_ALIVE();
    }
}
 
void CMainFrame::SetAfmAliveCount(int count)
{
    m_nAfmAliveCount = count;
    g_pLog->DisplayMessage(_T("Afm Alive Count Set : %d"), m_nAfmAliveCount);
}
 
void CMainFrame::CheckAFMAlive()
{
    CMainFrame* pMain = static_cast<CMainFrame*>(AfxGetMainWnd());
 
    int nModuleCount = pMain->Module_GetModuleStatusCount(0);
    for (int nModuleNum = 0; nModuleNum < nModuleCount; nModuleNum++)
    {
//         CModuleStatus *pModuleStatus = pMain->Module_GetModuleStatus(nModuleNum);//nModuleIndex); 나중에 확인
//         if (pModuleStatus==NULL) 
//         {
//             return;
//         }
// 
//         SExternalIndex* pExternalIndex = pModuleStatus->GetExternalIndex();
//         if(pExternalIndex == NULL) return;
 
        CAfmControl* pAFMControl = ISP2P_GetAFMControl(nModuleNum);
        if (pAFMControl==NULL) return;
 
 
        if (pAFMControl->IsSetTime())
        {
            CTime currentTime = pAFMControl->GetLastAliveTime();
            if (m_lastAfmAliveTime[nModuleNum] != currentTime)
            {
                m_lastAfmAliveTime[nModuleNum] = currentTime;
                m_nSameTimeCount[nModuleNum] = 0;
                m_nSendAFMRestart = 0;
//                g_pLog->DisplayMessage(_T("AFM[%d] Alive Received!! "), nModuleNum);
            }
            else
            {
                g_pLog->DisplayMessage(_T("AFM[%d] Alive lost...%d!!"), nModuleNum, m_nSameTimeCount[nModuleNum]);
                m_nSameTimeCount[nModuleNum]++;
            }
            if (m_nSameTimeCount[nModuleNum] > m_nAfmAliveCount)
            {
                if (!m_nSendAFMRestart)    // AFM에게 restart 명령을 보내지 않은 경우
                {
                    g_pLog->DisplayMessage(_T("AFM[%d] Alive lost... RESTART AFM"), nModuleNum);
                    SendAFMReset();
                    m_nSendAFMRestart = 1;
                    m_nSameTimeCount[nModuleNum] = 0;
                }
 
                else    // AFM에게 restart 명령을 보냈는데 일정시간 동안 응답이 없는 경우
                {
                    g_pLog->DisplayMessage(_T("AFM[%d] Alive Lost... Send to PCControl Alarm AFM Fail"), nModuleNum);
                CMainFrame* pMain = static_cast<CMainFrame*>(AfxGetMainWnd());
                CSequenceProcessor* pSequenceProcessor = pMain->IRP2P_GetSequenceProcessor();
                if (pSequenceProcessor == NULL)    return;
 
                //pSequenceProcessor->SendSignalToSignalControl(7);    // PCControlSend_AFFail
                pAFMControl->KillTime();
                    m_nSendAFMRestart = 0;
                }
                m_nSameTimeCount[nModuleNum] = 0;
            }
        }
    }
}
 
void CMainFrame::SendAFMReset()
{
    // Make Script File /////////////////
    CString strScript1;
    CString strScript2;
 
//#define strScript1 "KILL^AFControl.exe"
//#define strScript2 "START^C:\\AFControl\\program\\AFControl.exe"
#define strScript "RESTART^AFControl.exe^C:\\AFControl\\program\\AFControl.exe"
    
    CFile file;
    if( FALSE == file.Open(_T("D:\\DIT_Review\\MakeScript.TSF"),CFile::modeCreate | CFile::modeWrite) ) //210805
    {
        g_pLog->DisplayMessage(_T("File[D:\\DIT_Review\\MakeScript.TSF] Create Fail!!")); //210805
        return;
    }
 
//     int nSize1 = sizeof(strScript1);
//     int nSize2 = sizeof(strScript2);
    int nSize = sizeof(strScript);
// 
//     char* pszBuf = new char[nSize+1];
// 
//     memset((void*)pszBuf,NULL,sizeof(char)*nSize+1);
// 
//     sprintf(pszBuf,"%s",strScript);
 
//     file.Write( strScript1,nSize1);
//     file.Write("\r\n",2);
//     file.Write( strScript2,nSize2);
    file.Write( strScript,nSize);
 
 
    file.Close();
 
//     if (pszBuf)
//         delete[] pszBuf;
//     pszBuf = NULL;
    /////////////////////////////////////
 
    // Copy Script File ////////////////
    
    CString strTarget;
    strTarget.Format(_T("\\\\126.100.100.220\\c\\RemoteTaskMgr\\T\\CopyScript.TSF"));
    
    BOOL bRet = CopyFile(_T("D:\\DIT_Review\\MakeScript.TSF"),strTarget,FALSE); //210805
    if(bRet == FALSE)
        g_pLog->DisplayMessage(_T("[%s]Send Script Fail!"), strTarget);
    /////////////////////////////////////
}
 
// 20171026 smok. Send ready to WSI log
void CMainFrame::CheckWsiReady()
{
    CMainFrame* pMain = static_cast<CMainFrame*>(AfxGetMainWnd());
    CSequenceProcessor* pSequenceProcessor = pMain->IRP2P_GetSequenceProcessor();
    if (pSequenceProcessor == NULL)    return;
 
    pSequenceProcessor->CheckWSIReady();
}
 
void CMainFrame::CheckWsiStart()
{
    CMainFrame* pMain = static_cast<CMainFrame*>(AfxGetMainWnd());
    CSequenceProcessor* pSequenceProcessor = pMain->IRP2P_GetSequenceProcessor();
    if (pSequenceProcessor == NULL)    return;
 
    pSequenceProcessor->CheckWSIStart();
}
/////////////////////////////////////////////////////
 
void CMainFrame::WSILongRunTest()
{
    /*CMainFrame* pMain = static_cast<CMainFrame*>(AfxGetMainWnd());
    CSequenceProcessor* pSequenceProcessor = pMain->IRP2P_GetSequenceProcessor();
    if (pSequenceProcessor == NULL)    return;
 
    if (m_wsiLongRunTestFlag == 0)
    {
        pSequenceProcessor->RecvSignalToSignalControl(4);
        m_wsiLongRunTestFlag = 1;
    }
    else if (m_wsiLongRunTestFlag == 1)
    {
        pSequenceProcessor->RecvSignalToSignalControl(5);
        m_wsiLongRunTestFlag = 2;
    }
    else if (m_wsiLongRunTestFlag == 2)
    {
        pSequenceProcessor->RecvSignalToSignalControl(6);
        m_wsiLongRunTestFlag = 3;
    }
    else if (m_wsiLongRunTestFlag == 3)
    {
        pSequenceProcessor->RecvSignalToSignalControl(7);
        m_wsiLongRunTestFlag = 4;
    }
    else if (m_wsiLongRunTestFlag == 4)
    {
        pSequenceProcessor->RecvSignalToSignalControl(8);
        m_wsiLongRunTestFlag = 5;
    }
    else if (m_wsiLongRunTestFlag == 5)
    {
        pSequenceProcessor->RecvSignalToSignalControl(9);
        m_wsiLongRunTestFlag = 0;
    }*/
 
}
 
void CMainFrame::SendSignalCheck()
{
    CMainFrame* pMain = static_cast<CMainFrame*>(AfxGetMainWnd());
    CSequenceProcessor* pSequenceProcessor = pMain->IRP2P_GetSequenceProcessor();
    if (pSequenceProcessor == NULL)    return;
 
    CSignalControl *pSignalcontrol =ISP2P_GetSignalControl();
    if(pSignalcontrol == NULL)
        return;
 
    WORD *pSendSignalValue = pSignalcontrol->GetSendSignalValue();
 
    const int nBitCount = sizeof(WORD) * 8;
    DWORD dwBitValue = 1;
 
    //index 0은 Alive 신호라서 1부터 시작
    for (int nIdx = 1; nIdx < pSignalcontrol->GetSendSignalCount(); nIdx++)
    {
        if(nIdx == 1)
        {
            if((pSendSignalValue[nIdx] == 0 && pSendSignalValue[nIdx + 1] == 0))
            {
                m_nSendSignalTimeCount = 0;
                break;
            }
        }
 
        if(pSendSignalValue[nIdx] >= 1)
        {
            m_nSendSignalTimeCount++;
            if(m_nSendSignalTimeCount == 50)// 타이머 100ms 마다 실행 100 * 50 = 5000ms 5초
            {
                dwBitValue = 1;
 
                for (int nSignalIdx=0; nSignalIdx<nBitCount; nSignalIdx++)
                {
                    if(dwBitValue & pSendSignalValue[nIdx])
                    {
                        //g_pLog->DisplayMessage(_T("제어에게 신호 보냈으나 처리 안된거 같음!!!"));
                        pSequenceProcessor->SendSignalOffToSignalControl(nIdx,nSignalIdx);
                        m_nSendSignalTimeCount = 0;
                        break;
                    }
 
                    dwBitValue = dwBitValue << 1;
                }
            }
        }
    }
}
 
 
void CMainFrame::OnDestroy()
{
    __super::OnDestroy();
}
 
BOOL CMainFrame::DestroyWindow()
{
    // TODO: 여기에 특수화된 코드를 추가 및/또는 기본 클래스를 호출합니다.
    KillTimer(ID_TIMER_RESET_COMM);
 
    return __super::DestroyWindow();
}
 
void CMainFrame::OnClose()
{
    // TODO: 여기에 메시지 처리기 코드를 추가 및/또는 기본값을 호출합니다.
    if (IDNO == AfxMessageBox(_T("프로그램 종료시 \nCIM OFFLINE \n설비 매뉴얼 모드\n상태를 먼저 확인해 주십시오.\n정말 종료 하시겠습니까?"), MB_YESNO | MB_ICONQUESTION))
    {
        return;
    }
 
    DeinitReviewInterface();
 
    __super::OnClose();
}
 
void CMainFrame::OnCopyDataSystemSetting(COPYDATA_SYSTEM2REV* pCopyData)
{
    if (pCopyData==NULL) return;
 
    CMainFrame* pMain = static_cast<CMainFrame*>(AfxGetMainWnd());
    CSequenceProcessor* pSequenceProcessor = pMain->IRP2P_GetSequenceProcessor();
    if (pSequenceProcessor == NULL)    return;
 
    switch (pCopyData->Message)
    {
    case MsgSystem2Rev_StandardCameraMotorPos:
        {
            COPYDATA_SYSTEM2REV copyData;
            const SMotorPosition* pMotorPosition = Module_GetStandardMotorPosition();
            if(pMotorPosition)
            {
                copyData.Message = MsgRev2System_StandardCameraMotorPos;
                copyData.dStandardCameraMotorPosX = pMotorPosition->dMotorPosX;
                copyData.dStandardCameraMotorPosY = pMotorPosition->dMotorPosY;
                SendData2SystemSetting(&copyData);
            }
        }
        break;
 
    case MsgSystem2Rev_FindAlignMark:
        {
            if (m_pAlignCameraControl)
            {
                COPYDATA_SYSTEM2REV copyData;
 
                CString strPath = _T("");
                VectorString vectorFilename;
                strPath = pCopyData->strTemplateImagePath1;
                vectorFilename.push_back(strPath);
                strPath = pCopyData->strTemplateImagePath2;
                vectorFilename.push_back(strPath);
 
                CAlignResult alignResult = m_pAlignCameraControl->FindAlignMark(vectorFilename);
 
                switch(alignResult.nResultCode)
                {
                case Align_Success:
                    g_pLog->DisplayMessage(_T("Align Mark Find Success!"));
                    copyData.Message = MsgRev2System_FindAlignMark;
                    copyData.nFirstAlignFindPixelX = (int)alignResult.dFindPixelX[0];
                    copyData.nFirstAlignFindPixelY = (int)alignResult.dFindPixelY[0];
                    copyData.nSecondAlignFindPixelX = (int)alignResult.dFindPixelX[1];
                    copyData.nSecondAlignFindPixelY = (int)alignResult.dFindPixelY[1];
                    g_pLog->DisplayMessage(_T("FirstFindPixelX : %d, FirstFindPixelY : %d"), copyData.nFirstAlignFindPixelX, copyData.nFirstAlignFindPixelY);
                    g_pLog->DisplayMessage(_T("SecondFindPixelX : %d, SecondFindPixelY : %d"),copyData.nSecondAlignFindPixelX, copyData.nSecondAlignFindPixelY);
                    SendData2SystemSetting(&copyData);
                    break;
 
                case Align_TempFail:
                    g_pLog->DisplayMessage(_T("Align Mark Template Fail!"));
                    g_pLog->DisplayMessage(_T("Template #1: %s"), vectorFilename[0]);
                    g_pLog->DisplayMessage(_T("Template #2: %s"), vectorFilename[1]);
                    break;
 
                case Align_Fail:
                    g_pLog->DisplayMessage(_T("Align Mark Matching Fail!"));
                    break;
                }
            }
        }
        break;
 
    case MsgSystem2Rev_ChangeSysConfig:
        {
            m_bChangeSysConfig = TRUE;
            g_pLog->DisplayMessage(_T("System Setting is Changed!"));
        }
        break;
 
    // [2017:5:2]-[WEZASW] : TFE Measurement 설비 UNIT Test(Signal 수동 발생)를 위한 추가
    case MsgSystem2Rev_SendAlignAlarmSignal:
        {
            COPYDATA_SYSTEM2REV copyData;
            copyData.Message = MsgRev2System_SendAlignAlarmSignal;
            SendData2SystemSetting(&copyData);
            pSequenceProcessor->CIM_SendAlignAlarmSignal(7);
        }
        break;
 
    case MsgSystem2Rev_SendNoPPIDSignal:
        {
            COPYDATA_SYSTEM2REV copyData;
            copyData.Message = MsgRev2System_SendNoPPIDSignal;
            SendData2SystemSetting(&copyData);
            pSequenceProcessor->CIM_SendNoPPIDSignal(7);
        }
        break;
    }
}
 
void CMainFrame::OnCopyDataRecipeEditor(COPYDATA_RECIPE2REV* pCopyData)
{
    if (pCopyData==NULL) return;
 
    CMainFrame* pMain = static_cast<CMainFrame*>(AfxGetMainWnd());
    if (pMain == NULL)    {    return;    }
    CSequenceProcessor* pSequenceProcessor = pMain->IRP2P_GetSequenceProcessor();
    if (pSequenceProcessor == NULL)    return;
    
    COPYDATA_RECIPE2REV copyData;
 
    switch (pCopyData->Message)
    {
    case MsgRecipe2Rev_StandardCameraGlassPos:
        {
            const SMotorPosition* pMotorPosition = Module_GetStandardMotorPosition();
            if(pMotorPosition)
            {
                copyData.Message = MsgRev2Recipe_StandardCameraGlassPos;
                copyData.dStandardCameraGlassPosX = pMotorPosition->dGlassPosX;
                copyData.dStandardCameraGlassPosY = pMotorPosition->dGlassPosY;
                SendData2RecipeEditor(&copyData);
            }
        }
        break;
    // [2017:4:17]-[WEZASW] : CIM(Recipe) 보고 항목 추가.
    case MsgRecipe2Rev_RecipeCreate:
        {
            g_pLog->DisplayMessage(_T("[CIM_REQ] PCControl -> PC. MsgRev2Recipe_RecipeCreate"));
            copyData.Message = MsgRev2Recipe_RecipeCreate;
            SendData2RecipeEditor(&copyData);
            pSequenceProcessor->CIM_SendRecipeCreate(pCopyData->nPPID);
        }
        break;
    case MsgRecipe2Rev_RecipeChange:
        {
            g_pLog->DisplayMessage(_T("[CIM_REQ] PCControl -> PC. MsgRev2Recipe_RecipeChange"));
            copyData.Message = MsgRev2Recipe_RecipeChange;
            SendData2RecipeEditor(&copyData);
            pSequenceProcessor->CIM_SendRecipeChange(pCopyData->nPPID);
        }
        break;
    case MsgRecipe2Rev_RecipeDelete:
        {
            g_pLog->DisplayMessage(_T("[CIM_REQ] PCControl -> PC. MsgRev2Recipe_RecipeDelete"));
            copyData.Message = MsgRev2Recipe_RecipeDelete;
            SendData2RecipeEditor(&copyData);
            pSequenceProcessor->CIM_SendRecipeDelete(pCopyData->nPPID);
        }
        break;
    case MsgRecipe2Rev_RecipeModify:
        {
            g_pLog->DisplayMessage(_T("[CIM_REQ] PCControl -> PC. MsgRev2Recipe_RecipeModify"));
            copyData.Message = MsgRev2Recipe_RecipeModify;
            SendData2RecipeEditor(&copyData);
            pSequenceProcessor->CIM_SendRecipeModify(pCopyData->nPPID);
        }
        break;
    }
}
 
void CMainFrame::OnCopyDataResetComm(RESETCOMM* pCopyData)
    {
    if (pCopyData==NULL) return;
 
    m_ResetComm = *pCopyData;
 
        KillTimer(m_ResetComm.iIdentTimer);
 
        SetTimer(m_ResetComm.iIdentTimer, m_ResetComm.iIntaval, NULL);
    }
 
void CMainFrame::OnCopyDataAoiServer(SAOIServerMessage* pCopyData)
{
    if (pCopyData==NULL) return;
 
    switch (pCopyData->nMessage)
    {
    case Message_AOIServer2ReviewSystem_AlignResult:
        {
            CCoordCalibrator *pCoordCalibrator = ISP2P_GetCoordCalibrator();
 
            if (pCoordCalibrator)
            {
                double dX = pCopyData->uMessageData[0].dValue;        // dx
                double dY = pCopyData->uMessageData[1].dValue;        // dy
                double dTheta = pCopyData->uMessageData[2].dValue;    // theta
 
                SAlignResult alignResult = pCoordCalibrator->GetAlignResult();
 
                // 원점위치 재계산
                alignResult.dOriginX += dX * 0.001;
                alignResult.dOriginY += dY * 0.001;
 
                // 회전 행렬 계산.
                alignResult.dRadian  = dTheta;
                alignResult.dDegree = ToDegree(alignResult.dRadian);
                alignResult.dSinValue = sin(alignResult.dRadian);
                alignResult.dCosValue = cos(alignResult.dRadian);
 
                // 원점을 중심으로 회전하는 행렬
                alignResult.SetRotateMatrix(alignResult.dOriginX, alignResult.dOriginY);
 
                pCoordCalibrator->SetAlignResult(alignResult);
 
                // dx, dy, Theta
                g_pLog->DisplayMessage(_T("dx = %f[um], dy = %f[um], theta = %f[Dedgree]"), dX, dY, alignResult.dDegree);
 
                // Origin Coordinate
                g_pLog->DisplayMessage(_T("Origin X = %f, Origin Y = %f"), alignResult.dOriginX, alignResult.dOriginY);
            }
            else
            {
                g_pLog->DisplayMessage(_T("Align Fail! , pCoordCalibrator is Null!"));
            }
 
            pCopyData->nMessage = Message_ReviewSystem2AOIServer_AlignResultAck;
 
            SendData2AOIServer(pCopyData);
 
        }
        break;
    default:
        break;
    }
}
 
BOOL CMainFrame::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
    // TODO: 여기에 메시지 처리기 코드를 추가 및/또는 기본값을 호출합니다.
 
    switch(pCopyDataStruct->dwData)
    {
    case COPYDATA_RAW_DOWNLOAD_ACK:
    case COPYDATA_RAW_UPLOAD_ACK:
    case COPYDATA_LOT_UPLOAD_ACK:
    case COPYDATA_IMAGE_UPLOAD_ACK:
    {
        CFTPCopyDataParam ftpCopyDataParam;
 
        ftpCopyDataParam = *(static_cast<CFTPCopyDataParam*>(pCopyDataStruct->lpData));
        if (ftpCopyDataParam.m_nSendResultCode == FALSE)
        {
            CMainFrame* pMain = static_cast<CMainFrame*>(AfxGetMainWnd());
            CSequenceProcessor* pSequenceProcessor = pMain->IRP2P_GetSequenceProcessor();
            if (pSequenceProcessor == NULL)    return FALSE;
            pSequenceProcessor->sendUploadResult2SequenceProcessor(ftpCopyDataParam.m_nSendResultCode);
        }
        
    }
    break;
    case COPYDATA_SYSTEMSETTING2REVIEWSYSTEM:
        {
            COPYDATA_SYSTEM2REV* pCopyData = (COPYDATA_SYSTEM2REV*)pCopyDataStruct->lpData;
            if (pCopyData) OnCopyDataSystemSetting(pCopyData);
        }
        break;
 
    case COPYDATA_RECIPEEDITOR2REVIEWSYSTEM:
        {
            COPYDATA_RECIPE2REV* pCopyData = (COPYDATA_RECIPE2REV*)pCopyDataStruct->lpData;
            if (pCopyData) OnCopyDataRecipeEditor(pCopyData);
        }
        break;
 
    case COPYDATA_RESETCOMM:
        {
            RESETCOMM* pCopyData = (RESETCOMM*)pCopyDataStruct->lpData;
            if (pCopyData) OnCopyDataResetComm(pCopyData);
        }
        break;
 
    case COPYDATA_AOISERVER_2_REVIEWSYSTEM :
        {
            SAOIServerMessage* pCopyData = (SAOIServerMessage*)pCopyDataStruct->lpData;
            if (pCopyData) OnCopyDataAoiServer(pCopyData);
        }
        break;
 
    case COPYDATA_REVIEWHISTORYOR2REVIEWSYSTEM:
    {
        DlgReviewOffSetTool* pReviewOffSetTool = m_pView->GetReviewOffSetToolHandle();
        pReviewOffSetTool->PostMessage(WM_OFFSETTOOL_MESSAGE, 0, 200);
 
    }
    break;
    }
 
    return __super::OnCopyData(pWnd, pCopyDataStruct);
}
 
BOOL CMainFrame::SendData2SystemSetting(COPYDATA_SYSTEM2REV* pData)
{
    HWND hWnd = ::FindWindow(NULL, _SYSTEM_SETTING_TITLE_);
 
    if (hWnd && pData)
    {
        COPYDATASTRUCT cds;
        cds.dwData = COPYDATA_SYSTEMSETTING2REVIEWSYSTEM;
        cds.cbData = sizeof(COPYDATA_SYSTEM2REV);
        cds.lpData = pData;
 
        ::SendMessage(hWnd, WM_COPYDATA, 0, (LPARAM)(LPVOID)&cds);
 
        return TRUE;
    }
 
    return FALSE;
}
 
BOOL CMainFrame::SendData2RecipeEditor(COPYDATA_RECIPE2REV* pData)
{
    HWND hWnd = ::FindWindow(NULL, _RECIPE_EDITOR_TITLE_);
 
    if (hWnd && pData)
    {
        COPYDATASTRUCT cds;
        cds.dwData = COPYDATA_RECIPEEDITOR2REVIEWSYSTEM;
        cds.cbData = sizeof(COPYDATA_RECIPE2REV);
        cds.lpData = pData;
 
        ::SendMessage(hWnd, WM_COPYDATA, 0, (LPARAM)(LPVOID)&cds);
 
        return TRUE;
    }
 
    return FALSE;
}
 
BOOL CMainFrame::SendData2AOIServer( SAOIServerMessage* pData )
{
    HWND hWnd = ::FindWindow(NULL, COPYDATA_AOISERVER_NAME);
 
    if (hWnd && pData)
    {
        COPYDATASTRUCT cds;
        cds.dwData = COPYDATA_AOISERVER_2_REVIEWSYSTEM;
        cds.cbData = sizeof(SAOIServerMessage);
        cds.lpData = pData;
 
        ::SendMessage(hWnd, WM_COPYDATA, 0, (LPARAM)(LPVOID)&cds);
 
        return TRUE;
    }
    return FALSE;
}