141 lines
5.1 KiB
C++
141 lines
5.1 KiB
C++
#include "P08_AlarmPage.h"
|
|
#include "ui_P08_AlarmPage.h"
|
|
#include <DataCenter/MySQL.h>
|
|
#include <QTimer>
|
|
#include <QDateTime>
|
|
#include <QDebug>
|
|
#include <GlobalDefinitions/Variable.h>
|
|
#include <Threads/MultiCoreManager.h>
|
|
MySQL P08_sql;
|
|
P08_AlarmPage::P08_AlarmPage(QWidget *parent) :
|
|
QWidget(parent),
|
|
ui(new Ui::P08_AlarmPage)
|
|
{
|
|
ui->setupUi(this);
|
|
QTimer::singleShot(1000, this, &P08_AlarmPage::WinInit);
|
|
}
|
|
|
|
P08_AlarmPage::~P08_AlarmPage()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
static QColor parseCssRgb(const QString &css)
|
|
{
|
|
// 正则把 "rgb(255, 0, 0)" 里的数字抠出来
|
|
QRegularExpression re("rgb\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)");
|
|
QRegularExpressionMatch m = re.match(css);
|
|
if (m.hasMatch())
|
|
return QColor(m.captured(1).toInt(),
|
|
m.captured(2).toInt(),
|
|
m.captured(3).toInt());
|
|
return Qt::black; // 解析失败就回退到黑色
|
|
}
|
|
void P08_AlarmPage::WinInit()
|
|
{
|
|
m_oldAlarmCode = 0;
|
|
//所用定时器初始化
|
|
QTimer* UIRefresh_Timer = new QTimer(this);
|
|
connect(UIRefresh_Timer, &QTimer::timeout, this, &P08_AlarmPage::UIRefreshTimeOut);
|
|
UIRefresh_Timer->setInterval(100); // 设置定时器间隔为 100 毫秒
|
|
UIRefresh_Timer->start();
|
|
#if defined(Q_OS_LINUX)
|
|
if(!P08_sql.isConnectOK) return;
|
|
// 使用多核心管理器异步加载历史报警数据
|
|
MultiCoreManager::instance()->submitTask([this]() {
|
|
if(!P08_sql.isConnectOK) return;
|
|
|
|
// 加载历史报警数据
|
|
auto rows = P08_sql.selectLatest("AlarmHistory", 100);
|
|
|
|
// 准备批量更新数据
|
|
QList<QPair<QString, QString>> alarmItems;
|
|
|
|
for (const auto &row : rows) {
|
|
// 0 时间 1 报警内容 2 排查方法 3 报警代码
|
|
QDateTime t = row[0].toDateTime();
|
|
QString text1 = row[1].toString();
|
|
QString CodeText = row[3].toString();
|
|
|
|
/* 把 16 位 code 拆成高 8 位设备码 + 低 8 位索引码 */
|
|
bool ok;
|
|
uint16_t code = CodeText.toUShort(&ok, 16); // 16 表示按 16 进制解析
|
|
uint8_t devCode = code / 256; // 高 8 位
|
|
uint8_t idxCode = code % 256; // 低 8 位
|
|
|
|
/* 显示格式: [时间] 设备码-索引码 内容 排查方法 */
|
|
QString AlarmTexts = QString("[%1] %2 %3")
|
|
.arg(t.toString("MM-dd hh:mm:ss"))
|
|
.arg(CodeText,text1);
|
|
|
|
QString colorStr = AlarmText[devCode][idxCode].TextColor;
|
|
alarmItems.append(qMakePair(AlarmTexts, colorStr));
|
|
|
|
if(m_oldAlarmCode == 0)
|
|
m_oldAlarmCode = code;
|
|
}
|
|
|
|
// 在主线程中更新UI
|
|
QMetaObject::invokeMethod(this, [this, alarmItems]() {
|
|
// 批量添加到列表
|
|
for (const auto& item : alarmItems) {
|
|
QListWidgetItem *listItem = new QListWidgetItem(item.first);
|
|
listItem->setForeground(parseCssRgb(item.second));
|
|
ui->ListWidget_AlarmList->addItem(listItem);
|
|
}
|
|
}, Qt::QueuedConnection);
|
|
}, "alarm_history_loading");
|
|
|
|
#elif defined(Q_OS_WIN)
|
|
//qDebug() << All_SqlTable[i] << otherFields;
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
void P08_AlarmPage::UIRefreshTimeOut()
|
|
{
|
|
// 使用多核心管理器处理报警数据
|
|
//MultiCoreManager::instance()->submitTask([this]() {
|
|
uint16_t NewAlarmCode = getNodeValue("AlarmCode.New").toUInt();
|
|
if (NewAlarmCode == m_oldAlarmCode)
|
|
return;
|
|
|
|
// 使用局部变量暂存最新报警码
|
|
uint16_t localOldAlarmCode = m_oldAlarmCode;
|
|
m_oldAlarmCode = NewAlarmCode;
|
|
|
|
<<<<<<< HEAD
|
|
// 解析设备码 + 索引码
|
|
uint8_t devCode = NewAlarmCode >> 8; // 高 8 位
|
|
uint8_t idxCode = NewAlarmCode & 0xFF; // 低 8 位
|
|
=======
|
|
uint16_t NewAlarmCode = getNodeValue("AlarmCode.New").toUInt();
|
|
if (NewAlarmCode == m_oldAlarmCode)
|
|
return;
|
|
m_oldAlarmCode = NewAlarmCode;
|
|
>>>>>>> f88b815723b08bbfe04dcdec05fc5555cef9352c
|
|
|
|
// 格式化字符串
|
|
QString timeStr = QDateTime::currentDateTime().toString("MM-dd hh:mm:ss");
|
|
QString alarmStr = QString("[%1] %2 %3")
|
|
.arg(timeStr)
|
|
.arg(QString::number(NewAlarmCode,16))
|
|
.arg(AlarmText[devCode][idxCode].AlarmText);
|
|
|
|
QString colorStr = AlarmText[devCode][idxCode].TextColor;
|
|
|
|
// 在主线程中更新UI
|
|
QMetaObject::invokeMethod(this, [this, alarmStr, colorStr]() {
|
|
QListWidgetItem *item = new QListWidgetItem(alarmStr);
|
|
item->setForeground(parseCssRgb(colorStr));
|
|
ui->ListWidget_AlarmList->addItem(item);
|
|
|
|
// 限制列表大小,防止内存溢出
|
|
if (ui->ListWidget_AlarmList->count() > 500) {
|
|
delete ui->ListWidget_AlarmList->takeItem(0);
|
|
}
|
|
}, Qt::QueuedConnection);
|
|
// }, "alarm_processing");
|
|
}
|