张世豪
15 小时以前 b272034a1fdbfe32b355fc6c264a4c45df107190
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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 "";
        }
    }
}