张世豪
15 小时以前 2eea735fd4ddf0ae047687780271ef3962d256cc
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package udpdell;
import Util.DeviceMessageParser;
import Util.LawnMowerCommandJsonGenerator;
import Util.MowerPathMessageGenerator;
import Util.Entity.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.Arrays;
import java.util.List;
 
public class Mqttserver {
//    public static void main(String[] args) {
//        GPSData();//解析设备上传GPS数据示例
//        responseData();//解析设备回复的响应数据示例
//        outputData();//设备控制指令生成示例
//        //String s = generateExampleMessage();//路径规划数据生成示例
//        //System.out.println(s);
//    }
 
    private static void responseData(){
        // 解析响应消息
        String responseJson = "{ \"msg_id\": \"hxzkresponse_20151105\", \"timestamp\": 1621234568300, \"device_id\": \"MOWER_001\", \"original_msg_id\": \"msg_123456793\", \"response\": { \"status\": \"success\", \"command\": \"start\", \"error_code\": 0, \"error_message\": \"\", \"additional_info\": { \"current_status\": \"running\", \"battery_level\": 84, \"current_position\": { \"lat\": \"3949.91202005,N\", \"lon\": \"11616.85440851,E\" } } } }";
 
        ResponseData responseData = null;
        try {
            responseData = DeviceMessageParser.parseResponseData(responseJson);
            System.out.println("原始消息ID: " + responseData.getOriginalMsgId());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            System.err.println("解析错误: " + e.getMessage());
        }
 
    }
 
 
 
 
    private static void GPSData() {
        // GPS数据示例(新协议格式,包含GPS、IMU和状态数据)
        String gpsJsonStr = "{\n" +
                "  \"msg_id\": \"hxzkgps_20151101\",\n" +
                "  \"timestamp\": 1621234567890,\n" +
                "  \"device_id\": \"MOWER_001\",\n" +
                "  \"data_type\": \"gps\",\n" +
                "  \"gps_raw\": \"$GNGGA,024830.90,3949.91202005,N,11616.85440851,E,4,26,0.7,49.6405,M,-8.7435,M,0.9,409*4A,2976,28,0,0,2,0\",\n" +
                "  \"imu_data\": {\n" +
                "    \"roll\": 1.2,\n" +
                "    \"pitch\": 0.5,\n" +
                "    \"yaw\": 185.5\n" +
                "  },\n" +
                "  \"status\": {\n" +
                "    \"battery_level\": 85,\n" +
                "    \"battery_voltage\": 24.5,\n" +
                "    \"operation_mode\": \"auto\",\n" +
                "    \"motor_status\": \"running\",\n" +
                "    \"blade_status\": \"rotating\",\n" +
                "    \"blade_height\": 10,\n" +
                "    \"self_check_status\": 1,\n" +
                "    \"error_code\": 0,\n" +
                "    \"error_message\": \"\"\n" +
                "  }\n" +
                "}";
        try {
            System.out.println("=== GPS数据解析示例(新协议格式) ===");
            GPSData gpsData2 = DeviceMessageParser.parseGPSData(gpsJsonStr);
            String string2 = gpsData2.toString();
            System.out.println(string2);
 
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            System.err.println("解析错误: " + e.getMessage());
        }
    }
 
 
    public static void outputData() {
        // 示例:使用便捷方法生成转向命令
        String json2 = LawnMowerCommandJsonGenerator.generateTurnCommandJson(
                "hxzkcontrol_20151105",
                null,
                "USER_001",
                "MOWER_001",
                180.0, // 转向180度
                0.5,   // 速度0.5米/秒
                10     // 持续10秒
        );
        System.out.println(json2);
    }
 
 
 
    public static String generateExampleMessage(int value) {
        // 创建边界点(使用XY坐标)
        HxzkPathMessage message = getmessage(value);
 
        return MowerPathMessageGenerator.toJson(message);
    }
 
    private static HxzkPathMessage getmessage(int value) {
        List<CoordinatePoint> boundaryPoints = Arrays.asList(
                 MowerPathMessageGenerator.createCoordinatePoint("100.5", "200.3"),
                 MowerPathMessageGenerator.createCoordinatePoint("150.8", "200.5"),
                 MowerPathMessageGenerator.createCoordinatePoint("150.9", "250.7")
        );
 
        // 创建导航点(使用XY坐标)
        List<CoordinatePoint> navigationPoints = Arrays.asList(
                 MowerPathMessageGenerator.createCoordinatePoint("100.5", "200.3"),
                 MowerPathMessageGenerator.createCoordinatePoint("150.8", "200.5"),
                 MowerPathMessageGenerator.createCoordinatePoint("150.9", "250.7")
        );
 
 
        // 创建基准站数据
        BasestationData basestationData =  MowerPathMessageGenerator.createBasestationData(
                "3949.84110064", "N",
                "11616.74587312", "E",
                45.2
        );
 
        // 创建路径数据
        PathData pathData =  MowerPathMessageGenerator.createPathData(
                "path_20230724_"+value,
                "WGS84_DM",
                boundaryPoints,
                navigationPoints,
                "parallel",
                boundaryPoints.size(),  // 边界点数量
                navigationPoints.size() // 导航点数量
        );
 
        // 创建完整消息
        HxzkPathMessage message =  MowerPathMessageGenerator.createMessage(
                "hxzkpath_"+value,
                System.currentTimeMillis(),
                "USER_"+value,
                "6528",
                "set_path",
                basestationData,
                pathData
        );
        return message;
    }
}