张世豪
2 天以前 e4b4347318f1f37c64b8055f1f1898da59a167ba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package sendMQTT;
 
 
 
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import sendMQTT.HTTPUtils.PropertiesFileUploader;
import set.Setsys;
import user.Usrdell;
 
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;
 
 
public class Server {
   static String host="tcp://39.99.43.227:1883";
   static String clientId="1231@qq.com";
 
    /**
     * 获取当前系统时间
     * @return 格式化的时间字符串,格式为 "yyyy-MM-dd HH:mm:ss"
     */
    public static String getCurrentSystemTime() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return now.format(formatter);
    }
 
    /**
     * 获取当前系统时间(自定义格式)
     * @param pattern 时间格式模式,例如 "yyyy-MM-dd HH:mm:ss"、"yyyy/MM/dd HH:mm:ss" 等
     * @return 格式化的时间字符串
     */
    public static String getCurrentSystemTime(String pattern) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return now.format(formatter);
    }
 
 
 
    private static void testCode() {
        String mes_id="hxzkcontrol_20151104";//消息ID
        String userId=Usrdell.getUserEmail();//用户ID
        String deviceID=Setsys.getMowerIdValue();//设备ID
        String protiesFilePath="./dikuai.properties";//地块信息配置文件
        String json = PropertiesFileUploader.movePathSend(mes_id,userId, deviceID, protiesFilePath);//生成给设备下发的JSON
        sendMQTT(json, host, clientId,deviceID);
    }
 
    private static void sendMQTT(String messageJson, String host, String clientId,String deviceID)  {
        try {
            String topic="app/p/mower/"+deviceID+"/path";
            MqttConnectOptions options = new MqttConnectOptions();
            options.setCleanSession(true);
            MqttClient client = new MqttClient(host, clientId);
            client.connect(options);
            // 添加关闭钩子,程序退出时关闭连接
            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                try {
                    if (client.isConnected()) {
                        client.disconnect();
                        client.close();
                    }
                } catch (Exception e) {
                }
            }));
            MqttMessage message = new MqttMessage();
            message.setPayload(messageJson.getBytes("UTF-8"));
            client.publish(topic, message);
            System.out.println("发送数据到主题: " + topic);
        } catch (MqttException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
 
}