LAPTOP-T815PCOQ\25526
2024-12-18 50a42e5d72e2f8cf92ff9b2273e0442977dbcefd
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
// PageAlarm.cpp : ÊµÏÖÎļþ
//
 
#include "stdafx.h"
#include "BondEq.h"
#include "CPageAlarm.h"
#include "afxdialogex.h"
#include "Common.h"
#include "ToolUnits.h"
 
 
// CPageAlarm ¶Ô»°¿ò
 
IMPLEMENT_DYNAMIC(CPageAlarm, CDialogEx)
 
CPageAlarm::CPageAlarm(CWnd* pParent /*=NULL*/)
    : CDialogEx(IDD_PAGE_ALARM, pParent)
{
    m_crBkgnd = PAGE_BACKGROUND_COLOR;
    m_hbrBkgnd = nullptr;
    m_pObserver = nullptr;
}
 
CPageAlarm::~CPageAlarm()
{
}
 
void CPageAlarm::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}
 
 
BEGIN_MESSAGE_MAP(CPageAlarm, CDialogEx)
    ON_WM_CTLCOLOR()
    ON_WM_DESTROY()
    ON_WM_SIZE()
END_MESSAGE_MAP()
 
 
// CPageAlarm ÏûÏ¢´¦Àí³ÌÐò
 
