116 lines
3.9 KiB
Java
116 lines
3.9 KiB
Java
package com.whdc.mqtt;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
|
|
|
public class Pub {
|
|
private final String clientId;
|
|
private final String topic;
|
|
public static String BROKER;
|
|
private final int qos;
|
|
private MqttClient client;
|
|
|
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
public Pub(String clientId, String topic) {
|
|
this.clientId = clientId;
|
|
this.topic = topic;
|
|
this.qos = 0;
|
|
}
|
|
|
|
public Pub(String clientId, String topic, int qos) {
|
|
this.clientId = clientId;
|
|
this.topic = topic;
|
|
this.qos = qos;
|
|
}
|
|
|
|
public void connect() throws MqttException {
|
|
this.client = new MqttClient("tcp://120.24.5.249:3189", "mqttx_2b072a3c", new MemoryPersistence());
|
|
MqttConnectOptions connOpts = new MqttConnectOptions();
|
|
connOpts.setUserName(Config.USERNAME);
|
|
connOpts.setPassword(Config.PASSWORD);
|
|
connOpts.setCleanSession(true);
|
|
this.client.connect(connOpts);
|
|
}
|
|
|
|
public void close() {
|
|
try {
|
|
this.client.disconnectForcibly(200);
|
|
this.client.close(true);
|
|
} catch (MqttException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void pub(String msg) throws MqttException {
|
|
MqttMessage message = new MqttMessage(msg.getBytes());
|
|
message.setQos(qos);
|
|
this.client.publish(topic, message);
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
try {
|
|
// 创建发布者
|
|
Pub publisher = new Pub("mqttx_2b072a3c", Config.TOPIC_WQ_R, 1);
|
|
|
|
// 连接到MQTT Broker
|
|
publisher.connect();
|
|
System.out.println("MQTT发布者连接成功!");
|
|
|
|
// 创建测试数据
|
|
String[] testData = {
|
|
createTestData("2025-11-20 10:00:00", "0000001", "61.3", "0.83", "1.4", "7.2"),
|
|
createTestData("2025-11-20 11:00:00", "0000002", "54.2", "0.99", "1.3", "7.5"),
|
|
// createTestData("2025-11-19 16:00:00", "0000003", "62.3", "0.78", "1.3", "7.5"),
|
|
// createTestData("2025-11-19 17:00:00", "0000001", "59.9", "0.83", "1.3", "7.7"),
|
|
// createTestData("2025-11-19 18:00:00", "0000002", "62.5", "0.85", "1.6", "7.8")
|
|
};
|
|
|
|
// 发送测试数据
|
|
for (int i = 0; i < testData.length; i++) {
|
|
String data = testData[i];
|
|
publisher.pub(data);
|
|
System.out.println("发送第 " + (i + 1) + " 条数据: " + data);
|
|
|
|
// 间隔2秒发送一条
|
|
Thread.sleep(2000);
|
|
}
|
|
|
|
System.out.println("所有测试数据发送完成!");
|
|
|
|
// 保持连接,等待用户输入后关闭
|
|
System.out.println("按回车键退出...");
|
|
System.in.read();
|
|
|
|
publisher.close();
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private static String createTestData(String dataTime, String mn, String cod, String ad, String p, String ph) {
|
|
try {
|
|
ObjectNode json = objectMapper.createObjectNode();
|
|
json.put("DataTime", dataTime);
|
|
json.put("Mn", mn);
|
|
json.put("CN", "2061");
|
|
json.put("w01018", cod); // COD
|
|
json.put("w21003", ad); // 氨氮
|
|
json.put("w21011", p); // 总磷
|
|
json.put("w01001", ph); // PH
|
|
|
|
return json.toString();
|
|
} catch (Exception e) {
|
|
throw new RuntimeException("创建测试数据失败", e);
|
|
}
|
|
}
|
|
}
|
|
|
|
|