101 lines
3.4 KiB
C++
101 lines
3.4 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>
|
||
MySQL P08_sql;
|
||
P08_AlarmPage::P08_AlarmPage(QWidget *parent) :
|
||
QWidget(parent),
|
||
ui(new Ui::P08_AlarmPage)
|
||
{
|
||
ui->setupUi(this);
|
||
QTimer::singleShot(10, 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); // 设置定时器间隔为 1000 毫秒(1 秒)
|
||
UIRefresh_Timer->start();
|
||
#if defined(Q_OS_LINUX)
|
||
auto rows = P08_sql.selectLatest("AlarmHistory", 100);
|
||
for (const auto &row : rows) {
|
||
|
||
// 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);
|
||
|
||
if(m_oldAlarmCode == 0)
|
||
m_oldAlarmCode = code;
|
||
}
|
||
#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 位
|
||
|
||
|
||
// 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);
|
||
|
||
QString colorStr = AlarmText[devCode][idxCode].TextColor;
|
||
QListWidgetItem *item = new QListWidgetItem(alarmStr);
|
||
item->setForeground(parseCssRgb(colorStr));
|
||
ui->ListWidget_AlarmList->addItem(item);
|
||
}
|