From c57cb0cd9feb4495c89246b2faec6d5e45c23c30 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期一, 15 十二月 2025 15:46:49 +0800
Subject: [PATCH] 新增了控制指令

---
 src/gecaoji/Device.java |  206 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 191 insertions(+), 15 deletions(-)

diff --git a/src/gecaoji/Device.java b/src/gecaoji/Device.java
index 0a54671..aa4b77d 100644
--- a/src/gecaoji/Device.java
+++ b/src/gecaoji/Device.java
@@ -2,7 +2,10 @@
 import baseStation.BaseStation;
 import set.Setsys;
 import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
 import java.util.Locale;
 import java.util.Properties;
 
@@ -72,6 +75,14 @@
     // 鍗槦鍙暟
     private String differentialAge;
     // 宸垎鏃堕棿
+    private String selfCheckStatus = "-1";
+    // 鍓茶崏鏈鸿嚜妫�鐘舵��
+    private String mowerStartStatus = "-1";
+    // 鍓茶崏鏈哄惎鍔ㄧ姸鎬侊細1寮�鍚紝0鐔勭伀锛�-1鏈煡
+    private String mowerLightStatus = "-1";
+    // 鍓茶崏鏈虹伅寮�鍏崇姸鎬侊細1寮�鍚紝0鍏抽棴锛�-1鏈煡
+    private String mowerBladeHeight = "-1";
+    // 鍓茶崏鏈哄垁鐩橀珮搴︼細-1鏈煡
 
     private static final double METERS_PER_DEGREE_LAT = 111320.0d;
     
@@ -102,6 +113,58 @@
         applyDefaults(gecaoji);
     }
 
