chenluhua1980
2025-12-10 db9d120efcfe76bb73df089dca8986eca9ee0e6f
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
// CComponentData1Dlg.cpp: 实现文件
//
 
#include "stdafx.h"
#include "BondEq.h"
#include "CComponentPLCDlg.h"
#include "afxdialogex.h"
#include "Log.h"
#include "ToolUnits.h"
#include "Common.h"
#include "AxisSettingsDlg.h"
#include "IOMonitoringDlg.h"
 
 
// CComponentData1Dlg 对话框
 
IMPLEMENT_DYNAMIC(CComponentPLCDlg, CComponentDlg)
 
CComponentPLCDlg::CComponentPLCDlg(CWnd* pParent /*=nullptr*/)
    : CComponentDlg(IDD_COMPONENT_PLC, pParent)
{
    m_pObserver = nullptr;
}
 
CComponentPLCDlg::~CComponentPLCDlg()
{
}
 
void CComponentPLCDlg::DoDataExchange(CDataExchange* pDX)
{
    CComponentDlg::DoDataExchange(pDX);
}
 
 
BEGIN_MESSAGE_MAP(CComponentPLCDlg, CComponentDlg)
    ON_WM_CTLCOLOR()
    ON_WM_DESTROY()
    ON_WM_SIZE()
    ON_BN_CLICKED(IDC_BUTTON_PLC_AXIS_SETTINGS, &CComponentPLCDlg::OnBnClickedButtonAxisSetting)
    ON_BN_CLICKED(IDC_BUTTON_PLC_IO, &CComponentPLCDlg::OnBnClickedButtonIO)
    ON_WM_TIMER()
END_MESSAGE_MAP()
 
 
// CComponentData1Dlg 消息处理程序
 
void CComponentPLCDlg::InitRxWindows()
{
    /* code */
    // 订阅数据
    IRxWindows* pRxWindows = RX_GetRxWindows();
    if (m_pObserver == NULL) {
        m_pObserver = pRxWindows->allocObserver([&](IAny* pAny) -> void {
            // onNext
            pAny->addRef();
            int code = pAny->getCode();
            if (RX_CODE_PLC1_CONNECTTD == code) {
                CComponent* pComponent;
                if (pAny->getPtrValue("ptr", (void*&)pComponent)
                    && pComponent == m_pContext) {
                    EnableGroup1(TRUE);
                }
            }
            else if (RX_CODE_PLC1_DISCONNECTTD == code) {
                CComponent* pComponent;
                if (pAny->getPtrValue("ptr", (void*&)pComponent)
                    && pComponent == m_pContext) {
                    EnableGroup1(FALSE);
                }
            }
            pAny->release();
            }, [&]() -> void {
                // onComplete
            }, [&](IThrowable* pThrowable) -> void {
                // onErrorm
                pThrowable->printf();
            });
 
        theApp.m_model.getObservable()->observeOn(pRxWindows->mainThread())
            ->subscribe(m_pObserver);
    }
}
 
BOOL CComponentPLCDlg::OnInitDialog()
{
    CComponentDlg::OnInitDialog();
    SetTimer(1, 200, NULL);
 
    return TRUE;  // return TRUE unless you set the focus to a control
                  // 异常: OCX 属性页应返回 FALSE
}
 
void CComponentPLCDlg::OnTimer(UINT_PTR nIDEvent)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    if (1 == nIDEvent) {
        KillTimer(1);
        InitRxWindows();
        CPLC* pPLC = (CPLC*)m_pContext;
        EnableGroup1(pPLC->isConnected());
    }
 
    CComponentDlg::OnTimer(nIDEvent);
}
 
HBRUSH CComponentPLCDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
 
    if (nCtlColor == CTLCOLOR_STATIC) {
        pDC->SetBkColor(m_crBkgnd);
    }
 
    if (m_hbrBkgnd == nullptr) {
        m_hbrBkgnd = CreateSolidBrush(m_crBkgnd);
    }
 
    return m_hbrBkgnd;
}
 
 
void CComponentPLCDlg::OnDestroy()
{
    CComponentDlg::OnDestroy();
 
    if (m_hbrBkgnd != nullptr) {
        ::DeleteObject(m_hbrBkgnd);
    }
 
    if (m_pObserver != NULL) {
        m_pObserver->unsubscribe();
        m_pObserver = NULL;
    }
}
 
 
void CComponentPLCDlg::OnSize(UINT nType, int cx, int cy)
{
    CComponentDlg::OnSize(nType, cx, cy);
    //if (GetDlgItem(IDC_LIST_ALARM) == nullptr) return;
/*
    int y = 12;
    int x = 12;
    CRect rcClient, rcItem;
    CWnd* pItem;
    GetClientRect(&rcClient);
 
    pItem = GetDlgItem(IDC_LIST_ALARM);
    pItem->MoveWindow(x, y, rcClient.Width() - x - 12, rcClient.Height() - y - 12);
*/
}
 
void CComponentPLCDlg::OnBnClickedButtonAxisSetting()
{
    std::string strName = RecipeManager::getInstance().getCurrentRecipeName();
    if (strName.empty()) {
        AfxMessageBox("未选择配方!", MB_ICONERROR);
    }
 
    CAxisSettingsDlg axisDlg;
    axisDlg.SetPLC((CPLC*)m_pContext);
    axisDlg.SetRecipeName(strName.c_str());
    axisDlg.DoModal();
}
 
void CComponentPLCDlg::OnBnClickedButtonIO()
{
    CIOMonitoringDlg dlg;
    dlg.SetIOManager("Cavity");
    dlg.SetPLC(theApp.m_model.getBonder().getPLC("PLC(1)"));
    dlg.DoModal();
}
 
void CComponentPLCDlg::EnableGroup1(BOOL bEnable)
{
    GetDlgItem(IDC_BUTTON_PLC_AXIS_SETTINGS)->EnableWindow(bEnable);
    GetDlgItem(IDC_BUTTON_PLC_IO)->EnableWindow(bEnable);
}