20260410:LBS读取bug

This commit is contained in:
zhugongping 2026-04-10 11:48:52 +08:00
parent c9e0766677
commit f176f765ab
2 changed files with 81 additions and 33 deletions

View File

@ -1,6 +1,11 @@
package com.fastbee.common.utils.dtu; package com.fastbee.common.utils.dtu;
import com.fastbee.common.utils.StringUtils;
import lombok.val;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -126,26 +131,56 @@ public class ATCommandParserUtils {
* 解析LBS基站定位响应 * 解析LBS基站定位响应
* 示例输入: 0d0a2b4c42533d2232382e3132353833382c3131322e383632363339220d0a4f4b0d0a * 示例输入: 0d0a2b4c42533d2232382e3132353833382c3131322e383632363339220d0a4f4b0d0a
* 对应文本: \r\n+LBS="28.125838,112.862639"\r\nOK\r\n * 对应文本: \r\n+LBS="28.125838,112.862639"\r\nOK\r\n
* * 例2:0d0a2b4c42533a20223131322e383539383437222c2232382e313237383337220d0a0d0a4f4b0d0a
* @param hexString 十六进制字符串 * 对应文本: \r\n+LBS="112.859847,28.127837"\r\nOK\r\n
* @param lbsValue LBS响应字符串
* @return 经纬度数组 [经度, 纬度],解析失败返回null * @return 经纬度数组 [经度, 纬度],解析失败返回null
*/ */
public static double[] parseLBS(String hexString) { public static BigDecimal[] parseLBS(String lbsValue) {
Map<String, String> result = parseATResponseFromHex(hexString);
String lbsValue = result.get("LBS"); if (lbsValue == null || lbsValue.trim().isEmpty()) {
if (lbsValue != null && lbsValue.contains(",")) { return null;
String[] parts = lbsValue.split(","); }
try { try {
// 返回 [经度,纬度] // 1. 清洗数据(去引号、空格)
double latitude = Double.parseDouble(parts[0]); String clean = lbsValue.replace("\"", "").trim();
double longitude = Double.parseDouble(parts[1]);
return new double[]{longitude, latitude}; // 2. 分割
} catch (NumberFormatException e) { String[] arr = clean.split(",");
if (arr.length != 2) {
return null; return null;
} }
BigDecimal v1 = new BigDecimal(arr[0].trim());
BigDecimal v2 = new BigDecimal(arr[1].trim());
BigDecimal lat;
BigDecimal lng;
// 3. 智能判断
if (isLatitude(v1) && isLongitude(v2)) {
// 输入:lat,lng
lat = v1;
lng = v2;
} else if (isLongitude(v1) && isLatitude(v2)) {
// 输入:lng,lat
lng = v1;
lat = v2;
} else {
// 兜底(默认按 lat,lng)
lat = v1;
lng = v2;
} }
// ✅ 统一返回:经度在前,纬度在后
return new BigDecimal[]{lng, lat};
} catch (Exception e) {
System.err.println("LBS解析失败: " + lbsValue);
return null; return null;
} }
}
/** /**
* 解析LBS响应,返回经纬度字符串 * 解析LBS响应,返回经纬度字符串
@ -277,30 +312,43 @@ public class ATCommandParserUtils {
return new String(data, StandardCharsets.UTF_8); return new String(data, StandardCharsets.UTF_8);
} }
/**
* 判断是否为有效纬度
*
* @param val 纬度值
* @return 是否为有效纬度
*/
private static boolean isLatitude(BigDecimal val) {
return val.compareTo(new BigDecimal("-90")) >= 0 &&
val.compareTo(new BigDecimal("90")) <= 0;
}
/**
* 判断是否为有效经度
*
* @param val 经度值
* @return 是否为有效经度
*/
private static boolean isLongitude(BigDecimal val) {
return val.compareTo(new BigDecimal("-180")) >= 0 &&
val.compareTo(new BigDecimal("180")) <= 0;
}
// 测试方法 // 测试方法
public static void main(String[] args) { public static void main(String[] args) {
// 测试IMEI解析 // 测试IMEI解析
String imeiHex = "0d0a2b494d45493a22383631383737303738323532353332220d0a0d0a4f4b0d0a"; String imeiHex = "0d0a2b494d45493a22383631383737303738323532353332220d0a0d0a4f4b0d0a";
System.out.println("IMEI解析结果: " + parseIMEI(imeiHex)); System.out.println("IMEI解析结果: " + parseATResponseFromHex(imeiHex));
// 测试ICCID解析 // 测试ICCID解析
String iccidHex = "0d0a2b49434349443a223839383630383538313132354432373832343033220d0a4f4b0d0a"; String iccidHex = "0d0a2b49434349443a223839383630383538313132354432373832343033220d0a4f4b0d0a";
System.out.println("ICCID解析结果: " + parseICCID(iccidHex)); System.out.println("ICCID解析结果: " + parseATResponseFromHex(iccidHex));
// 测试LBS解析(修正后应能正常解析) // 测试LBS解析(修正后应能正常解析)
String lbsHex = "0d0a2b4c42533d2232382e3132353833382c3131322e383632363339220d0a4f4b0d0a"; String lbsHex = "0d0a2b4c42533a20223131322e383539383437222c2232382e313237383337220d0a0d0a4f4b0d0a";
System.out.println("LBS解析结果: " + parseLBSString(lbsHex)); Map<String, String> lbs = parseATResponseFromHex(lbsHex);
double[] lbs = parseLBS(lbsHex); System.out.println("LBS解析结果: " + lbs);
if (lbs != null) { BigDecimal[] lbsDecimal = parseLBS(lbs.get("LBS"));
System.out.println("经度: " + lbs[0] + ", 纬度: " + lbs[1]); System.out.println("LBS解析结果: " + Arrays.toString(lbsDecimal));
}
// 测试通用解析
String response = decodeHexResponse(imeiHex);
System.out.println("\n解码后的响应: " + response);
// 测试解析所有字段
Map<String, String> allFields = parseATResponseFromHex(imeiHex);
System.out.println("解析结果: " + allFields);
} }
} }

View File

@ -327,10 +327,10 @@ public class DeviceDebugLogAspect {
} }
String lbsValue = atCommandMap.get("LBS"); String lbsValue = atCommandMap.get("LBS");
if (StringUtils.isNotEmpty(lbsValue)) { if (StringUtils.isNotEmpty(lbsValue)) {
String[] lbsArray = lbsValue.split(","); BigDecimal[] lbsArray = ATCommandParserUtils.parseLBS(lbsValue);
if (lbsArray.length == 2) { if (lbsArray.length == 2) {
updateDevice.setLongitude(new BigDecimal(lbsArray[1])); updateDevice.setLongitude(lbsArray[0]);
updateDevice.setLatitude(new BigDecimal(lbsArray[0])); updateDevice.setLatitude(lbsArray[1]);
} }
} }
updateDevice.setDeviceId(device.getDeviceId()); updateDevice.setDeviceId(device.getDeviceId());