#include "CAiAlarmDataCollect.h" #include "pub_logger_api/logger.h" #include "CAlarmMsgManage.h" #include #include "CAlarmDataCollect.h" using namespace iot_service; CAiAlarmDataCollect * CAiAlarmDataCollect::m_pInstance = NULL; CAiAlarmDataCollect::CAiAlarmDataCollect() : CIntelliAlmApi4Clt(), m_referenceCount(0), m_bFaultRecallState(false), m_pAlternateTimer(Q_NULLPTR) { m_rtdbMutex = new QMutex(); m_rtdbPriorityOrderAccess = new iot_dbms::CRdbAccess(); m_rtdbPriorityOrderAccess->open("base", "alarm_level_define"); } CAiAlarmDataCollect::~CAiAlarmDataCollect() { LOGDEBUG("CAiAlarmDataCollect::~CAiAlarmDataCollect()"); qDebug() << "~CAiAlarmDataCollect()"; } void CAiAlarmDataCollect::refrence() { m_referenceCount++; } int CAiAlarmDataCollect::getRefrenceCount() { return m_referenceCount; } CAiAlarmDataCollect *CAiAlarmDataCollect::instance() { if(NULL == m_pInstance) { m_pInstance = new CAiAlarmDataCollect(); } return m_pInstance; } void CAiAlarmDataCollect::initialize() { resumeThread(); if(!m_pAlternateTimer) { m_pAlternateTimer = new QTimer(); m_pAlternateTimer->setInterval(1000); connect(m_pAlternateTimer, &QTimer::timeout, this, &CAiAlarmDataCollect::slotAiAlarmStateChanged); connect(this, &CAiAlarmDataCollect::sigTimerShot, this, &CAiAlarmDataCollect::slotTimerShot, Qt::QueuedConnection); } if(!m_bFaultRecallState) { emit sigTimerShot(true); } } void CAiAlarmDataCollect::release() { emit sigTimerShot(false); suspendThread(true); } bool CAiAlarmDataCollect::isFaultRecallState() { return m_bFaultRecallState; } bool CAiAlarmDataCollect::requestDelAlm(iot_idl::SIntelliAlmDel &objDelAlm) { LOGDEBUG("请求删除!"); return iot_service::CIntelliAlmApi4Clt::requestDelAlm(objDelAlm); } bool CAiAlarmDataCollect::requestSeprAlm(iot_idl::SIntelliAlmSepr &objSeprAlm) { LOGDEBUG("请求分离!"); return iot_service::CIntelliAlmApi4Clt::requestSeprAlm(objSeprAlm); } bool CAiAlarmDataCollect::requestMergeAlm(iot_idl::SIntelliAlmMerge &objMergeAlm) { LOGDEBUG("请求合并!"); return iot_service::CIntelliAlmApi4Clt::requestMergeAlm(objMergeAlm); } void CAiAlarmDataCollect::handleAllAlmMsg(int nDomainId, iot_idl::SIntelliAlmAdd &objAllAlm) { LOGINFO("========== AiAlarmDataCollect handleAllAlmMsg =========="); //< 清空该域缓存 QMutexLocker locker(m_rtdbMutex); CAlarmMsgManage::instance()->removeAiAlarmMsgByDomainID(nDomainId); //构建告警 int nAlarmCount = objAllAlm.alm_info_size(); QList almList; for(int nAddMsgIndex(0); nAddMsgIndex < nAlarmCount; nAddMsgIndex++) { iot_idl::SIntelliAlmInfo msg = objAllAlm.alm_info(nAddMsgIndex); AiAlarmMsgPtr alm(new CAiAlarmMsgInfo()); alm->initialize(msg); alm->priorityOrder = queryPriorityOrder(alm->priority); almList.append(alm); } CAlarmMsgManage::instance()->addAiAllAlarmMsg(almList);//添加全部完成后,通知model重新拉取数据 LOGDEBUG("AiAlarmDataCollect handleAllAlmMsg():count[%d]",nAlarmCount); emit sigMsgRefresh(); } void CAiAlarmDataCollect::handleAddAlmMsg(iot_idl::SIntelliAlmAdd &objAddAlm) { LOGINFO("========== AiAlarmDataCollect handleAddAlmMsg =========="); QMutexLocker locker(m_rtdbMutex); //构建告警 int nAlarmCount = objAddAlm.alm_info_size(); QList almList; for(int nAddMsgIndex(0); nAddMsgIndex < nAlarmCount; nAddMsgIndex++) { iot_idl::SIntelliAlmInfo msg = objAddAlm.alm_info(nAddMsgIndex); AiAlarmMsgPtr alm(new CAiAlarmMsgInfo()); alm->initialize(msg); alm->priorityOrder = queryPriorityOrder(alm->priority); almList.append(alm); } CAlarmMsgManage::instance()->addAiAlarmMsg(almList); //单独添加的时候,来一条通知一次model添加 LOGDEBUG("AiAlarmDataCollect handleAddAlmMsg():count[%d]",nAlarmCount); } void CAiAlarmDataCollect::handleDelAlmMsg(iot_idl::SIntelliAlmDel &objDelAlm) { LOGINFO("========== AiAlarmDataCollect handleDelAlmMsg =========="); QMutexLocker locker(m_rtdbMutex); //构建告警 int nAlarmCount = objDelAlm.uuid_base64_size(); QList uuidList; for(int nDelMsgIndex(0); nDelMsgIndex < nAlarmCount; nDelMsgIndex++) { QString uuid = QString::fromStdString(objDelAlm.uuid_base64(nDelMsgIndex)); uuidList.append(uuid); } CAlarmMsgManage::instance()->delAiAlarmMsg(uuidList); LOGDEBUG("AiAlarmDataCollect handleDelAlmMsg():count[%d]",nAlarmCount); } void CAiAlarmDataCollect::handleBrokenAlmMsg(iot_idl::SIntelliAlmBroken &objBrokenAlm) { //先保证能够编译通过 LOGINFO("========== AiAlarmDataCollect handleBrokenAlmMsg =========="); QMutexLocker locker(m_rtdbMutex); int nAlarmCount = objBrokenAlm.uuid_base64_size(); QList uuidList; for(int nBrokenMsgIndex(0); nBrokenMsgIndex < nAlarmCount; nBrokenMsgIndex++) { QString uuid = QString::fromStdString(objBrokenAlm.uuid_base64(nBrokenMsgIndex)); uuidList.append(uuid); } CAlarmMsgManage::instance()->brokenAiAlarmMsg(uuidList); LOGDEBUG("AiAlarmDataCollect handleBrokenAlmMsg():count[%d]",nAlarmCount); } void CAiAlarmDataCollect::handleReleaseAlmMsg(iot_idl::SIntelliAlmRelease &objReleaseAlm) { //先保证能够编译通过 LOGINFO("========== AiAlarmDataCollect handleReleaseAlmMsg =========="); QMutexLocker locker(m_rtdbMutex); int nAlarmCount = objReleaseAlm.uuid_base64_size(); QList uuidList; for(int nReleaseMsgIndex(0); nReleaseMsgIndex < nAlarmCount; nReleaseMsgIndex++) { QString uuid = QString::fromStdString(objReleaseAlm.uuid_base64(nReleaseMsgIndex)); uuidList.append(uuid); } CAlarmMsgManage::instance()->releaseAiAlarmMsg(uuidList); LOGDEBUG("AiAlarmDataCollect handleReleaseAlmMsg():count[%d]",nAlarmCount); } void CAiAlarmDataCollect::destory() { LOGINFO("退出时:CAiAlarmDataCollect::destory()打开窗口的个数m_referenceCount:[%d]",m_referenceCount); if(--m_referenceCount > 0) { return; } slotTimerShot(false); suspendThread(); { QMutexLocker locker(m_rtdbMutex); if(Q_NULLPTR != m_pAlternateTimer) { m_pAlternateTimer->stop(); m_pAlternateTimer->deleteLater(); } m_pAlternateTimer = Q_NULLPTR; } delete m_rtdbMutex; delete m_rtdbPriorityOrderAccess; m_pInstance = NULL; delete this; } void CAiAlarmDataCollect::slotTimerShot(const bool start) { if(m_pAlternateTimer) { if(start) { m_pAlternateTimer->start(); } else { m_pAlternateTimer->stop(); } } } void CAiAlarmDataCollect::slotSwitchFaultRecallState(bool bFaultRecallState) { m_bFaultRecallState = bFaultRecallState; release(); m_pInstance->reinit(m_bFaultRecallState); CAlarmMsgManage::instance()->release(); //initialize(); } int CAiAlarmDataCollect::queryPriorityOrder(int &id) { iot_dbms::CTableLockGuard locker(*m_rtdbPriorityOrderAccess); iot_dbms::CVarType value = -1000; m_rtdbPriorityOrderAccess->getColumnValueByKey((void*)&id, "priority_order", value); return value.toInt(); } void CAiAlarmDataCollect::slotAiAlarmStateChanged() { QMutexLocker locker(m_rtdbMutex); CAlarmMsgManage::instance()->dealDelayAi(); }