|
|
@ -12,14 +12,16 @@ import org.eclipse.paho.mqttv5.common.MqttException; |
|
|
import org.eclipse.paho.mqttv5.common.MqttMessage; |
|
|
import org.eclipse.paho.mqttv5.common.MqttMessage; |
|
|
import org.eclipse.paho.mqttv5.common.packet.MqttProperties; |
|
|
import org.eclipse.paho.mqttv5.common.packet.MqttProperties; |
|
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
|
|
|
|
@Slf4j |
|
|
@Slf4j |
|
|
public class PtrMqttClient implements MqttCallback { |
|
|
public class PtrMqttClient implements MqttCallback { |
|
|
public final AmrMessageHandler amrMessageHandler; |
|
|
public final AmrMessageHandler amrMessageHandler; |
|
|
public final EnvPayload.MqttConfig mqttConfig; |
|
|
public final EnvPayload.MqttConfig mqttConfig; |
|
|
public final MqttClient client; |
|
|
private final MqttClient client; |
|
|
|
|
|
|
|
|
@SneakyThrows |
|
|
@SneakyThrows |
|
|
public PtrMqttClient(AmrMessageHandler handler, EnvPayload.MqttConfig mqttConfig) { |
|
|
public PtrMqttClient(AmrMessageHandler handler, EnvPayload.MqttConfig mqttConfig, String clientId) { |
|
|
this.amrMessageHandler = handler; |
|
|
this.amrMessageHandler = handler; |
|
|
this.mqttConfig = mqttConfig; |
|
|
this.mqttConfig = mqttConfig; |
|
|
|
|
|
|
|
|
@ -27,17 +29,56 @@ public class PtrMqttClient implements MqttCallback { |
|
|
String username = mqttConfig.getUsername(); |
|
|
String username = mqttConfig.getUsername(); |
|
|
String password = mqttConfig.getPassword(); |
|
|
String password = mqttConfig.getPassword(); |
|
|
|
|
|
|
|
|
client = new MqttClient(brokerUrl, mqttConfig.getClientId()); |
|
|
client = new MqttClient(brokerUrl, clientId); |
|
|
|
|
|
MqttConnectionOptions options = new MqttConnectionOptions(); |
|
|
|
|
|
options.setServerURIs(new String[]{brokerUrl}); |
|
|
|
|
|
options.setAutomaticReconnect(true); |
|
|
|
|
|
options.setUserName(username); |
|
|
|
|
|
options.setPassword(password.getBytes()); |
|
|
|
|
|
options.setConnectionTimeout(10); |
|
|
|
|
|
options.setKeepAliveInterval(20); |
|
|
|
|
|
|
|
|
client.setCallback(this); |
|
|
client.setCallback(this); |
|
|
|
|
|
|
|
|
|
|
|
client.connect(options); |
|
|
|
|
|
client.subscribe("/agv_robot/status", 0); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SneakyThrows |
|
|
|
|
|
public void publish(String topic, String payload) { |
|
|
|
|
|
this.client.publish(topic, payload.getBytes(StandardCharsets.UTF_8), 0, false); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SneakyThrows |
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
|
|
String brokerUrl = "tcp://10.10.203.239:1885"; |
|
|
|
|
|
String clientId = "yvan-rcs-dev"; |
|
|
|
|
|
String topic = "test/topic"; |
|
|
|
|
|
String content = "Hello from Java"; |
|
|
|
|
|
String username = "admin"; |
|
|
|
|
|
String password = "admin"; |
|
|
|
|
|
int qos = 0; |
|
|
|
|
|
|
|
|
|
|
|
MqttClient client = new MqttClient(brokerUrl, clientId); |
|
|
MqttConnectionOptions options = new MqttConnectionOptions(); |
|
|
MqttConnectionOptions options = new MqttConnectionOptions(); |
|
|
options.setServerURIs(new String[]{brokerUrl}); |
|
|
options.setServerURIs(new String[]{brokerUrl}); |
|
|
options.setAutomaticReconnect(true); // 启用自动重连
|
|
|
options.setAutomaticReconnect(true); |
|
|
options.setUserName(username); |
|
|
options.setUserName(username); |
|
|
options.setPassword(password.getBytes()); |
|
|
options.setPassword(password.getBytes()); |
|
|
|
|
|
options.setConnectionTimeout(10); |
|
|
|
|
|
options.setKeepAliveInterval(20); |
|
|
|
|
|
|
|
|
|
|
|
System.out.println("Connecting to broker..."); |
|
|
client.connect(options); |
|
|
client.connect(options); |
|
|
client.subscribe("/agv_robot/status", 0); |
|
|
System.out.println("Connected"); |
|
|
|
|
|
|
|
|
|
|
|
System.out.println("Publishing message..."); |
|
|
|
|
|
long start = System.currentTimeMillis(); |
|
|
|
|
|
client.publish(topic, content.getBytes(), qos, false); |
|
|
|
|
|
System.out.println("Published in " + (System.currentTimeMillis() - start) + " ms"); |
|
|
|
|
|
|
|
|
|
|
|
client.disconnect(); |
|
|
|
|
|
System.out.println("Disconnected"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
@ -81,10 +122,13 @@ public class PtrMqttClient implements MqttCallback { |
|
|
|
|
|
|
|
|
@SneakyThrows |
|
|
@SneakyThrows |
|
|
public void stop() { |
|
|
public void stop() { |
|
|
if (client.isConnected()) { |
|
|
if (client != null && client.isConnected()) { |
|
|
client.disconnect(); |
|
|
client.disconnect(); |
|
|
} |
|
|
|
|
|
client.close(); |
|
|
client.close(); |
|
|
log.info("MQTT client stopped"); |
|
|
log.info("MQTT client disconnected and closed."); |
|
|
|
|
|
} else { |
|
|
|
|
|
log.warn("MQTT client is not connected, no action taken."); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|