diff --git a/springboot-init-main/src/main/java/com/yupi/project/config/MqttConfig.java b/springboot-init-main/src/main/java/com/yupi/project/config/MqttConfig.java index 6b1358b..8f87ee8 100644 --- a/springboot-init-main/src/main/java/com/yupi/project/config/MqttConfig.java +++ b/springboot-init-main/src/main/java/com/yupi/project/config/MqttConfig.java @@ -12,8 +12,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.util.StringUtils; -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; import java.util.UUID; @Slf4j @@ -24,9 +22,6 @@ public class MqttConfig { private final MqttProperties mqttProperties; private final MqttCallbackHandler mqttCallbackHandler; - // No @Autowired here. This field will be set by the mqttClientBean() method. - private MqttClient mqttClient; - @Bean public MqttConnectOptions mqttConnectOptions() { MqttConnectOptions options = new MqttConnectOptions(); @@ -46,45 +41,9 @@ public class MqttConfig { @Bean public MqttClient mqttClientBean(MqttConnectOptions mqttConnectOptions) throws MqttException { String clientId = mqttProperties.getClientIdPrefix() + UUID.randomUUID().toString().replace("-", ""); - // Create the client instance MqttClient client = new MqttClient(mqttProperties.getBrokerUrl(), clientId, new MemoryPersistence()); client.setCallback(mqttCallbackHandler); mqttCallbackHandler.setMqttClient(client); - - // Assign the created client to the instance field for @PostConstruct/@PreDestroy usage - this.mqttClient = client; - return client; // Return it to be managed by Spring as a bean - } - - @PostConstruct - public void connect() { - try { - if (this.mqttClient != null && !this.mqttClient.isConnected()) { - log.info("Attempting to connect to MQTT broker: {} with client ID: {}", mqttProperties.getBrokerUrl(), this.mqttClient.getClientId()); - // Pass the MqttConnectOptions bean directly to the connect method - this.mqttClient.connect(mqttConnectOptions()); - } else if (this.mqttClient == null) { - log.error("MqttClient instance is null (was not set by mqttClientBean), cannot connect."); - } - } catch (MqttException e) { - log.error("Error connecting to MQTT broker: ", e); - } - } - - @PreDestroy - public void disconnect() { - try { - if (this.mqttClient != null && this.mqttClient.isConnected()) { - log.info("Disconnecting from MQTT broker: {}", this.mqttClient.getServerURI()); - this.mqttClient.disconnect(); - log.info("Successfully disconnected from MQTT broker."); - } - if (this.mqttClient != null) { - // Ensure close is called even if disconnect fails or was not connected - this.mqttClient.close(); - } - } catch (MqttException e) { - log.error("Error disconnecting/closing MQTT client: ", e); - } + return client; } } \ No newline at end of file diff --git a/springboot-init-main/src/main/java/com/yupi/project/mqtt/MqttConnectionManager.java b/springboot-init-main/src/main/java/com/yupi/project/mqtt/MqttConnectionManager.java new file mode 100644 index 0000000..1e68659 --- /dev/null +++ b/springboot-init-main/src/main/java/com/yupi/project/mqtt/MqttConnectionManager.java @@ -0,0 +1,73 @@ +package com.yupi.project.mqtt; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +@RequiredArgsConstructor +public class MqttConnectionManager implements ApplicationListener, DisposableBean { + + private final MqttClient mqttClient; + private final MqttConnectOptions mqttConnectOptions; + // private final MqttProperties mqttProperties; // Injected if needed for logging brokerUrl, or get from mqttClient.getServerURI() + + @Override + public void onApplicationEvent(ContextRefreshedEvent event) { + // Ensure this logic runs only once, for the root application context + if (event.getApplicationContext().getParent() == null) { + connectToMqtt(); + } + } + + private void connectToMqtt() { + try { + if (mqttClient != null && !mqttClient.isConnected()) { + log.info("Attempting to connect to MQTT broker: {} with client ID: {}", mqttClient.getServerURI(), mqttClient.getClientId()); + mqttClient.connect(mqttConnectOptions); + // Subscription logic is handled by MqttCallbackHandler.connectComplete + // which is triggered by the Paho client library upon successful connection. + } else if (mqttClient == null) { + log.error("MqttClient bean is null, cannot connect."); + } + } catch (MqttException e) { + log.error("Error connecting to MQTT broker: ", e); + // Consider retry logic or specific actions based on requirements + } + } + + @Override + public void destroy() throws Exception { + disconnectFromMqtt(); + } + + private void disconnectFromMqtt() { + try { + if (mqttClient != null && mqttClient.isConnected()) { + log.info("Disconnecting from MQTT broker: {}", mqttClient.getServerURI()); + mqttClient.disconnect(); + log.info("Successfully disconnected from MQTT broker."); + } + } catch (MqttException e) { + log.error("Error disconnecting from MQTT broker: ", e); + } finally { + try { + if (mqttClient != null) { // No need to check isOpen(), close() handles its state. + log.info("Closing MQTT client for client ID: {}", mqttClient.getClientId()); + mqttClient.close(); + log.info("MQTT client closed successfully for client ID: {}", mqttClient.getClientId()); + } + } catch (MqttException e) { + // Log if close() itself throws an exception, though it's less common if called after disconnect or on an unopen client. + log.error("Error closing MQTT client: ", e); + } + } + } +} \ No newline at end of file diff --git a/springboot-init-main/target/classes/META-INF/spring-configuration-metadata.json b/springboot-init-main/target/classes/META-INF/spring-configuration-metadata.json deleted file mode 100644 index 9267002..0000000 --- a/springboot-init-main/target/classes/META-INF/spring-configuration-metadata.json +++ /dev/null @@ -1,57 +0,0 @@ -{ - "groups": [ - { - "name": "mqtt", - "type": "com.yupi.project.config.properties.MqttProperties", - "sourceType": "com.yupi.project.config.properties.MqttProperties" - } - ], - "properties": [ - { - "name": "mqtt.broker-url", - "type": "java.lang.String", - "sourceType": "com.yupi.project.config.properties.MqttProperties" - }, - { - "name": "mqtt.client-id-prefix", - "type": "java.lang.String", - "sourceType": "com.yupi.project.config.properties.MqttProperties" - }, - { - "name": "mqtt.command-topic-base", - "type": "java.lang.String", - "sourceType": "com.yupi.project.config.properties.MqttProperties" - }, - { - "name": "mqtt.connection-timeout", - "type": "java.lang.Integer", - "sourceType": "com.yupi.project.config.properties.MqttProperties" - }, - { - "name": "mqtt.default-qos", - "type": "java.lang.Integer", - "sourceType": "com.yupi.project.config.properties.MqttProperties" - }, - { - "name": "mqtt.keep-alive-interval", - "type": "java.lang.Integer", - "sourceType": "com.yupi.project.config.properties.MqttProperties" - }, - { - "name": "mqtt.password", - "type": "java.lang.String", - "sourceType": "com.yupi.project.config.properties.MqttProperties" - }, - { - "name": "mqtt.status-topic-base", - "type": "java.lang.String", - "sourceType": "com.yupi.project.config.properties.MqttProperties" - }, - { - "name": "mqtt.username", - "type": "java.lang.String", - "sourceType": "com.yupi.project.config.properties.MqttProperties" - } - ], - "hints": [] -} \ No newline at end of file diff --git a/springboot-init-main/target/classes/application-prod.yml b/springboot-init-main/target/classes/application-prod.yml deleted file mode 100644 index 876203b..0000000 --- a/springboot-init-main/target/classes/application-prod.yml +++ /dev/null @@ -1,6 +0,0 @@ -spring: - datasource: - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/my_db - username: root - password: 123456 \ No newline at end of file diff --git a/springboot-init-main/target/classes/application.yml b/springboot-init-main/target/classes/application.yml deleted file mode 100644 index 4651005..0000000 --- a/springboot-init-main/target/classes/application.yml +++ /dev/null @@ -1,62 +0,0 @@ -spring: - application: - name: mqtt-charging-system - # DataSource Config - datasource: - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://yuyun-us1.stormrain.cn:3306/mqtt_power?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai - username: root - password: mysql_a4MQ4P - mvc: - pathmatch: - matching-strategy: ANT_PATH_MATCHER - # session 失效时间(秒) - session: - timeout: 86400 -server: - port: 7529 - servlet: - context-path: /api - session: - timeout: 86400 # 设置session的过期时间,单位为秒,这里设置为1天 -mybatis-plus: - configuration: - map-underscore-to-camel-case: true - log-impl: org.apache.ibatis.logging.stdout.StdOutImpl - global-config: - db-config: - logic-delete-field: isDelete # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2) - logic-delete-value: 1 # 逻辑已删除值(默认为 1) - logic-not-delete-value: 0 # 逻辑未删除值(默认为 0) - -# Logging configuration -logging: - level: - # Set root logger level (e.g., INFO, WARN, ERROR, DEBUG) - root: INFO - # Set specific package levels - com.yupi.project: DEBUG # Example: Set your project's base package to DEBUG - org.springframework.web: INFO # Set Spring Web logging level - org.springframework.security: DEBUG # Enable Spring Security DEBUG logging - org.mybatis: INFO # Set MyBatis logging level - # ... other specific loggers - #file: - #name: logs/application.log # Log file name - #path: ./logs # Log file path - #pattern: - #console: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" - #file: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" - -# =================================================================== -# MQTT Configurations -# =================================================================== -mqtt: - broker-url: tcp://broker.emqx.io:1883 - username: # Public broker, no credentials specified for connection - password: # Public broker, no credentials specified for connection - client-id-prefix: backend-yupi-mqtt-power- # Unique client ID prefix for our project - default-qos: 1 # Default Quality of Service (0, 1, 2) - connection-timeout: 30 # Connection timeout in seconds - keep-alive-interval: 60 # Keep alive interval in seconds - command-topic-base: yupi_mqtt_power_project/robot/command # Prefixed base topic for sending commands - status-topic-base: yupi_mqtt_power_project/robot/status # Prefixed base topic for receiving status \ No newline at end of file diff --git a/springboot-init-main/target/classes/banner.txt b/springboot-init-main/target/classes/banner.txt deleted file mode 100644 index afa86fa..0000000 --- a/springboot-init-main/target/classes/banner.txt +++ /dev/null @@ -1 +0,0 @@ -我的项目 by 程序员鱼皮 https://github.com/liyupi \ No newline at end of file diff --git a/springboot-init-main/target/classes/com/yupi/project/MyApplication.class b/springboot-init-main/target/classes/com/yupi/project/MyApplication.class deleted file mode 100644 index 4f0f794..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/MyApplication.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/annotation/AuthCheck.class b/springboot-init-main/target/classes/com/yupi/project/annotation/AuthCheck.class deleted file mode 100644 index 1b4dce9..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/annotation/AuthCheck.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/aop/AuthInterceptor.class b/springboot-init-main/target/classes/com/yupi/project/aop/AuthInterceptor.class deleted file mode 100644 index 78da2fd..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/aop/AuthInterceptor.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/aop/LogInterceptor.class b/springboot-init-main/target/classes/com/yupi/project/aop/LogInterceptor.class deleted file mode 100644 index 7feb313..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/aop/LogInterceptor.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/common/BaseResponse.class b/springboot-init-main/target/classes/com/yupi/project/common/BaseResponse.class deleted file mode 100644 index eb7721f..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/common/BaseResponse.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/common/DeleteRequest.class b/springboot-init-main/target/classes/com/yupi/project/common/DeleteRequest.class deleted file mode 100644 index ac4ff82..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/common/DeleteRequest.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/common/ErrorCode.class b/springboot-init-main/target/classes/com/yupi/project/common/ErrorCode.class deleted file mode 100644 index f0b287e..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/common/ErrorCode.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/common/PageRequest.class b/springboot-init-main/target/classes/com/yupi/project/common/PageRequest.class deleted file mode 100644 index 6bdc7dc..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/common/PageRequest.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/common/ResultUtils.class b/springboot-init-main/target/classes/com/yupi/project/common/ResultUtils.class deleted file mode 100644 index 510f40f..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/common/ResultUtils.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/config/Knife4jConfig.class b/springboot-init-main/target/classes/com/yupi/project/config/Knife4jConfig.class deleted file mode 100644 index 3709f8f..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/config/Knife4jConfig.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/config/MqttConfig.class b/springboot-init-main/target/classes/com/yupi/project/config/MqttConfig.class deleted file mode 100644 index 13a21f7..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/config/MqttConfig.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/config/MyBatisPlusConfig.class b/springboot-init-main/target/classes/com/yupi/project/config/MyBatisPlusConfig.class deleted file mode 100644 index 00b9176..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/config/MyBatisPlusConfig.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/config/SecurityConfig.class b/springboot-init-main/target/classes/com/yupi/project/config/SecurityConfig.class deleted file mode 100644 index c7cb674..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/config/SecurityConfig.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/config/properties/MqttProperties.class b/springboot-init-main/target/classes/com/yupi/project/config/properties/MqttProperties.class deleted file mode 100644 index 2958c62..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/config/properties/MqttProperties.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/constant/CommonConstant.class b/springboot-init-main/target/classes/com/yupi/project/constant/CommonConstant.class deleted file mode 100644 index fd93bdc..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/constant/CommonConstant.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/constant/UserConstant.class b/springboot-init-main/target/classes/com/yupi/project/constant/UserConstant.class deleted file mode 100644 index dadddf9..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/constant/UserConstant.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/controller/UserController.class b/springboot-init-main/target/classes/com/yupi/project/controller/UserController.class deleted file mode 100644 index 249d1e0..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/controller/UserController.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/exception/BusinessException.class b/springboot-init-main/target/classes/com/yupi/project/exception/BusinessException.class deleted file mode 100644 index 7e8522c..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/exception/BusinessException.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/exception/GlobalExceptionHandler.class b/springboot-init-main/target/classes/com/yupi/project/exception/GlobalExceptionHandler.class deleted file mode 100644 index 96b6dfd..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/exception/GlobalExceptionHandler.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/mapper/UserMapper.class b/springboot-init-main/target/classes/com/yupi/project/mapper/UserMapper.class deleted file mode 100644 index 92e8844..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/mapper/UserMapper.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/model/dto/user/UserAdminAddRequest.class b/springboot-init-main/target/classes/com/yupi/project/model/dto/user/UserAdminAddRequest.class deleted file mode 100644 index e968b65..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/model/dto/user/UserAdminAddRequest.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/model/dto/user/UserAdminUpdateRequest.class b/springboot-init-main/target/classes/com/yupi/project/model/dto/user/UserAdminUpdateRequest.class deleted file mode 100644 index ff3b494..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/model/dto/user/UserAdminUpdateRequest.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/model/dto/user/UserLoginRequest.class b/springboot-init-main/target/classes/com/yupi/project/model/dto/user/UserLoginRequest.class deleted file mode 100644 index b89fbfa..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/model/dto/user/UserLoginRequest.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/model/dto/user/UserRegisterRequest.class b/springboot-init-main/target/classes/com/yupi/project/model/dto/user/UserRegisterRequest.class deleted file mode 100644 index d6c8343..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/model/dto/user/UserRegisterRequest.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/model/entity/User.class b/springboot-init-main/target/classes/com/yupi/project/model/entity/User.class deleted file mode 100644 index 7a852b6..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/model/entity/User.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/model/enums/UserRoleEnum.class b/springboot-init-main/target/classes/com/yupi/project/model/enums/UserRoleEnum.class deleted file mode 100644 index 812dca5..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/model/enums/UserRoleEnum.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/mqtt/MqttCallbackHandler.class b/springboot-init-main/target/classes/com/yupi/project/mqtt/MqttCallbackHandler.class deleted file mode 100644 index bdc163e..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/mqtt/MqttCallbackHandler.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/service/UserService.class b/springboot-init-main/target/classes/com/yupi/project/service/UserService.class deleted file mode 100644 index a0739d6..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/service/UserService.class and /dev/null differ diff --git a/springboot-init-main/target/classes/com/yupi/project/service/impl/UserServiceImpl.class b/springboot-init-main/target/classes/com/yupi/project/service/impl/UserServiceImpl.class deleted file mode 100644 index c31d2e6..0000000 Binary files a/springboot-init-main/target/classes/com/yupi/project/service/impl/UserServiceImpl.class and /dev/null differ diff --git a/springboot-init-main/target/classes/mapper/UserMapper.xml b/springboot-init-main/target/classes/mapper/UserMapper.xml deleted file mode 100644 index f9e87c8..0000000 --- a/springboot-init-main/target/classes/mapper/UserMapper.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - id,userName,userAccount, - userAvatar,gender,userRole, - userPassword,createTime,updateTime, - isDelete - -