Browse Source

feat: 修复mqttTemplate bean丢失

yangliu 4 months ago
parent
commit
34b65ca4c1

+ 45 - 45
portal-service-application/src/main/java/com/hfln/portal/application/controller/TestController.java

@@ -1,45 +1,45 @@
-package com.hfln.portal.application.controller;
-
-
-import cn.hfln.framework.catchlog.CatchAndLog;
-import cn.hfln.framework.dto.ApiResult;
-import cn.hfln.framework.mqtt.annotation.MqttSubscribe;
-import cn.hfln.framework.mqtt.template.MqttTemplate;
-import com.hfln.portal.infrastructure.service.UserService;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-@RestController
-@CatchAndLog
-@Slf4j
-public class TestController {
-    
-
-    @Autowired
-    private UserService userService;
-
-    @Autowired
-    private MqttTemplate mqttPublisher;
-
-    @GetMapping("/demo")
-    public ApiResult<Boolean> get() {
-//        UserInfo userInfo = new UserInfo();
-//        userInfo.setPhone("13127529598");
-//        userInfo.setPassword(PasswordUtil.encrypt("123456"));
-//        userInfo.setGender(1);
-//        userInfo.setOpenid("wasdasd2212sada");
-//        userService.save(userInfo);
-        mqttPublisher.send("test","asdasdsada");
-//        if (-1<0){
-//            throw new BizException(ErrorEnum.ERROR_BIZ_FAIL.getErrorCode(),ErrorEnum.ERROR_BIZ_FAIL.getErrorMessage());
-//        }
-        return ApiResult.success(userService.checkOpenId("123456"));
-    }
-    
-    @MqttSubscribe(topic = "test")
-    public void test(String topic , String payload){
-        log.info("topic :{},message:{}",topic,payload);
-    }
-}
+//package com.hfln.portal.application.controller;
+//
+//
+//import cn.hfln.framework.catchlog.CatchAndLog;
+//import cn.hfln.framework.dto.ApiResult;
+//import cn.hfln.framework.mqtt.annotation.MqttSubscribe;
+//import cn.hfln.framework.mqtt.template.MqttTemplate;
+//import com.hfln.portal.infrastructure.service.UserService;
+//import lombok.extern.slf4j.Slf4j;
+//import org.springframework.beans.factory.annotation.Autowired;
+//import org.springframework.web.bind.annotation.GetMapping;
+//import org.springframework.web.bind.annotation.RestController;
+//
+//@RestController
+//@CatchAndLog
+//@Slf4j
+//public class TestController {
+//
+//
+//    @Autowired
+//    private UserService userService;
+//
+//    @Autowired
+//    private MqttTemplate mqttPublisher;
+//
+//    @GetMapping("/demo")
+//    public ApiResult<Boolean> get() {
+////        UserInfo userInfo = new UserInfo();
+////        userInfo.setPhone("13127529598");
+////        userInfo.setPassword(PasswordUtil.encrypt("123456"));
+////        userInfo.setGender(1);
+////        userInfo.setOpenid("wasdasd2212sada");
+////        userService.save(userInfo);
+//        mqttPublisher.send("test","asdasdsada");
+////        if (-1<0){
+////            throw new BizException(ErrorEnum.ERROR_BIZ_FAIL.getErrorCode(),ErrorEnum.ERROR_BIZ_FAIL.getErrorMessage());
+////        }
+//        return ApiResult.success(userService.checkOpenId("123456"));
+//    }
+//
+//    @MqttSubscribe(topic = "test")
+//    public void test(String topic , String payload){
+//        log.info("topic :{},message:{}",topic,payload);
+//    }
+//}

+ 26 - 6
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/DeviceGatewayImpl.java

