Files
EJM_Display/Pages/P08_AlarmPage.cpp

134 lines
4.7 KiB
C++
Raw Normal View History

2025-08-20 23:06:28 +08:00
#include "P08_AlarmPage.h"
#include "ui_P08_AlarmPage.h"
2025-09-15 22:28:43 +08:00
#include <DataCenter/MySQL.h>
#include <QTimer>
#include <QDateTime>
#include <QDebug>
#include <GlobalDefinitions/Variable.h>
2025-10-21 11:11:52 +08:00
2025-09-15 22:28:43 +08:00
MySQL P08_sql;
2025-08-20 23:06:28 +08:00
P08_AlarmPage::P08_AlarmPage(QWidget *parent) :
QWidget(parent),
ui(new Ui::P08_AlarmPage)
{
ui->setupUi(this);
2025-10-21 11:11:52 +08:00
QTimer::singleShot(1000, this, &P08_AlarmPage::WinInit);
2025-08-20 23:06:28 +08:00
}
P08_AlarmPage::~P08_AlarmPage()
{
delete ui;
}
2025-09-30 17:42:22 +08:00
2025-09-15 22:28:43 +08:00
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; // 解析失败就回退到黑色
}
2025-09-30 17:42:22 +08:00
void P08_AlarmPage::WinInit()
2025-09-15 22:28:43 +08:00
{
2025-09-30 17:42:22 +08:00
m_oldAlarmCode = 0;
//所用定时器初始化
QTimer* UIRefresh_Timer = new QTimer(this);
connect(UIRefresh_Timer, &QTimer::timeout, this, &P08_AlarmPage::UIRefreshTimeOut);
2025-10-21 11:11:52 +08:00
UIRefresh_Timer->setInterval(100); // 设置定时器间隔为 100 毫秒
2025-09-30 17:42:22 +08:00
UIRefresh_Timer->start();
#if defined(Q_OS_LINUX)
2025-10-21 11:11:52 +08:00
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");
2025-09-15 22:28:43 +08:00
2025-09-30 17:42:22 +08:00
#elif defined(Q_OS_WIN)
//qDebug() << All_SqlTable[i] << otherFields;
#endif
}
void P08_AlarmPage::UIRefreshTimeOut()
{
2025-10-21 11:11:52 +08:00
// 使用多核心管理器处理报警数据
//MultiCoreManager::instance()->submitTask([this]() {
uint16_t NewAlarmCode = getNodeValue("AlarmCode.New").toUInt();
if (NewAlarmCode == m_oldAlarmCode)
return;
2025-09-30 17:42:22 +08:00
2025-10-21 11:11:52 +08:00
// 使用局部变量暂存最新报警码
uint16_t localOldAlarmCode = m_oldAlarmCode;
m_oldAlarmCode = NewAlarmCode;
2025-09-30 17:42:22 +08:00
2025-10-21 11:11:52 +08:00
// 解析设备码 + 索引码
uint8_t devCode = NewAlarmCode >> 8; // 高 8 位
uint8_t idxCode = NewAlarmCode & 0xFF; // 低 8 位
2025-09-30 17:42:22 +08:00
2025-10-21 11:11:52 +08:00
// 格式化字符串
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);
2025-09-30 17:42:22 +08:00
2025-10-21 11:11:52 +08:00
QString colorStr = AlarmText[devCode][idxCode].TextColor;
2025-09-15 22:28:43 +08:00
2025-10-21 11:11:52 +08:00
// 在主线程中更新UI
QMetaObject::invokeMethod(this, [this, alarmStr, colorStr]() {
QListWidgetItem *item = new QListWidgetItem(alarmStr);
item->setForeground(parseCssRgb(colorStr));
ui->ListWidget_AlarmList->addItem(item);
2025-09-15 22:28:43 +08:00
2025-10-21 11:11:52 +08:00
// 限制列表大小,防止内存溢出
if (ui->ListWidget_AlarmList->count() > 500) {
delete ui->ListWidget_AlarmList->takeItem(0);
}
}, Qt::QueuedConnection);
// }, "alarm_processing");
2025-09-15 22:28:43 +08:00
}