|
|
@@ -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;
|
|
|
+ }
|
|
|
}
|