You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
8.7 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#pragma execution_character_set("utf-8")
#include "widget.h"
#include "ui_widget.h"
#include <QHBoxLayout>
#include <QMessageBox>
#include <QEventLoop>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QProcess>
#include <QDir>
#include "countdownmessagebox.h"
#include "common.h"
#include <QDebug>
#define MAXITEMS 20
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
, m_settingDlg(new SettingWgt(this))
, m_udpDataThread(new UdpDataHandler(this))
, m_showSetting(false)
, m_isSwitching(false)
{
ui->setupUi(this);
initUI();
connect(m_settingDlg, &SettingWgt::SettingFinishSig, this, &Widget::on_pushButton_setting_clicked);
connect(m_udpDataThread, &UdpDataHandler::GetDoneSig, this, &Widget::RegisterDoneSlot);
connect(m_udpDataThread, &UdpDataHandler::GetConnStatusSig, this, &Widget::GetConnStatusSlot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::initUI()
{
this->setWindowTitle("");
//this->setWindowFlags(Qt::Window | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint);
this->setWindowFlags(Qt::Window | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint | Qt::CustomizeWindowHint);
ui->list_log->setEditTriggers(QAbstractItemView::NoEditTriggers);
QHBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(m_settingDlg);
ui->widget_setting->setLayout(layout);
ui->widget_setting->hide();
ui->lineEdit_snId->setFocus();
}
void Widget::setSNLineEditFocus()
{
// 全选LineEdit并且焦点放在LineEdit上
ui->lineEdit_snId->selectAll();
ui->lineEdit_snId->setFocus();
}
int Widget::checkSNId(const QString &snId)
{
if (snId.isEmpty())
{
QMessageBox::warning(this, "产品SN错误", "产品SN为空! 请重新扫码!");
LWarn("Product SN id is empty!");
return SNEmptyError;
}
QRegExp reg("[a-z]");
if (reg.indexIn(snId) != -1)
{
QMessageBox::information(this,"提示",QString("产品SN(%1)包含小写! 请重新扫码!").arg(snId));
LWarn("Product SN contains lowercase!");
ui->lineEdit_snId->setText("");
return SNLowerCaseError;
}
if (snId.contains(" "))
{
QMessageBox::information(this,"提示",QString("产品SN(%1)包含空格! 请重新扫码!").arg(snId));
LWarn("Product SN contains Spaces!");
ui->lineEdit_snId->setText("");
return SNSpaceError;
}
return NoError;
}
int Widget::startIECSProgram(const QString& workPath, const QString& snId)
{
QProcess *process = new QProcess(this);
process->start(workPath);
if (!process->waitForStarted())
{
LError("Failed to start the program with arguments:{}", process->errorString());
return ExecIECSError;
}
m_udpDataThread->sendSnId(snId);
m_isSwitching = false;
return NoError;
}
int Widget::checkForRepairProgram(const int mode, const QString& sn, bool& isFind)
{
auto workModeCfg = Common::GetInstance()->getWorkModeConfigs();
QDir workDir(workModeCfg[mode].workPath);
if (!workDir.cdUp())
{
LError("Workdir cd up failed!");
return -1;
}
QString dbPath = workDir.absolutePath() + "/config.db";
return databaseQuery(dbPath, sn, isFind);
}
int Widget::databaseQuery(const QString& databasePath, const QString& deviceSn, bool& isFind)
{
// 创建和打开 SQLite 数据库
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(databasePath); // 使用传入的数据库路径
if (!db.open())
{
LError("Failed to open database:{}", db.lastError().text());
isFind = false;
return DatabaseOpenError;
}
// 创建查询对象
QSqlQuery query(db);
// 准备 SQL 查询
query.prepare("SELECT COUNT(1) FROM history WHERE device_sn = :device_sn;");
query.bindValue(":device_sn", deviceSn);
// 执行查询
if (!query.exec()) {
LError("Failed to execute query:{}", query.lastError().text());
isFind = false;
db.close();
QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
return DatabaseSelectError;
}
// 处理查询结果
if (query.next())
{
int count = query.value(0).toInt();
isFind = (count > 0);
db.close();
QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
return NoError;
}
isFind = false;
db.close();
QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
return DatabaseUnknownError;
}
void Widget::RegisterDoneSlot(bool ret)
{
qDebug()<<"11111";
ui->lineEdit_snId->setEnabled(true);
}
void Widget::GetConnStatusSlot(bool ret)
{
if (!ret && !m_isSwitching)
{
qDebug()<<"ret:"<<ret<<",m_isSwitching:"<<m_isSwitching;
ui->lineEdit_snId->setEnabled(true);
}
}
void Widget::on_pushButton_setting_clicked()
{
if (ui->widget_setting->isHidden())
{
ui->pushButton_setting->setIcon(QIcon(":/resources/folding.png"));
ui->widget_setting->show();
m_settingDlg->loadConfig();
int currentWidth = this->width();
this->adjustSize();
this->resize(currentWidth, this->height()); // 只保持高度变化
}
else
{
ui->pushButton_setting->setIcon(QIcon(":/resources/unfolding.png"));
ui->widget_setting->hide();
int currentWidth = this->width();
this->adjustSize();
this->resize(currentWidth, this->height()); // 只保持高度变化
setSNLineEditFocus();
}
}
void Widget::addLogItem(const QString &text)
{
if (ui->list_log->count() >= MAXITEMS)
{
delete ui->list_log->takeItem(0);
}
ui->list_log->addItem(text); // 添加新条目
}
int Widget::switchProgram(const QString snId, const int mode, const bool isRepair)
{
static bool isFirstRepair = true;
if (isRepair && isFirstRepair)
{
isFirstRepair = false;
CountdownMessageBox::showCountdownMessageBox(this, 3, "正在切换返修程序包!");
}
else if (!isRepair)
{
isFirstRepair = true;
}
auto workModeCfgs = Common::GetInstance()->getWorkModeConfigs();
auto workModeCfg = workModeCfgs[mode];
QString workPath = isRepair ? workModeCfg.reworkPath : workModeCfg.workPath;
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [this, timer, snId, workPath]() {
if (Common::GetInstance()->getCurrentWorkMode() == -1)
{
m_isSwitching = true;
m_udpDataThread->stopClose();
timer->stop(); // 停止定时器
// 启动 IECS 程序
startIECSProgram(workPath, snId);
}
});
timer->start(1000);
return 0;
}
void Widget::on_lineEdit_snId_returnPressed()
{
QString snId = ui->lineEdit_snId->text();
int ret = 0;
ret = checkSNId(snId);
if (ret != 0)
{
LError("checkSNId error!code:{}", ret);
return;
}
const auto selectedProducts = Common::GetInstance()->getSelectedProducts();
QString productFormat{};
for (auto& products : selectedProducts.keys())
{
if (snId.contains(products))
{
productFormat = products;
break;
}
}
if (productFormat.isEmpty())
{
QMessageBox::information(this, "警告", "当前SN与启用设值SN不匹配请检查设置或重新扫码");
return;
}
ui->lineEdit_snId->setEnabled(false);
auto currWorkMode = Common::GetInstance()->getCurrentWorkMode();
bool isRepair = false;
// 查找数据库,判断是否需要返修程序
ret = checkForRepairProgram(selectedProducts[productFormat], snId, isRepair);
if (ret != NoError)
{
LError("checkForRepairProgram error!{}", ret);
return;
}
auto selectMode = selectedProducts[productFormat];
if (selectMode != currWorkMode || isRepair)
{
// 切换工作模式
if (currWorkMode != -1)
{
m_udpDataThread->sendClose();
}
//TODO:不相等时m_udpDataThread发送关闭信号等待Common单例类中的标志位变成-1后m_udpDataThread停止发送关闭信号并启动IECS程序
//同时需要去IECS程序路径中查找数据库判断是否使用返修程序
switchProgram(snId, selectMode, isRepair);
return;
}
m_udpDataThread->sendSnId(snId);
}
void Widget::on_pushButton_sendSnId_clicked()
{
on_lineEdit_snId_returnPressed();
}