void CPageAlarm::InitRxWindow()
{
    /* 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_ALARM_ON == code) {
                CAlarm* pAlarm;
                if (pAny->getObject("obj", (IRxObject*&)pAlarm)) {
                    AddAlarm(nullptr, pAlarm);
                }
            }
            else if (RX_CODE_ALARM_OFF == code) {
                CAlarm* pAlarm;
                if (pAny->getObject("obj", (IRxObject*&)pAlarm)) {
                    UpdateAlarm(nullptr, pAlarm);
                }
            }
 
            pAny->release();
        }, [&]() -> void {
            // onComplete
        }, [&](IThrowable* pThrowable) -> void {
            // onErrorm
            pThrowable->printf();
        });
 
        theApp.m_model.getObservable()->observeOn(pRxWindows->mainThread())
            ->subscribe(m_pObserver);
    }
}
 
BOOL CPageAlarm::OnInitDialog()
{
    CDialogEx::OnInitDialog();
    InitRxWindow();
 
 
    // ±¨±í¿Ø¼þ
    CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_ALARM);
    DWORD dwStyle = pListCtrl->GetExtendedStyle();
    dwStyle |= LVS_EX_FULLROWSELECT;
    dwStyle |= LVS_EX_GRIDLINES;
    pListCtrl->SetExtendedStyle(dwStyle);
 
    HIMAGELIST imageList = ImageList_Create(24, 24, ILC_COLOR24, 1, 1);
    ListView_SetImageList(pListCtrl->GetSafeHwnd(), imageList, LVSIL_SMALL);
    pListCtrl->InsertColumn(0, _T(""), LVCFMT_RIGHT, 0);
    pListCtrl->InsertColumn(2, _T("¾¯¸æID"), LVCFMT_LEFT, 100);
    pListCtrl->InsertColumn(3, _T("ÃèÊö"), LVCFMT_LEFT, 580);
    pListCtrl->InsertColumn(4, _T("·¢Éúʱ¼ä"), LVCFMT_LEFT, 180);
    pListCtrl->InsertColumn(5, _T("½â³ýʱ¼ä"), LVCFMT_LEFT, 180);
 
 
    Resize();
    LoadAlarms();
 
    return TRUE;  // return TRUE unless you set the focus to a control
                  // Òì³£: OCX ÊôÐÔÒ³Ó¦·µ»Ø FALSE
}
 
 
HBRUSH CPageAlarm::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 CPageAlarm::OnDestroy()
{
    CDialogEx::OnDestroy();
 
    if (m_hbrBkgnd != nullptr) {
        ::DeleteObject(m_hbrBkgnd);
    }
 
    if (m_pObserver != NULL) {
        m_pObserver->unsubscribe();
        m_pObserver = NULL;
    }
}
 
void CPageAlarm::OnSize(UINT nType, int cx, int cy)
{
    CDialogEx::OnSize(nType, cx, cy);
    if (GetDlgItem(IDC_LIST_ALARM) == nullptr) return;
    Resize();
}
 
void CPageAlarm::Resize()
{
    CWnd* pItem;
    CRect rcClient;
    GetClientRect(&rcClient);
 
    pItem = GetDlgItem(IDC_LIST_ALARM);
    pItem->MoveWindow(12, 12, rcClient.Width() - 24, rcClient.Height() - 24);
}
 
void CPageAlarm::LoadAlarms()
{
    CAlarmMonitor* pMonitor = (CAlarmMonitor*)theApp.m_model.getBonder().GetComponent(ALARM_MONITOR);
 
 
    // µ±Ç°ÕýÔÚ·¢ÉúµÄ±¨¾¯
    pMonitor->Lock();
    std::map<int, CAlarm*>& alarmings = pMonitor->getAlarmingMap();
    for (auto item : alarmings) {
        AddAlarm(pMonitor, item.second);
    }
    pMonitor->Unlock();
 
 
    // »ñÈ¡ÀúÊ·±¨¾¯Êý¾Ý
    auto vecData = AlarmManager::getInstance().getAllAlarms();
 
    // Ìî³äÊý¾Ý
    CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_ALARM);
    for (auto item : vecData) {
        pListCtrl->InsertItem(0, _T(""));
        pListCtrl->SetItemText(0, 1, item[0].c_str());
        pListCtrl->SetItemText(0, 2, item[1].c_str());
        pListCtrl->SetItemText(0, 3, item[2].c_str());
        pListCtrl->SetItemText(0, 4, item[3].c_str());
    }
}
 
void CPageAlarm::AddAlarm(CAlarmMonitor* pMonitor, CAlarm* pAlarm)
{
    if (pMonitor == nullptr) {
        pMonitor = (CAlarmMonitor*)theApp.m_model.getBonder().GetComponent(ALARM_MONITOR);
    }
    CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_ALARM);
    pListCtrl->InsertItem(0, _T(""));
    pListCtrl->SetItemData(0, (DWORD_PTR)pAlarm);
    pAlarm->addRef();
    pListCtrl->SetItemText(0, 1, std::to_string(pAlarm->getId()).c_str());
    pListCtrl->SetItemText(0, 2, pMonitor->getAlarmText(pAlarm->getId()));
    pListCtrl->SetItemText(0, 3, CToolUnits::timeToString2(pAlarm->getOnTime()).c_str());
    if (pAlarm->getOffTime() > 0) {
        pListCtrl->SetItemText(0, 4, CToolUnits::timeToString2(pAlarm->getOffTime()).c_str());
    }
}
 
void CPageAlarm::UpdateAlarm(CAlarmMonitor* pMonitor, CAlarm* pAlarm)
{
    if (pMonitor == nullptr) {
        pMonitor = (CAlarmMonitor*)theApp.m_model.getBonder().GetComponent(ALARM_MONITOR);
    }
    CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_ALARM);
    for (int i = 0; i < pListCtrl->GetItemCount(); i++) {
        if (pListCtrl->GetItemData(i) == (DWORD_PTR)pAlarm) {
            if (pAlarm->getOffTime() > 0) {
                pListCtrl->SetItemText(i, 4, CToolUnits::timeToString2(pAlarm->getOffTime()).c_str());
            }
        }
    }
}
 
BOOL CPageAlarm::DestroyWindow()
{
    CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_ALARM);
    for (int i = 0; i < pListCtrl->GetItemCount(); i++) {
        CAlarm* pAlarm = (CAlarm*)pListCtrl->GetItemData(i);
        if (pAlarm != nullptr) {
            pAlarm->release();
        }
    }
 
    return CDialogEx::DestroyWindow();
}