1.更新MDNS广播的服务类型命名规范,防止出现多设备打开找不到MDNS的广播的问题 2.更新区分前后板的TAG

This commit is contained in:
2024-09-02 10:22:41 +08:00
parent e0c1e37191
commit 09c5843358
22 changed files with 501 additions and 304 deletions

View File

@@ -1,7 +1,6 @@
// MainWidget.cpp
#include "MainWidget.h"
void onThreadFinished(QThread* thread, ClientHandler* handler)
{
qDebug() << "Thread finished. Deleting handler and thread.";
@@ -27,36 +26,79 @@ MainWidget::MainWidget(QWidget* parent) :
multicastTimer(new QTimer(this))
#endif
{
leftLens_imageLabel = new QLabel(this);
rightLens_imageLabel = new QLabel(this);
videoLabel = new QLabel(this);
leftLens_imageLabel = new QLabel(this);
rightLens_imageLabel = new QLabel(this);
videoLabel = new QLabel(this);
frontFuncConfigLineEdit = new QLineEdit(this);
backFuncConfigLineEdit = new QLineEdit(this);
licenseHwInfoEdit = new QTextEdit(this);
UuidHwInfoEdit = new QTextEdit(this);
backFuncConfigLineEdit = new QLineEdit(this);
licenseHwInfoEdit = new QTextEdit(this);
UuidHwInfoEdit = new QTextEdit(this);
// 创建 mDNS 服务的 Server 和 Hostname 对象
QMdnsEngine::Server myserver;
QMdnsEngine::Hostname hostname(&myserver);
// 创建服务提供者Provider
QMdnsEngine::Provider provider(&myserver, &hostname);
// 定义要广播的服务
QMdnsEngine::Service service;
service.setName("Test Service");
service.setType("_test._tcp"); // 服务类型,包括 _tcp 或 _udp
service.setPort(12345); // 服务使用的端口号
// 启动服务广播
provider.update(service);
qDebug() << "mDNS service started. Broadcasting service:"
<< service.name() << service.type() << "on port" << service.port();
setupUI();
// 打印线程池状态信息
setupTimerForThreadPoolInfo();
server = new QTcpServer(this);
/*
// 创建 DataHandler 对象并连接信号
DataHandler* dataHandler = new DataHandler(leftLens_imageLabel, rightLens_imageLabel, videoLabel, licenseHwInfoEdit, &devInfoLineEdits,
frontBoardOneClickTest, frontBoardTest, frontBoardFuncConfig,
frontBoardDevInfoJson, frontBoardLicenseJson,
backBoardOneClickTest, backBoardTest, backBoardFuncConfig, backBoardDevInfoJson,
backBoardUuidJson, getPicJson, getVideoJson, this);
//FactoryToolSendGetUuidToHttpServer();
//FactoryToolSendGetLicenseToHttpServer();
//FactoryToolSendPostComfirmToHttpServer();
//FactoryToolSendPostTestToHttpServer();
//FactoryToolSendPostLicenseToHttpServer();
connect(this, &MainWidget::openFocusWindowRequested, dataHandler, &DataHandler::handleOpenFocusWindow);
connect(dataHandler, &DataHandler::statusUpdated, this, &MainWidget::onStatusUpdated);
*/
connect(server, &QTcpServer::newConnection, this, [this]() {
// 检查是否有挂起的连接
while (server->hasPendingConnections()) {
QTcpSocket* socket = server->nextPendingConnection();
int clientId = nextClientId++;
int clientId = nextClientId ++;
QHostAddress clientIp = socket->peerAddress();
quint16 clientPort = socket->peerPort();
QString ipString = clientIp.toString();
if (ipString.startsWith("::ffff:")) {
ipString = ipString.mid(7);
}
qDebug() << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
qDebug() << "> A client is connected. ID:" << clientId;
qDebug() << "> Client IP:" << ipString << " Port:" << clientPort;
qDebug() << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
if (ipString.startsWith("10.10.10")) {
qDebug() << "The client IP address is front board !!!";
isBackBoardOrAllBoard = 0;
}
else {
qDebug() << "The client IP address is back board or all board";
isBackBoardOrAllBoard = clientId;
}
stopMdnsService();
connectedClientsCount++;
connectedClientsCount ++;
updateServerButtonText();
QTimer* timer = new QTimer(this);
@@ -76,11 +118,11 @@ MainWidget::MainWidget(QWidget* parent) :
}
});
QThread* thread = new QThread(this);
QThread* thread = new QThread(this);
ClientHandler* handler = new ClientHandler(socket, frontBoardOneClickTest, frontBoardTest, frontBoardFuncConfig,
frontBoardDevInfoJson, frontBoardLicenseJson,
backBoardOneClickTest, backBoardTest, backBoardFuncConfig, backBoardDevInfoJson,
backBoardUuidJson, getPicJson, getVideoJson, clientId, nullptr);
backBoardUuidJson, getPicJson, getVideoJson, clientId, isBackBoardOrAllBoard, nullptr);
// 将 ClientHandler 移动到线程池中的线程
handler->moveToThread(thread);
@@ -91,7 +133,7 @@ MainWidget::MainWidget(QWidget* parent) :
});
// 将sendData信号连接到主线程中的槽上
connect(handler, &ClientHandler::sendData, this, [socket](const QByteArray& data) {
connect(handler, &ClientHandler::sendData, this, [socket](const QByteArray& data, int isBoardType) {
/*socket->write(data);
socket->flush();*/
//qDebug() << "---------------------data.size():" << data.size();
@@ -99,7 +141,12 @@ MainWidget::MainWidget(QWidget* parent) :
QDataStream stream(&prefix, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << quint32(0x55AA55AA);
stream << quint16(0x0420);
if (isBoardType != 0) {
stream << quint16(0x0421);
}
else {
stream << quint16(0x0420);
}
stream << quint32(data.size());
prefix.append(data);
@@ -124,15 +171,20 @@ MainWidget::MainWidget(QWidget* parent) :
connect(handler, &ClientHandler::allItemsProcessed, this, &MainWidget::onAllItemsProcessed);
connect(handler, &ClientHandler::selectClientDisconnected, this, &MainWidget::onDisconnectClient);
#if 1
// 创建 DataHandler 对象并连接信号
DataHandler* dataHandler = new DataHandler(leftLens_imageLabel, rightLens_imageLabel, videoLabel, licenseHwInfoEdit, &devInfoLineEdits,
frontBoardOneClickTest, frontBoardTest, frontBoardFuncConfig,
frontBoardDevInfoJson, frontBoardLicenseJson,
backBoardOneClickTest, backBoardTest, backBoardFuncConfig, backBoardDevInfoJson,
backBoardUuidJson, getPicJson, getVideoJson, this);
connect(handler, &ClientHandler::dataReceived, dataHandler, &DataHandler::handleData);
connect(dataHandler, &DataHandler::statusUpdated, this, &MainWidget::onStatusUpdated);
dataHandlers[clientId] = dataHandler;
connect(this, &MainWidget::openFocusWindowRequested, dataHandler, &DataHandler::handleOpenFocusWindow);
connect(handler, &ClientHandler::dataReceived, dataHandler, &DataHandler::handleData);
connect(dataHandler, &DataHandler::statusUpdated, this, &MainWidget::onStatusUpdated);
#else
connect(handler, &ClientHandler::dataReceived, dataHandler, &DataHandler::handleData);
#endif
connect(handler, &ClientHandler::startReadTimer, this, &MainWidget::startClientReadTimer);
connect(handler, &ClientHandler::stopReadTimer, this, &MainWidget::stopClientReadTimer);
@@ -157,7 +209,6 @@ MainWidget::MainWidget(QWidget* parent) :
connectionStatusCheckTimer = new QTimer(this);
connect(connectionStatusCheckTimer, &QTimer::timeout, this, &MainWidget::onCheckConnectionStatus);
//connectionStatusCheckTimer->start(100); // 每100ms检查一次连接状态
connect(mdnsTimer, &QTimer::timeout, this, &MainWidget::startMdnsService);
#if TEST_UDP_BROADCAST
@@ -199,6 +250,12 @@ MainWidget::~MainWidget()
delete timer;
}
clientReadTimers.clear();
for (auto dataHandler : dataHandlers) {
dataHandler->deleteLater();
}
dataHandlers.clear();
for (ClientHandler* handler : clients) {
handler->deleteLater();
}
@@ -228,6 +285,206 @@ MainWidget::~MainWidget()
qDebug() << "MainWidget destroyed";
}
// 处理客户端断开连接信号
void MainWidget::onClientDisconnected(ClientHandler* handler)
{
int clientId = handler->getClientId();
qDebug() << " preVideoClientId :" << handler->preVideoClientId;
qDebug() << " clientId :" << clientId;
if (handler->preVideoClientId == clientId) {
qDebug() << "Current path: " << QDir::currentPath();
QString filePath = QDir::currentPath() + "/add1.h264";
if (QFile::exists(filePath)) {
QFile file(filePath);
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
qDebug() << "File add1.h264 cleared successfully.";
}
else {
qDebug() << "Failed to clear file add1.h264:" << file.errorString();
}
}
else {
qDebug() << "File add1.h264 does not exist.";
}
}
if (clientTimers.contains(clientId)) {
QTimer* timer = clientTimers[clientId];
timer->stop();
delete timer;
clientTimers.remove(clientId);
}
clients.removeOne(handler);
/*
将 ClientHandler 对象的删除操作放入事件队列中,等待事件循环处理。
在事件循环执行时,会安全地删除 ClientHandler 对象,这包括释放其占用的资源和内存
*/
handler->deleteLater(); // 延迟删除 ClientHandler 对象
qDebug() << "-----------dataHandlers-----------";
if (dataHandlers.contains(clientId)) {
DataHandler* dataHandler = dataHandlers[clientId];
dataHandlers.remove(clientId); // 从容器中移除
dataHandler->deleteLater(); // 延迟删除 DataHandler 对象
}
// 更新连接数并更新按键文本
connectedClientsCount--;
qDebug() << " connectedClientsCount :" << connectedClientsCount;
if (nextClientId <= 2) nextClientId--;
deviceConnected = true;
updateServerButtonText();
}
// 更新按键文本的函数
void MainWidget::updateServerButtonText()
{
if (deviceConnected) {
if (connectedClientsCount > 0) {
startServerButton->setText(tr("正在监听(Listening)\n%1 台设备连接").arg(connectedClientsCount));
}
else if (connectedClientsCount == 0) {
#if START_MDNS // 这里会开启多个mdns广播去掉
startMdnsService();
#endif
deviceConnected = false;
startServerButton->setText(tr("正在监听(Listening)"));
}
}
else if (connectedClientsCount == 0) {
startServerButton->setText(tr("开始监听\n(Start Listening...)"));
}
else {
startServerButton->setText(tr("正在监听(Listening)\n%1 台设备连接").arg(connectedClientsCount));
}
}
// 设置 UI
void MainWidget::setupUI()
{
startServerButton = new QPushButton("开始监听\n(Start Listening...)", this);
startServerButton->setFixedSize(190, 70);
sendAllButton = new QPushButton("一键功能测试", this);
sendAllButton->setFixedSize(190, 70);
sendAllButton->setEnabled(false);
statusListWidget = new QListWidget(this);
statusListWidget->setMinimumSize(350, 840);
statusListWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QHBoxLayout* buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(startServerButton);
buttonLayout->addWidget(sendAllButton);
buttonLayout->addStretch();
QVBoxLayout* leftLayout = new QVBoxLayout;
leftLayout->addLayout(buttonLayout);
leftLayout->setStretch(0, 1);
leftLayout->addWidget(statusListWidget);
leftLayout->setStretch(1, 200);
saveCheckBox = new QCheckBox("", this);
saveCheckBox->setChecked(true);
selectFileButton = new QPushButton("Save", this);
selectFileButton->setFixedSize(45, 28);
clearLogButton = new QPushButton("Clear", this);
clearLogButton->setFixedSize(55, 28);
filePathLineEdit = new QLineEdit(this);
filePathLineEdit->setFixedSize(250, 28);
filePathLineEdit->setReadOnly(true);
QHBoxLayout* fileLayout = new QHBoxLayout;
fileLayout->addWidget(saveCheckBox);
fileLayout->addWidget(selectFileButton);
fileLayout->addWidget(filePathLineEdit);
fileLayout->addWidget(clearLogButton);
leftLayout->addLayout(fileLayout);
leftLayout->setStretch(2, 1);
// 读取 JSON 配置文件
readJsonConfig();
QWidget* leftContainer = new QWidget(this);
leftContainer->setLayout(leftLayout);
leftContainer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// 前板
QGroupBox* frontDeviceInfoGroupBox = createFrontDeviceInfoGroupBox();
QGroupBox* frontLicenseGroupBox = createLicenseGroupBox();
QHBoxLayout* frontGroupBoxLayout = new QHBoxLayout;
frontGroupBoxLayout->addWidget(frontDeviceInfoGroupBox, 1);
frontGroupBoxLayout->addWidget(frontLicenseGroupBox, 1);
QTabWidget* frontTabWidget = new QTabWidget(this);
frontTabWidget->setFixedSize(900, 315);
QWidget* frontFunctionTestTab = createFunctionTestTab(frontBoardTest, "frontBoardTest");
QWidget* frontFunctionConfigTab = createFunctionConfigTab(frontBoardFuncConfig, "frontBoardFuncConfig");
frontTabWidget->addTab(frontFunctionTestTab, "前板测试项");
frontTabWidget->addTab(frontFunctionConfigTab, "前板配置项");
QTabWidget* mainTabWidget = new QTabWidget(this);
QWidget* frontDevAreaTab = new QWidget(this);
QVBoxLayout* frontDevLayout = new QVBoxLayout(frontDevAreaTab);
frontDevLayout->addLayout(frontGroupBoxLayout);
frontDevLayout->addWidget(frontTabWidget, 2, Qt::AlignTop | Qt::AlignLeft);
mainTabWidget->addTab(frontDevAreaTab, "前 板");
// 后板
QGroupBox* backDeviceInfoGroupBox1 = createBackDeviceInfoGroupBox();
QGroupBox* BackConnectServerGroupBox = createBackConnectServerGroupBox();
QHBoxLayout* backGroupBoxLayout = new QHBoxLayout;
backGroupBoxLayout->addWidget(backDeviceInfoGroupBox1, 1);
backGroupBoxLayout->addWidget(BackConnectServerGroupBox, 1);
QTabWidget* backTabWidget = new QTabWidget(this);
backTabWidget->setFixedSize(900, 315);
QWidget* backFunctionTestTab = createFunctionTestTab(backBoardTest, "backBoardTest");
QWidget* backFunctionConfigTab = createFunctionConfigTab(backBoardFuncConfig, "backBoardFuncConfig");
backTabWidget->addTab(backFunctionTestTab, "后板测试项");
backTabWidget->addTab(backFunctionConfigTab, "后板配置项");
QWidget* backDevAreaTab = new QWidget(this);
QVBoxLayout* backDevLayout = new QVBoxLayout(backDevAreaTab);
backDevLayout->addLayout(backGroupBoxLayout);
backDevLayout->addWidget(backTabWidget, 2, Qt::AlignTop | Qt::AlignLeft);
mainTabWidget->addTab(backDevAreaTab, "后 板");
// 整机
QWidget* allDevAreaTab = new QWidget(this);
QVBoxLayout* allDevLayout = new QVBoxLayout(allDevAreaTab);
mainTabWidget->addTab(allDevAreaTab, "整 机");
QTabWidget* tabWidget_media = new QTabWidget(this);
QWidget* imageDisplayTab = createImageDisplayTab();
QWidget* videoDisplayTab = createVideoDisplayTab();
tabWidget_media->addTab(imageDisplayTab, "图像显示区");
tabWidget_media->addTab(videoDisplayTab, "视频显示区");
QVBoxLayout* rightVerticalLayout = new QVBoxLayout;
rightVerticalLayout->addWidget(mainTabWidget, 5);
rightVerticalLayout->addWidget(tabWidget_media, 4);
QHBoxLayout* mainLayout = new QHBoxLayout;
mainLayout->addWidget(leftContainer, 1, Qt::AlignTop | Qt::AlignLeft);
mainLayout->addLayout(rightVerticalLayout, 3);
setLayout(mainLayout);
setWindowTitle("SL100 工厂产测工具 - V0.0.1");
resize(1340, 900);
connect(startServerButton, &QPushButton::clicked, this, &MainWidget::onStartServerClicked);
connect(sendAllButton, &QPushButton::clicked, this, &MainWidget::onSendAllClicked);
connect(statusListWidget, &QListWidget::itemChanged, this, &MainWidget::scrollToBottom);
connect(selectFileButton, &QPushButton::clicked, this, &MainWidget::onSelectFileButtonClicked);
connect(clearLogButton, &QPushButton::clicked, this, &MainWidget::onclearLogButtonClicked);
connect(saveCheckBox, &QCheckBox::stateChanged, this, &MainWidget::onSaveCheckBoxStateChanged);
}
// 服务器响应
void MainWidget::onHttpRequestFinished(const QJsonObject& response)
{
@@ -546,131 +803,6 @@ void MainWidget::readJsonConfig()
getVideoJson = readJson_getVideo();
}
// 设置 UI
void MainWidget::setupUI()
{
startServerButton = new QPushButton("开始监听\n(Start Listening...)", this);
startServerButton->setFixedSize(190, 70);
sendAllButton = new QPushButton("一键功能测试", this);
sendAllButton->setFixedSize(190, 70);
sendAllButton->setEnabled(false);
statusListWidget = new QListWidget(this);
statusListWidget->setMinimumSize(350, 840);
statusListWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QHBoxLayout* buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(startServerButton);
buttonLayout->addWidget(sendAllButton);
buttonLayout->addStretch();
QVBoxLayout* leftLayout = new QVBoxLayout;
leftLayout->addLayout(buttonLayout);
leftLayout->setStretch(0, 1);
leftLayout->addWidget(statusListWidget);
leftLayout->setStretch(1, 200);
saveCheckBox = new QCheckBox("", this);
saveCheckBox->setChecked(true);
selectFileButton = new QPushButton("Save", this);
selectFileButton->setFixedSize(45, 28);
clearLogButton = new QPushButton("Clear", this);
clearLogButton->setFixedSize(55, 28);
filePathLineEdit = new QLineEdit(this);
filePathLineEdit->setFixedSize(250, 28);
filePathLineEdit->setReadOnly(true);
QHBoxLayout* fileLayout = new QHBoxLayout;
fileLayout->addWidget(saveCheckBox);
fileLayout->addWidget(selectFileButton);
fileLayout->addWidget(filePathLineEdit);
fileLayout->addWidget(clearLogButton);
leftLayout->addLayout(fileLayout);
leftLayout->setStretch(2, 1);
// 读取 JSON 配置文件
readJsonConfig();
QWidget* leftContainer = new QWidget(this);
leftContainer->setLayout(leftLayout);
leftContainer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// 前板
QGroupBox* frontDeviceInfoGroupBox = createFrontDeviceInfoGroupBox();
QGroupBox* frontLicenseGroupBox = createLicenseGroupBox();
QHBoxLayout* frontGroupBoxLayout = new QHBoxLayout;
frontGroupBoxLayout->addWidget(frontDeviceInfoGroupBox, 1);
frontGroupBoxLayout->addWidget(frontLicenseGroupBox, 1);
QTabWidget* frontTabWidget = new QTabWidget(this);
frontTabWidget->setFixedSize(900, 315);
QWidget* frontFunctionTestTab = createFunctionTestTab(frontBoardTest, "frontBoardTest");
QWidget* frontFunctionConfigTab = createFunctionConfigTab(frontBoardFuncConfig, "frontBoardFuncConfig");
frontTabWidget->addTab(frontFunctionTestTab, "前板测试项");
frontTabWidget->addTab(frontFunctionConfigTab, "前板配置项");
QTabWidget* mainTabWidget = new QTabWidget(this);
QWidget* frontDevAreaTab = new QWidget(this);
QVBoxLayout* frontDevLayout = new QVBoxLayout(frontDevAreaTab);
frontDevLayout->addLayout(frontGroupBoxLayout);
frontDevLayout->addWidget(frontTabWidget, 2, Qt::AlignTop | Qt::AlignLeft);
mainTabWidget->addTab(frontDevAreaTab, "前 板");
// 后板
QGroupBox* backDeviceInfoGroupBox1 = createBackDeviceInfoGroupBox();
QGroupBox* BackConnectServerGroupBox = createBackConnectServerGroupBox();
QHBoxLayout* backGroupBoxLayout = new QHBoxLayout;
backGroupBoxLayout->addWidget(backDeviceInfoGroupBox1, 1);
backGroupBoxLayout->addWidget(BackConnectServerGroupBox, 1);
QTabWidget* backTabWidget = new QTabWidget(this);
backTabWidget->setFixedSize(900, 315);
QWidget* backFunctionTestTab = createFunctionTestTab(backBoardTest, "backBoardTest");
QWidget* backFunctionConfigTab = createFunctionConfigTab(backBoardFuncConfig, "backBoardFuncConfig");
backTabWidget->addTab(backFunctionTestTab, "后板测试项");
backTabWidget->addTab(backFunctionConfigTab, "后板配置项");
QWidget* backDevAreaTab = new QWidget(this);
QVBoxLayout* backDevLayout = new QVBoxLayout(backDevAreaTab);
backDevLayout->addLayout(backGroupBoxLayout);
backDevLayout->addWidget(backTabWidget, 2, Qt::AlignTop | Qt::AlignLeft);
mainTabWidget->addTab(backDevAreaTab, "后 板");
// 整机
QWidget* allDevAreaTab = new QWidget(this);
QVBoxLayout* allDevLayout = new QVBoxLayout(allDevAreaTab);
mainTabWidget->addTab(allDevAreaTab, "整 机");
QTabWidget* tabWidget_media = new QTabWidget(this);
QWidget* imageDisplayTab = createImageDisplayTab();
QWidget* videoDisplayTab = createVideoDisplayTab();
tabWidget_media->addTab(imageDisplayTab, "图像显示区");
tabWidget_media->addTab(videoDisplayTab, "视频显示区");
QVBoxLayout* rightVerticalLayout = new QVBoxLayout;
rightVerticalLayout->addWidget(mainTabWidget, 5);
rightVerticalLayout->addWidget(tabWidget_media, 4);
QHBoxLayout* mainLayout = new QHBoxLayout;
mainLayout->addWidget(leftContainer, 1, Qt::AlignTop | Qt::AlignLeft);
mainLayout->addLayout(rightVerticalLayout, 3);
setLayout(mainLayout);
setWindowTitle("SL100 工厂产测工具 - V0.0.1");
resize(1340, 900);
connect(startServerButton, &QPushButton::clicked, this, &MainWidget::onStartServerClicked);
connect(sendAllButton, &QPushButton::clicked, this, &MainWidget::onSendAllClicked);
connect(statusListWidget, &QListWidget::itemChanged, this, &MainWidget::scrollToBottom);
connect(selectFileButton, &QPushButton::clicked, this, &MainWidget::onSelectFileButtonClicked);
connect(clearLogButton, &QPushButton::clicked, this, &MainWidget::onclearLogButtonClicked);
connect(saveCheckBox, &QCheckBox::stateChanged, this, &MainWidget::onSaveCheckBoxStateChanged);
}
void MainWidget::onSelectFileButtonClicked()
{
QString filePath = QFileDialog::getSaveFileName(this, tr("选择文件路径"), "", tr("Text Files (*.txt);;All Files (*)"));
@@ -754,75 +886,14 @@ void MainWidget::onDisconnectClient(int clientId)
clientTimers.remove(clientId);
}
handler->deleteLater();
connectedClientsCount--;
connectedClientsCount --;
updateServerButtonText();
break;
}
}
// 启动 mDNS 服务广播
startMdnsService();
}
// 处理客户端断开连接信号
void MainWidget::onClientDisconnected(ClientHandler* handler)
{
int clientId = handler->getClientId();
qDebug() << " preVideoClientId :" << handler->preVideoClientId;
qDebug() << " clientId :" << clientId;
if (handler->preVideoClientId == clientId) {
qDebug() << "Current path: " << QDir::currentPath();
QString filePath = QDir::currentPath() + "/add1.h264";
if (QFile::exists(filePath)) {
QFile file(filePath);
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { // 使用 Truncate 模式打开文件以清空内容
qDebug() << "File add1.h264 cleared successfully.";
}
else {
qDebug() << "Failed to clear file add1.h264:" << file.errorString();
}
}
else {
qDebug() << "File add1.h264 does not exist.";
}
}
if (clientTimers.contains(clientId)) {
QTimer* timer = clientTimers[clientId];
timer->stop();
delete timer;
clientTimers.remove(clientId);
}
clients.removeOne(handler);
/*
将 ClientHandler 对象的删除操作放入事件队列中,等待事件循环处理。
在事件循环执行时,会安全地删除 ClientHandler 对象,这包括释放其占用的资源和内存
*/
handler->deleteLater(); // 延迟删除 ClientHandler 对象
// 更新连接数并更新按键文本
connectedClientsCount--;
qDebug() << " connectedClientsCount :" << connectedClientsCount;
if (nextClientId <= 2) nextClientId--;
deviceConnected = true;
updateServerButtonText();
}
// 更新按键文本的函数
void MainWidget::updateServerButtonText()
{
if (deviceConnected) {
deviceConnected = false;
startServerButton->setText(tr("正在监听(Listening)"));
if (connectedClientsCount == 0) startMdnsService();
}
else if (connectedClientsCount == 0) {
startServerButton->setText(tr("开始监听\n(Start Listening...)"));
}
else {
startServerButton->setText(tr("正在监听(Listening)\n%1 台设备连接").arg(connectedClientsCount));
if (connectedClientsCount == 0) {
// 启动 mDNS 服务广播
startMdnsService();
}
}
@@ -1026,7 +1097,7 @@ void MainWidget::startMdnsService()
QString formattedTime = currentTime.toString("yyyy-MM-dd hh:mm:ss.zzz");
qDebug() << "[" << formattedTime << "]:" << "Start Mdns Broadcast Service";
QString serviceName = "SL100 FactoryTool Mdns Broadcast Service";
QString serviceType = "_SL100_FactoryTool-service._tcp";
QString serviceType = "_myservice._tcp.local";
quint16 port = TCP_CONNECT_PORT;
mServiceProvider->startServiceBroadcast(serviceName, serviceType, port);
@@ -1086,13 +1157,15 @@ void MainWidget::onStartServerClicked()
startServerButton->setText(tr("开始监听\n(Start Listening...)"));
startServerButton->setStyleSheet("");
sendAllButton->setEnabled(false);
stopMdnsService();
}
}
void MainWidget::sendDataToClient(int clientId, const QByteArray& data)
{
if ((clientId == 0) && (isBackBoardOrAllBoard)) {
clientId = isBackBoardOrAllBoard;
}
if (clients_1.contains(clientId)) {
ClientHandler* handler = clients_1[clientId];
handler->sendDataToClient(data); // 调用 ClientHandler 中的方法发送数据
@@ -1232,9 +1305,7 @@ void MainWidget::onOpenFocusWindowClicked()
int itemIndex = button->property("getVideoIndex").toInt();
//qDebug() << "New Button clicked with itemIndex:" << itemIndex;
if (itemIndex == 2) {
// 创建并显示新窗口
NewButtonDialog dialog(this);
dialog.exec();
emit openFocusWindowRequested(itemIndex); // 发送信号
}
}
}
@@ -1301,7 +1372,7 @@ void MainWidget::onSendBackFuncItemClicked()
QPushButton* button = qobject_cast<QPushButton*>(sender());
int itemIndex = button->property("backBoardFuncConfig").toInt();
for (ClientHandler* handler : clients) {
if (handler->getClientId() == handler->controlClientId) {
if ((isBackBoardOrAllBoard) || (handler->getClientId() == handler->controlClientId)) {
QString text = backFuncConfigLineEdit->text();
qDebug() << "Text in backFuncConfigLineEdit:" << text;
handler->sendBackFuncItem(itemIndex, text);
@@ -1342,7 +1413,7 @@ void MainWidget::onSendBackItemClicked()
QPushButton* button = qobject_cast<QPushButton*>(sender());
int itemIndex = button->property("backBoardTest").toInt();
for (ClientHandler* handler : clients) {
if (handler->getClientId() == handler->controlClientId) {
if ((isBackBoardOrAllBoard) || (handler->getClientId() == handler->controlClientId)) {
handler->sendBackItem(itemIndex);
break;
}