Files
EJM_Display/Pages/P08_AlarmPage.cpp

101 lines
3.4 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>
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-09-30 17:42:22 +08:00
QTimer::singleShot(3000, 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);
UIRefresh_Timer->setInterval(100); // 设置定时器间隔为 1000 毫秒1 秒)
UIRefresh_Timer->start();
#if defined(Q_OS_LINUX)
2025-09-15 22:28:43 +08:00
auto rows = P08_sql.selectLatest("AlarmHistory", 100);
for (const auto &row : rows) {
2025-09-30 17:42:22 +08:00
2025-09-15 22:28:43 +08:00
// 0 时间 1 报警内容 2 排查方法 3 报警代码
QDateTime t = row[0].toDateTime();
QString text1 = row[1].toString();
QString text2 = row[2].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;
QListWidgetItem *item = new QListWidgetItem(AlarmTexts);
item->setForeground(parseCssRgb(colorStr));
ui->ListWidget_AlarmList->addItem(item);
2025-09-30 17:42:22 +08:00
if(m_oldAlarmCode == 0)
m_oldAlarmCode = code;
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()
{
uint16_t NewAlarmCode = gOPC_NodeValue["ns=6;s=::AsGlobalPV:AlarmCode.New"].toUInt();
if (NewAlarmCode == m_oldAlarmCode)
return;
m_oldAlarmCode = NewAlarmCode;
// 2. 解析设备码 + 索引码
uint8_t devCode = NewAlarmCode >> 8; // 高 8 位
uint8_t idxCode = NewAlarmCode & 0xFF; // 低 8 位
2025-09-15 22:28:43 +08:00
2025-09-30 17:42:22 +08:00
// 4. 格式化字符串
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-15 22:28:43 +08:00
2025-09-30 17:42:22 +08:00
QString colorStr = AlarmText[devCode][idxCode].TextColor;
QListWidgetItem *item = new QListWidgetItem(alarmStr);
item->setForeground(parseCssRgb(colorStr));
ui->ListWidget_AlarmList->addItem(item);
2025-09-15 22:28:43 +08:00
}