diff --git a/fastbee-common/src/main/java/com/fastbee/common/utils/dtu/ATCommandParserUtils.java b/fastbee-common/src/main/java/com/fastbee/common/utils/dtu/ATCommandParserUtils.java index ebaf6f91..398b2844 100644 --- a/fastbee-common/src/main/java/com/fastbee/common/utils/dtu/ATCommandParserUtils.java +++ b/fastbee-common/src/main/java/com/fastbee/common/utils/dtu/ATCommandParserUtils.java @@ -1,6 +1,11 @@ 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.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; @@ -126,25 +131,55 @@ public class ATCommandParserUtils { * 解析LBS基站定位响应 * 示例输入: 0d0a2b4c42533d2232382e3132353833382c3131322e383632363339220d0a4f4b0d0a * 对应文本: \r\n+LBS="28.125838,112.862639"\r\nOK\r\n - * - * @param hexString 十六进制字符串 + * 例2:0d0a2b4c42533a20223131322e383539383437222c2232382e313237383337220d0a0d0a4f4b0d0a + * 对应文本: \r\n+LBS="112.859847,28.127837"\r\nOK\r\n + * @param lbsValue LBS响应字符串 * @return 经纬度数组 [经度, 纬度],解析失败返回null */ - public static double[] parseLBS(String hexString) { - Map result = parseATResponseFromHex(hexString); - String lbsValue = result.get("LBS"); - if (lbsValue != null && lbsValue.contains(",")) { - String[] parts = lbsValue.split(","); - try { - // 返回 [经度,纬度] - double latitude = Double.parseDouble(parts[0]); - double longitude = Double.parseDouble(parts[1]); - return new double[]{longitude, latitude}; - } catch (NumberFormatException e) { + public static BigDecimal[] parseLBS(String lbsValue) { + + if (lbsValue == null || lbsValue.trim().isEmpty()) { + return null; + } + + try { + // 1. 清洗数据(去引号、空格) + String clean = lbsValue.replace("\"", "").trim(); + + // 2. 分割 + String[] arr = clean.split(","); + if (arr.length != 2) { 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; } /** @@ -277,30 +312,43 @@ public class ATCommandParserUtils { 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) { // 测试IMEI解析 String imeiHex = "0d0a2b494d45493a22383631383737303738323532353332220d0a0d0a4f4b0d0a"; - System.out.println("IMEI解析结果: " + parseIMEI(imeiHex)); + System.out.println("IMEI解析结果: " + parseATResponseFromHex(imeiHex)); // 测试ICCID解析 String iccidHex = "0d0a2b49434349443a223839383630383538313132354432373832343033220d0a4f4b0d0a"; - System.out.println("ICCID解析结果: " + parseICCID(iccidHex)); + System.out.println("ICCID解析结果: " + parseATResponseFromHex(iccidHex)); // 测试LBS解析(修正后应能正常解析) - String lbsHex = "0d0a2b4c42533d2232382e3132353833382c3131322e383632363339220d0a4f4b0d0a"; - System.out.println("LBS解析结果: " + parseLBSString(lbsHex)); - double[] lbs = parseLBS(lbsHex); - if (lbs != null) { - System.out.println("经度: " + lbs[0] + ", 纬度: " + lbs[1]); - } - - // 测试通用解析 - String response = decodeHexResponse(imeiHex); - System.out.println("\n解码后的响应: " + response); - - // 测试解析所有字段 - Map allFields = parseATResponseFromHex(imeiHex); - System.out.println("解析结果: " + allFields); + String lbsHex = "0d0a2b4c42533a20223131322e383539383437222c2232382e313237383337220d0a0d0a4f4b0d0a"; + Map lbs = parseATResponseFromHex(lbsHex); + System.out.println("LBS解析结果: " + lbs); + BigDecimal[] lbsDecimal = parseLBS(lbs.get("LBS")); + System.out.println("LBS解析结果: " + Arrays.toString(lbsDecimal)); } } diff --git a/fastbee-iot-data/src/main/java/com/fastbee/iot/data/aspect/DeviceDebugLogAspect.java b/fastbee-iot-data/src/main/java/com/fastbee/iot/data/aspect/DeviceDebugLogAspect.java index 302dd6fa..5c79dcf5 100644 --- a/fastbee-iot-data/src/main/java/com/fastbee/iot/data/aspect/DeviceDebugLogAspect.java +++ b/fastbee-iot-data/src/main/java/com/fastbee/iot/data/aspect/DeviceDebugLogAspect.java @@ -327,10 +327,10 @@ public class DeviceDebugLogAspect { } String lbsValue = atCommandMap.get("LBS"); if (StringUtils.isNotEmpty(lbsValue)) { - String[] lbsArray = lbsValue.split(","); + BigDecimal[] lbsArray = ATCommandParserUtils.parseLBS(lbsValue); if (lbsArray.length == 2) { - updateDevice.setLongitude(new BigDecimal(lbsArray[1])); - updateDevice.setLatitude(new BigDecimal(lbsArray[0])); + updateDevice.setLongitude(lbsArray[0]); + updateDevice.setLatitude(lbsArray[1]); } } updateDevice.setDeviceId(device.getDeviceId());