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);
|
}
|
|
}
|
|
}
|