2023-02-16 14:17:36 +08:00

276 lines
7.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.zhgd.xmgl.util;
import java.io.UnsupportedEncodingException;
/**
* 车辆系统控制工具类
*
* @author Administrator
*
*/
public class yunboCarUtils {
// 公共变量
static String randoms = "0";
/**
* 语音播报函数 入参: "语音内容"
*
* @param plateNumber
* (播放车牌语音) "粤B12345" content播放语音"欢迎光临;临时车不允许进场;此车已出场;一路平安"
* @return
*/
public static String pushvoicetodevices(String plateNumber, String content) {
// 拼接--AA55(包头) 01(流水号) 64(地址) 00(业务类型) 22(命令) 0001(长度)
// 01(内容) 0556(校验) AF(结束符) --欢迎光临
String randomsd = ontheVariable(randoms);
// 内容匹配
String contentConversion = null;
int extent = 00;
String transitions = null;
try {
transitions = toGBK(plateNumber);// 车牌内容转换后
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if ("欢迎光临".equals(content)) {
// 01编号
contentConversion = "01";
} else if ("一路平安".equals(content)) {
// 02编号
contentConversion = "02";
} else if ("临时车不允许进场".equals(content)) {
// 2165编号
contentConversion = "2165";
} else if ("此车已出场".equals(content)) {
// 06编号
contentConversion = "06";
}
String Conversion = transitions + contentConversion;// 车牌+内容(欢迎光临)
extent = Conversion.length() / 2;
// 长度补位
String hex1 = Integer.toHexString(extent);
int s1 = hex1.length();
String bh2 = null;// 最终长度一位00
if (s1 <= 1) {
StringBuffer sb = new StringBuffer();
StringBuffer zh = sb.append("0").append(hex1.toUpperCase());
bh2 = zh.toString();
} else {
bh2 = Integer.toHexString(extent);
}
String send = "AA55";
String pjaxj = randomsd + "64" + "00" + "2200" + bh2 + Conversion;
// 校验码
byte[] bytes = hexStringToByteArray(pjaxj + "0000");
// 补充位数
String hex = getCRC(bytes);
int s = hex.length();
String bh1 = null;
if (s <= 3) {
if (s == 1) {
StringBuffer sb = new StringBuffer();
StringBuffer zh = sb.append("000").append(hex.toUpperCase());
bh1 = zh.toString();
}
if (s == 2) {
StringBuffer sb = new StringBuffer();
StringBuffer zh = sb.append("00").append(hex.toUpperCase());
bh1 = zh.toString();
}
if (s == 3) {
StringBuffer sb = new StringBuffer();
StringBuffer zh = sb.append("0").append(hex.toUpperCase());
bh1 = zh.toString();
}
} else {
bh1 = hex.toUpperCase();
}
String sends = send + pjaxj + bh1 + "AF";
return sends;
}
/***
* 下发显示命令 参数:
*
* @param type
* 01表示第一行 02 表示第二行 03表示第三行 04表示第四行
* @param content
* 表示显示内容
* @return
*/
public static String pushLedtexttodevice(String type, String content) {
String randomsd = ontheVariable(randoms);
int extent = 0;
// 长度
try {
extent = toGBK(content).length() / 2 + 4;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String hex1 = Integer.toHexString(extent);
int s1 = hex1.length();
String bh2 = null;
if (s1 <= 1) {
StringBuffer sb = new StringBuffer();
StringBuffer zh = sb.append("0").append(hex1.toUpperCase());
bh2 = zh.toString();
} else {
bh2 = Integer.toHexString(extent);
}
// 校验码
String pjaxj = null;
try {
pjaxj = randomsd + "64" + "00" + "2700" + bh2 + type + "0F" + "01"
+ "00" + toGBK(content);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] bytes = hexStringToByteArray(pjaxj + "0000");
// 补充位数
String hex = getCRC(bytes);
int s = hex.length();
String bh1 = null;
if (s <= 3) {
if (s == 1) {
StringBuffer sb = new StringBuffer();
StringBuffer zh = sb.append("000").append(hex.toUpperCase());
bh1 = zh.toString();
}
if (s == 2) {
StringBuffer sb = new StringBuffer();
StringBuffer zh = sb.append("00").append(hex.toUpperCase());
bh1 = zh.toString();
}
if (s == 3) {
StringBuffer sb = new StringBuffer();
StringBuffer zh = sb.append("0").append(hex.toUpperCase());
bh1 = zh.toString();
}
} else {
bh1 = hex.toUpperCase();
}
String send = "AA55" + pjaxj + bh1 + "AF";
return send;
}
// 车牌内容转GBK码
private static String toGBK(String source)
throws UnsupportedEncodingException {
StringBuilder sb = new StringBuilder();
byte[] bytes = source.getBytes("GBK");
for (byte b : bytes) {
sb.append(Integer.toHexString((b & 0xff)).toUpperCase());
}
return sb.toString();
}
/**
* 10进制转16进制(长度编译)
*
* @param systems
* @return
*/
private static String systems(String systems) {
int systemsd = Integer.valueOf(systems);
String hex = Integer.toHexString(systemsd);
int s = hex.length();
if (s <= 9) {
if (s == 1) {
StringBuffer sb = new StringBuffer();
StringBuffer zh = sb.append("000").append(hex.toUpperCase());
String bh = zh.toString();
return bh;
}
if (s == 2) {
StringBuffer sb = new StringBuffer();
StringBuffer zh = sb.append("00").append(hex.toUpperCase());
String bh = zh.toString();
return bh;
}
if (s == 3) {
StringBuffer sb = new StringBuffer();
StringBuffer zh = sb.append("0").append(hex.toUpperCase());
String bh = zh.toString();
return bh;
}
} else {
return hex.toUpperCase();
}
return hex;
}
/**
* 流水号
*
* @param randoms
* @return
*/
private static String ontheVariable(String randoms) {
int transform = Integer.valueOf(randoms);
if (transform < 9) {
int randomsd = transform + 1;
StringBuffer sb = new StringBuffer();
StringBuffer zh = sb.append("0").append(randomsd);
randoms = zh.toString();
return randoms;
}
if (transform >= 9 && transform < 99) {
int randomsd = transform + 1;
randoms = String.valueOf(randomsd);
return randoms;
}
if (transform >= 99) {
transform = 0;
if (transform < 9) {
int randomsd = transform + 1;
StringBuffer sb = new StringBuffer();
StringBuffer zh = sb.append("0").append(randomsd);
randoms = zh.toString();
return randoms;
}
}
return randoms;
}
private static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] b = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
// 两位一组,表示一个字节,把这样表示的16进制字符串还原成一个字节
b[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
.digit(s.charAt(i + 1), 16));
}
return b;
}
/**
* 计算CRC16校验码
*
* @param bytes
* @return
*/
private static String getCRC(byte[] bytes) {
int CRC = 0x0000ffff;
int POLYNOMIAL = 0x0000a001;
int i, j;
for (i = 0; i < bytes.length; i++) {
CRC ^= ((int) bytes[i] & 0x000000ff);
for (j = 0; j < 8; j++) {
if ((CRC & 0x00000001) != 0) {
CRC >>= 1;
CRC ^= POLYNOMIAL;
} else {
CRC >>= 1;
}
}
}
return Integer.toHexString(CRC);
}
}