+    /**
+     * 淇濆瓨鎵�鏈夊睘鎬у埌device.properties鏂囦欢
+     */
+    public void saveToProperties() {
+        Properties properties = new Properties();
+        
+        // 鍔犺浇鐜版湁灞炴�э紙淇濈暀鍏朵粬灞炴�э級
+        try (FileInputStream input = new FileInputStream("device.properties")) {
+            properties.load(input);
+        } catch (IOException e) {
+            // 濡傛灉鏂囦欢涓嶅瓨鍦紝缁х画鍒涘缓鏂版枃浠�
+        }
+        
+        // 璁剧疆鎵�鏈夎澶囧睘鎬�
+        if (mowerName != null) properties.setProperty("mowerName", mowerName);
+        if (mowerModel != null) properties.setProperty("mowerModel", mowerModel);
+        if (mowerNumber != null) properties.setProperty("mowerNumber", mowerNumber);
+        if (mowingWidth != null) properties.setProperty("mowingWidth", mowingWidth);
+        if (mowingHeight != null) properties.setProperty("mowingHeight", mowingHeight);
+        if (baseStationNumber != null) properties.setProperty("baseStationNumber", baseStationNumber);
+        if (baseStationCardNumber != null) properties.setProperty("baseStationCardNumber", baseStationCardNumber);
+        if (baseStationCoordinates != null) properties.setProperty("baseStationCoordinates", baseStationCoordinates);
+        if (deviceCardnumber != null) properties.setProperty("deviceCardnumber", deviceCardnumber);
+        if (createTime != null) properties.setProperty("createTime", createTime);
+        if (GupdateTime != null) properties.setProperty("GupdateTime", GupdateTime);
+        if (BupdateTime != null) properties.setProperty("BupdateTime", BupdateTime);
+        if (realtimeLatitude != null) properties.setProperty("realtimeLatitude", realtimeLatitude);
+        if (realtimeLongitude != null) properties.setProperty("realtimeLongitude", realtimeLongitude);
+        if (realtimeAltitude != null) properties.setProperty("realtimeAltitude", realtimeAltitude);
+        if (realtimeX != null) properties.setProperty("realtimeX", realtimeX);
+        if (realtimeY != null) properties.setProperty("realtimeY", realtimeY);
+        if (realtimeSpeed != null) properties.setProperty("realtimeSpeed", realtimeSpeed);
+        if (heading != null) properties.setProperty("heading", heading);
+        if (pitch != null) properties.setProperty("pitch", pitch);
+        if (battery != null) properties.setProperty("battery", battery);
+        if (positioningStatus != null) properties.setProperty("positioningStatus", positioningStatus);
+        if (satelliteCount != null) properties.setProperty("satelliteCount", satelliteCount);
+        if (differentialAge != null) properties.setProperty("differentialAge", differentialAge);
+        if (selfCheckStatus != null) properties.setProperty("selfCheckStatus", selfCheckStatus);
+        if (mowerStartStatus != null) properties.setProperty("mowerStartStatus", mowerStartStatus);
+        if (mowerLightStatus != null) properties.setProperty("mowerLightStatus", mowerLightStatus);
+        if (mowerBladeHeight != null) properties.setProperty("mowerBladeHeight", mowerBladeHeight);
+        
+        // 淇濆瓨鍒版枃浠�
+        try (FileOutputStream output = new FileOutputStream("device.properties");
+             OutputStreamWriter writer = new OutputStreamWriter(output, StandardCharsets.UTF_8)) {
+            properties.store(writer, "Updated device properties");
+        } catch (IOException ex) {
+            System.err.println("鏃犳硶淇濆瓨 device.properties: " + ex.getMessage());
+        }
+    }
+
     private void loadPropertiesInto(Device target, Properties properties) {
         if (target == null) {
             return;
@@ -131,6 +194,10 @@
         target.positioningStatus = properties.getProperty("positioningStatus", "-1");
         target.satelliteCount = properties.getProperty("satelliteCount", "-1");
         target.differentialAge = properties.getProperty("differentialAge", "-1");
+        target.selfCheckStatus = properties.getProperty("selfCheckStatus", "-1");
+        target.mowerStartStatus = properties.getProperty("mowerStartStatus", "-1");
+        target.mowerLightStatus = properties.getProperty("mowerLightStatus", "-1");
+        target.mowerBladeHeight = properties.getProperty("mowerBladeHeight", "-1");
     }
 
     private void applyDefaults(Device target) {
@@ -162,6 +229,10 @@
         target.positioningStatus = "-1";
         target.satelliteCount = "-1";
         target.differentialAge = "-1";
+        target.selfCheckStatus = "-1";
+        target.mowerStartStatus = "-1";
+        target.mowerLightStatus = "-1";
+        target.mowerBladeHeight = "-1";
     }
 
     public static synchronized Device initializeActiveDevice(String mowerId) { // 鏍规嵁璁惧ID鍒濆鍖栨椿璺冭澶�
@@ -260,6 +331,18 @@
             case "differentialAge":
                 this.differentialAge = value;
                 return true;
+            case "selfCheckStatus":
+                this.selfCheckStatus = value;
+                return true;
+            case "mowerStartStatus":
+                this.mowerStartStatus = value;
+                return true;
+            case "mowerLightStatus":
+                this.mowerLightStatus = value;
+                return true;
+            case "mowerBladeHeight":
+                this.mowerBladeHeight = value;
+                return true;
             default:
                 System.err.println("鏈煡瀛楁: " + fieldName);
                 return false;
@@ -274,6 +357,14 @@
         device.applyGNGGAUpdate(gnggaData, deviceId);
     }
 
+    public static synchronized void updateFromSerialGNGGA(String gnggaData) { // 涓插彛鏁版嵁鏇存柊璺緞锛堟棤闇�璁惧缂栧彿鍖归厤锛�
+        Device device = gecaoji;
+        if (device == null) {
+            return;
+        }
+        device.chuankouGNGGAUpdate(gnggaData);
+    }
+
     private void applyGNGGAUpdate(String gnggaData, String deviceId) { // 鎵цGNGGA鏇存柊閫昏緫
         if (gnggaData == null) {
             return;
@@ -297,10 +388,7 @@
                 return;
             }
         }
-        if (hasMeaningfulValue(mowerNumber) && hasMeaningfulValue(incomingDeviceId) && !mowerNumber.equals(incomingDeviceId)) {
-            return;
-        }
-        if (hasMeaningfulValue(incomingDeviceId)) {
+        if (hasMeaningfulValue(incomingDeviceId) && !incomingDeviceId.equals(mowerNumber)) {
             mowerNumber = incomingDeviceId;
         }
 
@@ -309,9 +397,19 @@
         String longitudeValue = sanitizeField(fields, 4);
         String longitudeHemisphere = sanitizeField(fields, 5);
 
-        realtimeLatitude = defaultIfEmpty(combineCoordinate(latitudeValue, latitudeHemisphere));
-        realtimeLongitude = defaultIfEmpty(combineCoordinate(longitudeValue, longitudeHemisphere));
-        realtimeAltitude = defaultIfEmpty(sanitizeField(fields, 9));
+        String combinedLatitude = combineCoordinate(latitudeValue, latitudeHemisphere);
+        if (hasMeaningfulValue(combinedLatitude)) {
+            realtimeLatitude = combinedLatitude;
+        }
+        String combinedLongitude = combineCoordinate(longitudeValue, longitudeHemisphere);
+        if (hasMeaningfulValue(combinedLongitude)) {
+            realtimeLongitude = combinedLongitude;
+        }
+
+        String altitudeValue = sanitizeField(fields, 9);
+        if (hasMeaningfulValue(altitudeValue)) {
+            realtimeAltitude = altitudeValue;
+        }
 
         positioningStatus = defaultIfEmpty(sanitizeField(fields, 6));
         satelliteCount = defaultIfEmpty(sanitizeField(fields, 7));
@@ -324,28 +422,68 @@
 
         updateRelativeCoordinates(latitudeValue, latitudeHemisphere, longitudeValue, longitudeHemisphere);
     }
+    
+    /**涓插彛鏇存柊GNGGA鏁版嵁*/
+    private void chuankouGNGGAUpdate(String gnggaData) { // 鎵цGNGGA鏇存柊閫昏緫
+        if (gnggaData == null) {
+            return;
+        }
+
+        String trimmed = gnggaData.trim();
+        if (trimmed.isEmpty() || !trimmed.startsWith("$GNGGA")) {
+            return;
+        }
+
+        String[] fields = trimmed.split(",");
+        if (fields.length < 15) {
+            System.err.println("GNGGA瀛楁鏁伴噺涓嶈冻: " + fields.length);
+            return;
+        }
+
+        
+        String latitudeValue = sanitizeField(fields, 2);
+        String latitudeHemisphere = sanitizeField(fields, 3);
+        String longitudeValue = sanitizeField(fields, 4);
+        String longitudeHemisphere = sanitizeField(fields, 5);
+
+        String combinedLatitude = combineCoordinate(latitudeValue, latitudeHemisphere);
+        if (hasMeaningfulValue(combinedLatitude)) {
+            realtimeLatitude = combinedLatitude;
+        }
+        String combinedLongitude = combineCoordinate(longitudeValue, longitudeHemisphere);
+        if (hasMeaningfulValue(combinedLongitude)) {
+            realtimeLongitude = combinedLongitude;
+        }
+
+        String altitudeValue = sanitizeField(fields, 9);
+        if (hasMeaningfulValue(altitudeValue)) {
+            realtimeAltitude = altitudeValue;
+        }
+
+        positioningStatus = defaultIfEmpty(sanitizeField(fields, 6));
+        satelliteCount = defaultIfEmpty(sanitizeField(fields, 7));
+        differentialAge = defaultIfEmpty(sanitizeField(fields, 13));       
+        realtimeSpeed ="0";        
+        GupdateTime = String.valueOf(System.currentTimeMillis());
+
+        updateRelativeCoordinates(latitudeValue, latitudeHemisphere, longitudeValue, longitudeHemisphere);
+    }
 
     private void updateRelativeCoordinates(String latValue, String latHemisphere,
                                            String lonValue, String lonHemisphere) { // 璁$畻鐩稿鍧愭爣
         if (!hasMeaningfulValue(latValue) || !hasMeaningfulValue(lonValue)
                 || !hasMeaningfulValue(latHemisphere) || !hasMeaningfulValue(lonHemisphere)) {
-            realtimeX = "-1";
-            realtimeY = "-1";
             return;
         }
 
         double mowerLat = toDecimalDegrees(latValue, latHemisphere);
         double mowerLon = toDecimalDegrees(lonValue, lonHemisphere);
         if (Double.isNaN(mowerLat) || Double.isNaN(mowerLon)) {
-            realtimeX = "-1";
-            realtimeY = "-1";
             return;
         }
 
         double[] baseLatLon = resolveBaseStationLatLon();
         if (baseLatLon == null) {
-            realtimeX = "-1";
-            realtimeY = "-1";
             return;
         }
 
@@ -357,8 +495,10 @@
         double eastMeters = deltaLonDeg * metersPerLon;
         double northMeters = deltaLatDeg * METERS_PER_DEGREE_LAT;
 
-        realtimeX = formatMeters(eastMeters);
-        realtimeY = formatMeters(northMeters);
+        if (Double.isFinite(eastMeters) && Double.isFinite(northMeters)) {
+            realtimeX = formatMeters(eastMeters);
+            realtimeY = formatMeters(northMeters);
+        }
     }
 
     private double[] resolveBaseStationLatLon() { // 瑙f瀽鍩虹珯缁忕含搴�
@@ -639,6 +779,38 @@
         this.differentialAge = differentialAge;
     }
 
+    public String getSelfCheckStatus() { // 鑾峰彇鑷鐘舵��
+        return selfCheckStatus;
+    }
+
+    public void setSelfCheckStatus(String selfCheckStatus) { // 璁剧疆鑷鐘舵��
+        this.selfCheckStatus = selfCheckStatus;
+    }
+
+    public String getMowerStartStatus() { // 鑾峰彇鍓茶崏鏈哄惎鍔ㄧ姸鎬�
+        return mowerStartStatus;
+    }
+
+    public void setMowerStartStatus(String mowerStartStatus) { // 璁剧疆鍓茶崏鏈哄惎鍔ㄧ姸鎬�
+        this.mowerStartStatus = mowerStartStatus;
+    }
+
+    public String getMowerLightStatus() { // 鑾峰彇鍓茶崏鏈虹伅寮�鍏崇姸鎬�
+        return mowerLightStatus;
+    }
+
+    public void setMowerLightStatus(String mowerLightStatus) { // 璁剧疆鍓茶崏鏈虹伅寮�鍏崇姸鎬�
+        this.mowerLightStatus = mowerLightStatus;
+    }
+
+    public String getMowerBladeHeight() { // 鑾峰彇鍓茶崏鏈哄垁鐩橀珮搴�
+        return mowerBladeHeight;
+    }
+
+    public void setMowerBladeHeight(String mowerBladeHeight) { // 璁剧疆鍓茶崏鏈哄垁鐩橀珮搴�
+        this.mowerBladeHeight = mowerBladeHeight;
+    }
+
     @Override
     public String toString() { // 杈撳嚭瀵硅薄淇℃伅
         return "Device{" +
@@ -666,6 +838,10 @@
                 ", positioningStatus='" + positioningStatus + '\'' +
                 ", satelliteCount='" + satelliteCount + '\'' +
                 ", differentialAge='" + differentialAge + '\'' +
+                ", selfCheckStatus='" + selfCheckStatus + '\'' +
+                ", mowerStartStatus='" + mowerStartStatus + '\'' +
+                ", mowerLightStatus='" + mowerLightStatus + '\'' +
+                ", mowerBladeHeight='" + mowerBladeHeight + '\'' +
                 '}';
     }
 }
\ No newline at end of file

--
Gitblit v1.10.0