Commit 3f01ba7538dd7533a43f3e66ee6c66b5b48b9c50
1 parent
b47176c9
修改:优化 EmqxService 实现
Showing
3 changed files
with
52 additions
and
22 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/emq/client/EmqxClient.java
| @@ -43,7 +43,7 @@ public class EmqxClient { | @@ -43,7 +43,7 @@ public class EmqxClient { | ||
| 43 | 43 | ||
| 44 | private void createMqttClient() { | 44 | private void createMqttClient() { |
| 45 | try { | 45 | try { |
| 46 | - mqttClient = new MqttClient(mqttConfig.getHostUrl(), "yudao-" + mqttConfig.getClientId(), new MemoryPersistence()); | 46 | + mqttClient = new MqttClient(mqttConfig.getHostUrl(), "yudao" + mqttConfig.getClientId(), new MemoryPersistence()); |
| 47 | mqttClient.setCallback(emqxCallback); | 47 | mqttClient.setCallback(emqxCallback); |
| 48 | } catch (MqttException e) { | 48 | } catch (MqttException e) { |
| 49 | log.error("创建MQTT客户端失败", e); | 49 | log.error("创建MQTT客户端失败", e); |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/emq/service/EmqxService.java
| 1 | package cn.iocoder.yudao.module.iot.emq.service; | 1 | package cn.iocoder.yudao.module.iot.emq.service; |
| 2 | 2 | ||
| 3 | -import lombok.extern.slf4j.Slf4j; | ||
| 4 | import org.eclipse.paho.client.mqttv3.MqttClient; | 3 | import org.eclipse.paho.client.mqttv3.MqttClient; |
| 5 | import org.eclipse.paho.client.mqttv3.MqttMessage; | 4 | import org.eclipse.paho.client.mqttv3.MqttMessage; |
| 6 | -import org.springframework.stereotype.Service; | ||
| 7 | 5 | ||
| 8 | /** | 6 | /** |
| 9 | * 用于处理MQTT消息的具体业务逻辑,如订阅回调 | 7 | * 用于处理MQTT消息的具体业务逻辑,如订阅回调 |
| 10 | * | 8 | * |
| 11 | * @author ahh | 9 | * @author ahh |
| 12 | */ | 10 | */ |
| 13 | -@Slf4j | ||
| 14 | -@Service | ||
| 15 | -public class EmqxService { | 11 | +public interface EmqxService { |
| 16 | 12 | ||
| 17 | - public void subscribeCallback(String topic, MqttMessage mqttMessage) { | ||
| 18 | - log.info("收到消息,主题: {}, 内容: {}", topic, new String(mqttMessage.getPayload())); | ||
| 19 | - // 根据不同的主题,处理不同的业务逻辑 | ||
| 20 | - if (topic.contains("/property/post")) { | ||
| 21 | - // 设备上报数据 | ||
| 22 | - } | ||
| 23 | - } | 13 | + /** |
| 14 | + * 订阅回调 | ||
| 15 | + * | ||
| 16 | + * @param topic 主题 | ||
| 17 | + * @param mqttMessage 消息 | ||
| 18 | + */ | ||
| 19 | + void subscribeCallback(String topic, MqttMessage mqttMessage); | ||
| 24 | 20 | ||
| 25 | - public void subscribe(MqttClient client) { | ||
| 26 | - try { | ||
| 27 | - // 订阅默认主题,可以根据需要修改 | ||
| 28 | - client.subscribe("$share/yudao/+/+/#", 1); | ||
| 29 | - log.info("订阅默认主题成功"); | ||
| 30 | - } catch (Exception e) { | ||
| 31 | - log.error("订阅默认主题失败", e); | ||
| 32 | - } | ||
| 33 | - } | 21 | + /** |
| 22 | + * 订阅主题 | ||
| 23 | + * | ||
| 24 | + * @param client MQTT 客户端 | ||
| 25 | + */ | ||
| 26 | + void subscribe(MqttClient client); | ||
| 34 | } | 27 | } |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/emq/service/EmqxServiceImpl.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.emq.service; | ||
| 2 | + | ||
| 3 | +import lombok.extern.slf4j.Slf4j; | ||
| 4 | +import org.eclipse.paho.client.mqttv3.MqttClient; | ||
| 5 | +import org.eclipse.paho.client.mqttv3.MqttMessage; | ||
| 6 | +import org.springframework.stereotype.Service; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * 用于处理MQTT消息的具体业务逻辑,如订阅回调 | ||
| 10 | + * | ||
| 11 | + * @author ahh | ||
| 12 | + */ | ||
| 13 | +@Slf4j | ||
| 14 | +@Service | ||
| 15 | +public class EmqxServiceImpl implements EmqxService { | ||
| 16 | + | ||
| 17 | + // TODO 多线程处理消息 | ||
| 18 | + @Override | ||
| 19 | + public void subscribeCallback(String topic, MqttMessage mqttMessage) { | ||
| 20 | + log.info("收到消息,主题: {}, 内容: {}", topic, new String(mqttMessage.getPayload())); | ||
| 21 | + // 根据不同的主题,处理不同的业务逻辑 | ||
| 22 | + if (topic.contains("/property/post")) { | ||
| 23 | + // 设备上报数据 | ||
| 24 | + } | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + @Override | ||
| 28 | + public void subscribe(MqttClient client) { | ||
| 29 | + try { | ||
| 30 | + // 订阅默认主题,可以根据需要修改 | ||
| 31 | + client.subscribe("$share/yudao/+/+/#", 1); | ||
| 32 | + log.info("订阅默认主题成功"); | ||
| 33 | + } catch (Exception e) { | ||
| 34 | + log.error("订阅默认主题失败", e); | ||
| 35 | + } | ||
| 36 | + } | ||
| 37 | +} |