kongdeqiang
2 天以前 199202813dd8ca536ed2334f5eeb6aba3ad25b21
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
package com.boying.controller;
 
import com.boying.controller.car.ServerMQTT;
import com.google.gson.Gson;
import io.lettuce.core.dynamic.annotation.Param;
import lombok.RequiredArgsConstructor;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author kdq
 * @version 1.0.0
 * @ClassName TestController.java
 * @Description TODO
 * @createTime 2025年03月15日 18:10:00
 */
@RestController
@RequestMapping("/mqtt")
@RequiredArgsConstructor
public class TestController {
    //MQTT安装的服务器地址和端口号
    public  final String HOST = "tcp://47.92.115.108:1883";
    //定义一个主题
    public final String TOPIC = "/MXXT";
    //定义MQTT的ID,可以在MQTT服务配置中指定
    private final String clientid = "jzadmin"+System.currentTimeMillis();
 
    private MqttClient client;
    private MqttTopic topic;
    private String userName = "jzadmin";
    private String passWord = "Sys123456";
    private MqttMessage message;
    @GetMapping("/test")
    public void test() throws MqttException, InterruptedException {
        ServerMQTT server = new ServerMQTT();
        String str1 = "{\n" +
                "  \"compId\":133,\n" +
                "  \"firWeight\": 18,\n" +
                "  \"secWeight\": 48,\n" +
                "  \"ponderationType\": \"外销\",\n" +
                "  \"printTime\": \"2022-07-21 18:29:32\",\n" +
                "  \"createTime\": \"2022-07-21 18:29:32\",\n" +
                "  \"outTime\": \"2022-07-21 18:29:32\",\n" +
                "  \"vehicleNo\": \"冀E12345\",\n" +
                "  \"executive\": 30 ,\n" +
                "  \"orderId\": 88\n" +
                "}";
 
        Map<String, Object> map = new HashMap<>();
        map.put("t",System.currentTimeMillis());
        map.put("f","control");
        map.put("d","JYGL-MTXX");
        map.put("s","0001");
        map.put("p",str1);
        Gson gson =new Gson();
        String str = gson.toJson(map);
        for (int i = 0; i < 1000; i++) {
            message = new MqttMessage();
            message.setQos(2);
            message.setRetained(true);
            message.setPayload(str.getBytes());
            server.publish(topic , message);
            System.out.println("message的保留状态为:"+message.isRetained() + "------ratained状态");
            Thread.sleep(1000);
        }
 
    }
 
}