提交mqtt后端修改

This commit is contained in:
marlonz
2025-06-17 16:40:17 +09:30
parent 0e57aad12d
commit b7c0201ca3
9 changed files with 94 additions and 7 deletions
@@ -91,6 +91,15 @@ public interface Constant {
*/
String SERVER_WEBSOCKET = "server.websocket";
/**
* mqtt gateway 配置
*/
String SERVER_MQTT_GATEWAY = "server.mqtt_gateway";
/**
* websocket地址
*/
/**
* ota地址
*/
@@ -66,5 +66,5 @@ public class JsonUtils {
throw new RuntimeException(e);
}
}
}
@@ -78,6 +78,10 @@ public class OTAController {
@GetMapping
@Hidden
public ResponseEntity<String> getOTA() {
String mqttUdpConfig = sysParamsService.getValue(Constant.SERVER_MQTT_GATEWAY, false);
if(StringUtils.isBlank(mqttUdpConfig)) {
return ResponseEntity.ok("OTA接口不正常,缺少websocket地址,请登录智控台,在参数管理找到【server.mqtt_udp】配置");
}
String wsUrl = sysParamsService.getValue(Constant.SERVER_WEBSOCKET, true);
if (StringUtils.isBlank(wsUrl) || wsUrl.equals("null")) {
return ResponseEntity.ok("OTA接口不正常,缺少websocket地址,请登录智控台,在参数管理找到【server.websocket】配置");
@@ -22,6 +22,9 @@ public class DeviceReportRespDTO {
@Schema(description = "WebSocket配置")
private Websocket websocket;
@Schema(description = "MQTT Gateway配置")
private MQTT mqtt;
@Getter
@Setter
@@ -70,4 +73,21 @@ public class DeviceReportRespDTO {
@Schema(description = "WebSocket服务器地址")
private String url;
}
@Getter
@Setter
public static class MQTT {
@Schema(description = "MQTT 配置网址")
private String endpoint;
@Schema(description = "MQTT 客户端唯一标识符")
private String client_id;
@Schema(description = "MQTT 认证用户名")
private String username;
@Schema(description = "MQTT 认证密码")
private String password;
@Schema(description = "ESP32 发布消息的主题")
private String publish_topic;
@Schema(description = "ESP32 订阅的主题")
private String subscribe_topic;
}
}
@@ -174,6 +174,24 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
}
response.setWebsocket(websocket);
// 添加MQTT UDP配置
// 从系统参数获取WebSocket URL,如果未配置不使用默认值
String mqttUdpConfig = sysParamsService.getValue(Constant.SERVER_MQTT_GATEWAY, false);
if(!StringUtils.isBlank(mqttUdpConfig)) {
DeviceReportRespDTO.MQTT mqtt= new DeviceReportRespDTO.MQTT();
mqtt.setEndpoint(mqttUdpConfig);
mqtt.setClient_id(clientId);
String userNameString = deviceById.getId().replace(":", "_");
mqtt.setUsername(userNameString);
mqtt.setPassword(deviceById.getBoard());
String topicString = deviceById.getBoard() + '/' + deviceById.getAgentId();
mqtt.setPublish_topic(topicString);
String subscribeString = "devices/p2p/"+userNameString;
mqtt.setSubscribe_topic(subscribeString);
response.setMqtt(mqtt);
}
if (deviceById != null) {
// 如果设备存在,则异步更新上次连接时间和版本信息
@@ -0,0 +1,3 @@
delete from `sys_params` where id = 108;
INSERT INTO `sys_params` (id, param_code, param_value, value_type, param_type, remark) VALUES (108, 'server.mqtt_udp', 'null', 'string', 1, 'mqtt udp 配置');
@@ -0,0 +1,5 @@
delete from `sys_params` where id = 108;
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 id = 109;
INSERT INTO `sys_params` (id, param_code, param_value, value_type, param_type, remark) VALUES (109, 'server.udp_gateway', 'null', 'string', 1, 'udp gateway 配置');
@@ -204,4 +204,18 @@ databaseChangeLog:
changes:
- sqlFile:
encoding: utf8
path: classpath:db/changelog/202506080955.sql
path: classpath:db/changelog/202506080955.sql
- changeSet:
id: 202506152342
author: marlonz
changes:
- sqlFile:
encoding: utf8
path: classpath:db/changelog/202506152342.sql
- changeSet:
id: 202506152350
author: marlonz
changes:
- sqlFile:
encoding: utf8
path: classpath:db/changelog/202506152350.sql