package com.example.SecondProject.Utils;
import java.util.Arrays;
public class ProtocolUtil {
public static String TAG = "ProtocolUtil";
private static String data = "";
public static void parseData_fragment(String str){
data += str;
int startIndex = data.indexOf("$");
if (startIndex < 0){
data = "";
return;
}
if (startIndex > 0) {
data = data.substring(startIndex);
}
int endIndex = data.indexOf("*");
if( endIndex < 1 ) return;
String intactData;
if(data.length() < endIndex+3){
intactData = data;
}else {
intactData = data.substring(0,endIndex+3);
data = data.substring(endIndex+3);
}
String XOR_str = intactData.substring(intactData.length() - 2);
parseData(intactData);
}
public static void parseData(String intactData){
int xorIndex = intactData.indexOf("*");
if(xorIndex == -1){return;}
String data_str = intactData.substring(0, xorIndex);
if(!data_str.contains(",")){return;}
String[] values = data_str.split(",", -1);
if (data_str.contains("FKI")) {
BDFKI(values);
}else if (data_str.contains("ICP")) {
BDICP(values);
} else if (data_str.contains("PWI")) {
BDPWI(values);
} else if (data_str.contains("TCI")){
BDTCI(values);
}
else {
parseRNSS(values);
}
}
public static void BDICP(String[] value){
try {
String cardID = value[1];
String cardFrequency = value[14];
String cardLevel = value[15];
System.out.println(TAG + "收到IC信息: 卡号-" + cardID + " 频度-" + cardFrequency + " 等级-" + cardLevel);
}catch (Exception e){
System.out.println(TAG+ "BDICP: 解析错误" + e.toString());
e.printStackTrace();
return;
}
}
public static void BDPWI(String[] values){
try {
int rdss2Count1 = Integer.parseInt(values[2]);
int index = 2 + (rdss2Count1*3) + 1;
int rdss3Count = Integer.parseInt(values[index]);
index++;
int s21[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
for (int i = 0 ;i < rdss3Count; i++) {
if(values.length < index+2){
return;
}
int id = Integer.parseInt(values[index]);
if (id > 21 || id <= 0) continue;
int number = Integer.parseInt(values[index+1]);
s21[id-1] = number;
index += 4;
}
System.out.println(TAG+ "收到信号数据:"+Arrays.toString(s21));
}catch (Exception e){
System.out.println(TAG+ "BDPWI: 解析错误" + e.toString());
e.printStackTrace();
return;
}
}
public static void BDFKI(String[] values){
try {
String type = values[2];
String result = values[3];
String reason = values[4];
if(result.equals("Y")){
System.out.println(TAG+ "收到反馈信息: " + type + "指令发送成功");
} else {
switch ( reason ){
case "1":
System.out.println(TAG+ "收到反馈信息: " + type + "指令发送失败:频度未到,发射被抑制");
break;
case "2":
System.out.println(TAG+ "收到反馈信息: " + type + "指令发送失败:接收到系统的抑制指令,发射被抑制");
break;
case "3":
System.out.println(TAG+ "收到反馈信息: " + type + "指令发送失败:当前设置为无线电静默状态,发射被抑制");
break;
case "4":
System.out.println(TAG+ "收到反馈信息: " + type + "指令发送失败:功率未锁定");
break;
case "5":
System.out.println(TAG+ "收到反馈信息: " + type + "指令发送失败:未检测到IC模块信息");
break;
default:
System.out.println(TAG+ "收到反馈信息: " + type + "指令发送失败,原因码是" + reason);
break;
}
}
}catch (Exception e){
System.out.println(TAG+ "BDFKI: 解析错误" + e.toString());
e.printStackTrace();
return;
}
}
public static void BDTCI(String[] values){
try {
String fromNumber = values[1];
String toNumber = values[2];
String messageType = values[5];
String timeHour = values[4];
String content = values[7];
System.out.println(TAG+ "收到通信信息: 发送号码-" + fromNumber + " 接收号码-" + toNumber + " 消息类型-" + messageType + " 消息时间-" + timeHour + " 消息内容-" + content );
}catch (Exception e){
System.out.println(TAG+ "BDTCI: 解析错误" + e.toString());
e.printStackTrace();
return;
}
}
public static void parseRNSS(String[] values){
try {
String head = values[0];
if (head.contains("GGA")){
if (values[6].equals("0")) return;
String latitude = analysisLonlat(values[2]) + "";
String longitude = analysisLonlat(values[4]) + "";
String altitude;
if(values[9] != null || !values[9].equals("")){
altitude = values[9];
}else {
altitude = "0";
}
System.out.println(TAG+ "解析RNSS定位数据(GGA): 纬度-" + latitude + " 经度-" + longitude + " 高度-" + altitude );
}else if (head.contains("GLL")){
if (values[6].equals("V")) return;
String latitude = analysisLonlat(values[1]) + "";
String longitude = analysisLonlat(values[3]) + "";
System.out.println(TAG+ "解析RNSS定位数据(GLL): 纬度-" + latitude + " 经度-" + longitude );
}
else {
return;
}
}catch (Exception e){
System.out.println(TAG+ "parseRNSS: 解析错误" + e.toString());
e.printStackTrace();
return;
}
}
public static double analysisLonlat(String value){
if(value.contains("N") || value.contains("E")){
return 0.0;
}
if (value.equals("")|| value == null) return 0.0;
double lonlat = Double.valueOf(value);
int dd = (int)lonlat / 100;
int mm = (int)lonlat % 100;
double ms = lonlat - (int)lonlat;
return dd+((mm+ms)/60.0);
}
public static String CCICR(int type, String info) {
String command = "CCICR," + type + "," + info;
return packaging(command);
}
public static String packaging(String tmp){
String hexCommand = DataUtil.string2Hex(tmp);
String hh = getCheckCode0007(hexCommand).toUpperCase();
return "24"+hexCommand+"2A"+DataUtil.string2Hex(hh)+"0D0A";
}
public static String getCheckCode0007(String strProtocol) {
strProtocol.replace(" ", "");
byte chrCheckCode = 0;
for (int i = 0; i < strProtocol.length(); i += 2) {
char chrTmp ;
chrTmp = strProtocol.charAt(i);
if (chrTmp == ' ') continue;
byte chTmp1 = (byte) (DataUtil.char2HexByte(chrTmp) << 4);
chrTmp = strProtocol.charAt(i + 1);
byte chTmp2 = (byte) (chTmp1 + (DataUtil.char2HexByte(chrTmp) & 15));
chrCheckCode = i == 0 ? chTmp2 : (byte) (chrCheckCode ^ chTmp2);
}
String strHexCheckCode = String.format("%x", Byte.valueOf(chrCheckCode));
if ((strHexCheckCode = strHexCheckCode.toUpperCase()).length() != 2) {
if (strHexCheckCode.length() > 2) {
strHexCheckCode = strHexCheckCode.substring(strHexCheckCode.length() - 2);
} else if (strHexCheckCode.length() < 2 && strHexCheckCode.length() > 0) {
strHexCheckCode = "0" + strHexCheckCode;
}
}
return strHexCheckCode;
}
}
class Main {
public static void main(String[] args) {
System.out.println("Hello world! - java.jsrun.net ");
}
}