| | |
| | | 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"; |
| | | |
| | |
| | | this.appVersion = appVersion; |
| | | } |
| | | |
| | | public int getIdleTrailDurationSeconds() { |
| | | return idleTrailDurationSeconds; |
| | | } |
| | | |
| | | public void setIdleTrailDurationSeconds(int seconds) { |
| | | this.idleTrailDurationSeconds = sanitizeIdleTrailDuration(String.valueOf(seconds)); |
| | | } |
| | | |
| | | /** |
| | | * 初始化方法 - 从properties文件读取数据 |
| | | * 如果属性值为-1,表示没有值,设置为null |
| | |
| | | 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")); |
| | | |
| | | System.out.println("数据初始化完成"); |
| | | |
| | |
| | | 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; |
| | |
| | | } |
| | | } |
| | | |
| | | 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 |
| | | */ |
| | |
| | | this.handheldMarkerId = null; |
| | | this.firmwareVersion = null; |
| | | this.appVersion = null; |
| | | this.idleTrailDurationSeconds = DEFAULT_IDLE_TRAIL_DURATION_SECONDS; |
| | | } |
| | | |
| | | /** |
| | |
| | | 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 |
| | |
| | | ", handheldMarkerId='" + handheldMarkerId + '\'' + |
| | | ", firmwareVersion='" + firmwareVersion + '\'' + |
| | | ", appVersion='" + appVersion + '\'' + |
| | | ", idleTrailDurationSeconds=" + idleTrailDurationSeconds + |
| | | '}'; |
| | | } |
| | | } |