package dikuai; import java.util.Locale; import gecaoji.Device; import gecaoji.MowerSafetyDistanceCalculator; /** * 割草安全距离计算工具类 * 用于根据割草机尺寸计算作业时的安全距离 */ public class Gecaoanquanjuli { /** * 根据割草机长度和宽度计算安全距离 * * 该方法会调用 MowerSafetyDistanceCalculator.calculateSafetyDistance() 方法 * 综合考虑割草机的尺寸、RTK定位精度等因素,计算作业时的安全边距 * * @param mowerLength 割草机长度,单位:米 * @param mowerWidth 割草机宽度,单位:米 * @return 安全距离,单位:米,保留两位小数。如果输入值无效(小于等于0),返回0.0 * * 使用示例: * double length = 0.8; // 割草机长度0.8米 * double width = 0.5; // 割草机宽度0.5米 * double safetyDistance = Gecaoanquanjuli.calculateSafetyDistance(length, width); * // 结果:safetyDistance = 0.XX(米,具体值取决于计算逻辑) */ public static double calculateSafetyDistance(double mowerLength, double mowerWidth) { if (mowerLength <= 0 || mowerWidth <= 0) { return 0.0; } try { return MowerSafetyDistanceCalculator.calculateSafetyDistance(mowerLength, mowerWidth); } catch (IllegalArgumentException e) { return 0.0; } } /** * 根据割草机长度和宽度计算安全距离(返回格式化字符串) * * @param mowerLength 割草机长度,单位:米 * @param mowerWidth 割草机宽度,单位:米 * @return 安全距离字符串,单位:米,保留两位小数。如果输入值无效,返回空字符串 * * 使用示例: * double length = 0.8; // 割草机长度0.8米 * double width = 0.5; // 割草机宽度0.5米 * String safetyDistanceStr = Gecaoanquanjuli.calculateSafetyDistanceString(length, width); * // 结果:safetyDistanceStr = "0.XX"(米,具体值取决于计算逻辑) */ public static String calculateSafetyDistanceString(double mowerLength, double mowerWidth) { double safetyDistance = calculateSafetyDistance(mowerLength, mowerWidth); if (safetyDistance <= 0) { return ""; } return String.format(Locale.US, "%.2f", safetyDistance); } /** * 根据割刀宽度计算安全距离(自动获取割草机长度) * * 该方法会从 Device 类获取割草机长度,如果没有配置则使用默认值0.8米 * 使用割刀宽度作为割草机宽度 * * @param bladeWidthMeters 割刀宽度,单位:米(将作为割草机宽度使用) * @return 安全距离,单位:米,保留两位小数。如果输入值无效,返回0.0 * * 使用示例: * double bladeWidth = 0.50; // 割刀宽度0.5米 * double safetyDistance = Gecaoanquanjuli.calculateSafetyDistanceFromBladeWidth(bladeWidth); * // 结果:safetyDistance = 0.XX(米,具体值取决于计算逻辑和割草机长度配置) */ public static double calculateSafetyDistanceFromBladeWidth(double bladeWidthMeters) { if (bladeWidthMeters <= 0) { return 0.0; } // 从Device类获取割草机长度,如果没有则使用默认值 double mowerLength = 0.8; // 默认长度0.8米 double mowerWidth = bladeWidthMeters; // 使用割刀宽度作为割草机宽度 Device device = Device.getGecaoji(); if (device != null) { String lengthStr = device.getMowerLength(); if (lengthStr != null && !lengthStr.trim().isEmpty() && !"-1".equals(lengthStr.trim())) { try { mowerLength = Double.parseDouble(lengthStr.trim()); } catch (NumberFormatException e) { // 使用默认值 } } } return calculateSafetyDistance(mowerLength, mowerWidth); } /** * 根据割刀宽度计算安全距离(返回格式化字符串,自动获取割草机长度) * * @param bladeWidthMeters 割刀宽度,单位:米(将作为割草机宽度使用) * @return 安全距离字符串,单位:米,保留两位小数。如果输入值无效,返回空字符串 * * 使用示例: * double bladeWidth = 0.50; // 割刀宽度0.5米 * String safetyDistanceStr = Gecaoanquanjuli.calculateSafetyDistanceStringFromBladeWidth(bladeWidth); * // 结果:safetyDistanceStr = "0.XX"(米,具体值取决于计算逻辑和割草机长度配置) */ public static String calculateSafetyDistanceStringFromBladeWidth(double bladeWidthMeters) { double safetyDistance = calculateSafetyDistanceFromBladeWidth(bladeWidthMeters); if (safetyDistance <= 0) { return ""; } return String.format(Locale.US, "%.2f", safetyDistance); } /** * 根据割刀宽度字符串计算安全距离(返回格式化字符串,自动获取割草机长度) * * @param bladeWidthStr 割刀宽度字符串,单位:米 * @return 安全距离字符串,单位:米,保留两位小数。如果输入值无效或无法解析,返回空字符串 * * 使用示例: * String bladeWidthStr = "0.50"; // 割刀宽度0.5米 * String safetyDistanceStr = Gecaoanquanjuli.calculateSafetyDistanceFromString(bladeWidthStr); * // 结果:safetyDistanceStr = "0.XX"(米,具体值取决于计算逻辑和割草机长度配置) */ public static String calculateSafetyDistanceFromString(String bladeWidthStr) { if (bladeWidthStr == null || bladeWidthStr.trim().isEmpty()) { return ""; } try { double bladeWidthMeters = Double.parseDouble(bladeWidthStr.trim()); return calculateSafetyDistanceStringFromBladeWidth(bladeWidthMeters); } catch (NumberFormatException e) { return ""; } } }