LAPTOP-T815PCOQ\25526
2024-11-27 2cd3e98d5d0bdf341d772b7e75869cd6f2b39280
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
#include "stdafx.h"
#include "AlarmMonitor.h"
#include "Common.h"
#include "CBonder.h"
#include "Log.h"
 
#define ALARM_MAX        200
 
CAlarmMonitor::CAlarmMonitor()
{
    m_nBeginAddr = 0;
    m_nLastId = 0;
    m_nLastLevel = 0;
}
 
 
CAlarmMonitor::~CAlarmMonitor()
{
 
}
 
std::string& CAlarmMonitor::getClassName()
{
    static std::string strClassName = "CAlarmMonitor";
    return strClassName;
}
 
void CAlarmMonitor::onRecvBroadcast(void* pSender, CIntent* pIntent)
{
 
}
 
void CAlarmMonitor::onData(int id, const void* pData, int size)
{
    const char* pszData = (const char*)pData;
    if (m_nIndex + 1 == id) {
        int addr = m_nBeginAddr * 2;
        int alarmId = (pszData[addr] & 0xff) | (pszData[addr + 1] & 0xff) << 8;
        int alarmLevel = (pszData[addr + 2] & 0xff) | (pszData[addr + 3] & 0xff) << 8;
        if (m_nLastId != alarmId) {
            LOGI("<CAlarmMonitor-%d>AlarmId:%d, AlarmLevel:%d", m_nIndex, alarmId, alarmLevel);
            Alarm(alarmId, alarmLevel);
        }
    }
}
 
int CAlarmMonitor::readAlarmListFromFile(const char* pszFilepath)
{
    CStdioFile file;
    if (!file.Open(pszFilepath, CFile::modeRead)) {
        return -1;
    }
 
    CString strLine, strNumber, strDescription;
    while (file.ReadString(strLine)) {
        int index = strLine.Find(",");
        if (index <= 0) continue;
 
        strNumber = strLine.Left(index).Trim();
        strDescription = strLine.Right(strLine.GetLength() - index - 1).Trim();
 
        int number = atoi(strNumber);
        m_mapAlarmText[number] = std::string((LPTSTR)(LPCTSTR)strDescription);
    }
 
    file.Close();
    return (int)m_mapAlarmText.size();
}
 
void CAlarmMonitor::init()
{
    CComponent::init();
}
 
void CAlarmMonitor::term()
{
    for (auto item : m_alarms) {
        item->release();
    }
}
 
void CAlarmMonitor::setBeginAddr(int nAddr)
{
    m_nBeginAddr = nAddr;
}
 
void CAlarmMonitor::OnTimer(UINT nTimerid)
{
 
}
 
const char* CAlarmMonitor::getAlarmText(int nID)
{
    auto iter = m_mapAlarmText.find(nID);
    if (iter == m_mapAlarmText.end()) return "";
 
    return iter->second.c_str();
}
 
void CAlarmMonitor::Alarm(int id, int level)
{
    BEQ::IUnit* pUnit = m_pBonder->getUnit(m_nIndex);
    ASSERT(pUnit);
 
    if (id != 0) {
        // ¾¯¸æ
        pUnit->setAlarm(id, level, getAlarmText(id));
        LOGI("<CAlarmMonitor-%d>É豸¸æ¾¯(%d, %d).", m_nIndex, id, level);
 
        CAlarm* pAlarm = new CAlarm(m_nIndex, 1, id, level, getAlarmText(id));
        AddAlarm(pAlarm);
        SendBroadcast(&CIntent(BC_CODE_ALARM_EVENT, "", pAlarm));
        m_nLastId = id;
        m_nLastLevel = level;
    }
    else {
        // ½â³ý¾¯¸æ
        pUnit->setAlarm(0, 0, "");
        LOGI("<CAlarmMonitor-%d>½â³ýÉ豸¸æ¾¯(%d, %d).", m_nIndex, m_nLastId, m_nLastLevel);
 
        CAlarm* pAlarm = new CAlarm(m_nIndex, 2, m_nLastId, m_nLastLevel, getAlarmText(m_nLastId));
        AddAlarm(pAlarm);
        SendBroadcast(&CIntent(BC_CODE_ALARM_EVENT, "", pAlarm));
        m_nLastId = 0;
        m_nLastLevel = 0;
    }
}
 
void CAlarmMonitor::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 CAlarmMonitor::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();
}
 
void CAlarmMonitor::getAlarmList(std::list<CAlarm*>& list)
{
    Lock();
    list = m_alarms;
    Unlock();
}