TopicUtil.java
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package vion.utils;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.math.NumberUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.extra.spring.SpringUtil;
import org.redisson.api.RedissonClient;
import vion.constant.AgentTypeEnum;
import vion.constant.MqttTopic;
import vion.constant.RedisKeyEnum;
import vion.model.monitor.Agent;
import vion.service.monitor.IAgentService;
/**
* @author vion
* @date 2024/11/4
*/
@Slf4j
public class TopicUtil {
public static String getEventTopic(String agentUid) {
return getTopic(agentUid, MqttTopic.STORE_EVENT_TOPIC, MqttTopic.MALL_EVENT_TOPIC);
}
public static String getServiceTopic(String agentUid) {
return getTopic(agentUid, MqttTopic.STORE_SERVICE_TOPIC, MqttTopic.MALL_SERVICE_TOPIC);
}
public static String getOTATopic(String agentUid) {
return getTopic(agentUid, MqttTopic.STORE_OTA_TOPIC, MqttTopic.MALL_OTA_TOPIC);
}
private static String getTopic(String agentUid, String storeTopic, String mallTopic) {
var agent = getAgent(agentUid);
if (agent == null) {
return null;
}
if (NumberUtil.equals(agent.getType(), AgentTypeEnum.STORE.getCode())) {
return StrUtil.format(storeTopic, agentUid);
} else if (ObjUtil.equals(agent.getType(), AgentTypeEnum.MALL.getCode())) {
return StrUtil.format(mallTopic, agentUid);
} else {
log.info("未知的agent类型");
return null;
}
}
private static Agent getAgent(String agentUid) {
var redissonClient = SpringUtil.getBean(RedissonClient.class);
var agent = (Agent) redissonClient.getBucket(RedisKeyEnum.AGENT.getVal() + agentUid).get();
if (ObjUtil.isNull(agent)) {
var agentService = SpringUtil.getBean(IAgentService.class);
var agentList = agentService.list();
agentList.forEach(a -> redissonClient.getBucket(RedisKeyEnum.AGENT.getVal() + a.getUid()).set(a));
agent = agentList.stream().filter(a -> StrUtil.equals(a.getUid(), agentUid)).findFirst().orElse(null);
}
if (ObjUtil.isNull(agent)) {
log.info("未找到agent信息");
return null;
}
return agent;
}
}