解析工具类
package com.jeesite.modules.utils;
import java.text.DecimalFormat;
import org.apache.commons.lang.StringUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
/**
* @Desc 解析GPS发送数据工具类
* @author PGQing
*/
@Slf4j
public class HexMathUtils {
/**
* @Desc 1.将收到的GPS定位数据解析成JSON格式
* @param hex
* @return
* @throws Exception
*/
public static JSONObject formatHexStr(String hexStr) throws Exception {
log.info("准备解析的数据:\n" + hexStr);
JSONObject result = new JSONObject();
try {
if (StringUtils.isNotBlank(hexStr) && hexStr.replaceAll(" ", "").length() == 110) {
JSONObject info = new JSONObject();
// 去空
String content = hexStr.replaceAll(" ", "");
// 长度
info.put("length", content.getBytes("UTF-8").length / 2);
// 消息头
info.put("head", content.substring(0, 2));
// 消息长度
info.put("length", content.substring(2, 4));
// 消息ID
info.put("id", content.substring(4, 6));
// 设备编号
info.put("no", content.substring(6, 14));
// 消息体
info.put("body", content.substring(14, 106));
// 校验位
info.put("bit", content.substring(106));
// 解析设备编号、消息体
result = formatJSONObject(info);
log.info("解析数据为:\n" + result.toJSONString());
}
} catch (Exce