From eea7689c3dcfd8cd88da5466d62eb38620e8456f Mon Sep 17 00:00:00 2001 From: FAN-yeB <1442100690@qq.com> Date: Wed, 10 Sep 2025 17:48:31 +0800 Subject: [PATCH] Revert "Merge branch 'mqtt' of https://github.com/xinnan-tech/xiaozhi-esp32-server into mqtt" This reverts commit 7b9e34c3e5247ddd2bab613ff05216d7c7c15b4d, reversing changes made to f81be335397b07749bc5bd713fd6590a3245d409. --- .../service/impl/DeviceServiceImpl.java | 30 +++++++++---------- .../resources/db/changelog/202509080921.sql | 7 +++++ .../resources/db/changelog/202509080922.sql | 8 ----- .../resources/db/changelog/202509080927.sql | 2 ++ .../db/changelog/db.changelog-master.yaml | 14 +++++++-- 5 files changed, 35 insertions(+), 26 deletions(-) create mode 100644 main/manager-api/src/main/resources/db/changelog/202509080921.sql delete mode 100644 main/manager-api/src/main/resources/db/changelog/202509080922.sql create mode 100644 main/manager-api/src/main/resources/db/changelog/202509080927.sql diff --git a/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java index 66380041..b4343276 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java @@ -9,7 +9,6 @@ import java.util.List; import java.util.Map; import java.util.TimeZone; import java.util.UUID; - import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; @@ -38,7 +37,6 @@ import xiaozhi.common.user.UserDetail; import xiaozhi.common.utils.ConvertUtils; import xiaozhi.common.utils.DateUtils; import xiaozhi.modules.device.dao.DeviceDao; -import xiaozhi.modules.device.dto.DeviceManualAddDTO; import xiaozhi.modules.device.dto.DevicePageUserDTO; import xiaozhi.modules.device.dto.DeviceReportReqDTO; import xiaozhi.modules.device.dto.DeviceReportRespDTO; @@ -50,6 +48,7 @@ import xiaozhi.modules.device.vo.UserShowDeviceListVO; import xiaozhi.modules.security.user.SecurityUser; import xiaozhi.modules.sys.service.SysParamsService; import xiaozhi.modules.sys.service.SysUserUtilService; +import xiaozhi.modules.device.dto.DeviceManualAddDTO; @Slf4j @Service @@ -180,11 +179,11 @@ public class DeviceServiceImpl extends BaseServiceImpl } response.setWebsocket(websocket); - + // 添加MQTT UDP配置 // 从系统参数获取MQTT Gateway地址,仅在配置有效时使用 String mqttUdpConfig = sysParamsService.getValue(Constant.SERVER_MQTT_GATEWAY, false); - if (mqttUdpConfig != null && !mqttUdpConfig.equals("null") && !mqttUdpConfig.isEmpty() && deviceById != null) { + if(mqttUdpConfig != null && !mqttUdpConfig.equals("null") && !mqttUdpConfig.isEmpty() && deviceById != null) { try { DeviceReportRespDTO.MQTT mqtt = buildMqttConfig(macAddress, clientId, deviceById); if (mqtt != null) { @@ -195,7 +194,7 @@ public class DeviceServiceImpl extends BaseServiceImpl log.error("生成MQTT配置失败: {}", e.getMessage()); } } - + if (deviceById != null) { // 如果设备存在,则异步更新上次连接时间和版本信息 String appVersion = deviceReport.getApplication() != null ? deviceReport.getApplication().getVersion() @@ -460,8 +459,7 @@ public class DeviceServiceImpl extends BaseServiceImpl /** * 生成MQTT密码签名 - * - * @param content 签名内容 (clientId + '|' + username) + * @param content 签名内容 (clientId + '|' + username) * @param secretKey 密钥 * @return Base64编码的HMAC-SHA256签名 */ @@ -475,16 +473,19 @@ public class DeviceServiceImpl extends BaseServiceImpl /** * 构建MQTT配置信息 - * * @param macAddress MAC地址 - * @param clientId 客户端ID (UUID) - * @param device 设备信息 + * @param clientId 客户端ID (UUID) + * @param device 设备信息 * @return MQTT配置对象 */ - private DeviceReportRespDTO.MQTT buildMqttConfig(String macAddress, String clientId, DeviceEntity device) - throws Exception { + private DeviceReportRespDTO.MQTT buildMqttConfig(String macAddress, String clientId, DeviceEntity device) throws Exception { // 从环境变量或系统参数获取签名密钥 - String signatureKey = sysParamsService.getValue("server.mqtt_signature_key", false); + String signatureKey = System.getenv("MQTT_SIGNATURE_KEY"); + if (StringUtils.isBlank(signatureKey)) { + // 如果环境变量没有,尝试从系统参数获取 + signatureKey = sysParamsService.getValue("mqtt.signature_key", false); + } + if (StringUtils.isBlank(signatureKey)) { log.warn("缺少MQTT_SIGNATURE_KEY,跳过MQTT配置生成"); return null; @@ -499,8 +500,7 @@ public class DeviceServiceImpl extends BaseServiceImpl Map userData = new HashMap<>(); // 尝试获取客户端IP try { - ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder - .getRequestAttributes(); + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (attributes != null) { HttpServletRequest request = attributes.getRequest(); String clientIp = request.getRemoteAddr(); diff --git a/main/manager-api/src/main/resources/db/changelog/202509080921.sql b/main/manager-api/src/main/resources/db/changelog/202509080921.sql new file mode 100644 index 00000000..4d89ee57 --- /dev/null +++ b/main/manager-api/src/main/resources/db/changelog/202509080921.sql @@ -0,0 +1,7 @@ +delete from `sys_params` where id = 108; +delete from `sys_params` where param_code = 'server.mqtt_gateway'; +INSERT INTO `sys_params` (id, param_code, param_value, value_type, param_type, remark) VALUES (108, 'server.mqtt_gateway', 'null', 'string', 1, 'mqtt gateway 配置'); + +delete from `sys_params` where param_code = 'server.udp_gateway'; +INSERT INTO `sys_params` (id, param_code, param_value, value_type, param_type, remark) VALUES (109, 'server.udp_gateway', 'null', 'string', 1, 'udp gateway 配置'); + diff --git a/main/manager-api/src/main/resources/db/changelog/202509080922.sql b/main/manager-api/src/main/resources/db/changelog/202509080922.sql deleted file mode 100644 index 424b3cd8..00000000 --- a/main/manager-api/src/main/resources/db/changelog/202509080922.sql +++ /dev/null @@ -1,8 +0,0 @@ -delete from `sys_params` where param_code = 'server.mqtt_gateway'; -INSERT INTO `sys_params` (id, param_code, param_value, value_type, param_type, remark) VALUES (116, 'server.mqtt_gateway', 'null', 'string', 1, 'mqtt gateway 配置'); - -delete from `sys_params` where param_code = 'server.mqtt_signature_key'; -INSERT INTO `sys_params` (id, param_code, param_value, value_type, param_type, remark) VALUES (117, 'server.mqtt_signature_key', 'null', 'string', 1, 'mqtt 密钥 配置'); - -delete from `sys_params` where param_code = 'server.udp_gateway'; -INSERT INTO `sys_params` (id, param_code, param_value, value_type, param_type, remark) VALUES (118, 'server.udp_gateway', 'null', 'string', 1, 'udp gateway 配置'); \ No newline at end of file diff --git a/main/manager-api/src/main/resources/db/changelog/202509080927.sql b/main/manager-api/src/main/resources/db/changelog/202509080927.sql new file mode 100644 index 00000000..4741af88 --- /dev/null +++ b/main/manager-api/src/main/resources/db/changelog/202509080927.sql @@ -0,0 +1,2 @@ +delete from `sys_params` where param_code = 'mqtt.signature_key'; +INSERT INTO `sys_params` (id, param_code, param_value, value_type, param_type, remark) VALUES (120, 'mqtt.signature_key', 'null', 'string', 1, 'mqtt 密钥 配置'); diff --git a/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml b/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml index c355a16a..ed66ebb7 100755 --- a/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml @@ -305,9 +305,17 @@ databaseChangeLog: path: classpath:db/changelog/202508131557.sql - changeSet: - id: 202509080922 - author: fyb + id: 202509080921 + author: fan changes: - sqlFile: encoding: utf8 - path: classpath:db/changelog/202509080922.sql \ No newline at end of file + path: classpath:db/changelog/202509080921.sql + + - changeSet: + id: 202509080927 + author: fan + changes: + - sqlFile: + encoding: utf8 + path: classpath:db/changelog/202509080927.sql \ No newline at end of file