#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();
|
}
|