Commit 6ab1d22c7890fd6c08d9fd3c492f1030dbe36a9a
1 parent
3cf1e65a
```
feat(mqtt): 添加MQTT API接口并优化消息接收器 添加了MqttApi控制器,提供订阅、取消订阅和发送MQTT消息的REST接口, 同时修复了MqttMessageReceiver中的代码格式问题,移除了多余的空行。 ```
Showing
2 changed files
with
62 additions
and
1 deletions
mqtt-integration/src/main/java/com/diligrp/mqtt/integration/handler/MqttMessageReceiver.java
mqtt-web/src/main/java/com/diligrp/mqtt/web/api/MqttApi.java
0 → 100644
| 1 | +package com.diligrp.mqtt.web.api; | |
| 2 | + | |
| 3 | +import com.diligrp.mqtt.core.model.SendModel; | |
| 4 | +import com.diligrp.mqtt.core.model.SubscribeModel; | |
| 5 | +import com.diligrp.mqtt.core.service.MqttMessageService; | |
| 6 | +import com.diligrp.mqtt.core.service.MqttTopicService; | |
| 7 | +import com.diligrp.mqtt.web.message.Message; | |
| 8 | +import jakarta.annotation.Resource; | |
| 9 | +import org.springframework.web.bind.annotation.RequestBody; | |
| 10 | +import org.springframework.web.bind.annotation.RequestMapping; | |
| 11 | +import org.springframework.web.bind.annotation.RestController; | |
| 12 | +/** | |
| 13 | + * 测试接口 | |
| 14 | + * { | |
| 15 | + * "topic": "/028037/CPS70/910151240930000205/notify", | |
| 16 | + * "qos": 1, | |
| 17 | + * "retained": false, | |
| 18 | + * "payload": { | |
| 19 | + * "rtc": "16697082132620", | |
| 20 | + * "index": "16697082132643219720", | |
| 21 | + * "tts": "你好你好你好", | |
| 22 | + * "print_esc": "G2EAGzMBHFMBAR0hABthAMnMu6ex4LrFo7odIQAbYQA4OTgzMTAwNDQ2ODQwMTAbSgEbYQAbMwEcUwEBHSEAG2EA1tW2y7HgusWjuh0hABthADM0NTY3ODk0G0oBG2EAGzMBHFMBAR0hABthAL3wtu5STUKjuh0hABthADAuMDHUqhtKARthABszARxTAQEdIQAbYQDTxbvdvfC27qO6HSEAG2EAMC4wMNSqG0oBG2EAGzMBHFMBAR0hABthAMq1uLa98Lbuo7odIQAbYQAwLjAx1KobSgEbYQAbMwEcUwEBHSEAG2EAvbvS18qxvOSjuh0hABthADIwMjAtMDktMjcgMTE6MTY6NTYbSgEbYQAbMwEcUwEBHSEAG2EA1qe4tsf+tcCjuh0hABthAM6i0MUbSgEbYQAbMwEcUwEBHSEAG2EAvbvS18Dg0M2juh0hABthALPJuaYbSgEbYQAbMwEcUwEBHSEAG2EAycy7p7aptaW6xaO6HSEAG2EAVUlTV0VCMjAyMDA5MjcxMTE2NDYyNjc3MTg5NDMwG0oBG2EAGzMBHFMBAR0hABthAL270teyzr+8usWjuh0hABthADE3MTg3NzE5MjEzThtKARtKARtKUQ==", | |
| 23 | + * "print_num": 1 | |
| 24 | + * }, | |
| 25 | + * "charsetType": "GBK", | |
| 26 | + * "sendEventType": "printer" | |
| 27 | + * } | |
| 28 | + */ | |
| 29 | + | |
| 30 | +/** | |
| 31 | + * @Author: zhangmeiyang | |
| 32 | + * @CreateTime: 2026-01-14 16:43 | |
| 33 | + * @Version: todo | |
| 34 | + */ | |
| 35 | +@RestController | |
| 36 | +@RequestMapping("/api/mqtt") | |
| 37 | +public class MqttApi { | |
| 38 | + | |
| 39 | + @Resource | |
| 40 | + private MqttTopicService mqttTopicService; | |
| 41 | + | |
| 42 | + @Resource | |
| 43 | + private MqttMessageService mqttMessageService; | |
| 44 | + | |
| 45 | + @RequestMapping("/subscribe") | |
| 46 | + public Message<?> subscribe(@RequestBody SubscribeModel subscribeModel) { | |
| 47 | + mqttTopicService.subscribeTopic(subscribeModel); | |
| 48 | + return Message.success(); | |
| 49 | + } | |
| 50 | + | |
| 51 | + @RequestMapping("/unsubscribe") | |
| 52 | + public Message<?> unsubscribe(@RequestBody SubscribeModel subscribeModel) { | |
| 53 | + mqttTopicService.unsubscribeTopic(subscribeModel.getTopic()); | |
| 54 | + return Message.success(); | |
| 55 | + } | |
| 56 | + | |
| 57 | + @RequestMapping("/send") | |
| 58 | + public Message<?> send(@RequestBody SendModel sendModel) { | |
| 59 | + mqttMessageService.sendMessage(sendModel); | |
| 60 | + return Message.success(); | |
| 61 | + } | |
| 62 | +} | ... | ... |