张世豪
2025-12-11 6f59464fc6e12a525ba3004864eceb1bc1573a31
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package set;
 
import java.io.*;
import java.util.Properties;
 
public class Setsys {
    public static final int DEFAULT_IDLE_TRAIL_DURATION_SECONDS = 60;
 
    private String mowerId;
    private String cuttingWidth;
    private String simCardNumber;
    private String handheldMarkerId;
    private String firmwareVersion;
    private String appVersion;
    private int idleTrailDurationSeconds = DEFAULT_IDLE_TRAIL_DURATION_SECONDS;
    
    private static final String PROPERTIES_FILE = "set.properties";
 
    // 无参构造方法
    public Setsys() {
    }
 
    // 带参构造方法
    public Setsys(String mowerId, String cuttingWidth, String simCardNumber, String handheldMarkerId, String firmwareVersion, String appVersion) {
        this.mowerId = mowerId;
        this.cuttingWidth = cuttingWidth;
        this.simCardNumber = simCardNumber;
        this.handheldMarkerId = handheldMarkerId;
        this.firmwareVersion = firmwareVersion;
        this.appVersion = appVersion;
    }
 
    // Getter和Setter方法
    public String getMowerId() {
        return mowerId;
    }
 
    public void setMowerId(String mowerId) {
        this.mowerId = mowerId;
    }
 
    public String getCuttingWidth() {
        return cuttingWidth;
    }
 
    public void setCuttingWidth(String cuttingWidth) {
        this.cuttingWidth = cuttingWidth;
    }
 
    public String getSimCardNumber() {
        return simCardNumber;
    }
 
    public void setSimCardNumber(String simCardNumber) {
        this.simCardNumber = simCardNumber;
    }
 
    public String getHandheldMarkerId() {
        return handheldMarkerId;
    }
 
    public void setHandheldMarkerId(String handheldMarkerId) {
        this.handheldMarkerId = handheldMarkerId;
    }
 
    public String getFirmwareVersion() {
        return firmwareVersion;
    }
 
    public void setFirmwareVersion(String firmwareVersion) {
        this.firmwareVersion = firmwareVersion;
    }
 
    public String getAppVersion() {
        return appVersion;
    }
 
    public void setAppVersion(String appVersion) {
        this.appVersion = appVersion;
    }
 
    public int getIdleTrailDurationSeconds() {
        return idleTrailDurationSeconds;
    }
 
    public void setIdleTrailDurationSeconds(int seconds) {
        this.idleTrailDurationSeconds = sanitizeIdleTrailDuration(String.valueOf(seconds));
    }
 
    /**
     * 初始化方法 - 从properties文件读取数据
     * 如果属性值为-1,表示没有值,设置为null
     */
    public void initializeFromProperties() {
        Properties props = new Properties();
        try (FileInputStream input = new FileInputStream(PROPERTIES_FILE)) {
            props.load(input);
            
            // 读取属性值,如果为-1则设置为null
            this.mowerId = "-1".equals(props.getProperty("mowerId")) ? null : props.getProperty("mowerId");
            this.cuttingWidth = "-1".equals(props.getProperty("cuttingWidth")) ? null : props.getProperty("cuttingWidth");
            this.simCardNumber = "-1".equals(props.getProperty("simCardNumber")) ? null : props.getProperty("simCardNumber");
            this.handheldMarkerId = "-1".equals(props.getProperty("handheldMarkerId")) ? null : props.getProperty("handheldMarkerId");
            this.firmwareVersion = "-1".equals(props.getProperty("firmwareVersion")) ? null : props.getProperty("firmwareVersion");
            this.appVersion = "-1".equals(props.getProperty("appVersion")) ? null : props.getProperty("appVersion");
            this.idleTrailDurationSeconds = sanitizeIdleTrailDuration(props.getProperty("idleTrailDurationSeconds"));
                        
        } catch (FileNotFoundException e) {           
            // 文件不存在时,设置所有属性为null
            setAllPropertiesToNull();
        } catch (IOException e) {            
            setAllPropertiesToNull();
        }
    }
 
