TRX 1 年之前
父節點
當前提交
9bdd5922a5

+ 23 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/model/mqtt/MqttConfigModel.java

@@ -0,0 +1,23 @@
+package com.zhongshu.card.client.model.mqtt;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.hibernate.internal.build.AllowPrintStacktrace;
+
+/**
+ * @author TRX
+ * @date 2024/7/30
+ */
+@Data
+@AllowPrintStacktrace
+@NoArgsConstructor
+public class MqttConfigModel {
+    @Schema(description = "地址")
+    private String url;
+    @Schema(description = "用户名")
+    private String userName;
+    @Schema(description = "密码")
+    private String passWord;
+}
+

+ 12 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/controller/mqtt/MqttController.java

@@ -1,5 +1,8 @@
 package com.zhongshu.card.server.core.controller.mqtt;
 
+import com.github.microservice.auth.security.annotations.ResourceAuth;
+import com.github.microservice.auth.security.type.AuthType;
+import com.zhongshu.card.client.model.mqtt.MqttConfigModel;
 import com.zhongshu.card.client.model.mqtt.SendMessageModel;
 import com.zhongshu.card.client.ret.ResultContent;
 import com.zhongshu.card.server.core.service.mqtt.MqttServiceImpl;
@@ -12,6 +15,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.List;
+
 /**
  *
  */
