Commit 084b73d277f5407186deaabb733c2eb34fd7d5e0

Authored by zhangmeiyang
1 parent a399fc10

```

feat(mqtt-core): 增加厂商枚举主题配置和自动订阅功能

- 为FirmEnum添加topics字段,支持关联TopicType集合
- 添加TopicType枚举定义在线服务和回执服务主题
- 实现TopicInit初始化类,系统启动时自动订阅厂商对应的主题
- 在PrintDeviceService接口中增加SN查询和厂商存在性检查方法
- 重构ReceiveEventType的matchEventType方法,使用Optional优化代码
- 新增isFirmExist方法用于检查厂商是否存在
```
mqtt-core/src/main/java/com/diligrp/mqtt/core/init/TopicInit.java 0 → 100644
  1 +package com.diligrp.mqtt.core.init;
  2 +
  3 +import com.diligrp.mqtt.core.service.MqttTopicService;
  4 +import com.diligrp.mqtt.core.service.PrintDeviceService;
  5 +import com.diligrp.mqtt.core.type.FirmEnum;
  6 +import jakarta.annotation.Resource;
  7 +import org.springframework.boot.ApplicationArguments;
  8 +import org.springframework.boot.ApplicationRunner;
  9 +import org.springframework.stereotype.Component;
  10 +
  11 +import java.util.Arrays;
  12 +
  13 +/**
  14 + * @Author: zhangmeiyang
  15 + * @CreateTime: 2026-01-15 15:16
  16 + * @Version: todo
  17 + */
  18 +@Component
  19 +public class TopicInit implements ApplicationRunner {
  20 +
  21 + @Resource
  22 + private PrintDeviceService printDeviceService;
  23 +
  24 + @Resource
  25 + private MqttTopicService mqttTopicService;
  26 +
  27 +
  28 + @Override
  29 + public void run(ApplicationArguments args) throws Exception {
  30 + Arrays.stream(FirmEnum.values())
  31 + .filter(f -> printDeviceService.isFirmExist(f.code))
  32 + .forEach(t -> t.topics.forEach(topic -> mqttTopicService.subscribeTopic(topic.value, topic.qos)));
  33 + }
  34 +}
mqtt-core/src/main/java/com/diligrp/mqtt/core/service/PrintDeviceService.java
@@ -10,8 +10,20 @@ import org.springframework.stereotype.Service; @@ -10,8 +10,20 @@ import org.springframework.stereotype.Service;
10 public interface PrintDeviceService { 10 public interface PrintDeviceService {
11 11
12 12
  13 + /**
  14 + * SN 查询
  15 + *
  16 + * @param sn
  17 + * @return {@link PrintDeviceDto }
  18 + */
13 PrintDeviceDto queryBySn(String sn); 19 PrintDeviceDto queryBySn(String sn);
14 20
  21 + /**
  22 + * 是确定存在
  23 + *
  24 + * @param firm 公司
  25 + * @return {@link Boolean }
  26 + */
15 Boolean isFirmExist(String firm); 27 Boolean isFirmExist(String firm);
16 28
17 29
mqtt-core/src/main/java/com/diligrp/mqtt/core/type/FirmEnum.java
1 package com.diligrp.mqtt.core.type; 1 package com.diligrp.mqtt.core.type;
2 2
3 -import lombok.Getter; 3 +import java.util.Set;
4 4
5 /** 5 /**
6 * @author lvqi 6 * @author lvqi
7 */ 7 */
8 -@Getter  
9 public enum FirmEnum { 8 public enum FirmEnum {
10 - CLOUD("cloud", "云打印"); 9 + CLOUD("cloud", "云打印", Set.of(TopicType.PRINTER_ONLINE_SERVICE, TopicType.PRINTER_RECEIPT_SERVICE));
11 10
12 - private final String code;  
13 - private final String name; 11 + public final String code;
  12 + public final String name;
  13 + public final Set<TopicType> topics;
14 14
15 - FirmEnum(String code, String name) { 15 + FirmEnum(String code, String name, Set<TopicType> topics) {
16 this.code = code; 16 this.code = code;
17 this.name = name; 17 this.name = name;
  18 + this.topics = topics;
18 } 19 }
19 20
20 21
21 -  
22 } 22 }
mqtt-core/src/main/java/com/diligrp/mqtt/core/type/ReceiveEventType.java
1 package com.diligrp.mqtt.core.type; 1 package com.diligrp.mqtt.core.type;
2 2
3 -import java.util.Objects; 3 +import java.util.Optional;
4 4
5 public enum ReceiveEventType { 5 public enum ReceiveEventType {
6 PRINTER_RECEIPT_SERVICE("/printSucc/"), 6 PRINTER_RECEIPT_SERVICE("/printSucc/"),
@@ -19,9 +19,18 @@ public enum ReceiveEventType { @@ -19,9 +19,18 @@ public enum ReceiveEventType {
19 * @return 匹配的事件类型,未匹配成功则返回null 19 * @return 匹配的事件类型,未匹配成功则返回null
20 */ 20 */
21 public static ReceiveEventType matchEventType(String topic) { 21 public static ReceiveEventType matchEventType(String topic) {
22 - if (Objects.isNull(topic)) {  
23 - return UNKNOWN_SERVICE;  
24 - } 22 + return Optional.ofNullable(topic)
  23 + .map(ReceiveEventType::of)
  24 + .orElse(UNKNOWN_SERVICE);
  25 + }
  26 +
  27 + /**
  28 + * 关于
  29 + *
  30 + * @param topic 主题
  31 + * @return {@link ReceiveEventType }
  32 + */
  33 + public static ReceiveEventType of(String topic) {
25 for (ReceiveEventType eventType : ReceiveEventType.values()) { 34 for (ReceiveEventType eventType : ReceiveEventType.values()) {
26 if (topic.startsWith(eventType.value)) { 35 if (topic.startsWith(eventType.value)) {
27 return eventType; 36 return eventType;
mqtt-core/src/main/java/com/diligrp/mqtt/core/type/TopicType.java 0 → 100644
  1 +package com.diligrp.mqtt.core.type;
  2 +
  3 +public enum TopicType {
  4 +
  5 + PRINTER_ONLINE_SERVICE("/priGoOnline/#", 1),
  6 + PRINTER_RECEIPT_SERVICE("/printSucc/#", 1);
  7 +
  8 + public final String value;
  9 + public final int qos;
  10 +
  11 + TopicType(String value, int qos) {
  12 + this.value = value;
  13 + this.qos = qos;
  14 + }
  15 +}