216 lines
5.3 KiB
C++
216 lines
5.3 KiB
C++
#include "dlgviscositysetting.h"
|
|
#include "ui_dlgviscositysetting.h"
|
|
|
|
#include <QDir>
|
|
#include <QSettings>
|
|
#include <QDebug>
|
|
|
|
#define WAITTIMEMAX 20
|
|
|
|
DlgViscositySetting::DlgViscositySetting(QWidget *parent) :
|
|
QDialog(parent),
|
|
ui(new Ui::DlgViscositySetting)
|
|
{
|
|
ui->setupUi(this);
|
|
}
|
|
|
|
DlgViscositySetting::~DlgViscositySetting()
|
|
{
|
|
if (timer) {
|
|
timer->stop();
|
|
delete timer;
|
|
timer = nullptr;
|
|
}
|
|
delete ui;
|
|
}
|
|
|
|
void DlgViscositySetting::bindSysPara(SysPara *sysPara)
|
|
{
|
|
this->sysPara = sysPara;
|
|
init();
|
|
}
|
|
|
|
int DlgViscositySetting::getAlertValue()
|
|
{
|
|
return ui->sb_alert->value();
|
|
}
|
|
|
|
int DlgViscositySetting::getCloseValue()
|
|
{
|
|
return ui->sb_close->value();
|
|
}
|
|
|
|
void DlgViscositySetting::init()
|
|
{
|
|
if (sysPara) {
|
|
loadConfig();
|
|
initTimer();
|
|
}
|
|
}
|
|
|
|
void DlgViscositySetting::initTimer()
|
|
{
|
|
if (timer == nullptr) {
|
|
timer = new QTimer(this);
|
|
connect(timer, &QTimer::timeout,
|
|
this, &DlgViscositySetting::onTimeOut);
|
|
timer->start(1000);
|
|
}
|
|
}
|
|
|
|
void DlgViscositySetting::loadConfig()
|
|
{
|
|
QSettings settings(kConfDir+kConfFile, QSettings::IniFormat);
|
|
|
|
// 若 ini 中没有值,使用 SpinBox 当前默认值
|
|
alertV = settings.value(kGroupViscosity + "/" + kKeyAlertThreshold,
|
|
ui->sb_alert->value()).toInt();
|
|
closeV = settings.value(kGroupViscosity + "/" + kKeyCloseThreshold,
|
|
ui->sb_close->value()).toInt();
|
|
if (alertV < 100) alertV = 250;
|
|
if (closeV < 160) closeV = 300;
|
|
ui->sb_alert->setValue(alertV);
|
|
ui->sb_close->setValue(closeV);
|
|
}
|
|
|
|
void DlgViscositySetting::saveConfig()
|
|
{
|
|
// 确保目录存在
|
|
QDir dir(kConfDir);
|
|
if (!dir.exists()) {
|
|
if (!dir.mkpath(".")) {
|
|
// qWarning() << "无法创建配置目录:" << kConfDir;
|
|
return;
|
|
}
|
|
}
|
|
|
|
QSettings settings(kConfDir+kConfFile, QSettings::IniFormat);
|
|
settings.setValue(kGroupViscosity + "/" + kKeyAlertThreshold, ui->sb_alert->value());
|
|
settings.setValue(kGroupViscosity + "/" + kKeyCloseThreshold, ui->sb_close->value());
|
|
settings.sync();
|
|
|
|
if (settings.status() != QSettings::NoError) {
|
|
qWarning() << "写入配置文件失败:" << kConfDir+kConfFile;
|
|
} else {
|
|
qDebug() << "配置已保存到:" << kConfDir+kConfFile;
|
|
}
|
|
}
|
|
|
|
short DlgViscositySetting::checkClose(int visCurr, int visSetting)
|
|
{
|
|
if (visCurr == 0) return 0;
|
|
|
|
if (visCurr - closeV >= visSetting) {
|
|
return 1;
|
|
} else if (visCurr + closeV <= visSetting) {
|
|
return 2;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
short DlgViscositySetting::checkAlert(int visCurr, int visSetting)
|
|
{
|
|
if (visCurr == 0) return 0;
|
|
|
|
if (visCurr - alertV >= visSetting) {
|
|
return 1;
|
|
} else if (visCurr + alertV <= visSetting) {
|
|
return 2;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static short alertTimes = 0;
|
|
static short closeTimes = 0;
|
|
// 如果提示了,就等10分钟后再重新检查,WAITTIMEMAX = 600
|
|
static short waitTimes = 0;
|
|
void DlgViscositySetting::onTimeOut()
|
|
{
|
|
// 1秒一次,5秒防抖
|
|
if (sysPara) {
|
|
visSetting = sysPara->para.viscosity;
|
|
visCurr = sysPara->status.viscosity;
|
|
// test
|
|
// sysPara->status.viscosity = 1000;
|
|
// sysPara->status.selfCheckDone = 1;
|
|
// visCurr = sysPara->status.viscosity;
|
|
|
|
ui->lb_settings->setText(QString::number(visSetting));
|
|
ui->lb_current->setText(QString::number(visCurr));
|
|
|
|
if (waitTimes-- > 0) {
|
|
return;
|
|
}
|
|
|
|
// 虽然处理并不严谨,但是直接最低值切换到最高值的情况几乎不会发生
|
|
// 是否触发关闭喷嘴
|
|
short closeV = checkClose(visCurr, visSetting);
|
|
if (closeV != 0) {
|
|
if (closeTimes++ > 5) {
|
|
bool closeFlag = closeV == 1? true : false;
|
|
emit sig_close(closeFlag);
|
|
closeTimes = 0;
|
|
waitTimes = WAITTIMEMAX;
|
|
alertTimes = 0;
|
|
return;
|
|
}
|
|
} else {
|
|
closeTimes = 0;
|
|
}
|
|
|
|
// 是否触发报警
|
|
short alertV = checkAlert(visCurr, visSetting);
|
|
if (alertV != 0) {
|
|
if (alertTimes++ > 5) {
|
|
bool alertFlag = alertV == 1? true : false;
|
|
emit sig_alert(alertFlag);
|
|
alertTimes = 0;
|
|
waitTimes = WAITTIMEMAX;
|
|
}
|
|
} else {
|
|
alertTimes = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void DlgViscositySetting::on_checkBtn_clicked()
|
|
{
|
|
saveConfig();
|
|
visSetting = sysPara->para.viscosity;
|
|
visCurr = sysPara->status.viscosity;
|
|
closeV = ui->sb_close->value();
|
|
alertV = ui->sb_alert->value();
|
|
}
|
|
|
|
void DlgViscositySetting::on_closeBtn_clicked()
|
|
{
|
|
this->hide();
|
|
}
|
|
|
|
void DlgViscositySetting::on_sb_alert_editingFinished()
|
|
{
|
|
int tmp = ui->sb_alert->value();
|
|
// 告警阈值最小100,关闭阈值至少比告警大60
|
|
if (tmp < 100) {
|
|
ui->sb_alert->setValue(100);
|
|
tmp = 100;
|
|
}
|
|
if (tmp > ui->sb_close->value()) {
|
|
ui->sb_close->setValue(tmp+60);
|
|
}
|
|
}
|
|
|
|
void DlgViscositySetting::on_sb_close_editingFinished()
|
|
{
|
|
int tmp = ui->sb_close->value();
|
|
// 告警阈值最小100,关闭阈值至少比告警大60
|
|
if (tmp < 160) {
|
|
ui->sb_close->setValue(160);
|
|
tmp = 160;
|
|
}
|
|
if (tmp < ui->sb_alert->value()) {
|
|
ui->sb_alert->setValue(tmp-60);
|
|
}
|
|
}
|