feat:取消设置界面设置按钮;增加返修包弹窗提醒

develop
lichongyuan 3 months ago
parent 930c75b87b
commit 5191cdeef4

@ -18,18 +18,20 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \ SOURCES += \
common.cpp \ common.cpp \
main.cpp \ main.cpp \
network/udpdatathread.cpp \ network/udpdatahandler.cpp \
network/udpsocketworker.cpp \ network/udpsocketworker.cpp \
ui/countdownmessagebox.cpp \
ui/settingwgt.cpp \ ui/settingwgt.cpp \
ui/widget.cpp ui/widget.cpp
HEADERS += \ HEADERS += \
common.h \ common.h \
network/udpdatathread.h \ network/udpdatahandler.h \
network/udpsocketworker.h \ network/udpsocketworker.h \
third_party/logger/EasySpdLog.h \ third_party/logger/EasySpdLog.h \
third_party/nlohmann/json.hpp \ third_party/nlohmann/json.hpp \
third_party/nlohmann/json_fwd.hpp \ third_party/nlohmann/json_fwd.hpp \
ui/countdownmessagebox.h \
ui/settingwgt.h \ ui/settingwgt.h \
ui/widget.h \ ui/widget.h \
utils/interface.h utils/interface.h

@ -1,6 +1,8 @@
#include "common.h" #include "common.h"
#include <fstream> #include <fstream>
#include <QDir> #include <QDir>
#include <QSettings>
#include <QDebug> #include <QDebug>
using namespace nlohmann; using namespace nlohmann;
@ -12,15 +14,17 @@ Common::Common()
bool Common::loadConfig(const QString &filePath) bool Common::loadConfig(const QString &filePath)
{ {
m_configFilePath = filePath; // 提取网络配置
std::ifstream jsonStream(filePath.toLocal8Bit()); QString iniFilePath = filePath + "/network.ini";
QSettings *configIni = new QSettings(iniFilePath, QSettings::IniFormat);
m_ipAddr = configIni->value("network/ip").toString();
m_srcPort = configIni->value("network/src_port").toInt();
m_dstPort = configIni->value("network/dst_port").toInt();
m_configFilePath = filePath + "/Config.json";
std::ifstream jsonStream(m_configFilePath.toLocal8Bit());
m_configJson = json::parse(jsonStream); m_configJson = json::parse(jsonStream);
jsonStream.close(); jsonStream.close();
// 提取网络配置
auto netCfg = m_configJson.at("network");
m_ipAddr = QString::fromStdString(netCfg["ip"]);
m_srcPort = netCfg["src_port"];
m_dstPort = netCfg["dst_port"];
// 提取工作模式 // 提取工作模式
auto workmodes = m_configJson.at("workmode"); auto workmodes = m_configJson.at("workmode");
for (const auto& workmode : workmodes) for (const auto& workmode : workmodes)
@ -53,7 +57,14 @@ bool Common::saveConfig(const QMap<int, ProductConfig> &productsCfg, const QMap<
return true; return true;
} }
QString Common::getHostAddress() const bool Common::updateProductsConfig(const QMap<int, ProductConfig> &productsCfg)
{
setProductConfig(productsCfg);
saveConfig();
return true;
}
QString Common::getIPAddr() const
{ {
return m_ipAddr; return m_ipAddr;
} }
@ -73,7 +84,7 @@ bool Common::saveConfig()
std::ofstream outFile(m_configFilePath.toLocal8Bit()); std::ofstream outFile(m_configFilePath.toLocal8Bit());
if (!outFile) if (!outFile)
{ {
qDebug() << "无法打开文件进行写入!"; LError("Write config json file error!");
return false; return false;
} }
outFile << m_configJson.dump(4); outFile << m_configJson.dump(4);

@ -14,21 +14,30 @@ class Common : public QObject, public Singleton<Common>
Q_OBJECT Q_OBJECT
friend class Singleton<Common>; friend class Singleton<Common>;
public: public:
// 加载Config文件
bool loadConfig(const QString& filePath); bool loadConfig(const QString& filePath);
// 保存到Config文件
bool saveConfig(const QMap<int, ProductConfig> &productsCfg, const QMap<int, ModeConfig> &workmodeCfg); bool saveConfig(const QMap<int, ProductConfig> &productsCfg, const QMap<int, ModeConfig> &workmodeCfg);
// 更新产品配置文件
QString getHostAddress() const; bool updateProductsConfig(const QMap<int, ProductConfig> &productsCfg);
// 获取IP地址
QString getIPAddr() const;
// 获取端口号
int getSrcPort() const; int getSrcPort() const;
int getDstPort() const; int getDstPort() const;
// 获取产品配置
QMap<int, ProductConfig> getProductConfigs(); QMap<int, ProductConfig> getProductConfigs();
// 设置产品配置
void setProductConfig(const QMap<int, ProductConfig>& productsCfg); void setProductConfig(const QMap<int, ProductConfig>& productsCfg);
// 获取工作模式
QMap<int, ModeConfig> getWorkModeConfigs(); QMap<int, ModeConfig> getWorkModeConfigs();
// 设置工作模式
void setWorkModeConfigs(const QMap<int, ModeConfig>& workmodeCfg); void setWorkModeConfigs(const QMap<int, ModeConfig>& workmodeCfg);
// 获取已选择的工作模式
const QMap<QString, Mode>& getSelectedProducts() const; const QMap<QString, Mode>& getSelectedProducts() const;
// 获取当前工作模式
int getCurrentWorkMode() const; int getCurrentWorkMode() const;
// 设置当前工作模式
void setCurrentWorkMode(const int workMode); void setCurrentWorkMode(const int workMode);
signals: signals:
@ -39,7 +48,7 @@ private:
void FilterSelectedProducts(); void FilterSelectedProducts();
private: private:
QString m_configFilePath{}; QString m_configFilePath{}; // 配置文件路径
nlohmann::json m_configJson{}; nlohmann::json m_configJson{};
QString m_currentWorkPath{}; // 当前工作路径 QString m_currentWorkPath{}; // 当前工作路径
std::atomic<int> m_currentMode{ -1 }; // 当前正在进行的工作模式 std::atomic<int> m_currentMode{ -1 }; // 当前正在进行的工作模式

@ -2,6 +2,7 @@
#include <QApplication> #include <QApplication>
#include "common.h"
#include "EasySpdLog.h" #include "EasySpdLog.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -10,6 +11,9 @@ int main(int argc, char *argv[])
// 初始化日志类 // 初始化日志类
auto loggerPath = QString(QApplication::applicationDirPath() + "/logs/log.log").toLocal8Bit(); auto loggerPath = QString(QApplication::applicationDirPath() + "/logs/log.log").toLocal8Bit();
SetLogger(loggerPath.data()); SetLogger(loggerPath.data());
// 加载配置文件
QString filePath = QApplication::applicationDirPath() + "/config";
Common::GetInstance()->loadConfig(filePath);
// //
Widget w; Widget w;
w.move(0, 0); w.move(0, 0);

@ -1,11 +1,11 @@
#include "udpdatathread.h" #include "udpdatahandler.h"
#include <QHostAddress> #include <QHostAddress>
#include "common.h" #include "common.h"
#include "EasySpdLog.h" #include "EasySpdLog.h"
#include <QDebug> #include <QDebug>
UdpDataThread::UdpDataThread(QObject *parent) UdpDataHandler::UdpDataHandler(QObject *parent)
: QObject(parent) : QObject(parent)
, m_workerThread(new QThread()) , m_workerThread(new QThread())
, m_udpWorker(nullptr) , m_udpWorker(nullptr)
@ -13,47 +13,46 @@ UdpDataThread::UdpDataThread(QObject *parent)
, m_connectStatus(false) , m_connectStatus(false)
, m_isSending(false) , m_isSending(false)
{ {
auto ipAddr = Common::GetInstance()->getHostAddress(); auto ipAddr = Common::GetInstance()->getIPAddr();
auto recvPort = Common::GetInstance()->getSrcPort(); auto recvPort = Common::GetInstance()->getSrcPort();
m_hostAddr.setAddress(ipAddr); m_hostAddr.setAddress(ipAddr);
m_udpWorker = new UdpSocketWorker(m_hostAddr, recvPort); m_udpWorker = new UdpSocketWorker(m_hostAddr, recvPort);
m_udpWorker->moveToThread(m_workerThread); m_udpWorker->moveToThread(m_workerThread);
m_workerThread->start(); m_workerThread->start();
connect(m_udpWorker, &UdpSocketWorker::dataReceived, this, &UdpDataThread::recvData); connect(m_udpWorker, &UdpSocketWorker::dataReceived, this, &UdpDataHandler::recvData);
connect(m_checkConnectTimer, &QTimer::timeout, this, &UdpDataThread::checkConnectSlot); connect(m_checkConnectTimer, &QTimer::timeout, this, &UdpDataHandler::checkConnectSlot);
m_checkConnectTimer->start(1000); m_checkConnectTimer->start(1000);
} }
UdpDataThread::~UdpDataThread() UdpDataHandler::~UdpDataHandler()
{ {
} }
void UdpDataThread::sendSnId(const QString &data) void UdpDataHandler::sendSnId(const QString &data)
{ {
QString snMsg = QString("5901%1%2").arg(data.length()).arg(data); QString snMsg = QString("5901%1%2").arg(data.length()).arg(data);
//m_udpWorker->sendData(snMsg.toLocal8Bit(), m_hostAddr, Common::GetInstance()->getDstPort());
m_udpWorker->startSending("send_sn", snMsg.toLocal8Bit(), m_hostAddr, Common::GetInstance()->getDstPort()); m_udpWorker->startSending("send_sn", snMsg.toLocal8Bit(), m_hostAddr, Common::GetInstance()->getDstPort());
} }
void UdpDataThread::sendStop() void UdpDataHandler::sendStop()
{ {
m_isSending = false; m_isSending = false;
} }
void UdpDataThread::sendClose() void UdpDataHandler::sendClose()
{ {
QString msg = QString("%1%2").arg(CONSTHEAD).arg("0301"); QString msg = QString("%1%2").arg(CONSTHEAD).arg("0301");
m_udpWorker->startSending("iecs_close", msg.toLocal8Bit(), m_hostAddr, Common::GetInstance()->getDstPort()); m_udpWorker->startSending("iecs_close", msg.toLocal8Bit(), m_hostAddr, Common::GetInstance()->getDstPort());
} }
void UdpDataThread::stopClose() void UdpDataHandler::stopClose()
{ {
m_udpWorker->stopSending("iecs_close"); m_udpWorker->stopSending("iecs_close");
} }
void UdpDataThread::recvData(const QByteArray &data) void UdpDataHandler::recvData(const QByteArray &data)
{ {
if (data.size() == 0) if (data.size() == 0)
return; return;
@ -68,7 +67,7 @@ void UdpDataThread::recvData(const QByteArray &data)
m_connectStatus = true; m_connectStatus = true;
int type = dataStr.mid(6, 1).toInt(); int type = dataStr.mid(6, 1).toInt();
Common::GetInstance()->setCurrentWorkMode(type); Common::GetInstance()->setCurrentWorkMode(type);
emit connectStatus(true); emit GetConnStatusSig(true);
break; break;
} }
case SIGNAL_SN: case SIGNAL_SN:
@ -80,7 +79,7 @@ void UdpDataThread::recvData(const QByteArray &data)
} }
else else
{ {
qDebug()<<"SIGNAL_SN:"<<data; LWarn("Unknown SIGNAL_SN:{}", data);
} }
break; break;
} }
@ -90,18 +89,18 @@ void UdpDataThread::recvData(const QByteArray &data)
bool ret = dataStr.mid(6, 1).toInt(); bool ret = dataStr.mid(6, 1).toInt();
if (status == 1) if (status == 1)
{ {
emit registerRet(ret); emit GetDoneSig(ret);
} }
break; break;
} }
default: default:
qDebug()<<"data:"<<data; LWarn("Unknow message:{}", data);
break; break;
} }
} }
void UdpDataThread::checkConnectSlot() void UdpDataHandler::checkConnectSlot()
{ {
static int times = 0; static int times = 0;
if (!m_connectStatus) if (!m_connectStatus)
@ -115,8 +114,7 @@ void UdpDataThread::checkConnectSlot()
if (times >= 3) if (times >= 3)
{ {
Common::GetInstance()->setCurrentWorkMode(-1); Common::GetInstance()->setCurrentWorkMode(-1);
emit connectStatus(false); emit GetConnStatusSig(false);
qDebug()<<"disconnect";
times = 0; times = 0;
} }
m_connectStatus = false; m_connectStatus = false;

@ -13,21 +13,27 @@ typedef enum {
SIGNAL_BEATDST //外挂软件状态 SIGNAL_BEATDST //外挂软件状态
}SIGNALTYPE; }SIGNALTYPE;
#define CONSTHEAD 59 #define CONSTHEAD 59 //数据头
class UdpDataThread : public QObject class UdpDataHandler : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
UdpDataThread(QObject *parent = nullptr); UdpDataHandler(QObject *parent = nullptr);
~UdpDataThread(); ~UdpDataHandler();
// 循环发送产品SN号
void sendSnId(const QString &data); void sendSnId(const QString &data);
// 停止发送产品SN号
void sendStop(); void sendStop();
// 发送关闭信号
void sendClose(); void sendClose();
// 停止发送关闭信号
void stopClose(); void stopClose();
signals: signals:
void connectStatus(bool); // 连接状态信号
void registerRet(bool); void GetConnStatusSig(bool);
// 注KEY是否完成信号
void GetDoneSig(bool);
private slots: private slots:
void recvData(const QByteArray &data); void recvData(const QByteArray &data);
void checkConnectSlot(); void checkConnectSlot();
@ -38,7 +44,7 @@ private:
QHostAddress m_hostAddr; QHostAddress m_hostAddr;
QTimer *m_checkConnectTimer; QTimer *m_checkConnectTimer;
// 判断注KEY软件状态 // 判断注KEY软件状态
volatile bool m_connectStatus; std::atomic<bool> m_connectStatus;
volatile bool m_isSending; volatile bool m_isSending;
QString m_currSn; QString m_currSn;
}; };

