zsh_root
2025-01-06 7857a444de69124e9f7fb45f98b0ae818b107f23
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package baowen;
 
 
import tools.Jiaoyan;
import tools.Judge.JugeNumber;
import tools.ShowMessage;
 
import java.math.BigInteger;
import java.util.Vector;
 
 
/**
 * 处理原始数据
 */
public class Dell_uanshi_byt {
 
    static Vector<YuanshiByt> yuanshivec = new Vector<>();
 
 
    /**
     * 插入原始测距数据,每个ip一个集合每个IP上来的数据装入各自IP之中
     */
    public static void intsert(String ip, String hex, String time, int timestamp, int port) {
        if (get_ys(ip) != null) {
            int lenth = hex.length() / 2;
            for (int i = 0; i < lenth; i++) {
                String hex1 = hex.substring(2 * i, (i + 1) * 2);
                get_ys(ip).getData().add(hex1);
                hex1 = null;
            }
            get_ys(ip).setTime(time);
            get_ys(ip).setTimestamp(timestamp);
            dell_ysbyt(get_ys(ip), ip, port);
        } else {
            YuanshiByt ysbyt = new YuanshiByt();
            Vector<String> data1 = new Vector<>();
            StringBuffer okhex = new StringBuffer();
            ysbyt.setIp(ip);
            ysbyt.setPort(port);
            ysbyt.setData(data1);
            ysbyt.setTimestamp(timestamp);
            ysbyt.setTime(time);
            ysbyt.setUsart_state(0);
            ysbyt.setPack(okhex);
            ysbyt.setDatalen(0);
            ysbyt.setType("");
            int lenth = hex.length() / 2;
            for (int i = 0; i < lenth; i++) {
                String hex1 = hex.substring(2 * i, (i + 1) * 2);
                data1.add(hex1);
                hex1 = null;
            }
            yuanshivec.add(ysbyt);
            dell_ysbyt(ysbyt, ip, port);
        }
 
    }
 
    /**
     * 通过IP找到该IP的数据集合
     */
    public static YuanshiByt get_ys(String ip) {
        YuanshiByt ysbyt = null;
        int size = yuanshivec.size();
        for (int i = 0; i < size; i++) {
            YuanshiByt ysbyt1 = yuanshivec.get(i);
            String ip1 = ysbyt1.getIp();
            if (ip1.equals(ip)) {
                ysbyt = ysbyt1;
                break;
            }
        }
        return ysbyt;
    }
 
 
    /**
     * 处理原始集合中的数据
     */
    public static void dell_ysbyt(YuanshiByt ysbyt, String ip, int port) {
        String time = ysbyt.getTime();
        int timestamp = ysbyt.getTimestamp();
        while (ysbyt.getData().size() > 0) {
            String hex = null;
            try {
                hex = ysbyt.getData().get(0);
            } catch (ArrayIndexOutOfBoundsException e) {
                return;
            }
            int usart_state = ysbyt.getUsart_state();
            //将原始数据集合中的第一条数据取出放入swich循环
            switch (usart_state) {
 
                case 0:
                    //如果数据是55则执行
                    if (hex.equals("55")) {
                        //将数据处理的状态修改为1
                        ysbyt.setUsart_state(1);
                        //同时插入数据在字符串中
                        ysbyt.getPack().append(hex);
                    }
                    break;
 
                case 1:
                    //如果数据是AA则执行
                    if (hex.equals("AA")) {//状态1情况下,等待接收到AA包头2,然后变成状态2
                        ysbyt.setUsart_state(2);
                        ysbyt.getPack().append(hex);
                    }
                    break;
 
                case 2:
                    //数据类型给TYPE
                    ysbyt.setType(hex);
                    ysbyt.setUsart_state(3);
                    ysbyt.getPack().append(hex);
                    break;
 
                case 3:
                    //报文数据段数据长度
                    boolean a1 = JugeNumber.isLetterDigit(hex);
                    int d = 0;
                    if (a1) {
                        BigInteger bigint = new BigInteger(hex, 16);
                        d = bigint.intValue();
                    } else {
                        ShowMessage.zidingyi("HEX to DEC error:" + hex);
                        ysbyt.getPack().setLength(0);
                        ysbyt.setType("");
                        ysbyt.setUsart_state(0);
                        break;
                    }
                    ysbyt.getPack().append(hex);
                    ysbyt.setUsart_state(4);
                    ysbyt.setDatalen(d);
                    break;
 
                case 4:
                    ysbyt.getPack().append(hex);
                    int lenth = ysbyt.getPack().toString().length();
                    int a = ysbyt.getDatalen() * 2 + 8;
                    if (lenth == a) {
                        //数据处理的状态
                        ysbyt.setUsart_state(0);
                        //校验
                        String check = Jiaoyan.check2(ysbyt.getPack().toString());
                        String panck_cmd = null;
                        try {
                            panck_cmd = ysbyt.getPack().toString().substring(lenth - 2, lenth) + ysbyt.getPack().substring(lenth - 4, lenth - 2);
                        } catch (Exception e) {
                            return;
                        }
 
 
                        //如果校验成功
                        if (check.equals(panck_cmd)) {
                            //如果是测距数据
                            String type = ysbyt.getType();
                            switch (type) {
                                case "39":
                                    Dell_55AA39.dell(ip, ysbyt.getPack().toString(), port);
                                    break;
                                case "40":
                                    Dell_55AA40.dellrgs(ip, ysbyt.getPack().toString(), port);
                                    break;
                            }
                        }
 
                        ysbyt.getPack().setLength(0);
                        ysbyt.setDatalen(0);
                        ysbyt.setType("");
                    } else if (lenth > a) {
                        ysbyt.getPack().setLength(0);
                        ysbyt.setDatalen(0);
                        ysbyt.setType("");
                    }
 
                    break;
 
            }
            //删除处理过的数据
            ysbyt.getData().removeElement(hex);
        }
 
    }
 
}