From de75ec84e295c3f952a200897aa22aa73d7d5867 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期一, 15 十二月 2025 19:37:11 +0800
Subject: [PATCH] 新增了串口割草机拖尾和缩放比例保存功能
---
src/zhuye/MapRenderer.java | 133 +++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 129 insertions(+), 4 deletions(-)
diff --git a/src/zhuye/MapRenderer.java b/src/zhuye/MapRenderer.java
index 635a1b1..8083749 100644
--- a/src/zhuye/MapRenderer.java
+++ b/src/zhuye/MapRenderer.java
@@ -18,6 +18,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Set;
+import set.Setsys;
import gecaoji.Device;
import gecaoji.Gecaoji;
import gecaoji.GecaojiMeg;
@@ -35,13 +36,15 @@
*/
public class MapRenderer {
// 瑙嗗浘鍙樻崲鍙傛暟
- private double scale = 1.0;
+ private static final double DEFAULT_SCALE = 20.0; // 榛樿缂╂斁姣斾緥
+ private double scale = DEFAULT_SCALE;
private double translateX = 0.0;
private double translateY = 0.0;
private Point lastDragPoint;
private static final double MIN_SCALE = 0.05d;
private static final double MAX_SCALE = 50.0d;
private static final double SCALE_EPSILON = 1e-6d;
+ private static final String MAP_SCALE_PROPERTY = "mapScale"; // 灞炴�ф枃浠朵腑鐨勯敭鍚�
// 涓婚棰滆壊
private final Color THEME_COLOR = new Color(46, 139, 87);
@@ -111,6 +114,40 @@
this.mowerUpdateTimer = createMowerTimer();
this.mowerInfoManager = new GecaojiMeg(visualizationPanel, mower);
setupMouseListeners();
+ // 浠庨厤缃枃浠惰鍙栦笂娆′繚瀛樼殑缂╂斁姣斾緥
+ loadScaleFromProperties();
+ }
+
+ /**
+ * 浠庨厤缃枃浠惰鍙栫缉鏀炬瘮渚�
+ */
+ private void loadScaleFromProperties() {
+ String scaleValue = Setsys.getPropertyValue(MAP_SCALE_PROPERTY);
+ if (scaleValue != null && !scaleValue.trim().isEmpty()) {
+ try {
+ double savedScale = Double.parseDouble(scaleValue.trim());
+ // 楠岃瘉缂╂斁姣斾緥鏄惁鍦ㄦ湁鏁堣寖鍥村唴
+ if (savedScale >= MIN_SCALE && savedScale <= MAX_SCALE) {
+ scale = savedScale;
+ } else {
+ scale = DEFAULT_SCALE;
+ }
+ } catch (NumberFormatException e) {
+ // 濡傛灉瑙f瀽澶辫触锛屼娇鐢ㄩ粯璁ゅ��
+ scale = DEFAULT_SCALE;
+ }
+ } else {
+ // 濡傛灉娌℃湁淇濆瓨鐨勫�硷紝浣跨敤榛樿鍊�
+ scale = DEFAULT_SCALE;
+ }
+ }
+
+ /**
+ * 淇濆瓨缂╂斁姣斾緥鍒伴厤缃枃浠�
+ */
+ private void saveScaleToProperties() {
+ Setsys setsys = new Setsys();
+ setsys.updateProperty(MAP_SCALE_PROPERTY, String.valueOf(scale));
}
/**
@@ -218,6 +255,8 @@
translateX += (newWorldX - worldX);
translateY += (newWorldY - worldY);
+ // 淇濆瓨缂╂斁姣斾緥鍒伴厤缃枃浠�
+ saveScaleToProperties();
visualizationPanel.repaint();
}
@@ -253,9 +292,11 @@
* 閲嶇疆瑙嗗浘
*/
public void resetView() {
- scale = 1.0;
+ scale = DEFAULT_SCALE;
translateX = 0.0;
translateY = 0.0;
+ // 淇濆瓨缂╂斁姣斾緥鍒伴厤缃枃浠�
+ saveScaleToProperties();
visualizationPanel.repaint();
}
@@ -482,7 +523,8 @@
if (device == null) {
return;
}
- if (!isHighPrecisionFix(device.getPositioningStatus())) {
+ // 浣跨敤鏇村鏉剧殑瀹氫綅鐘舵�佸垽鏂紝鍏佽鐘舵��1鍜�4鏄剧ず鎷栧熬
+ if (!isValidFixForTrail(device.getPositioningStatus())) {
return;
}
@@ -504,6 +546,56 @@
idleMowerTrail.addLast(new tuowei.TrailSample(now, new Point2D.Double(position.x, position.y)));
pruneIdleMowerTrail(now);
}
+
+ /**
+ * 寮哄埗鏇存柊鎷栧熬锛堢敤浜庢敹鍒�$GNGGA鏁版嵁鏃剁珛鍗虫洿鏂帮級
+ * 杩欎釜鏂规硶浼氬埛鏂癿ower浣嶇疆骞剁珛鍗虫坊鍔犲埌鎷栧熬
+ */
+ public void forceUpdateIdleMowerTrail() {
+ long now = System.currentTimeMillis();
+ pruneIdleMowerTrail(now);
+
+ if (idleTrailSuppressed || realtimeTrackRecording) {
+ if (!idleMowerTrail.isEmpty()) {
+ clearIdleMowerTrail();
+ }
+ return;
+ }
+
+ Device device = Device.getGecaoji();
+ if (device == null) {
+ return;
+ }
+ // 浣跨敤鏇村鏉剧殑瀹氫綅鐘舵�佸垽鏂紝鍏佽鐘舵��1鍜�4鏄剧ず鎷栧熬
+ if (!isValidFixForTrail(device.getPositioningStatus())) {
+ return;
+ }
+
+ // 鍒锋柊mower浣嶇疆锛屼娇鐢ㄦ渶鏂扮殑Device鏁版嵁
+ mower.refreshFromDevice();
+ Point2D.Double position = mower.getPosition();
+ if (position == null || !Double.isFinite(position.x) || !Double.isFinite(position.y)) {
+ return;
+ }
+
+ tuowei.TrailSample lastSample = idleMowerTrail.peekLast();
+ if (lastSample != null) {
+ Point2D.Double lastPoint = lastSample.getPoint();
+ double dx = position.x - lastPoint.x;
+ double dy = position.y - lastPoint.y;
+ if (Math.hypot(dx, dy) < IDLE_TRAIL_SAMPLE_DISTANCE_METERS) {
+ return;
+ }
+ }
+
+ idleMowerTrail.addLast(new tuowei.TrailSample(now, new Point2D.Double(position.x, position.y)));
+ pruneIdleMowerTrail(now);
+
+ // 绔嬪嵆閲嶇粯锛岀‘淇濇嫋灏惧強鏃舵樉绀�
+ if (visualizationPanel != null) {
+ visualizationPanel.repaint();
+ }
+ }
private void pruneIdleMowerTrail(long now) {
if (idleMowerTrail.isEmpty()) {
@@ -1233,6 +1325,31 @@
return false;
}
}
+
+ /**
+ * 鍒ゆ柇瀹氫綅鐘舵�佹槸鍚︽湁鏁堬紝鍙敤浜庢樉绀烘嫋灏�
+ * 鎺ュ彈鐘舵��1锛堝崟鐐瑰畾浣嶏級鍜�4锛堝浐瀹氳В锛�
+ */
+ private boolean isValidFixForTrail(String fixQuality) {
+ if (fixQuality == null) {
+ return false;
+ }
+ String trimmed = fixQuality.trim();
+ if (trimmed.isEmpty()) {
+ return false;
+ }
+ // 鎺ュ彈鐘舵��1锛堝崟鐐瑰畾浣嶏級鍜�4锛堝浐瀹氳В锛�
+ if ("1".equals(trimmed) || "4".equals(trimmed)) {
+ return true;
+ }
+ try {
+ double value = Double.parseDouble(trimmed);
+ // 鎺ュ彈1.0鎴�4.0
+ return Math.abs(value - 1.0d) < 1e-6 || Math.abs(value - 4.0d) < 1e-6;
+ } catch (NumberFormatException ex) {
+ return false;
+ }
+ }
private boolean isPointInsideActiveBoundary(Point2D.Double point) {
if (point == null || !Double.isFinite(point.x) || !Double.isFinite(point.y)) {
@@ -1339,7 +1456,15 @@
* 璁剧疆瑙嗗浘鍙樻崲鍙傛暟锛堢敤浜庣▼搴忓寲鎺у埗锛�
*/
public void setViewTransform(double scale, double translateX, double translateY) {
- this.scale = scale;
+ // 闄愬埗缂╂斁鑼冨洿
+ scale = Math.max(MIN_SCALE, Math.min(scale, MAX_SCALE));
+ // 濡傛灉缂╂斁姣斾緥鏀瑰彉浜嗭紝淇濆瓨鍒伴厤缃枃浠�
+ if (Math.abs(this.scale - scale) > SCALE_EPSILON) {
+ this.scale = scale;
+ saveScaleToProperties();
+ } else {
+ this.scale = scale;
+ }
this.translateX = translateX;
this.translateY = translateY;
visualizationPanel.repaint();
--
Gitblit v1.10.0