    /**
     * 更新属性值并同步到properties文件
     * @param propertyName 属性名
     * @param value 属性值
     * @return 是否更新成功
     */
    public boolean updateProperty(String propertyName, String value) {
        // 更新内存中的属性值
        switch (propertyName) {
            case "mowerId":
                this.mowerId = value;
                break;
            case "cuttingWidth":
                this.cuttingWidth = value;
                break;
            case "simCardNumber":
                this.simCardNumber = value;
                break;
            case "handheldMarkerId":
                this.handheldMarkerId = value;
                break;
            case "firmwareVersion":
                this.firmwareVersion = value;
                break;
            case "appVersion":
                this.appVersion = value;
                break;
            case "idleTrailDurationSeconds":
                int durationSeconds = sanitizeIdleTrailDuration(value);
                this.idleTrailDurationSeconds = durationSeconds;
                value = String.valueOf(durationSeconds);
                break;
            default:
                System.err.println("未知的属性名: " + propertyName);
                return false;
        }
 
        // 更新properties文件
        return updatePropertiesFile(propertyName, value);
    }
 
    /**
     * 更新properties文件中的属性值
     */
    private boolean updatePropertiesFile(String propertyName, String value) {
        Properties props = new Properties();
        
        try (FileInputStream input = new FileInputStream(PROPERTIES_FILE)) {
            // 先加载现有属性
            props.load(input);
        } catch (IOException e) {
            System.err.println("读取属性文件失败: " + e.getMessage());
        }
 
        // 设置新值
        props.setProperty(propertyName, value);
 
        // 写回文件
        try (FileOutputStream output = new FileOutputStream(PROPERTIES_FILE)) {
            props.store(output, "Mower Configuration Properties - Updated");
            System.out.println("属性 " + propertyName + " 已更新为: " + value);
            return true;
        } catch (IOException e) {
            System.err.println("更新属性文件失败: " + e.getMessage());
            return false;
        }
    }
 
    private int sanitizeIdleTrailDuration(String raw) {
        if (raw == null) {
            return DEFAULT_IDLE_TRAIL_DURATION_SECONDS;
        }
        String trimmed = raw.trim();
        if (trimmed.isEmpty() || "-1".equals(trimmed)) {
            return DEFAULT_IDLE_TRAIL_DURATION_SECONDS;
        }
        try {
            int value = Integer.parseInt(trimmed);
            if (value < 5 || value > 600) {
                return DEFAULT_IDLE_TRAIL_DURATION_SECONDS;
            }
            return value;
        } catch (NumberFormatException ex) {
            return DEFAULT_IDLE_TRAIL_DURATION_SECONDS;
        }
    }
 
    /**
     * 设置所有属性为null
     */
    private void setAllPropertiesToNull() {
        this.mowerId = null;
        this.cuttingWidth = null;
        this.simCardNumber = null;
    this.handheldMarkerId = null;
        this.firmwareVersion = null;
        this.appVersion = null;
        this.idleTrailDurationSeconds = DEFAULT_IDLE_TRAIL_DURATION_SECONDS;
    }
 
    /**
     * 显示当前所有属性值
     */
    public void displayProperties() {
        System.out.println("当前属性值:");
        System.out.println("mowerId: " + (mowerId != null ? mowerId : "未设置"));
        System.out.println("cuttingWidth: " + (cuttingWidth != null ? cuttingWidth : "未设置"));
        System.out.println("simCardNumber: " + (simCardNumber != null ? simCardNumber : "未设置"));
    System.out.println("handheldMarkerId: " + (handheldMarkerId != null ? handheldMarkerId : "未设置"));
        System.out.println("firmwareVersion: " + (firmwareVersion != null ? firmwareVersion : "未设置"));
        System.out.println("appVersion: " + (appVersion != null ? appVersion : "未设置"));
        System.out.println("idleTrailDurationSeconds: " + idleTrailDurationSeconds);
    }
 
    // 根据set.properties中的键名获取对应的值,没有或为-1时返回null
    public static String getPropertyValue(String key) {
        if (key == null || key.trim().isEmpty()) {
            return null;
        }
 
        Properties props = new Properties();
        try (FileInputStream input = new FileInputStream(PROPERTIES_FILE)) {
            props.load(input);
            String value = props.getProperty(key);
            if (value == null || "-1".equals(value)) {
                return null;
            }
            return value;
        } catch (FileNotFoundException e) {
            System.err.println("属性文件未找到: " + PROPERTIES_FILE);
        } catch (IOException e) {
            System.err.println("读取属性文件时出错: " + e.getMessage());
        }
        return null;
    }
 
    @Override
    public String toString() {
        return "Set{" +
                "mowerId='" + mowerId + '\'' +
                ", cuttingWidth='" + cuttingWidth + '\'' +
                ", simCardNumber='" + simCardNumber + '\'' +
                ", handheldMarkerId='" + handheldMarkerId + '\'' +
                ", firmwareVersion='" + firmwareVersion + '\'' +
                ", appVersion='" + appVersion + '\'' +
                ", idleTrailDurationSeconds=" + idleTrailDurationSeconds +
                '}';
    }
}