@@ -79,7 +79,7 @@ public class DeviceGatewayImpl implements DeviceGateway {
     @Autowired
     private UserService userService;
 
-    @Autowired
+    @Autowired(required = false)
     private MqttTemplate mqttTemplate;
 
     @Autowired
@@ -488,7 +488,11 @@ public class DeviceGatewayImpl implements DeviceGateway {
         JSONObject msg = new JSONObject();
         msg.put("dev_id", clientId);
 
-        mqttTemplate.send(topic, msg.toJSONString());
+        if (mqttTemplate != null) {
+            mqttTemplate.send(topic, msg.toJSONString());
+        } else {
+            log.warn("MQTT template is not available, message not sent to topic: {}", topic);
+        }
     }
 
     @Override
@@ -496,7 +500,11 @@ public class DeviceGatewayImpl implements DeviceGateway {
 
         String topic = String.format(TopicConstants.TOPIC_DEV_REBOOT, clientId);
 
-        mqttTemplate.send(topic, "1");
+        if (mqttTemplate != null) {
+            mqttTemplate.send(topic, "1");
+        } else {
+            log.warn("MQTT template is not available, message not sent to topic: {}", topic);
+        }
     }
 
     @Override
@@ -504,7 +512,11 @@ public class DeviceGatewayImpl implements DeviceGateway {
 
         String topic = String.format(TopicConstants.TOPIC_DEV_UPDATEOTA, clientId);
 
-        mqttTemplate.send(topic, "1");
+        if (mqttTemplate != null) {
+            mqttTemplate.send(topic, "1");
+        } else {
+            log.warn("MQTT template is not available, message not sent to topic: {}", topic);
+        }
     }
 
     @Override
@@ -544,7 +556,11 @@ public class DeviceGatewayImpl implements DeviceGateway {
         JSONObject msg = new JSONObject();
         msg.put("dev_id", clientId);
 
-        mqttTemplate.send(TopicConstants.TOPIC_DEV_GET_INFO, msg.toJSONString());
+        if (mqttTemplate != null) {
+            mqttTemplate.send(TopicConstants.TOPIC_DEV_GET_INFO, msg.toJSONString());
+        } else {
+            log.warn("MQTT template is not available, message not sent to topic: {}", TopicConstants.TOPIC_DEV_GET_INFO);
+        }
     }
 
     @Override
@@ -553,7 +569,11 @@ public class DeviceGatewayImpl implements DeviceGateway {
         JSONObject msg = new JSONObject();
         msg.put("dev_id", clientId);
 
-        mqttTemplate.send(TopicConstants.TOPIC_DEV_GET_PARAM, msg.toJSONString());
+        if (mqttTemplate != null) {
+            mqttTemplate.send(TopicConstants.TOPIC_DEV_GET_PARAM, msg.toJSONString());
+        } else {
+            log.warn("MQTT template is not available, message not sent to topic: {}", TopicConstants.TOPIC_DEV_GET_PARAM);
+        }
     }
 
     @Override

+ 6 - 2
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/mqtt/MqttSubHandle.java

@@ -28,7 +28,7 @@ public class MqttSubHandle {
     private static final int QOS = 1;                  // MQTT服务质量等级
     private static final boolean RETAIN = true;        // MQTT保留消息标志
 
-    @Autowired
+    @Autowired(required = false)
     private MqttTemplate mqttTemplate; // MQTT消息发布器
 
     @Autowired
@@ -61,7 +61,11 @@ public class MqttSubHandle {
         String event = obj.getString("event");
         // 跌倒确认返回
         if (event.equals("跌倒确认")) {
-            mqttTemplate.send("/mps/fall_event/ack", clientId);
+            if (mqttTemplate != null) {
+                mqttTemplate.send("/mps/fall_event/ack", clientId);
+            } else {
+                log.warn("MQTT template is not available, message not sent to topic: /mps/fall_event/ack");
+            }
         }
 
         byte messageType = obj.getByteValue("message_type");

+ 29 - 29
portal-service-server/src/main/resources/bootstrap-local.yml

@@ -4,11 +4,11 @@ spring:
       discovery:
         server-addr: 8.130.28.21:8848
         group: DEFAULT_GROUP
-        namespace: local
+        namespace: dev
       config:
         server-addr: 8.130.28.21:8848
         group: DEFAULT_GROUP
-        namespace: local
+        namespace: dev
         file-extension: yaml
   redis:
     # 地址
@@ -17,7 +17,6 @@ spring:
     port: 6379
     # 数据库索引
     database: 5
-    # 密码
     # 连接超时时间
     timeout: 10s
     lettuce:
@@ -32,7 +31,7 @@ spring:
         max-wait: -1ms
   datasource:
     driver-class-name: com.mysql.cj.jdbc.Driver
-    url: jdbc:mysql://8.130.28.21:3306/lnxx_dev?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=Asia/Shanghai
+    url: jdbc:mysql://8.130.28.21:3306/jkld?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=Asia/Shanghai
     username: root
     password: Hfln@1024
     druid:
@@ -54,6 +53,14 @@ lnxx:
   wechat:
     appid: wx60b2cd643b46d5eb
     secret: 15ebd7bed7b73d806eba2944f4e07592
+    #微信公众号
+    office:
+      account:
+        appId: wx9b479fa3c2af4f21
+        secret: 426effb7b58f9ba7c5f6dd1062db0074
+        templateId: -wNA7XW0_4hscmIUK-hmolNpccd-zMlyGnKUvpGdfZQ
+    bind:
+      callbackUrl: https://jkld.radar-power.cn
   # 短信验证码相关
   sms:
     tencent:
@@ -81,32 +88,25 @@ sa-token:
   # 是否输出操作日志
   is-log: true
 
-
-#mqtt:
-#  uri: tcp://8.130.28.21:1883
-#  client-id: 0cb90a3a7442429a9772bbf5bad10b01
-#  default-topic: test
-#  connection-timeout: 10 # 连接超时
-#  automatic-reconnect: true
-#  clean-session: false   # 持久会话建议设置为 false
-#  keep-alive-interval: 60 # 保持心跳间隔(秒)
-emqx:
+# mqtt相关
+mqtt:
   enabled: true
-  server-uris:
-    - tcp://8.130.28.21:1883
-  client-id: ${random.uuid}
+  broker: tcp://8.130.28.21:1883
+  client-id: ${spring.application.name}-${random.uuid}
   username: admin
   password: public
-  connection-timeout: 30
-  keep-alive-interval: 60
-  automatic-reconnect: true
-  max-reconnect-delay: 10000
+  timeout: 30
+  keepalive: 60
   clean-session: true
-  max-inflight: 100
-  monitor:
-    enabled: true
-    interval: 30
-    max-reconnect-attempts: 10
-    reconnect-cooldown: 60
-  message:
-    process-timeout: 30
+  automatic-reconnect: true
+
+# oss 存储
+oss:
+  accessKey:
+    id: LTAI5t9ZjmmHVs7nseZx3xWT
+    secret: TbmTSDP9ztXrLa4gXBhAmjerQcKGj7
+  endpoint: oss-cn-shanghai.aliyuncs.com
+  bucket: lnxx.oss-cn-shanghai.aliyuncs.com
+  region: cn-shanghai
+  expire:
+    seconds: 300