mqtt配置
This commit is contained in:
@ -0,0 +1,79 @@
|
||||
package com.xzzn.web.controller.ems;
|
||||
|
||||
import com.xzzn.framework.manager.MqttLifecycleManager;
|
||||
import com.xzzn.framework.web.service.MqttPublisher;
|
||||
import com.xzzn.framework.web.service.MqttSubscriber;
|
||||
import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@Service
|
||||
public class MqttMessageController implements MqttPublisher, MqttSubscriber {
|
||||
|
||||
private final MqttLifecycleManager mqttLifecycleManager;
|
||||
|
||||
@Autowired
|
||||
public MqttMessageController(MqttLifecycleManager mqttLifecycleManager) {
|
||||
this.mqttLifecycleManager = mqttLifecycleManager;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
// 订阅系统状态主题
|
||||
subscribe("system/status", 1, this::handleSystemStatus);
|
||||
|
||||
// 订阅设备数据主题
|
||||
subscribe("devices/data", 0, this::handleDeviceData);
|
||||
}
|
||||
|
||||
// 处理系统状态消息
|
||||
private void handleSystemStatus(String topic, MqttMessage message) {
|
||||
String payload = new String(message.getPayload());
|
||||
System.out.println("[SYSTEM] Status update: " + payload);
|
||||
// 业务处理逻辑
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 处理设备数据
|
||||
private void handleDeviceData(String topic, MqttMessage message) {
|
||||
String payload = new String(message.getPayload());
|
||||
System.out.println("[DEVICE] data: " + payload);
|
||||
// 业务处理逻辑
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publish(String topic, String message) throws MqttException {
|
||||
mqttLifecycleManager.publish(topic, message, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publish(String topic, String message, int qos) throws MqttException {
|
||||
mqttLifecycleManager.publish(topic, message, qos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void subscribe(String topic, int qos, IMqttMessageListener listener) {
|
||||
mqttLifecycleManager.subscribe(topic, qos, listener);
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 发送设备控制命令
|
||||
public void sendDeviceCommand(String deviceId, String command) {
|
||||
try {
|
||||
String topic = "devices/" + deviceId + "/commands";
|
||||
publish(topic, command, 1);
|
||||
} catch (MqttException e) {
|
||||
System.err.println("Failed to send command to device " + deviceId);
|
||||
}
|
||||
}
|
||||
}
|
@ -127,3 +127,12 @@ xss:
|
||||
excludes: /system/notice
|
||||
# 匹配链接
|
||||
urlPatterns: /system/*,/monitor/*,/tool/*
|
||||
|
||||
mqtt:
|
||||
broker.url: tcp://122.51.194.184:1883
|
||||
client.id: ems-cloud
|
||||
username: admin
|
||||
password: pass123
|
||||
connection-timeout: 15
|
||||
keep-alive-interval: 30
|
||||
automatic-reconnect: true
|
Reference in New Issue
Block a user