@@ -23,6 +28,13 @@ public class MqttController {
 
     @Autowired
     MqttServiceImpl mqttService;
+    
+    @Operation(summary = "得到mqtt连接配置")
+    @ResourceAuth(value = "user", type = AuthType.User)
+    @RequestMapping(value = "getMqttConfig", method = {RequestMethod.GET})
+    public ResultContent<List<MqttConfigModel>> getMqttConfig() {
+        return mqttService.getMqttConfig();
+    }
 
     @Operation(summary = "发送指令")
     @RequestMapping(value = "sendMessage", method = {RequestMethod.POST})

+ 17 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/dao/mqtt/MqttConfigDao.java

@@ -0,0 +1,17 @@
+package com.zhongshu.card.server.core.dao.mqtt;
+
+import com.github.microservice.components.data.mongo.mongo.dao.MongoDao;
+import com.zhongshu.card.server.core.domain.mqtt.ArtemisMessage;
+import com.zhongshu.card.server.core.domain.mqtt.MqttConfig;
+
+/**
+ * 消息Dao
+ *
+ * @author TRX
+ * @date 2024/3/21
+ */
+public interface MqttConfigDao extends MongoDao<MqttConfig> {
+
+    MqttConfig findTopById(String id);
+
+}

+ 3 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/domain/mqtt/ArtemisMessage.java

@@ -26,6 +26,9 @@ public class ArtemisMessage extends SuperMain {
     @Schema(description = "用户userId")
     private String userId;
 
+    @Schema(description = "用户名称")
+    private String userName;
+
     @Schema(description = "消息类型:接收 发送")
     private ArtemisType artemisType;
 

+ 27 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/domain/mqtt/MqttConfig.java

@@ -0,0 +1,27 @@
+package com.zhongshu.card.server.core.domain.mqtt;
+
+import com.zhongshu.card.server.core.domain.base.SuperMain;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.checkerframework.common.aliasing.qual.NonLeaked;
+import org.springframework.data.mongodb.core.mapping.Document;
+
+/**
+ * @author TRX
+ * @date 2024/7/30
+ */
+@Data
+@Document
+@AllArgsConstructor
+@NoArgsConstructor
+@NonLeaked
+public class MqttConfig extends SuperMain {
+    @Schema(description = "地址")
+    private String url;
+    @Schema(description = "用户名")
+    private String userName;
+    @Schema(description = "密码")
+    private String passWord;
+}

+ 52 - 1
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/mqtt/MqttServiceImpl.java

@@ -3,28 +3,39 @@ package com.zhongshu.card.server.core.service.mqtt;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import com.google.gson.JsonObject;
+import com.zhongshu.card.client.model.mqtt.MqttConfigModel;
 import com.zhongshu.card.client.model.mqtt.SendMessageModel;
 import com.zhongshu.card.client.ret.ResultContent;
 import com.zhongshu.card.client.utils.type.ArtemisType;
 import com.zhongshu.card.server.core.dao.mqtt.ArtemisMessageDao;
+import com.zhongshu.card.server.core.dao.mqtt.MqttConfigDao;
 import com.zhongshu.card.server.core.dao.org.UserCountDao;
 import com.zhongshu.card.server.core.domain.mqtt.ArtemisMessage;
+import com.zhongshu.card.server.core.domain.mqtt.MqttConfig;
+import com.zhongshu.card.server.core.domain.org.UserAccount;
 import com.zhongshu.card.server.core.httpRequest.ApiRequestService;
 import com.zhongshu.card.server.core.service.base.RedisService;
 import com.zhongshu.card.server.core.service.base.SuperService;
 import com.zhongshu.card.server.core.service.mqtt.mqttConfig.client.MQClient;
 import com.zhongshu.card.server.core.service.mqtt.mqttConfig.constant.MQConstant;
+import com.zhongshu.card.server.core.util.BeanUtils;
 import com.zhongshu.card.server.core.util.DateUtils;
 import jakarta.jms.Message;
 import jakarta.jms.TextMessage;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.activemq.artemis.jms.client.ActiveMQBytesMessage;
 import org.apache.activemq.artemis.jms.client.ActiveMQTopic;
+import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Sort;
 import org.springframework.jms.annotation.JmsListener;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.UUID;
+import java.util.stream.Collectors;
 
 /**
  * @author TRX
@@ -49,6 +60,12 @@ public class MqttServiceImpl extends SuperService {
     @Autowired
     ArtemisMessageDao artemisMessageDao;
 
+    @Autowired
+    UserCountDao userCountDao;
+
+    @Autowired
+    MqttConfigDao mqttConfigDao;
+
     /**
      * 发送指令
      *
@@ -58,11 +75,16 @@ public class MqttServiceImpl extends SuperService {
     public ResultContent sendMessage(SendMessageModel param) {
         String msg = "发送成功";
         try {
+            String userId = param.getUserId();
+            if (StringUtils.isEmpty(userId)) {
+                userId = getCurrentUserId();
+            }
             String id = UUID.randomUUID().toString();
             JsonObject jsonObject = new JsonObject();
             jsonObject.addProperty("id", id);
             jsonObject.addProperty("data", param.getMessage());
-            jsonObject.addProperty("time", DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternyyyySSS));
+            jsonObject.addProperty("timeStr", DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternyyyySSS));
+            jsonObject.addProperty("time", System.currentTimeMillis());
             jsonObject.addProperty("ttl", 10 * 1000);
             mqClient.sendObject(param.getTopic(), jsonObject.toString());
 
@@ -74,6 +96,12 @@ public class MqttServiceImpl extends SuperService {
             artemisMessage.setTimeStr(DateUtils.paresTime(System.currentTimeMillis(),
                     DateUtils.patternyyyySSS));
             artemisMessage.setUserId(param.getUserId());
+            if (StringUtils.isNotEmpty(userId)) {
+                UserAccount userAccount = userCountDao.findTopByUserId(userId);
+                if (ObjectUtils.isNotEmpty(userAccount)) {
+                    artemisMessage.setUserName(userAccount.getName());
+                }
+            }
             artemisMessageDao.save(artemisMessage);
             log.info("mqtt msg 发送成功");
         } catch (Exception e) {
@@ -126,4 +154,27 @@ public class MqttServiceImpl extends SuperService {
         }
     }
 
+    /**
+     * 得到平台的MQTT配置
+     *
+     * @return
+     */
+    public ResultContent<List<MqttConfigModel>> getMqttConfig() {
+        List<MqttConfig> list = mqttConfigDao.findAll(Sort.by(Sort.Order.asc("sort"),
+                Sort.Order.asc("createTime")));
+        List<MqttConfigModel> models = new ArrayList<>();
+        if (ObjectUtils.isNotEmpty(list)) {
+            models = list.stream().map(this::toModel).collect(Collectors.toList());
+        }
+        return ResultContent.buildSuccess(models);
+    }
+
+    public MqttConfigModel toModel(MqttConfig entity) {
+        MqttConfigModel model = null;
+        if (entity != null) {
+            model = new MqttConfigModel();
+            BeanUtils.copyProperties(entity, model);
+        }
+        return model;
+    }
 }