diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 6e687da..3d34788 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -21,34 +21,34 @@ MainWindow::MainWindow(QWidget *parent) { // 初始化设置对象 settings = new QSettings("TuxiApp", "LightStripSN", this); - + setupUI(); - + // 创建MQTT客户端 mqttClient = new MqttClient(this); - + // 连接信号和槽 connect(connectBtn, &QPushButton::clicked, this, &MainWindow::onConnectClicked); connect(disconnectBtn, &QPushButton::clicked, this, &MainWindow::onDisconnectClicked); connect(sendLightAllBtn, &QPushButton::clicked, this, &MainWindow::onSendLightAllClicked); connect(clearSnBtn, &QPushButton::clicked, this, &MainWindow::onClearSnListClicked); connect(searchLightStripBtn, &QPushButton::clicked, this, &MainWindow::onSearchLightStripClicked); - + connect(mqttClient, &MqttClient::connected, this, &MainWindow::onMqttConnected); connect(mqttClient, &MqttClient::disconnected, this, &MainWindow::onMqttDisconnected); connect(mqttClient, &MqttClient::messageReceived, this, &MainWindow::onMessageReceived); connect(mqttClient, &MqttClient::errorOccurred, this, &MainWindow::onMqttError); - + connect(otaUpgradeBtn, &QPushButton::clicked, this, &MainWindow::onOtaUpgradeClicked); connect(otaDowngradeBtn, &QPushButton::clicked, this, &MainWindow::onOtaDowngradeClicked); connect(getVersionBtn, &QPushButton::clicked, this, &MainWindow::onGetVersionClicked); - + // 初始化灯条管理器 lightStripManager = nullptr; - + // 加载已保存的灯条SN列表 loadSnList(); - + // 初始化状态 updateConnectionStatus(false); currentVersionEdit->setText("请点击获取版本"); @@ -58,74 +58,121 @@ MainWindow::~MainWindow() { } +/* +void MainWindow::setupMenuBar() +{ + // 获取主窗口的菜单栏 + QMenuBar *mainMenuBar = this->menuBar(); + + // 创建帮助菜单 + QMenu *helpMenu = mainMenuBar->addMenu("帮助(&H)"); + + // 创建版本更新说明菜单项 + versionUpdateAction = new QAction("版本更新说明(&V)", this); + helpMenu->addAction(versionUpdateAction); + + // 连接信号槽 + connect(versionUpdateAction, &QAction::triggered, this, &MainWindow::showVersionUpdateInfo); +} + +void MainWindow::showVersionUpdateInfo() +{ + QString versionInfo = + "版本更新说明\n\n" + "版本 1.3.0 (2024-01-15)\n" + "• 新增版本更新说明菜单\n" + "• 优化MQTT连接稳定性\n" + "• 修复已知问题\n\n" + "版本 1.2.0 (2023-12-20)\n" + "• 改进用户界面\n" + "• 增强数据处理能力\n\n" + "版本 1.1.0 (2023-11-10)\n" + "• 初始版本发布\n" + "• 基础功能实现"; + + QMessageBox::information(this, "版本更新说明", versionInfo); +} +*/ + void MainWindow::setupUI() { setWindowTitle("兔喜Test1.2 Author:Zhangzhenghao Email:zzh9953477@gmail.com"); - + // 参考qt_bak的合理尺寸设置,增加竖向高度 setMinimumSize(850, 720); // 增加最小高度 resize(900, 770); // 增加初始高度,从750增加到850 - + // 获取屏幕尺寸并设置合适的窗口大小 QScreen *screen = QGuiApplication::primaryScreen(); if (screen) { QRect screenGeometry = screen->geometry(); int screenWidth = screenGeometry.width(); int screenHeight = screenGeometry.height(); - + // 设置合理的窗口大小,增加竖向比例 - int windowWidth = qMin(1000, static_cast(screenWidth * 0.6)); + int windowWidth = qMin(1000, static_cast(screenWidth * 0.6)); int windowHeight = qMin(900, static_cast(screenHeight * 0.7)); // 增加到75% - + // 确保高度明显大于宽度 if (windowHeight < windowWidth * 0.95) { windowHeight = static_cast(windowWidth * 0.9); // 高度等于宽度 } - + resize(windowWidth, windowHeight); - + // 居中显示 move((screenWidth - windowWidth) / 2, (screenHeight - windowHeight) / 2 - 50); } - + setWindowIcon(QIcon(":/image/src/tuxi.ico")); centralWidget = new QWidget(this); setCentralWidget(centralWidget); mainLayout = new QVBoxLayout(centralWidget); mainLayout->setSpacing(10); // 减少布局间距 mainLayout->setContentsMargins(15, 10, 15, 10); // 减少边距 - + + // 获取适合当前主题的文字颜色 + QString textColor = getTextColorForTheme(); + // MQTT连接区域 connectionGroup = new QGroupBox("MQTT连接设置", this); - connectionGroup->setStyleSheet("QGroupBox { font-weight: bold; font-size: 12px; padding-top: 10px; }"); + connectionGroup->setStyleSheet(QString("QGroupBox { font-weight: bold; font-size: 12px; padding-top: 10px; color: %1; }").arg(textColor)); QVBoxLayout *connectionLayout = new QVBoxLayout(connectionGroup); connectionLayout->setSpacing(10); - + // 服务器和端口 - 使用水平布局 QHBoxLayout *serverLayout = new QHBoxLayout(); - serverLayout->addWidget(new QLabel("服务器:")); + QLabel *serverLabel = new QLabel("服务器:"); + serverLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor)); + serverLayout->addWidget(serverLabel); brokerEdit = new QLineEdit("tx-mqtt.zt-express.com"); brokerEdit->setMinimumHeight(30); serverLayout->addWidget(brokerEdit); - serverLayout->addWidget(new QLabel("端口:")); + QLabel *portLabel = new QLabel("端口:"); + portLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor)); + serverLayout->addWidget(portLabel); portEdit = new QLineEdit("1883"); portEdit->setMinimumHeight(30); portEdit->setMaximumWidth(100); serverLayout->addWidget(portEdit); connectionLayout->addLayout(serverLayout); - + // 用户名和密码 QHBoxLayout *authLayout = new QHBoxLayout(); - authLayout->addWidget(new QLabel("用户名:")); + QLabel *usernameLabel = new QLabel("用户名:"); + usernameLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor)); + authLayout->addWidget(usernameLabel); usernameEdit = new QLineEdit("TJ251679787196"); usernameEdit->setMinimumHeight(30); authLayout->addWidget(usernameEdit); - authLayout->addWidget(new QLabel("密码:")); + QLabel *passwordLabel = new QLabel("密码:"); + passwordLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor)); + authLayout->addWidget(passwordLabel); passwordEdit = new QLineEdit(); passwordEdit->setEchoMode(QLineEdit::Password); passwordEdit->setMinimumHeight(30); authLayout->addWidget(passwordEdit); connectionLayout->addLayout(authLayout); - + // 连接按钮 QHBoxLayout *btnLayout = new QHBoxLayout(); connectBtn = new QPushButton("连接"); @@ -138,37 +185,43 @@ void MainWindow::setupUI() { btnLayout->addWidget(disconnectBtn); btnLayout->addStretch(); connectionLayout->addLayout(btnLayout); - + mainLayout->addWidget(connectionGroup); - + // 设备控制区域 deviceGroup = new QGroupBox("设备控制", this); deviceGroup->setStyleSheet("QGroupBox { font-weight: bold; font-size: 12px; padding-top: 10px; }"); QHBoxLayout *deviceLayout = new QHBoxLayout(deviceGroup); deviceLayout->setSpacing(10); - deviceLayout->addWidget(new QLabel("需要测试的设备SN:")); + QLabel *deviceSnLabel = new QLabel("需要测试的设备SN:"); + deviceSnLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor)); + deviceLayout->addWidget(deviceSnLabel); deviceSnEdit = new QLineEdit("TJ251617198122"); deviceSnEdit->setMinimumHeight(30); deviceLayout->addWidget(deviceSnEdit); - + mainLayout->addWidget(deviceGroup); - + // 点亮参数控制区域 lightGroup = new QGroupBox("点亮参数设置", this); lightGroup->setStyleSheet("QGroupBox { font-weight: bold; font-size: 12px; padding-top: 10px; }"); QVBoxLayout *lightLayout = new QVBoxLayout(lightGroup); lightLayout->setSpacing(10); - + // 第一行:颜色和闪烁 QHBoxLayout *row1Layout = new QHBoxLayout(); - row1Layout->addWidget(new QLabel("颜色:")); + QLabel *colorLabel = new QLabel("颜色:"); + colorLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor)); + row1Layout->addWidget(colorLabel); colorCombo = new QComboBox(); colorCombo->addItems({"8-灭", "1-红", "2-黄", "3-蓝", "4-绿", "5-青", "6-白", "7-紫"}); colorCombo->setCurrentIndex(6); colorCombo->setMinimumHeight(30); row1Layout->addWidget(colorCombo); - - row1Layout->addWidget(new QLabel("闪烁:")); + + QLabel *flashLabel = new QLabel("闪烁:"); + flashLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor)); + row1Layout->addWidget(flashLabel); flashCombo = new QComboBox(); flashCombo->addItems({"0-关闭", "1-开启"}); flashCombo->setCurrentIndex(0); @@ -176,17 +229,21 @@ void MainWindow::setupUI() { row1Layout->addWidget(flashCombo); row1Layout->addStretch(); lightLayout->addLayout(row1Layout); - + // 第二行:闪烁间隔和点亮时长 QHBoxLayout *row2Layout = new QHBoxLayout(); - row2Layout->addWidget(new QLabel("闪烁间隔(秒):")); + QLabel *flashIntervalLabel = new QLabel("闪烁间隔(秒):"); + flashIntervalLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor)); + row2Layout->addWidget(flashIntervalLabel); flashIntervalSpin = new QSpinBox(); flashIntervalSpin->setRange(1, 60); flashIntervalSpin->setValue(4); flashIntervalSpin->setMinimumHeight(30); row2Layout->addWidget(flashIntervalSpin); - - row2Layout->addWidget(new QLabel("点亮时长(秒):")); + + QLabel *lightDurationLabel = new QLabel("点亮时长(秒):"); + lightDurationLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor)); + row2Layout->addWidget(lightDurationLabel); lightDurationSpin = new QSpinBox(); lightDurationSpin->setRange(1, 300); lightDurationSpin->setValue(30); @@ -194,26 +251,28 @@ void MainWindow::setupUI() { row2Layout->addWidget(lightDurationSpin); row2Layout->addStretch(); lightLayout->addLayout(row2Layout); - + // 第三行:声音和发送按钮 QHBoxLayout *row3Layout = new QHBoxLayout(); - row3Layout->addWidget(new QLabel("声音:")); + QLabel *soundLabel = new QLabel("声音:"); + soundLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor)); + row3Layout->addWidget(soundLabel); soundCombo = new QComboBox(); soundCombo->addItems({"0-关闭", "1-开启"}); soundCombo->setCurrentIndex(0); soundCombo->setMinimumHeight(30); row3Layout->addWidget(soundCombo); - + row3Layout->addStretch(); sendLightAllBtn = new QPushButton("发送全部点亮"); - sendLightAllBtn->setMinimumHeight(40); + sendLightAllBtn->setMinimumHeight(35); sendLightAllBtn->setMinimumWidth(120); sendLightAllBtn->setStyleSheet("QPushButton { background-color: #4CAF50; color: white; font-weight: bold; padding: 10px 20px; border-radius: 6px; font-size: 14px; }"); row3Layout->addWidget(sendLightAllBtn); lightLayout->addLayout(row3Layout); - + mainLayout->addWidget(lightGroup); - + // 灯条SN管理区域 - 简化为一个按钮 snGroup = new QGroupBox("灯条SN管理", this); snGroup->setStyleSheet( @@ -225,18 +284,18 @@ void MainWindow::setupUI() { " border: 2px solid #ddd; " " border-radius: 8px; " "}" - ); + ); snGroup->setMinimumHeight(120); // 减少最小高度 snGroup->setMaximumHeight(140); // 设置最大高度限制 - + QVBoxLayout *snLayout = new QVBoxLayout(snGroup); snLayout->setSpacing(8); // 减少组件间距 snLayout->setContentsMargins(10, 20, 10, 10); // 减少内边距 - + // 统计信息和搜索按钮布局 QHBoxLayout *snHeaderLayout = new QHBoxLayout(); snHeaderLayout->setSpacing(8); - + snCountLabel = new QLabel("已发现灯条: 0 个"); snCountLabel->setStyleSheet( "QLabel { " @@ -249,9 +308,9 @@ void MainWindow::setupUI() { " border-radius: 6px; " " min-height: 12px; " "}" - ); + ); snHeaderLayout->addWidget(snCountLabel); - + snHeaderLayout->addStretch(); searchLightStripBtn = new QPushButton("搜索灯条", this); searchLightStripBtn->setMinimumHeight(26); // 减少按钮高度 @@ -269,10 +328,10 @@ void MainWindow::setupUI() { "QPushButton:hover { " " background-color: #1976D2; " "}" - ); + ); snHeaderLayout->addWidget(searchLightStripBtn); snLayout->addLayout(snHeaderLayout); - + // 打开管理器按钮 openManagerBtn = new QPushButton("打开灯条SN管理器"); openManagerBtn->setMinimumHeight(32); // 减少按钮高度 @@ -293,41 +352,43 @@ void MainWindow::setupUI() { "QPushButton:pressed { " " background-color: #1565C0; " "}" - ); + ); connect(openManagerBtn, &QPushButton::clicked, this, &MainWindow::openLightStripManager); snLayout->addWidget(openManagerBtn); - + mainLayout->addWidget(snGroup); - + // 消息显示区域 messageGroup = new QGroupBox("消息日志", this); QVBoxLayout *msgLayout = new QVBoxLayout(messageGroup); messageDisplay = new QTextEdit(); messageDisplay->setReadOnly(true); msgLayout->addWidget(messageDisplay); - + mainLayout->addWidget(messageGroup); - + // 状态栏 statusLabel = new QLabel("未连接"); statusBar()->addWidget(statusLabel); - + // OTA升级区域 otaGroup = new QGroupBox("OTA升降级", this); QVBoxLayout *otaLayout = new QVBoxLayout(otaGroup); - + // 当前版本显示 QHBoxLayout *versionLayout = new QHBoxLayout(); - versionLayout->addWidget(new QLabel("当前版本:")); + QLabel *versionLabel = new QLabel("当前版本:"); + versionLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor)); + versionLayout->addWidget(versionLabel); currentVersionEdit = new QLineEdit(this); currentVersionEdit->setReadOnly(true); versionLayout->addWidget(currentVersionEdit); - + // 创建获取版本按钮 getVersionBtn = new QPushButton("获取版本", this); versionLayout->addWidget(getVersionBtn); otaLayout->addLayout(versionLayout); - + // OTA按钮 QHBoxLayout *otaButtonLayout = new QHBoxLayout(); otaUpgradeBtn = new QPushButton("升级", this); @@ -335,17 +396,20 @@ void MainWindow::setupUI() { otaButtonLayout->addWidget(otaUpgradeBtn); otaButtonLayout->addWidget(otaDowngradeBtn); otaLayout->addLayout(otaButtonLayout); - + // 下载进度条 otaProgressBar = new QProgressBar(this); // 修正变量名 otaProgressBar->setVisible(false); otaLayout->addWidget(otaProgressBar); - + // OTA状态标签 otaStatusLabel = new QLabel("就绪", this); otaLayout->addWidget(otaStatusLabel); - + mainLayout->addWidget(otaGroup); + + // 在setupUI的最后调用菜单创建 + createMenus(); } void MainWindow::onConnectClicked() { @@ -354,17 +418,17 @@ void MainWindow::onConnectClicked() { QString username = usernameEdit->text().trimmed(); QString password = passwordEdit->text(); QString deviceSn = deviceSnEdit->text().trimmed(); - + if (broker.isEmpty()) { QMessageBox::warning(this, "警告", "请输入MQTT服务器地址"); return; } - + if (portNum <= 0 || portNum > 65535) { QMessageBox::warning(this, "警告", "请输入有效的端口号 (1-65535)"); return; } - + // 设置用户名和密码(如果有的话) if (!username.isEmpty()) { mqttClient->setUsername(username); @@ -372,14 +436,14 @@ void MainWindow::onConnectClicked() { if (!password.isEmpty()) { mqttClient->setPassword(password); } - + // 连接到MQTT服务器 mqttClient->connectToHost(broker, static_cast(portNum)); - + // 更新状态 statusLabel->setText("正在连接..."); connectBtn->setEnabled(false); - + // 注意:订阅操作应该在连接成功后进行,在onMqttConnected()函数中处理 } @@ -391,26 +455,26 @@ void MainWindow::onDisconnectClicked() void MainWindow::onSendLightAllClicked() { QString deviceSn = deviceSnEdit->text().trimmed(); - + if (deviceSn.isEmpty()) { messageDisplay->append(QString("[%1] 错误: 请输入设备SN") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); return; } - + if (!mqttClient->isConnected()) { messageDisplay->append(QString("[%1] 错误: MQTT未连接") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); return; } - + // 获取参数值 QString color = QString::number(colorCombo->currentIndex()); QString flash = QString::number(flashCombo->currentIndex()); QString flashInterval = QString::number(flashIntervalSpin->value()); QString lightDuration = QString::number(lightDurationSpin->value()); QString sound = QString::number(soundCombo->currentIndex()); - + // 构造消息内容 QJsonObject dataObj; dataObj["color"] = color; @@ -418,37 +482,37 @@ void MainWindow::onSendLightAllClicked() dataObj["flashInterval"] = flashInterval; dataObj["lightDuration"] = lightDuration; dataObj["sound"] = sound; - + QJsonObject msgObj; msgObj["data"] = dataObj; msgObj["msgType"] = "3027"; - + QJsonObject rootObj; rootObj["deviceId"] = deviceSn; rootObj["messageId"] = "1933039995430551552"; // 修复:将QByteArray转换为QString rootObj["msg"] = QString::fromUtf8(QJsonDocument(msgObj).toJson(QJsonDocument::Compact)); rootObj["timestamp"] = 2147483647; - + QString message = QJsonDocument(rootObj).toJson(QJsonDocument::Compact); - + // 发送到指定主题 QString topic = QString("iot/10045/%1/message/adviceDevice").arg(deviceSn); - + if (mqttClient->publish(topic, message)) { messageDisplay->append(QString("[%1] 发送全部点亮指令到设备 %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(deviceSn)); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(deviceSn)); messageDisplay->append(QString("主题: %1").arg(topic)); messageDisplay->append(QString("参数: 颜色=%1, 闪烁=%2, 间隔=%3s, 时长=%4s, 声音=%5") - .arg(colorCombo->currentText()) - .arg(flashCombo->currentText()) - .arg(flashInterval) - .arg(lightDuration) - .arg(soundCombo->currentText())); + .arg(colorCombo->currentText()) + .arg(flashCombo->currentText()) + .arg(flashInterval) + .arg(lightDuration) + .arg(soundCombo->currentText())); } else { messageDisplay->append(QString("[%1] 发送全部点亮指令失败") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); } } @@ -456,57 +520,57 @@ void MainWindow::onMqttConnected() { statusLabel->setText("MQTT连接成功"); statusLabel->setStyleSheet("QLabel { color: green; font-weight: bold; }"); updateConnectionStatus(true); - + QString deviceSn = deviceSnEdit->text().trimmed(); if (!deviceSn.isEmpty()) { // 订阅原有主题 QString responseTopic = QString("iot/10045/%1/response").arg(deviceSn); QString stationTopic = QString("iot/10045/%1/station/report").arg(deviceSn); - + // 订阅light report主题 QString lightReportTopic = QString("iot/10045/%1/light/report").arg(deviceSn); - + // 订阅adviceDevice主题(用于搜索灯条响应) QString adviceDeviceTopic = QString("iot/10045/%1/message/adviceDevice").arg(deviceSn); QString resourceReportTopic = QString("iot/10045/%1/resource/report").arg(deviceSn); - + mqttClient->subscribe(responseTopic); mqttClient->subscribe(stationTopic); mqttClient->subscribe(lightReportTopic); mqttClient->subscribe(adviceDeviceTopic); mqttClient->subscribe(resourceReportTopic); - + messageDisplay->append(QString("[%1] 已订阅主题: %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(responseTopic)); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(responseTopic)); messageDisplay->append(QString("[%1] 已订阅主题: %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(stationTopic)); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(stationTopic)); messageDisplay->append(QString("[%1] 已订阅主题: %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(lightReportTopic)); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(lightReportTopic)); messageDisplay->append(QString("[%1] 已订阅主题: %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(adviceDeviceTopic)); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(adviceDeviceTopic)); messageDisplay->append(QString("[%1] 已订阅主题: %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(resourceReportTopic)); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(resourceReportTopic)); } } void MainWindow::onMqttDisconnected() { messageDisplay->append(QString("[%1] MQTT连接断开") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); updateConnectionStatus(false); } void MainWindow::onMqttError(const QString &error) { messageDisplay->append(QString("[%1] MQTT错误: %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(error)); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(error)); updateConnectionStatus(false); } @@ -516,11 +580,11 @@ void MainWindow::onMessageReceived(const QString &topic, const QString &message) messageDisplay->append(QString("Topic: %1").arg(topic)); messageDisplay->append(QString("Message: %1").arg(message)); messageDisplay->append("---"); - + // 处理消息格式问题:提取纯JSON部分 QString jsonMessage = message; QString realTopic = topic; - + // 如果topic是默认的incoming/topic,尝试从消息中提取真实主题 if (topic == "incoming/topic" && message.contains("{")) { // 查找消息中的主题信息(格式:%topic{json}) @@ -532,17 +596,17 @@ void MainWindow::onMessageReceived(const QString &topic, const QString &message) } } } - + // 去除开头的单引号 if (jsonMessage.startsWith("'")) { jsonMessage = jsonMessage.mid(1); } - + // 去除结尾的单引号 if (jsonMessage.endsWith("'")) { jsonMessage = jsonMessage.left(jsonMessage.length() - 1); } - + // 检查消息是否包含主题信息(格式:%topic{json}或topic{json}) if (jsonMessage.contains("{")) { int jsonStart = jsonMessage.indexOf("{"); @@ -550,28 +614,28 @@ void MainWindow::onMessageReceived(const QString &topic, const QString &message) jsonMessage = jsonMessage.mid(jsonStart); } } - + // 解析JSON消息 QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(jsonMessage.toUtf8(), &error); - + if (error.error != QJsonParseError::NoError) { messageDisplay->append(QString("[%1] JSON解析错误: %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(error.errorString())); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(error.errorString())); return; } - + QJsonObject root = doc.object(); messageDisplay->append(QString("[%1] JSON解析成功").arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); - + // 检查是否包含msg字段(优先处理) if (root.contains("msg")) { messageDisplay->append(QString("[%1] 找到msg字段").arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); - + QJsonValue msgValue = root["msg"]; QJsonObject msgObj; - + // msg可能是字符串或对象 if (msgValue.isString()) { QJsonDocument msgDoc = QJsonDocument::fromJson(msgValue.toString().toUtf8()); @@ -582,75 +646,75 @@ void MainWindow::onMessageReceived(const QString &topic, const QString &message) msgObj = msgValue.toObject(); messageDisplay->append(QString("[%1] msg是对象类型").arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); } - + // 解析data中的baseVersion if (msgObj.contains("data")) { messageDisplay->append(QString("[%1] 找到data字段").arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); - + QJsonObject dataObj = msgObj["data"].toObject(); if (dataObj.contains("baseVersion")) { QString version = dataObj["baseVersion"].toString(); messageDisplay->append(QString("[%1] 找到baseVersion: %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(version)); - + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(version)); + // 确保currentVersionEdit存在 if (currentVersionEdit) { currentVersionEdit->setText(version); messageDisplay->append(QString("[%1] 版本已设置到输入框: %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(version)); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(version)); } else { messageDisplay->append(QString("[%1] 错误: currentVersionEdit为空") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); } - + statusLabel->setText(QString("获取到当前版本: %1").arg(version)); return; } } } - + // 检查是否包含resource字段(直接在根对象中) if (root.contains("resource")) { QString resource = root["resource"].toString(); QString version = resource.trimmed(); messageDisplay->append(QString("[%1] 找到resource字段: %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(version)); - + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(version)); + if (currentVersionEdit) { currentVersionEdit->setText(version); messageDisplay->append(QString("[%1] 版本已设置到输入框: %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(version)); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(version)); } statusLabel->setText(QString("获取到当前版本: %1").arg(version)); return; } - + // 检查是否是light/report主题(使用提取的真实主题) if (realTopic.contains("/light/report")) { messageDisplay->append("[DEBUG] 检测到light/report消息,开始解析SN"); processLightReportMessage(jsonMessage); } - + // 检查是否是station/report主题且有待处理的OTA操作 if (otaOperationPending && (realTopic.contains("/station/report") || realTopic.contains("/resource/report"))) { QString operationType = isUpgradeOperation ? "升级" : "降级"; QString successMessage = QString("OTA%1成功 版本: %2") - .arg(operationType) - .arg(pendingOtaVersion); - + .arg(operationType) + .arg(pendingOtaVersion); + messageDisplay->append(QString("[%1] %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(successMessage)); - + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(successMessage)); + // 重置OTA状态 otaOperationPending = false; pendingOtaVersion.clear(); isUpgradeOperation = false; - + statusLabel->setText(successMessage); } } @@ -661,7 +725,7 @@ void MainWindow::processOtaMessage(const QJsonObject &otaData) QString zipPath = otaData["zipPath"].toString(); int installType = otaData["installType"].toInt(); int immediately = otaData["immediately"].toInt(); - + otaStatusLabel->setText(QString("收到OTA任务: 版本 %1").arg(version)); } @@ -672,16 +736,16 @@ void MainWindow::onOtaUpgradeClicked() { QMessageBox::warning(this, "警告", "请先输入设备SN"); return; } - + // 设置OTA状态跟踪 otaOperationPending = true; pendingOtaVersion = "1.1.41"; isUpgradeOperation = true; - + // 构造符合您格式的OTA升级消息 QJsonObject data; data["msgType"] = "1014"; - + QJsonObject otaData; otaData["needWifi"] = 2; otaData["zipPath"] = "http://180.163.74.83:8000/tx_ota_1.1.41.zip"; @@ -689,9 +753,9 @@ void MainWindow::onOtaUpgradeClicked() { otaData["immediately"] = 0; otaData["installTime"] = QDateTime::currentMSecsSinceEpoch() + 300000; // 5分钟后安装 otaData["version"] = "1.1.41"; - + data["data"] = otaData; - + QJsonObject root; root["deviceId"] = deviceSn; // 使用输入的设备SN root["header"] = QJsonValue::Null; @@ -699,21 +763,21 @@ void MainWindow::onOtaUpgradeClicked() { root["msg"] = QString::fromUtf8(QJsonDocument(data).toJson(QJsonDocument::Compact)); root["productId"] = ""; root["timestamp"] = QDateTime::currentMSecsSinceEpoch(); - + QJsonDocument doc(root); QString message = QString::fromUtf8(doc.toJson(QJsonDocument::Compact)); - + // 构建正确的MQTT主题 QString topic = QString("iot/10045/%1/message/adviceDevice").arg(deviceSn); - + if (mqttClient && mqttClient->isConnected()) { mqttClient->publish(topic, message); statusLabel->setText(QString("已发送OTA升级命令到设备 %1 (版本 1.1.41)").arg(deviceSn)); - + // 显示发送的消息内容 - QMessageBox::information(this, "OTA升级命令", - QString("已发送升级命令:\n设备SN: %1\n版本: 1.1.41\n主题: %2\n消息: %3") - .arg(deviceSn, topic, message)); + QMessageBox::information(this, "OTA升级命令", + QString("已发送升级命令:\n设备SN: %1\n版本: 1.1.41\n主题: %2\n消息: %3") + .arg(deviceSn, topic, message)); } else { statusLabel->setText("MQTT未连接,无法发送OTA命令"); // 如果发送失败,重置状态 @@ -728,16 +792,16 @@ void MainWindow::onOtaDowngradeClicked() { QMessageBox::warning(this, "警告", "请先输入设备SN"); return; } - + // 设置OTA状态跟踪 otaOperationPending = true; pendingOtaVersion = "1.1.40"; isUpgradeOperation = false; - + // 构造符合您格式的OTA降级消息 QJsonObject data; data["msgType"] = "1014"; - + QJsonObject otaData; otaData["needWifi"] = 2; otaData["zipPath"] = "http://180.163.74.83:8000/tx_ota_1.1.40.zip"; @@ -745,9 +809,9 @@ void MainWindow::onOtaDowngradeClicked() { otaData["immediately"] = 0; otaData["installTime"] = QDateTime::currentMSecsSinceEpoch() + 300000; // 5分钟后安装 otaData["version"] = "1.1.40"; - + data["data"] = otaData; - + QJsonObject root; root["deviceId"] = deviceSn; // 使用输入的设备SN root["header"] = QJsonValue::Null; @@ -755,21 +819,21 @@ void MainWindow::onOtaDowngradeClicked() { root["msg"] = QString::fromUtf8(QJsonDocument(data).toJson(QJsonDocument::Compact)); root["productId"] = ""; root["timestamp"] = QDateTime::currentMSecsSinceEpoch(); - + QJsonDocument doc(root); QString message = QString::fromUtf8(doc.toJson(QJsonDocument::Compact)); - + // 构建正确的MQTT主题 QString topic = QString("iot/10045/%1/message/adviceDevice").arg(deviceSn); - + if (mqttClient && mqttClient->isConnected()) { mqttClient->publish(topic, message); statusLabel->setText(QString("已发送OTA降级命令到设备 %1 (版本 1.1.40)").arg(deviceSn)); - + // 显示发送的消息内容 - QMessageBox::information(this, "OTA降级命令", - QString("已发送降级命令:\n设备SN: %1\n版本: 1.1.40\n主题: %2\n消息: %3") - .arg(deviceSn, topic, message)); + QMessageBox::information(this, "OTA降级命令", + QString("已发送降级命令:\n设备SN: %1\n版本: 1.1.40\n主题: %2\n消息: %3") + .arg(deviceSn, topic, message)); } else { statusLabel->setText("MQTT未连接,无法发送OTA命令"); // 如果发送失败,重置状态 @@ -785,12 +849,12 @@ void MainWindow::sendSearchLightStripCommand() { QString deviceSn = deviceSnEdit->text().trimmed(); if (deviceSn.isEmpty()) { messageDisplay->append(QString("[%1] 错误: 设备SN不能为空") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); return; } - + QString topic = QString("iot/10045/%1/message/adviceDevice").arg(deviceSn); - + // 构造消息 QJsonObject message; message["deviceId"] = ""; @@ -798,37 +862,37 @@ void MainWindow::sendSearchLightStripCommand() { message["messageId"] = "1958474134031921152"; message["productId"] = ""; message["timestamp"] = QDateTime::currentMSecsSinceEpoch(); - + // 构造msg字段 QJsonObject msgData; msgData["scene"] = "1"; msgData["timeout"] = "120"; - + QJsonObject msgObj; msgObj["data"] = msgData; msgObj["msgType"] = "5005"; - + QJsonDocument msgDoc(msgObj); message["msg"] = QString::fromUtf8(msgDoc.toJson(QJsonDocument::Compact)); - + QJsonDocument doc(message); QString jsonString = doc.toJson(QJsonDocument::Compact); - + // 发送消息 if (mqttClient->publish(topic, jsonString)) { messageDisplay->append(QString("[%1] 发送搜索灯条命令") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); messageDisplay->append(QString("Topic: %1").arg(topic)); messageDisplay->append(QString("Message: %1").arg(jsonString)); messageDisplay->append("---"); - + // 弹出提示框 QMessageBox::information(this, "搜索灯条", "不要做其他指令,正在搜索灯条,请等待2分钟返回结果..."); } else { messageDisplay->append(QString("[%1] 发送搜索灯条命令失败") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss"))); messageDisplay->append("---"); - + // 弹出错误提示框 QMessageBox::warning(this, "错误", "发送搜索灯条命令失败,请检查网络连接"); } @@ -838,14 +902,14 @@ void MainWindow::sendOtaCommand(const QString& version, bool isUpgrade) { QJsonObject msg; msg["action"] = isUpgrade ? "upgrade" : "downgrade"; msg["version"] = version; - + QJsonObject root; root["type"] = "ota_command"; root["msg"] = QString::fromUtf8(QJsonDocument(msg).toJson(QJsonDocument::Compact)); - + QJsonDocument doc(root); QString message = QString::fromUtf8(doc.toJson(QJsonDocument::Compact)); - + if (mqttClient && mqttClient->isConnected()) { mqttClient->publish("device/ota/command", message); statusLabel->setText(QString("已发送OTA%1命令 (版本 %2)").arg(isUpgrade ? "升级" : "降级", version)); @@ -861,15 +925,15 @@ void MainWindow::onGetVersionClicked() { QMessageBox::warning(this, "警告", "请先输入设备SN"); return; } - + // 构造获取版本的消息 QJsonObject data; data["msgType"] = "2335"; - + QJsonObject cmdData; cmdData["cmd"] = "cat /userdata/tx_version"; data["data"] = cmdData; - + QJsonObject root; root["deviceId"] = deviceSn; root["header"] = QJsonValue::Null; @@ -877,13 +941,13 @@ void MainWindow::onGetVersionClicked() { root["msg"] = QString::fromUtf8(QJsonDocument(data).toJson(QJsonDocument::Compact)); root["productId"] = ""; root["timestamp"] = QDateTime::currentMSecsSinceEpoch(); - + QJsonDocument doc(root); QString message = QString::fromUtf8(doc.toJson(QJsonDocument::Compact)); - + // 使用正确的发送主题 QString topic = QString("iot/10045/%1/message/adviceDevice").arg(deviceSn); - + if (mqttClient && mqttClient->isConnected()) { mqttClient->publish(topic, message); statusLabel->setText(QString("已发送获取版本命令到设备 %1").arg(deviceSn)); @@ -897,13 +961,13 @@ void MainWindow::updateConnectionStatus(bool connected) connectBtn->setEnabled(!connected); disconnectBtn->setEnabled(connected); sendLightAllBtn->setEnabled(connected); - + // 新增:连接成功后禁用连接参数输入框,断开后重新启用 usernameEdit->setEnabled(!connected); portEdit->setEnabled(!connected); passwordEdit->setEnabled(!connected); brokerEdit->setEnabled(!connected); // 同时也禁用服务器地址输入框 - + // 修改连接按钮样式:连接成功后设置灰色背景,断开后恢复绿色 if (connected) { connectBtn->setStyleSheet("QPushButton { background-color: #cccccc; color: #666666; font-weight: bold; padding: 8px 16px; border-radius: 4px; }"); @@ -918,33 +982,33 @@ void MainWindow::updateConnectionStatus(bool connected) void MainWindow::processLightReportMessage(const QString &message) { messageDisplay->append(QString("[DEBUG] 开始处理light/report消息: %1").arg(message)); - + QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8(), &error); - + if (error.error != QJsonParseError::NoError) { messageDisplay->append(QString("[ERROR] JSON解析失败: %1").arg(error.errorString())); return; } - + QJsonObject rootObj = doc.object(); messageDisplay->append(QString("[DEBUG] 根对象键: %1").arg(QStringList(rootObj.keys()).join(", "))); - + QJsonObject msgObj = rootObj["msg"].toObject(); messageDisplay->append(QString("[DEBUG] msg对象键: %1").arg(QStringList(msgObj.keys()).join(", "))); - + QJsonObject dataObj = msgObj["data"].toObject(); messageDisplay->append(QString("[DEBUG] data对象键: %1").arg(QStringList(dataObj.keys()).join(", "))); - + QJsonArray lightsArray = dataObj["lights"].toArray(); messageDisplay->append(QString("[DEBUG] lights数组长度: %1").arg(lightsArray.size())); - + int newSnCount = 0; for (const QJsonValue &lightValue : lightsArray) { QJsonObject lightObj = lightValue.toObject(); QString sn = lightObj["sn"].toString(); messageDisplay->append(QString("[DEBUG] 处理灯条SN: %1").arg(sn)); - + if (!sn.isEmpty() && !uniqueSnSet.contains(sn)) { messageDisplay->append(QString("[DEBUG] 添加新SN到列表: %1").arg(sn)); addSnToList(sn); @@ -955,7 +1019,7 @@ void MainWindow::processLightReportMessage(const QString &message) { messageDisplay->append(QString("[DEBUG] SN已存在,跳过: %1").arg(sn)); } } - + if (newSnCount > 0) { messageDisplay->append(QString("[INFO] 新发现 %1 个灯条SN").arg(newSnCount)); saveSnList(); @@ -969,12 +1033,12 @@ void MainWindow::addSnToList(const QString &sn) if (uniqueSnSet.contains(sn)) { return; } - + uniqueSnSet.insert(sn); - + // 更新主窗口的计数显示 snCountLabel->setText(QString("已发现灯条: %1 个").arg(uniqueSnSet.size())); - + // 如果灯条管理器已打开,同步添加(但不重复更新MainWindow的计数) if (lightStripManager) { // 临时断开信号连接,避免重复更新 @@ -986,7 +1050,7 @@ void MainWindow::addSnToList(const QString &sn) snCountLabel->setText(QString("已发现灯条: %1 个").arg(count)); }); } - + // 移除这里的saveSnList()调用,让调用方决定何时保存 // saveSnList(); } @@ -1012,17 +1076,17 @@ void MainWindow::onClearSnListClicked() delete item->widget(); delete item; } - + // 清空复选框列表和SN集合 lightStripCheckBoxes.clear(); uniqueSnSet.clear(); - + // 更新计数显示 snCountLabel->setText("已发现灯条: 0 个"); - + // 不再调用saveSnList(),让LightStripManager管理数据 // saveSnList(); // 删除这行 - + // 如果LightStripManager存在,调用公有的清空方法 if (lightStripManager) { lightStripManager->clearAllData(); @@ -1033,27 +1097,27 @@ void MainWindow::openLightStripManager() { if (!lightStripManager) { lightStripManager = new LightStripManager(this); - + // 连接信号 connect(lightStripManager, &LightStripManager::lightControlRequested, this, [this](const QStringList &sns, const QString &color, bool flash, int interval, int duration, bool sound) { // 处理点亮控制请求 // 实现MQTT发送逻辑 - + // 检查MQTT连接状态 if (!mqttClient->isConnected()) { qDebug() << "MQTT未连接,无法发送灯条控制命令"; QMessageBox::warning(this, "警告", "MQTT未连接,请先连接MQTT服务器"); return; } - + // 获取设备SN(从界面获取) QString deviceSn = deviceSnEdit->text().trimmed(); if (deviceSn.isEmpty()) { QMessageBox::warning(this, "警告", "请先输入需要测试的设备SN"); return; } - + // 构建内层msg数据 QJsonObject msgData; QRegularExpression re("\\d+"); @@ -1068,7 +1132,7 @@ void MainWindow::openLightStripManager() msgData["lightDuration"] = QString::number(duration); msgData["scene"] = "3"; // 根据业务需求设置 msgData["sound"] = sound ? "1" : "0"; - + // 构建lights数组 QJsonArray lightsArray; for (const QString &sn : sns) { @@ -1077,136 +1141,136 @@ void MainWindow::openLightStripManager() lightsArray.append(lightObj); } msgData["lights"] = lightsArray; - + // 构建完整的msg对象 QJsonObject fullMsg; fullMsg["data"] = msgData; fullMsg["msgType"] = "3015"; // 根据业务协议设置 - + // 转换msg为JSON字符串 QJsonDocument msgDoc(fullMsg); QString msgString = msgDoc.toJson(QJsonDocument::Compact); - + // 构建最外层消息 QJsonObject outerMessage; outerMessage["deviceId"] = ""; // 根据需要设置 outerMessage["messageId"] = QString::number(QDateTime::currentMSecsSinceEpoch()); // 生成唯一消息ID outerMessage["msg"] = msgString; outerMessage["timestamp"] = QDateTime::currentMSecsSinceEpoch(); - + // 转换为最终JSON字符串 QJsonDocument finalDoc(outerMessage); QString finalMessage = finalDoc.toJson(QJsonDocument::Compact); - + // 构建MQTT主题 QString topic = QString("iot/10045/%1/message/adviceDevice").arg(deviceSn); - + // 发送MQTT消息 bool success = mqttClient->publish(topic, finalMessage); - + if (success) { qDebug() << "成功发送灯条控制命令"; qDebug() << "主题:" << topic; qDebug() << "消息:" << finalMessage; - + // 显示成功提示 QString resultMessage = QString("已向设备 %1 发送控制命令,控制 %2 个灯条") - .arg(deviceSn) - .arg(sns.size()); + .arg(deviceSn) + .arg(sns.size()); QMessageBox::information(this, "成功", resultMessage); } else { qDebug() << "发送灯条控制命令失败"; QMessageBox::warning(this, "错误", "发送MQTT消息失败,请检查网络连接"); } }); - + connect(lightStripManager, &LightStripManager::snSelectionChanged, this, [this](const QStringList &selectedSns) { // 处理选择变化 qDebug() << "Selected SNs changed:" << selectedSns; }); - + // 新增:连接数量变化信号 connect(lightStripManager, &LightStripManager::snCountChanged, this, [this](int count) { // 同步更新MainWindow中的灯条数量显示 snCountLabel->setText(QString("已发现灯条: %1 个").arg(count)); }); - + // 新增:连接身份信息绑定信号 connect(lightStripManager, &LightStripManager::identityBindingRequested, this, [this](const QString &sn, const QString &label1, const QString &label2, const QString &label3) { // 处理身份信息绑定请求 - + // 检查MQTT连接状态 if (!mqttClient->isConnected()) { qDebug() << "MQTT未连接,无法发送身份信息绑定命令"; QMessageBox::warning(this, "警告", "MQTT未连接,请先连接MQTT服务器"); return; } - + // 获取设备SN(从界面获取) QString deviceSn = deviceSnEdit->text().trimmed(); if (deviceSn.isEmpty()) { QMessageBox::warning(this, "警告", "请先输入需要测试的设备SN"); return; } - + // 构建身份信息绑定消息 QJsonObject msgData; msgData["sn"] = sn; msgData["label1"] = label1; msgData["label2"] = label2; msgData["label3"] = label3; - + // 构建完整的msg对象 QJsonObject fullMsg; fullMsg["data"] = msgData; fullMsg["msgType"] = "3022"; // 身份信息绑定消息类型 - + // 转换msg为JSON字符串 QJsonDocument msgDoc(fullMsg); QString msgString = msgDoc.toJson(QJsonDocument::Compact); - + // 构建最外层消息 QJsonObject outerMessage; outerMessage["deviceId"] = ""; outerMessage["messageId"] = QString::number(QDateTime::currentMSecsSinceEpoch()); outerMessage["msg"] = msgString; outerMessage["timestamp"] = QDateTime::currentMSecsSinceEpoch(); - + // 转换为最终JSON字符串 QJsonDocument finalDoc(outerMessage); QString finalMessage = finalDoc.toJson(QJsonDocument::Compact); - + // 构建MQTT主题 QString topic = QString("iot/10045/%1/message/adviceDevice").arg(deviceSn); - + // 发送MQTT消息 bool success = mqttClient->publish(topic, finalMessage); - + if (success) { qDebug() << "成功发送身份信息绑定命令"; qDebug() << "主题:" << topic; qDebug() << "消息:" << finalMessage; - + // 在消息显示区域显示发送的消息 messageDisplay->append(QString("[%1] 发送身份信息绑定到设备 %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(deviceSn)); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(deviceSn)); messageDisplay->append(QString("主题: %1").arg(topic)); messageDisplay->append(QString("灯条SN: %1, Label1: %2, Label2: %3, Label3: %4") - .arg(sn).arg(label1).arg(label2).arg(label3)); + .arg(sn).arg(label1).arg(label2).arg(label3)); } else { qDebug() << "发送身份信息绑定命令失败"; QMessageBox::warning(this, "错误", "发送MQTT消息失败,请检查网络连接"); } }); - + // 新增:连接分组点亮信号 connect(lightStripManager, &LightStripManager::groupLightRequested, - this, [this](const QString &label1, const QString &label2, const QString &label3, - int rule1, int rule2, int rule3, const QString &color, int flash, int duration, bool sound, int flashInterval) { + this, [this](const QString &label1, const QString &label2, const QString &label3, + int rule1, int rule2, int rule3, const QString &color, int flash, int duration, bool sound, int flashInterval) { // 检查MQTT连接状态 if (!mqttClient->isConnected()) { @@ -1214,14 +1278,14 @@ void MainWindow::openLightStripManager() QMessageBox::warning(this, "警告", "MQTT未连接,请先连接MQTT服务器"); return; } - + // 获取设备SN QString deviceSn = deviceSnEdit->text().trimmed(); if (deviceSn.isEmpty()) { QMessageBox::warning(this, "警告", "请先输入需要测试的设备SN"); return; } - + // 构建labelConfig对象 QJsonObject labelConfig; labelConfig["label1"] = label1; @@ -1230,7 +1294,7 @@ void MainWindow::openLightStripManager() labelConfig["label1Rule"] = QString("%1").arg(rule1, 2, 10, QChar('0')); labelConfig["label2Rule"] = QString("%1").arg(rule2, 2, 10, QChar('0')); labelConfig["label3Rule"] = QString("%1").arg(rule3, 2, 10, QChar('0')); - + // 构建分组点亮消息数据 QJsonObject msgData; msgData["labelConfig"] = labelConfig; @@ -1239,62 +1303,62 @@ void MainWindow::openLightStripManager() msgData["flash"] = QString::number(flash); msgData["flashInterval"] = QString::number(flashInterval); msgData["lightDuration"] = QString::number(duration); - + // 构建完整的msg对象 QJsonObject fullMsg; fullMsg["msgType"] = "3023"; fullMsg["data"] = msgData; - + // 转换msg为JSON字符串 QJsonDocument msgDoc(fullMsg); QString msgString = msgDoc.toJson(QJsonDocument::Compact); - + // 构建最外层消息 QJsonObject outerMessage; outerMessage["deviceId"] = ""; outerMessage["messageId"] = QString::number(QDateTime::currentMSecsSinceEpoch()); outerMessage["msg"] = msgString; outerMessage["timestamp"] = QDateTime::currentMSecsSinceEpoch(); - + // 转换为最终JSON字符串 QJsonDocument finalDoc(outerMessage); QString finalMessage = finalDoc.toJson(QJsonDocument::Compact); - + // 构建MQTT主题 QString topic = QString("iot/10045/%1/message/adviceDevice").arg(deviceSn); - + // 发送MQTT消息 bool success = mqttClient->publish(topic, finalMessage); - + if (success) { qDebug() << "成功发送分组点亮命令"; qDebug() << "主题:" << topic; qDebug() << "消息:" << finalMessage; - + // 在消息显示区域显示发送的消息 messageDisplay->append(QString("[%1] 发送分组点亮命令到设备 %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(deviceSn)); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(deviceSn)); messageDisplay->append(QString("主题: %1").arg(topic)); messageDisplay->append(QString("匹配规则: Label1=%1(rule:%2), Label2=%3(rule:%4), Label3=%5(rule:%6)") - .arg(label1).arg(rule1, 2, 10, QChar('0')) - .arg(label2).arg(rule2, 2, 10, QChar('0')) - .arg(label3).arg(rule3, 2, 10, QChar('0'))); + .arg(label1).arg(rule1, 2, 10, QChar('0')) + .arg(label2).arg(rule2, 2, 10, QChar('0')) + .arg(label3).arg(rule3, 2, 10, QChar('0'))); } else { qDebug() << "发送分组点亮命令失败"; QMessageBox::warning(this, "错误", "发送MQTT消息失败,请检查网络连接"); } }); - + // 连接关闭信号 connect(lightStripManager, &QWidget::destroyed, this, &MainWindow::onLightStripManagerClosed); - + // 同步当前的SN列表到管理器 QStringList snList = uniqueSnSet.values(); lightStripManager->syncSnListFromMainWindow(snList); } - + lightStripManager->show(); lightStripManager->raise(); lightStripManager->activateWindow(); @@ -1322,9 +1386,62 @@ void MainWindow::publishMqttMessage(const QString &topic, const QString &message if (mqttClient && mqttClient->isConnected()) { mqttClient->publish(topic, message); messageDisplay->append(QString("[%1] 发送消息到主题: %2") - .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) - .arg(topic)); + .arg(QDateTime::currentDateTime().toString("hh:mm:ss")) + .arg(topic)); } else { QMessageBox::warning(this, "错误", "MQTT未连接,无法发送消息"); } } + +void MainWindow::createMenus() +{ + // 直接创建菜单,不使用成员变量 + QMenu *helpMenu = menuBar()->addMenu("帮助(&H)"); + + QAction *aboutAction = helpMenu->addAction("关于程序(&A)"); + connect(aboutAction, &QAction::triggered, this, &MainWindow::showAbout); + + QAction *useGuideAction = helpMenu->addAction("使用说明(&U)"); + connect(useGuideAction, &QAction::triggered, this, &MainWindow::showUseGuide); +} + +void MainWindow::showAbout() +{ + QMessageBox::about(this, "关于程序", + "兔喜MQTT测试程序\n\n" + "版本: 1.2.0\n" + "构建日期: 2025-09-12\n\n" + "功能特性:\n" + "• 修复清空全部sn未生效的问题\n" + "• 修复label匹配值错误的问题\n" + "• 匹配系统颜色。修复浅色模式下字体看不清\n" + "• 增加窗口版本显示\n"); +} + +void MainWindow::showUseGuide() +{ + QMessageBox::about(this, "使用说明", + "切换主题后看不清文字,请重启应用!!\n" + "1. 连接MQTT服务器\n" + "2. 输入需要测试的设备SN\n" + "3. 首次使用先升级版本,1.1.16及之前的版本可能不支持升级\n" + "4. 可以做其他指令,如:查询版本、搜索灯带、灯带SN管理\n\n" + ); +} + +// 新增:检测系统主题并返回适合的文字颜色 +QString MainWindow::getTextColorForTheme() const { + // 获取系统调色板 + QPalette palette = QApplication::palette(); + + // 检查窗口背景色的亮度来判断是否为深色主题 + QColor backgroundColor = palette.color(QPalette::Window); + + // 计算亮度 (使用相对亮度公式) + double luminance = (0.299 * backgroundColor.red() + + 0.587 * backgroundColor.green() + + 0.114 * backgroundColor.blue()) / 255.0; + + // 如果背景较暗(亮度小于0.5),使用白色文字;否则使用黑色文字 + return (luminance < 0.5) ? "white" : "black"; +} diff --git a/src/mainwindow.h b/src/mainwindow.h index f99e59b..3e9d955 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -29,6 +29,12 @@ #include #include "mqttclient.h" #include "lightstripmanager.h" +#include +#include +#include +#include +#include // 新增:用于获取系统调色板 +#include class MainWindow : public QMainWindow { @@ -37,9 +43,24 @@ class MainWindow : public QMainWindow public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); + +private slots: + void showAbout(); // 简化为关于对话框 + void showUseGuide(); // 新增:使用说明对话框 + +private: + void setupUI(); + void createMenus(); // 重命名为更清晰的函数名 + + // 不使用成员变量存储菜单指针,直接在函数中创建 + // void setupMenuBar(); + + // 注释掉菜单相关成员变量 + // QAction *versionUpdateAction; // 新增:公共接口 QString getDeviceSn() const; + QString getTextColorForTheme() const; bool isMqttConnected() const; void publishMqttMessage(const QString &topic, const QString &message); @@ -65,8 +86,6 @@ private slots: void onLightStripManagerClosed(); // 新增:灯条管理器关闭时的处理 private: - void setupUI(); - void setupMenuBar(); void setupToolBar(); void updateConnectionStatus(bool connected); void processOtaMessage(const QJsonObject &otaData);