@ -0,0 +1,37 @@
#pragma execution_character_set("utf-8")
#include "countdownmessagebox.h"
CountdownMessageBox::CountdownMessageBox(int timeoutSeconds, QWidget *parent)
: QMessageBox(parent)
, m_timeoutSeconds(timeoutSeconds)
{
// 初始显示时间
setText(tr("还有%1秒关闭...").arg(m_timeoutSeconds));
// 创建定时器
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, &CountdownMessageBox::updateCountdown);
m_timer->start(1000); // 每秒触发一次
}
void CountdownMessageBox::showCountdownMessageBox(QWidget *parent, int timeoutSeconds, const QString &title)
{
CountdownMessageBox msgBox(timeoutSeconds, parent);
msgBox.setWindowTitle(title);
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.exec();
}
void CountdownMessageBox::updateCountdown()
{
if (m_timeoutSeconds > 0)
{
--m_timeoutSeconds;
setText(tr("还有%1秒关闭...").arg(m_timeoutSeconds));
}
else
{
m_timer->stop();
accept(); // 关闭对话框
}
}

@ -0,0 +1,19 @@
#ifndef COUNTDOWNMESSAGEBOX_H
#define COUNTDOWNMESSAGEBOX_H
#include <QMessageBox>
#include <QTimer>
class CountdownMessageBox : public QMessageBox
{
public:
explicit CountdownMessageBox(int timeoutSeconds, QWidget *parent = nullptr);
static void showCountdownMessageBox(QWidget *parent, int timeoutSeconds, const QString &title);
private slots:
void updateCountdown();
private:
int m_timeoutSeconds;
QTimer *m_timer;
};
#endif // COUNTDOWNMESSAGEBOX_H

