From d463857e23b786827abb0a00bd304fd163ca9560 Mon Sep 17 00:00:00 2001 From: awesome <3109372170@qq.com> Date: Sun, 11 Jan 2026 17:20:14 +0800 Subject: [PATCH] fix: #11 --- src/BarcodeWidget.cpp | 2 +- src/mqtt/mqtt_client.cpp | 90 ++++++++++++++++++-------------- src/mqtt/mqtt_client.h | 1 + translations/app_en_US.ts | 106 +++++++++++++++++++------------------- translations/app_zh_CN.ts | 106 +++++++++++++++++++------------------- 5 files changed, 158 insertions(+), 147 deletions(-) diff --git a/src/BarcodeWidget.cpp b/src/BarcodeWidget.cpp index ec1041c6..fcbf02dc 100644 --- a/src/BarcodeWidget.cpp +++ b/src/BarcodeWidget.cpp @@ -260,7 +260,7 @@ BarcodeWidget::BarcodeWidget(QWidget *parent) config.host, config.port, config.client_id, [this](const std::string &topic, const std::string &payload) { emit mqttMessageReceived(QString::fromStdString(topic), QByteArray::fromStdString(payload)); }); - subscriber_->subscribe("test/topic"); + subscriber_->subscribe(config.topic); messageWidget = std::make_unique(); diff --git a/src/mqtt/mqtt_client.cpp b/src/mqtt/mqtt_client.cpp index a0591551..36133be6 100644 --- a/src/mqtt/mqtt_client.cpp +++ b/src/mqtt/mqtt_client.cpp @@ -9,56 +9,66 @@ using json = nlohmann::json; MqttConfig MqttSubscriber::loadMqttConfig(const std::string &filename) { - std::ifstream file(filename); - json config_json; + using json = nlohmann::json; - if (file.is_open()) { - file >> config_json; - } - - // 默认配置,如果配置文件不存在则使用默认值 - std::string host = "127.0.0.1"; - uint16_t port = 1883; - std::string client_id; + MqttConfig config{}; + json root; - // 文件存在则更新上面的默认值 - if (config_json.contains("mqtt") && config_json["mqtt"].is_object()) { - auto &mqtt = config_json["mqtt"]; - if (mqtt.contains("host") && mqtt["host"].is_string()) { - host = mqtt["host"].get(); - } - if (mqtt.contains("port") && mqtt["port"].is_number()) { - port = mqtt["port"].get(); - } - if (mqtt.contains("client_id") && mqtt["client_id"].is_string()) { - client_id = mqtt["client_id"].get(); + // 1) 读取文件(容错解析) + try { + std::ifstream ifs(filename); + if (ifs) { + ifs >> root; + } else { + spdlog::warn("无法打开配置文件: {}", filename); } - } + } catch (const json::parse_error &e) { + spdlog::error("解析配置文件失败 ({}): {}", filename, e.what()); + } catch (const std::exception &e) { spdlog::error("读取配置文件异常 ({}): {}", filename, e.what()); } - spdlog::info("MQTT 配置文件: Host={}, Port={}, Client ID={}", host, port, client_id); + // 2) 取得 mqtt 节点(不存在则使用空对象) + json mqtt = root.value("mqtt", json::object()); - MqttConfig config; - config.host = host; - config.port = port; + // 3) 基本字段:使用默认值 + config.host = mqtt.value("host", std::string("localhost")); + config.port = static_cast(mqtt.value("port", 1883)); - // 如果 client_id 不存在或为空,则生成并保存 - if (client_id.empty()) { - config.client_id = generate_client_id(); - spdlog::info("client_id 不存在,生成ID: {}", config.client_id); + bool updated = false; - // 更新配置 - config_json["mqtt"]["host"] = host; - config_json["mqtt"]["port"] = port; - config_json["mqtt"]["client_id"] = config.client_id; - - std::ofstream out_file(filename); - if (out_file.is_open()) { - out_file << config_json.dump(4); + // 4) client_id:不存在或为空则生成 + std::string client_id = mqtt.value("client_id", std::string{}); + if (client_id.empty()) { + client_id = generate_client_id(); + mqtt["client_id"] = client_id; + updated = true; + spdlog::info("client_id 不存在,生成ID: {}", client_id); + } + config.client_id = std::move(client_id); + + // 5) topic:不存在或为空则设置默认 + std::string topic = mqtt.value("topic", std::string{}); + if (topic.empty()) { + topic = "test/topic"; + mqtt["topic"] = topic; + updated = true; + spdlog::info("topic 不存在,设置默认 topic: {}", topic); + } + config.topic = std::move(topic); + + // 6) 仅在有更新时写回 + if (updated) { + root["mqtt"] = mqtt; + std::ofstream ofs(filename, std::ios::trunc); + if (ofs) { + ofs << root.dump(4); + } else { + spdlog::warn("无法打开配置文件以写入: {}", filename); } - } else { - config.client_id = client_id; } + spdlog::info( + "MQTT 配置: Host={}, Port={}, ClientID={}, Topic={}", config.host, config.port, config.client_id, config.topic); + return config; } diff --git a/src/mqtt/mqtt_client.h b/src/mqtt/mqtt_client.h index 4e8273ba..1bb68227 100644 --- a/src/mqtt/mqtt_client.h +++ b/src/mqtt/mqtt_client.h @@ -16,6 +16,7 @@ struct MqttConfig { std::string host; uint16_t port; std::string client_id; + std::string topic; }; /** diff --git a/translations/app_en_US.ts b/translations/app_en_US.ts index 9133b20d..252bcfdc 100644 --- a/translations/app_en_US.ts +++ b/translations/app_en_US.ts @@ -78,110 +78,110 @@ BarcodeWidget - + 帮助 help - + 工具 tool - + 设置 settings - + 关于软件 About the software - + MQTT实时消息监控窗口 MQTT Real-time Message Monitoring Window - + 打开摄像头扫码 Open the camera to scan the code - + Base64 Base64 - + 文本输入 Text input - + 选择一个文件或图片 Select a file or image - + 浏览 Browse - + 生成 generate - + 解码 decode - + 保存 save - + 请选择任意文件来生成条码 Please select any file to generate the barcode - + 可以解码PNG图片中的条码 It can decode barcodes in PNG images - + 选择条码类型: Select barcode type: - + 宽度: width: - + 高度: height: @@ -349,7 +349,7 @@ or enter content - + 语言 language @@ -358,107 +358,107 @@ or enter content CameraWidget - + 摄像头预览 Camera preview - + 摄像头 Camera - + 显示设置 Display settings - + 二维码类型 QR code type - + 全选 Select All - + 清空 Clear - + 后处理 Post-processing - + 图像增强 Image enhancement - + 调试 Debug - + 保存识别帧 Save recognised frame - - + + 时间 Time - - + + 图像 Image - - + + 类型 Type - - + + 内容 Content - + [隐藏] PNG 数据 [Hidden] PNG Data - + [隐藏] 图片宽度 [隐藏] 图片宽度 - + [隐藏] 图片高度 [Hidden] Image Height @@ -479,25 +479,25 @@ or enter content - + 摄像头就绪... Camera ready... - + 导出 Export - + 导出 HTML (.html) Export HTML (.html) - + 导出 XLSX (.xlsx) Export XLSX (.xlsx) @@ -562,42 +562,42 @@ or enter content - + 错误 Error - + 无法打开摄像头 Unable to open the camera - + 摄像头已启动 Camera has started - + 摄像头已停止 Camera has stopped - + 摄像头运行中... Camera is running... - + 检测到 Detected - + code - + 条码类型: %1 内容: %2 时间: %3 @@ -610,7 +610,7 @@ Time: %3 - + 扫描结果 Scan results diff --git a/translations/app_zh_CN.ts b/translations/app_zh_CN.ts index e0884855..ed5c5529 100644 --- a/translations/app_zh_CN.ts +++ b/translations/app_zh_CN.ts @@ -78,110 +78,110 @@ BarcodeWidget - + 帮助 - + 工具 - + 设置 - + 关于软件 - + MQTT实时消息监控窗口 - + 打开摄像头扫码 - + Base64 - + 文本输入 - + 选择一个文件或图片 - + 浏览 - + 生成 - + 解码 - + 保存 - + 请选择任意文件来生成条码 - + 可以解码PNG图片中的条码 - + 选择条码类型: - + 宽度: - + 高度: @@ -338,7 +338,7 @@ - + 语言 @@ -347,107 +347,107 @@ CameraWidget - + 摄像头预览 - + 摄像头 - + 显示设置 - + 二维码类型 - + 全选 - + 清空 - + 后处理 - + 图像增强 - + 调试 - + 保存识别帧 - - + + 时间 - - + + 图像 - - + + 类型 - - + + 内容 - + [隐藏] PNG 数据 - + [隐藏] 图片宽度 - + [隐藏] 图片高度 @@ -468,25 +468,25 @@ - + 摄像头就绪... - + 导出 - + 导出 HTML (.html) - + 导出 XLSX (.xlsx) @@ -547,42 +547,42 @@ - + 错误 - + 无法打开摄像头 - + 摄像头已启动 - + 摄像头已停止 - + 摄像头运行中... - + 检测到 - + - + 条码类型: %1 内容: %2 时间: %3 @@ -591,7 +591,7 @@ - + 扫描结果