|
|
|
@ -38,73 +38,36 @@ public class MqttUtil {
|
|
|
|
|
|
|
|
|
|
private MqttDeliveryToken token;
|
|
|
|
|
|
|
|
|
|
public void connect(){
|
|
|
|
|
try {
|
|
|
|
|
client = new MqttClient(broker, publishClientid, new MemoryPersistence());
|
|
|
|
|
} catch (MqttException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
public void connect() throws Exception {
|
|
|
|
|
client = new MqttClient(broker, publishClientid, new MemoryPersistence());
|
|
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
|
options.setUserName(username);
|
|
|
|
|
options.setPassword(password.toCharArray());
|
|
|
|
|
options.setConnectionTimeout(60);
|
|
|
|
|
options.setKeepAliveInterval(30);
|
|
|
|
|
try {
|
|
|
|
|
client.connect(options);
|
|
|
|
|
} catch (MqttException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
client.connect(options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void disconnect(){
|
|
|
|
|
public void disconnect() {
|
|
|
|
|
try {
|
|
|
|
|
client.disconnect();
|
|
|
|
|
} catch (MqttException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
// close client
|
|
|
|
|
try {
|
|
|
|
|
client.close();
|
|
|
|
|
} catch (MqttException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean publish(String content){
|
|
|
|
|
public boolean publish(String content) throws Exception {
|
|
|
|
|
MqttMessage message = new MqttMessage(content.getBytes());
|
|
|
|
|
message.setQos(qos);
|
|
|
|
|
// publish message
|
|
|
|
|
try {
|
|
|
|
|
token = client.getTopic(topic).publish(message);
|
|
|
|
|
token.waitForCompletion();
|
|
|
|
|
} catch (MqttException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
token = client.getTopic(topic).publish(message);
|
|
|
|
|
token.waitForCompletion();
|
|
|
|
|
return token.isComplete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean publish2MQTT(String content) {
|
|
|
|
|
// MqttClient client;
|
|
|
|
|
// MqttDeliveryToken token;
|
|
|
|
|
// try {
|
|
|
|
|
// client = new MqttClient(broker, publishClientid, new MemoryPersistence());
|
|
|
|
|
// } catch (MqttException e) {
|
|
|
|
|
// throw new RuntimeException(e);
|
|
|
|
|
// }
|
|
|
|
|
// MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
|
// options.setUserName(username);
|
|
|
|
|
// options.setPassword(password.toCharArray());
|
|
|
|
|
// options.setConnectionTimeout(60);
|
|
|
|
|
// options.setKeepAliveInterval(30);
|
|
|
|
|
// // connect
|
|
|
|
|
// try {
|
|
|
|
|
// client.connect(options);
|
|
|
|
|
// } catch (MqttException e) {
|
|
|
|
|
// throw new RuntimeException(e);
|
|
|
|
|
// }
|
|
|
|
|
// create message and setup QoS
|
|
|
|
|
|
|
|
|
|
MqttMessage message = new MqttMessage(content.getBytes());
|
|
|
|
|
message.setQos(qos);
|
|
|
|
|
// publish message
|
|
|
|
@ -114,30 +77,12 @@ public class MqttUtil {
|
|
|
|
|
} catch (MqttException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
// disconnect
|
|
|
|
|
// try {
|
|
|
|
|
// client.disconnect();
|
|
|
|
|
// } catch (MqttException e) {
|
|
|
|
|
// throw new RuntimeException(e);
|
|
|
|
|
// }
|
|
|
|
|
// // close client
|
|
|
|
|
// try {
|
|
|
|
|
// client.close();
|
|
|
|
|
// } catch (MqttException e) {
|
|
|
|
|
// throw new RuntimeException(e);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
return token.isComplete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void subScribeMQTT() {
|
|
|
|
|
|
|
|
|
|
// String broker = "tcp://139.196.211.222:10883";
|
|
|
|
|
// String topic = "mqtt/test";
|
|
|
|
|
// String username = "test";
|
|
|
|
|
// String password = "AliOS%1688";
|
|
|
|
|
// String clientid = "subscribe_client";
|
|
|
|
|
// int qos = 0;
|
|
|
|
|
try {
|
|
|
|
|
MqttClient client = new MqttClient(broker, subscribeClientid, new MemoryPersistence());
|
|
|
|
|
// connect options
|
|
|
|
|