@ -17,14 +17,12 @@ SettingWgt::SettingWgt(QWidget *parent) :
ui(new Ui::SettingDlg) ui(new Ui::SettingDlg)
{ {
ui->setupUi(this); ui->setupUi(this);
QString filePath = QApplication::applicationDirPath() + "/config/Config.json";
Common::GetInstance()->loadConfig(filePath);
m_productsCfg = Common::GetInstance()->getProductConfigs(); m_productsCfg = Common::GetInstance()->getProductConfigs();
m_workmodesCfg = Common::GetInstance()->getWorkModeConfigs(); m_workmodesCfg = Common::GetInstance()->getWorkModeConfigs();
m_productNums = m_productsCfg.size(); m_productNums = m_productsCfg.size();
m_workModeNums = m_workmodesCfg.size(); m_workModeNums = m_workmodesCfg.size();
initUI(); //一定放在前面 initUI(); //一定放在前面
loadConfig();
} }
SettingWgt::~SettingWgt() SettingWgt::~SettingWgt()
@ -43,25 +41,30 @@ void SettingWgt::initUI()
{ {
QGroupBox *groupBox = new QGroupBox(workmodeCfg.name, ui->frame_set_mode); QGroupBox *groupBox = new QGroupBox(workmodeCfg.name, ui->frame_set_mode);
QGridLayout *groupLayout = new QGridLayout(groupBox); QGridLayout *groupLayout = new QGridLayout(groupBox);
QLabel *nameLab = new QLabel("正常", groupBox); QLabel *nameLab = new QLabel("正常", groupBox);
groupLayout->addWidget(nameLab, 0, 0); groupLayout->addWidget(nameLab, 0, 0);
QLineEdit *workPathEdit = new QLineEdit(workmodeCfg.workPath, groupBox); QLineEdit *workPathEdit = new QLineEdit(workmodeCfg.workPath, groupBox);
workPathEdit->setEnabled(false);
groupLayout->addWidget(workPathEdit, 0, 1); groupLayout->addWidget(workPathEdit, 0, 1);
QPushButton *selectworkPathBtn = new QPushButton("<", groupBox); QPushButton *selectworkPathBtn = new QPushButton("<", groupBox);
selectworkPathBtn->setMaximumWidth(30); selectworkPathBtn->setMaximumWidth(30);
groupLayout->addWidget(selectworkPathBtn, 0, 2); groupLayout->addWidget(selectworkPathBtn, 0, 2);
connect(selectworkPathBtn, &QPushButton::clicked, selectWorkPathMapper, qOverload<>(&QSignalMapper::map)); connect(selectworkPathBtn, &QPushButton::clicked, selectWorkPathMapper, qOverload<>(&QSignalMapper::map));
selectWorkPathMapper->setMapping(selectworkPathBtn, workmodeCfg.index); selectWorkPathMapper->setMapping(selectworkPathBtn, workmodeCfg.index);
selectworkPathBtn->hide();
QLabel *reworknameLab = new QLabel("返工", groupBox); QLabel *reworknameLab = new QLabel("返工", groupBox);
groupLayout->addWidget(reworknameLab, 1, 0); groupLayout->addWidget(reworknameLab, 1, 0);
QLineEdit *reworkPathEdit = new QLineEdit(workmodeCfg.reworkPath, groupBox); QLineEdit *reworkPathEdit = new QLineEdit(workmodeCfg.reworkPath, groupBox);
groupLayout->addWidget(reworkPathEdit, 1, 1); groupLayout->addWidget(reworkPathEdit, 1, 1);
reworkPathEdit->setEnabled(false);
QPushButton *selectReworkPathBtn = new QPushButton("<", groupBox); QPushButton *selectReworkPathBtn = new QPushButton("<", groupBox);
selectReworkPathBtn->setMaximumWidth(30); selectReworkPathBtn->setMaximumWidth(30);
groupLayout->addWidget(selectReworkPathBtn, 1, 2); groupLayout->addWidget(selectReworkPathBtn, 1, 2);
connect(selectReworkPathBtn, &QPushButton::clicked, selectReworkPathMapper, qOverload<>(&QSignalMapper::map)); connect(selectReworkPathBtn, &QPushButton::clicked, selectReworkPathMapper, qOverload<>(&QSignalMapper::map));
selectReworkPathMapper->setMapping(selectReworkPathBtn, workmodeCfg.index); selectReworkPathMapper->setMapping(selectReworkPathBtn, workmodeCfg.index);
selectReworkPathBtn->hide();
m_workPathLineEdits.insert(workmodeCfg.index, qMakePair(workPathEdit, reworkPathEdit)); m_workPathLineEdits.insert(workmodeCfg.index, qMakePair(workPathEdit, reworkPathEdit));
@ -102,7 +105,7 @@ void SettingWgt::initUI()
layout->addWidget(checkbox, i + 1, 1); layout->addWidget(checkbox, i + 1, 1);
// 产品SN适配设置输入框创建 // 产品SN适配设置输入框创建
QLineEdit *lineEdit = new QLineEdit(ui->frame_setting); QLineEdit *lineEdit = new QLineEdit(ui->frame_setting);
//lineEdit->setEnabled(false); lineEdit->setEnabled(false);
m_snFormatLineEdits.push_back(lineEdit); m_snFormatLineEdits.push_back(lineEdit);
layout->addWidget(lineEdit, i + 1, 2); layout->addWidget(lineEdit, i + 1, 2);
// 工作模式下拉框创建 // 工作模式下拉框创建
@ -113,15 +116,17 @@ void SettingWgt::initUI()
} }
m_modeComboBoxes.push_back(comboBox); m_modeComboBoxes.push_back(comboBox);
layout->addWidget(comboBox, i + 1, 3); layout->addWidget(comboBox, i + 1, 3);
comboBox->setEnabled(false);
} }
// 将 QSignalMapper 的信号连接到槽函数 // 将 QSignalMapper 的信号连接到槽函数
connect(checkBoxMapper, static_cast<void(QSignalMapper::*)(int)>(&QSignalMapper::mapped), this, &SettingWgt::onCheckBoxStateChanged); connect(checkBoxMapper, static_cast<void(QSignalMapper::*)(int)>(&QSignalMapper::mapped), this, &SettingWgt::onCheckBoxStateChanged);
loadConfig();
} }
void SettingWgt::loadConfig() void SettingWgt::loadConfig()
{ {
// 显示产品配置 // 显示产品配置
for (const auto& product : m_productsCfg) for (const auto product : m_productsCfg)
{ {
int index = product.productId; int index = product.productId;
bool selected = product.selected; bool selected = product.selected;
@ -139,14 +144,20 @@ void SettingWgt::loadConfig()
void SettingWgt::onCheckBoxStateChanged(int index) void SettingWgt::onCheckBoxStateChanged(int index)
{ {
QCheckBox *checkBox = m_selectCheckBoxes[index]; QCheckBox *checkBox = m_selectCheckBoxes[index];
if (checkBox && checkBox->isChecked()) if (checkBox)
{
if (checkBox->isChecked())
{ {
QString formatStr = m_snFormatLineEdits[index]->text(); QString formatStr = m_snFormatLineEdits[index]->text();
if (formatStr.isEmpty()) if (formatStr.isEmpty())
{ {
checkBox->setCheckState(Qt::Unchecked); checkBox->setCheckState(Qt::Unchecked);
QMessageBox::warning(this, "启用失败", "产品SN适配为空请先进行设置再进行启用"); QMessageBox::warning(this, "启用失败", "产品SN适配为空请先进行设置再进行启用");
return;
}
} }
m_productsCfg[index + 1].selected = checkBox->isChecked();
Common::GetInstance()->updateProductsConfig(m_productsCfg);
} }
} }

@ -7,6 +7,7 @@
#include <QLineEdit> #include <QLineEdit>
#include <QComboBox> #include <QComboBox>
#include <QGroupBox> #include <QGroupBox>
#include <QPushButton>
#include "common.h" #include "common.h"
#include "nlohmann/json.hpp" #include "nlohmann/json.hpp"

@ -13,7 +13,7 @@
<property name="windowTitle"> <property name="windowTitle">
<string>Form</string> <string>Form</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout" stretch="2,7,1"> <layout class="QVBoxLayout" name="verticalLayout" stretch="2,7">
<item> <item>
<widget class="QFrame" name="frame_set_mode"> <widget class="QFrame" name="frame_set_mode">
<property name="frameShape"> <property name="frameShape">
@ -34,30 +34,6 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton_ok">
<property name="text">
<string>确认</string>
</property>
</widget>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
<resources/> <resources/>

@ -10,6 +10,7 @@
#include <QSqlError> #include <QSqlError>
#include <QProcess> #include <QProcess>
#include <QDir> #include <QDir>
#include "countdownmessagebox.h"
#include "common.h" #include "common.h"
#include <QDebug> #include <QDebug>
@ -20,19 +21,16 @@ Widget::Widget(QWidget *parent)
: QWidget(parent) : QWidget(parent)
, ui(new Ui::Widget) , ui(new Ui::Widget)
, m_settingDlg(new SettingWgt(this)) , m_settingDlg(new SettingWgt(this))
, m_udpDataThread(new UdpDataThread(this)) , m_udpDataThread(new UdpDataHandler(this))
, m_showSetting(false) , m_showSetting(false)
, m_isSwitching(false)
{ {
QString filePath = QApplication::applicationDirPath() + "/config/Config.json";
Common::GetInstance()->loadConfig(filePath);
ui->setupUi(this); ui->setupUi(this);
initUI(); initUI();
connect(m_settingDlg, &SettingWgt::SettingFinishSig, [this](){ connect(m_settingDlg, &SettingWgt::SettingFinishSig, this, &Widget::on_pushButton_setting_clicked);
on_pushButton_setting_clicked(); connect(m_udpDataThread, &UdpDataHandler::GetDoneSig, this, &Widget::RegisterDoneSlot);
}); connect(m_udpDataThread, &UdpDataHandler::GetConnStatusSig, this, &Widget::GetConnStatusSlot);
connect(m_udpDataThread, &UdpDataThread::registerRet, this, &Widget::RegisterRetSlot);
connect(m_udpDataThread, &UdpDataThread::connectStatus, this, &Widget::ConnectStatusSlot);
} }
Widget::~Widget() Widget::~Widget()
@ -90,13 +88,13 @@ int Widget::startIECSProgram(const QString& workPath, const QString& snId)
{ {
QProcess *process = new QProcess(this); QProcess *process = new QProcess(this);
process->start(workPath); process->start(workPath);
qDebug()<<"startIECSProgram:"<<snId;
if (!process->waitForStarted()) if (!process->waitForStarted())
{ {
LError("Failed to start the program with arguments:{}", process->errorString()); LError("Failed to start the program with arguments:{}", process->errorString());
return ExecIECSError; return ExecIECSError;
} }
m_udpDataThread->sendSnId(snId); m_udpDataThread->sendSnId(snId);
m_isSwitching = false;
return NoError; return NoError;
} }
@ -104,7 +102,11 @@ int Widget::checkForRepairProgram(const int mode, const QString& sn, bool& isFin
{ {
auto workModeCfg = Common::GetInstance()->getWorkModeConfigs(); auto workModeCfg = Common::GetInstance()->getWorkModeConfigs();
QDir workDir(workModeCfg[mode].workPath); QDir workDir(workModeCfg[mode].workPath);
workDir.cdUp(); if (!workDir.cdUp())
{
LError("Workdir cd up failed!");
return -1;
}
QString dbPath = workDir.absolutePath() + "/config.db"; QString dbPath = workDir.absolutePath() + "/config.db";
return databaseQuery(dbPath, sn, isFind); return databaseQuery(dbPath, sn, isFind);
} }
@ -114,6 +116,7 @@ int Widget::databaseQuery(const QString& databasePath, const QString& deviceSn,
// 创建和打开 SQLite 数据库 // 创建和打开 SQLite 数据库
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(databasePath); // 使用传入的数据库路径 db.setDatabaseName(databasePath); // 使用传入的数据库路径
if (!db.open()) if (!db.open())
{ {
LError("Failed to open database:{}", db.lastError().text()); LError("Failed to open database:{}", db.lastError().text());
@ -123,38 +126,47 @@ int Widget::databaseQuery(const QString& databasePath, const QString& deviceSn,
// 创建查询对象 // 创建查询对象
QSqlQuery query(db); QSqlQuery query(db);
// 准备 SQL 查询 // 准备 SQL 查询
query.prepare("SELECT COUNT(1) FROM history WHERE device_sn = :device_sn;"); query.prepare("SELECT COUNT(1) FROM history WHERE device_sn = :device_sn;");
query.bindValue(":device_sn", deviceSn); query.bindValue(":device_sn", deviceSn);
// 执行查询 // 执行查询
if (!query.exec()) { if (!query.exec()) {
LError("Failed to execute query:{}", query.lastError().text()); LError("Failed to execute query:{}", query.lastError().text());
isFind = false; isFind = false;
db.close(); db.close();
QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
return DatabaseSelectError; return DatabaseSelectError;
} }
// 处理查询结果 // 处理查询结果
if (query.next()) if (query.next())
{ {
int count = query.value(0).toInt(); int count = query.value(0).toInt();
isFind = (count > 0); isFind = (count > 0);
db.close(); db.close();
QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
return NoError; return NoError;
} }
isFind = false; isFind = false;
db.close(); db.close();
QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
return DatabaseUnknownError; return DatabaseUnknownError;
} }
void Widget::RegisterRetSlot(bool ret) void Widget::RegisterDoneSlot(bool ret)
{ {
qDebug()<<"11111";
ui->lineEdit_snId->setEnabled(true); ui->lineEdit_snId->setEnabled(true);
} }
void Widget::ConnectStatusSlot(bool ret) void Widget::GetConnStatusSlot(bool ret)
{ {
if (!ret) if (!ret && !m_isSwitching)
{ {
qDebug()<<"ret:"<<ret<<",m_isSwitching:"<<m_isSwitching;
ui->lineEdit_snId->setEnabled(true); ui->lineEdit_snId->setEnabled(true);
} }
} }
@ -164,8 +176,8 @@ void Widget::on_pushButton_setting_clicked()
if (ui->widget_setting->isHidden()) if (ui->widget_setting->isHidden())
{ {
ui->pushButton_setting->setIcon(QIcon(":/resources/folding.png")); ui->pushButton_setting->setIcon(QIcon(":/resources/folding.png"));
m_settingDlg->loadConfig();
ui->widget_setting->show(); ui->widget_setting->show();
m_settingDlg->loadConfig();
int currentWidth = this->width(); int currentWidth = this->width();
this->adjustSize(); this->adjustSize();
this->resize(currentWidth, this->height()); // 只保持高度变化 this->resize(currentWidth, this->height()); // 只保持高度变化
@ -190,9 +202,34 @@ void Widget::addLogItem(const QString &text)
ui->list_log->addItem(text); // 添加新条目 ui->list_log->addItem(text); // 添加新条目
} }
int Widget::switchProgram() 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; return 0;
} }
@ -218,7 +255,7 @@ void Widget::on_lineEdit_snId_returnPressed()
} }
if (productFormat.isEmpty()) if (productFormat.isEmpty())
{ {
QMessageBox::information(this, "警告", "当前SN与启用设值SN不匹配请检查或重新扫码"); QMessageBox::information(this, "警告", "当前SN与启用设值SN不匹配请检查设置或重新扫码");
return; return;
} }
ui->lineEdit_snId->setEnabled(false); ui->lineEdit_snId->setEnabled(false);
@ -232,8 +269,7 @@ void Widget::on_lineEdit_snId_returnPressed()
return; return;
} }
auto selectMode = selectedProducts[productFormat]; auto selectMode = selectedProducts[productFormat];
auto workModeCfgs = Common::GetInstance()->getWorkModeConfigs(); if (selectMode != currWorkMode || isRepair)
if (selectMode != currWorkMode)
{ {
// 切换工作模式 // 切换工作模式
if (currWorkMode != -1) if (currWorkMode != -1)
@ -242,20 +278,7 @@ void Widget::on_lineEdit_snId_returnPressed()
} }
//TODO:不相等时m_udpDataThread发送关闭信号等待Common单例类中的标志位变成-1后m_udpDataThread停止发送关闭信号并启动IECS程序 //TODO:不相等时m_udpDataThread发送关闭信号等待Common单例类中的标志位变成-1后m_udpDataThread停止发送关闭信号并启动IECS程序
//同时需要去IECS程序路径中查找数据库判断是否使用返修程序 //同时需要去IECS程序路径中查找数据库判断是否使用返修程序
auto workModeCfg = workModeCfgs[selectMode]; switchProgram(snId, selectMode, isRepair);
QString workPath = isRepair ? workModeCfg.reworkPath : workModeCfg.workPath;
qDebug()<<"workPath:"<<workPath;
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [this, timer, snId, workPath]() {
if (Common::GetInstance()->getCurrentWorkMode() == -1)
{
m_udpDataThread->stopClose();
timer->stop(); // 停止定时器
// 启动 IECS 程序
startIECSProgram(workPath, snId);
}
});
timer->start(1000);
return; return;
} }
m_udpDataThread->sendSnId(snId); m_udpDataThread->sendSnId(snId);

