张世豪
6 天以前 5d6d890cfd10466d5d14ff5177adcc888baaa0e4
src/set/Setsys.java
@@ -4,11 +4,16 @@
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 boolean boundaryLengthVisible = false;  // 默认关闭显示边界距离
    
    private static final String PROPERTIES_FILE = "set.properties";
@@ -17,10 +22,11 @@
    }
    // 带参构造方法
    public Setsys(String mowerId, String cuttingWidth, String simCardNumber, String firmwareVersion, String appVersion) {
    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;
    }
@@ -50,6 +56,14 @@
        this.simCardNumber = simCardNumber;
    }
    public String getHandheldMarkerId() {
        return handheldMarkerId;
    }
    public void setHandheldMarkerId(String handheldMarkerId) {
        this.handheldMarkerId = handheldMarkerId;
    }
    public String getFirmwareVersion() {
        return firmwareVersion;
    }
@@ -66,6 +80,22 @@
        this.appVersion = appVersion;
    }
    public int getIdleTrailDurationSeconds() {
        return idleTrailDurationSeconds;
    }
    public void setIdleTrailDurationSeconds(int seconds) {
        this.idleTrailDurationSeconds = sanitizeIdleTrailDuration(String.valueOf(seconds));
    }
    public boolean isBoundaryLengthVisible() {
        return boundaryLengthVisible;
    }
    public void setBoundaryLengthVisible(boolean visible) {
        this.boundaryLengthVisible = visible;
    }
    /**
     * 初始化方法 - 从properties文件读取数据
     * 如果属性值为-1,表示没有值,设置为null
@@ -79,17 +109,17 @@
            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");
            System.out.println("数据初始化完成");
        } catch (FileNotFoundException e) {
            System.err.println("属性文件未找到: " + PROPERTIES_FILE);
            this.idleTrailDurationSeconds = sanitizeIdleTrailDuration(props.getProperty("idleTrailDurationSeconds"));
            String boundaryLengthVisibleStr = props.getProperty("boundaryLengthVisible");
            this.boundaryLengthVisible = "true".equalsIgnoreCase(boundaryLengthVisibleStr);
        } catch (FileNotFoundException e) {
            // 文件不存在时,设置所有属性为null
            setAllPropertiesToNull();
        } catch (IOException e) {
            System.err.println("读取属性文件时出错: " + e.getMessage());
        } catch (IOException e) {
            setAllPropertiesToNull();
        }
    }
@@ -112,15 +142,29 @@
            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;
            case "boundaryLengthVisible":
                this.boundaryLengthVisible = "true".equalsIgnoreCase(value);
                break;
            case "mapScale":
                // mapScale不需要在内存中存储,直接更新到文件
                break;
            default:
                System.err.println("未知的属性名: " + propertyName);
                return false;
                // 对于其他属性,也允许直接更新到文件(不打印错误)
                break;
        }
        // 更新properties文件
@@ -146,7 +190,6 @@
        // 写回文件
        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());
@@ -154,6 +197,25 @@
        }
    }
    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
     */
@@ -161,8 +223,11 @@
        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;
        this.boundaryLengthVisible = false;  // 默认关闭
    }
    /**
@@ -173,8 +238,10 @@
        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
@@ -205,8 +272,10 @@
                "mowerId='" + mowerId + '\'' +
                ", cuttingWidth='" + cuttingWidth + '\'' +
                ", simCardNumber='" + simCardNumber + '\'' +
                ", handheldMarkerId='" + handheldMarkerId + '\'' +
                ", firmwareVersion='" + firmwareVersion + '\'' +
                ", appVersion='" + appVersion + '\'' +
                ", idleTrailDurationSeconds=" + idleTrailDurationSeconds +
                '}';
    }
}