@ -3,7 +3,7 @@
#include <QWidget> #include <QWidget>
#include "settingwgt.h" #include "settingwgt.h"
#include "udpdatathread.h" #include "udpdatahandler.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { class Widget; } namespace Ui { class Widget; }
@ -24,10 +24,10 @@ private:
int databaseQuery(const QString& databasePath, const QString& deviceSn, bool& isFind); int databaseQuery(const QString& databasePath, const QString& deviceSn, bool& isFind);
private slots: private slots:
void RegisterRetSlot(bool ret); void RegisterDoneSlot(bool ret);
void ConnectStatusSlot(bool ret); void GetConnStatusSlot(bool ret);
void addLogItem(const QString& text); void addLogItem(const QString& text);
int switchProgram(); int switchProgram(const QString snId, const int mode, const bool isRepair);
void on_pushButton_setting_clicked(); void on_pushButton_setting_clicked();
void on_lineEdit_snId_returnPressed(); void on_lineEdit_snId_returnPressed();
void on_pushButton_sendSnId_clicked(); void on_pushButton_sendSnId_clicked();
@ -35,7 +35,8 @@ private slots:
private: private:
Ui::Widget *ui; Ui::Widget *ui;
SettingWgt *m_settingDlg; SettingWgt *m_settingDlg;
UdpDataThread *m_udpDataThread; UdpDataHandler *m_udpDataThread;
bool m_showSetting; bool m_showSetting;
bool m_isSwitching;
}; };
#endif // WIDGET_H #endif // WIDGET_H

Loading…
Cancel
Save