| | |
| | | import gecaoji.Device; |
| | | import gecaoji.Gecaoji; |
| | | import gecaoji.MowerBoundaryChecker; |
| | | import publicway.buttonset; |
| | | import set.Sets; |
| | | import set.debug; |
| | | import udpdell.UDPServer; |
| | | import zhangaiwu.AddDikuai; |
| | | import yaokong.Control04; |
| | | import yaokong.RemoteControlDialog; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Locale; |
| | | import java.util.Objects; |
| | | import java.util.concurrent.atomic.AtomicBoolean; |
| | | import java.util.function.Consumer; |
| | | import java.awt.geom.Point2D; |
| | | |
| | | import publicway.Gpstoxuzuobiao; |
| | | import publicway.Fanhuibutton; |
| | | |
| | | /** |
| | | * 首页界面 - 适配6.5寸竖屏,使用独立的MapRenderer进行绘制 |
| | | */ |
| | |
| | | // 地图渲染器 |
| | | private MapRenderer mapRenderer; |
| | | |
| | | private boolean pathPreviewActive; |
| | | |
| | | private final Consumer<String> serialLineListener = line -> { |
| | | SwingUtilities.invokeLater(() -> { |
| | | updateDataPacketCountLabel(); |
| | | // 如果收到$GNGGA数据,立即更新拖尾 |
| | | if (line != null && line.trim().startsWith("$GNGGA")) { |
| | | if (mapRenderer != null) { |
| | | if (mapRenderer != null && !pathPreviewActive) { |
| | | mapRenderer.forceUpdateIdleMowerTrail(); |
| | | } |
| | | } |
| | |
| | | private JPanel floatingButtonColumn; |
| | | private Runnable endDrawingCallback; |
| | | private JButton pathPreviewReturnButton; |
| | | private boolean pathPreviewActive; |
| | | private Runnable pathPreviewReturnAction; |
| | | private JButton settingsReturnButton; // 返回系统设置页面的悬浮按钮 |
| | | private JButton saveManualBoundaryButton; // 保存手动绘制边界的按钮 |
| | |
| | | private boolean storedStopButtonActive; |
| | | private String storedStatusBeforeDrawing; |
| | | private boolean handheldCaptureInlineUiActive; |
| | | private WangfanDraw returnPathDrawer; // 往返路径绘制管理器 |
| | | private Timer handheldCaptureStatusTimer; |
| | | private String handheldCaptureStoredStatusText; |
| | | private Color handheldStartButtonOriginalBackground; |
| | |
| | | // 初始化地图渲染器 |
| | | mapRenderer = new MapRenderer(visualizationPanel); |
| | | applyIdleTrailDurationFromSettings(); |
| | | |
| | | // 初始化往返路径绘制管理器 |
| | | returnPathDrawer = new WangfanDraw(this, mapRenderer, new WangfanDraw.DrawingHelper() { |
| | | @Override |
| | | public double[] resolveBaseLatLon() { |
| | | return resolveCircleBaseLatLon(); |
| | | } |
| | | |
| | | @Override |
| | | public Coordinate getLatestCoordinate() { |
| | | return Shouye.this.getLatestCoordinate(); |
| | | } |
| | | |
| | | @Override |
| | | public double parseDMToDecimal(String dmm, String direction) { |
| | | return Shouye.this.parseDMToDecimal(dmm, direction); |
| | | } |
| | | |
| | | @Override |
| | | public double[] convertLatLonToLocal(double lat, double lon, double baseLat, double baseLon) { |
| | | return Shouye.this.convertLatLonToLocal(lat, lon, baseLat, baseLon); |
| | | } |
| | | |
| | | @Override |
| | | public boolean arePointsClose(Point2D.Double a, Point2D.Double b) { |
| | | return Shouye.this.arePointsClose(a, b); |
| | | } |
| | | |
| | | @Override |
| | | public void enterDrawingControlMode() { |
| | | Shouye.this.enterDrawingControlMode(); |
| | | } |
| | | |
| | | @Override |
| | | public void exitDrawingControlMode() { |
| | | Shouye.this.exitDrawingControlMode(); |
| | | } |
| | | |
| | | @Override |
| | | public boolean isDrawingPaused() { |
| | | return drawingPaused; |
| | | } |
| | | }); |
| | | |
| | | // 设置 MapRenderer 的往返路径绘制管理器 |
| | | if (mapRenderer != null) { |
| | | mapRenderer.setReturnPathDrawer(returnPathDrawer); |
| | | } |
| | | |
| | | // 初始化对话框引用为null,延迟创建 |
| | | legendDialog = null; |
| | |
| | | private void showSettingsReturnButton() { |
| | | ensureFloatingButtonInfrastructure(); |
| | | if (settingsReturnButton == null) { |
| | | settingsReturnButton = createFloatingTextButton("返回"); |
| | | settingsReturnButton = Fanhuibutton.createReturnButton(null); |
| | | settingsReturnButton.setToolTipText("返回系统设置"); |
| | | settingsReturnButton.addActionListener(e -> { |
| | | // 关闭所有相关模式 |
| | |
| | | remoteBtn.addActionListener(e -> showRemoteControlDialog()); |
| | | areaSelectBtn.addActionListener(e -> { |
| | | // 点击“地块”直接打开地块管理对话框(若需要可传入特定地块编号) |
| | | // Dikuaiguanli.showDikuaiManagement(this, null); |
| | | // 直接进入地块管理界面 |
| | | Dikuaiguanli.showDikuaiManagement(this, null); |
| | | }); |
| | | baseStationBtn.addActionListener(e -> showBaseStationDialog()); |
| | |
| | | } |
| | | |
| | | private void handleDrawingStopFromControlPanel() { |
| | | if (endDrawingCallback != null) { |
| | | // 如果是往返路径绘制模式,调用完成绘制回调 |
| | | if (returnPathDrawer != null && returnPathDrawer.isActive()) { |
| | | returnPathDrawer.stop(); |
| | | returnPathDrawer.executeFinishCallback(); |
| | | } else if (endDrawingCallback != null) { |
| | | endDrawingCallback.run(); |
| | | } else { |
| | | addzhangaiwu.finishDrawingSession(); |
| | |
| | | updateStopButtonIcon(); |
| | | } |
| | | if (statusLabel != null) { |
| | | statusLabel.setText(storedStatusBeforeDrawing != null ? storedStatusBeforeDrawing : "待机"); |
| | | // 如果是往返路径绘制,退出时恢复为"待机" |
| | | if (returnPathDrawer != null && returnPathDrawer.isActive()) { |
| | | statusLabel.setText("待机"); |
| | | } else { |
| | | statusLabel.setText(storedStatusBeforeDrawing != null ? storedStatusBeforeDrawing : "待机"); |
| | | } |
| | | } |
| | | storedStatusBeforeDrawing = null; |
| | | } |
| | |
| | | startBtn.setText(drawingPaused ? "开始绘制" : "暂停绘制"); |
| | | } |
| | | if (stopBtn != null) { |
| | | stopBtn.setText("结束绘制"); |
| | | // 如果是往返路径绘制模式,显示"完成绘制",否则显示"结束绘制" |
| | | stopBtn.setText((returnPathDrawer != null && returnPathDrawer.isActive()) ? "完成绘制" : "结束绘制"); |
| | | } |
| | | } |
| | | |
| | |
| | | mapRenderer.setHandheldMowerIconActive(active); |
| | | } |
| | | |
| | | public void setStatusLabelText(String text) { |
| | | if (statusLabel != null) { |
| | | statusLabel.setText(text); |
| | | } |
| | | } |
| | | |
| | | public boolean startMowerBoundaryCapture() { |
| | | if (mapRenderer == null) { |
| | | return false; |
| | |
| | | if (drawingControlModeActive) { |
| | | updateDrawingControlButtonLabels(); |
| | | if (statusLabel != null) { |
| | | statusLabel.setText(paused ? "绘制暂停" : "绘制中"); |
| | | if (returnPathDrawer != null && returnPathDrawer.isActive()) { |
| | | statusLabel.setText("正在绘制往返路径"); |
| | | } else { |
| | | statusLabel.setText(paused ? "绘制暂停" : "绘制中"); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | enterDrawingControlMode(); |
| | | |
| | | // 隐藏返回设置按钮(如果显示绘制按钮,则不应该显示返回按钮) |
| | | if (settingsReturnButton != null) { |
| | | settingsReturnButton.setVisible(false); |
| | | } |
| | | // if (settingsReturnButton != null) { |
| | | // settingsReturnButton.setVisible(false); |
| | | // } |
| | | |
| | | // 显示"正在绘制边界"提示 |
| | | if (drawingBoundaryLabel != null) { |
| | | // 如果是往返路径绘制,不显示此标签(状态栏已显示"正在绘制往返路径") |
| | | // boolean isReturnPathDrawing = returnPathDrawer != null && returnPathDrawer.isActive(); |
| | | // drawingBoundaryLabel.setVisible(!isReturnPathDrawing); |
| | | drawingBoundaryLabel.setVisible(true); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | private double parseDMToDecimal(String dmm, String direction) { |
| | | if (dmm == null || dmm.trim().isEmpty()) { |
| | | return Double.NaN; |
| | | } |
| | | try { |
| | | String trimmed = dmm.trim(); |
| | | int dotIndex = trimmed.indexOf('.'); |
| | | if (dotIndex < 2) { |
| | | return Double.NaN; |
| | | } |
| | | int degrees = Integer.parseInt(trimmed.substring(0, dotIndex - 2)); |
| | | double minutes = Double.parseDouble(trimmed.substring(dotIndex - 2)); |
| | | double decimal = degrees + minutes / 60.0; |
| | | if ("S".equalsIgnoreCase(direction) || "W".equalsIgnoreCase(direction)) { |
| | | decimal = -decimal; |
| | | } |
| | | return decimal; |
| | | } catch (NumberFormatException ex) { |
| | | return Double.NaN; |
| | | } |
| | | return Gpstoxuzuobiao.parseDMToDecimal(dmm, direction); |
| | | } |
| | | |
| | | private double[] convertLatLonToLocal(double lat, double lon, double baseLat, double baseLon) { |
| | | double deltaLat = lat - baseLat; |
| | | double deltaLon = lon - baseLon; |
| | | double meanLatRad = Math.toRadians((baseLat + lat) / 2.0); |
| | | double eastMeters = deltaLon * METERS_PER_DEGREE_LAT * Math.cos(meanLatRad); |
| | | double northMeters = deltaLat * METERS_PER_DEGREE_LAT; |
| | | return new double[]{eastMeters, northMeters}; |
| | | return Gpstoxuzuobiao.convertLatLonToLocal(lat, lon, baseLat, baseLon); |
| | | } |
| | | |
| | | private CircleSolution fitCircleFromPoints(List<double[]> points) { |
| | |
| | | endDrawingButton.setVisible(false); |
| | | } |
| | | if (pathPreviewReturnButton == null) { |
| | | pathPreviewReturnButton = createFloatingTextButton("返回"); |
| | | pathPreviewReturnButton = Fanhuibutton.createReturnButton(e -> handlePathPreviewReturn()); |
| | | pathPreviewReturnButton.setToolTipText("返回新增地块步骤"); |
| | | pathPreviewReturnButton.addActionListener(e -> handlePathPreviewReturn()); |
| | | } |
| | | pathPreviewReturnButton.setVisible(true); |
| | | if (floatingButtonPanel != null) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 更新导航预览状态显示 |
| | | * @param active 是否处于导航预览模式 |
| | | */ |
| | | public void updateNavigationPreviewStatus(boolean active) { |
| | | setNavigationPreviewLabelVisible(active); |
| | | } |
| | | |
| | | /** |
| | | * 更新割草进度显示 |
| | | * @param percentage 完成百分比 |
| | | * @param completedArea 已完成面积(平方米) |
| | | * @param totalArea 总面积(平方米) |
| | | */ |
| | | public void updateMowingProgress(double percentage, double completedArea, double totalArea) { |
| | | if (mowingProgressLabel == null) { |
| | | return; |
| | | } |
| | | if (totalArea <= 0) { |
| | | mowingProgressLabel.setText("--%"); |
| | | mowingProgressLabel.setToolTipText("暂无地块面积数据"); |
| | | return; |
| | | } |
| | | double percent = Math.max(0.0, Math.min(100.0, percentage)); |
| | | mowingProgressLabel.setText(String.format(Locale.US, "%.1f%%", percent)); |
| | | mowingProgressLabel.setToolTipText(String.format(Locale.US, "%.1f㎡ / %.1f㎡", completedArea, totalArea)); |
| | | } |
| | | |
| | | /** |
| | | * 更新割草机速度显示 |
| | | * @param speed 速度值(单位:km/h) |
| | | */ |
| | | public void updateMowerSpeed(double speed) { |
| | | if (mowerSpeedValueLabel == null) { |
| | | return; |
| | | } |
| | | if (speed < 0) { |
| | | mowerSpeedValueLabel.setText("--"); |
| | | } else { |
| | | mowerSpeedValueLabel.setText(String.format(Locale.US, "%.1f", speed)); |
| | | } |
| | | if (mowerSpeedUnitLabel != null) { |
| | | mowerSpeedUnitLabel.setText("km/h"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取可视化面板实例 |
| | | */ |
| | | public JPanel getVisualizationPanel() { |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 启动往返路径绘制 |
| | | * @param finishCallback 完成绘制时的回调 |
| | | * @param isHandheld 是否使用手持设备模式 |
| | | * @return 是否成功启动 |
| | | */ |
| | | public boolean startReturnPathDrawing(Runnable finishCallback, boolean isHandheld) { |
| | | if (returnPathDrawer == null) { |
| | | return false; |
| | | } |
| | | return returnPathDrawer.start(finishCallback, isHandheld); |
| | | } |
| | | |
| | | /** |
| | | * 停止往返路径绘制 |
| | | */ |
| | | public void stopReturnPathDrawing() { |
| | | if (returnPathDrawer != null) { |
| | | returnPathDrawer.stop(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取往返路径绘制管理器 |
| | | */ |
| | | public WangfanDraw getReturnPathDrawer() { |
| | | return returnPathDrawer; |
| | | } |
| | | |
| | | /** |
| | | * 启动往返路径预览 |
| | | * @param coordinatesStr 路径坐标字符串 (x,y;x,y) |
| | | * @param returnCallback 返回回调 |
| | | */ |
| | | public void startReturnPathPreview(String coordinatesStr, Runnable returnCallback) { |
| | | if (returnPathDrawer == null || coordinatesStr == null || coordinatesStr.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | // 解析坐标 |
| | | List<Point2D.Double> points = new ArrayList<>(); |
| | | String[] pairs = coordinatesStr.split(";"); |
| | | for (String pair : pairs) { |
| | | String[] xy = pair.split(","); |
| | | if (xy.length == 2) { |
| | | try { |
| | | double x = Double.parseDouble(xy[0]); |
| | | double y = Double.parseDouble(xy[1]); |
| | | points.add(new Point2D.Double(x, y)); |
| | | } catch (NumberFormatException e) { |
| | | // 忽略无效坐标 |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (points.isEmpty()) { |
| | | JOptionPane.showMessageDialog(this, "没有有效的路径点可预览", "提示", JOptionPane.WARNING_MESSAGE); |
| | | return; |
| | | } |
| | | |
| | | // 设置预览点 |
| | | returnPathDrawer.setPoints(points); |
| | | if (mapRenderer != null) { |
| | | mapRenderer.setPreviewReturnPath(points); |
| | | } |
| | | |
| | | // 开启预览模式 |
| | | pathPreviewActive = true; |
| | | if (mapRenderer != null) { |
| | | mapRenderer.clearIdleTrail(); |
| | | } |
| | | pathPreviewReturnAction = returnCallback; |
| | | |
| | | // 确保悬浮按钮基础设施已创建 |
| | | ensureFloatingButtonInfrastructure(); |
| | | |
| | | // 创建或显示返回按钮 |
| | | if (pathPreviewReturnButton == null) { |
| | | // 使用 Fanhuibutton 创建返回按钮 |
| | | pathPreviewReturnButton = publicway.Fanhuibutton.createReturnButton(e -> { |
| | | // 停止预览 |
| | | stopReturnPathPreview(); |
| | | }); |
| | | pathPreviewReturnButton.setToolTipText("返回绘制页面"); |
| | | } |
| | | |
| | | // 隐藏其他悬浮按钮 |
| | | hideFloatingDrawingControls(); |
| | | |
| | | // 显示返回按钮 |
| | | pathPreviewReturnButton.setVisible(true); |
| | | floatingButtonPanel.setVisible(true); |
| | | if (floatingButtonPanel.getParent() != visualizationPanel) { |
| | | visualizationPanel.add(floatingButtonPanel, BorderLayout.SOUTH); |
| | | } |
| | | rebuildFloatingButtonColumn(); |
| | | |
| | | visualizationPanel.revalidate(); |
| | | visualizationPanel.repaint(); |
| | | } |
| | | |
| | | /** |
| | | * 停止往返路径预览 |
| | | */ |
| | | private void stopReturnPathPreview() { |
| | | pathPreviewActive = false; |
| | | |
| | | // 清空预览点 |
| | | if (returnPathDrawer != null) { |
| | | returnPathDrawer.clearPoints(); |
| | | } |
| | | if (mapRenderer != null) { |
| | | mapRenderer.setPreviewReturnPath(null); |
| | | } |
| | | |
| | | // 隐藏返回按钮 |
| | | if (pathPreviewReturnButton != null) { |
| | | pathPreviewReturnButton.setVisible(false); |
| | | } |
| | | |
| | | // 隐藏悬浮面板 |
| | | if (floatingButtonPanel != null) { |
| | | floatingButtonPanel.setVisible(false); |
| | | } |
| | | |
| | | // 执行返回回调 |
| | | if (pathPreviewReturnAction != null) { |
| | | pathPreviewReturnAction.run(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 显示边界预览(原始边界-紫色,优化后边界-绿色) |
| | | * @param dikuai 地块对象 |
| | | * @param optimizedBoundary 优化后的边界坐标字符串 |
| | | * @param returnCallback 返回回调 |
| | | */ |
| | | public static void showBoundaryPreview(dikuai.Dikuai dikuai, String optimizedBoundary, Runnable returnCallback) { |
| | | Shouye shouye = getInstance(); |
| | | if (shouye == null || shouye.mapRenderer == null || dikuai == null) { |
| | | return; |
| | | } |
| | | |
| | | // 获取原始边界XY坐标 |
| | | String originalBoundaryXY = dikuai.getBoundaryOriginalXY(); |
| | | |
| | | // 设置边界预览 |
| | | shouye.mapRenderer.setBoundaryPreview(originalBoundaryXY, optimizedBoundary); |
| | | |
| | | // 停止绘制割草机实时拖尾 |
| | | if (shouye.mapRenderer != null) { |
| | | shouye.mapRenderer.setIdleTrailSuppressed(true); |
| | | } |
| | | |
| | | // 设置返回回调 |
| | | shouye.pathPreviewReturnAction = returnCallback; |
| | | shouye.pathPreviewActive = true; |
| | | |
| | | // 确保悬浮按钮基础设施已创建 |
| | | shouye.ensureFloatingButtonInfrastructure(); |
| | | |
| | | // 创建或显示返回按钮 |
| | | if (shouye.pathPreviewReturnButton == null) { |
| | | shouye.pathPreviewReturnButton = publicway.Fanhuibutton.createReturnButton(e -> shouye.handleBoundaryPreviewReturn()); |
| | | shouye.pathPreviewReturnButton.setToolTipText("返回边界编辑页面"); |
| | | } |
| | | |
| | | // 隐藏其他悬浮按钮 |
| | | shouye.hideFloatingDrawingControls(); |
| | | |
| | | // 显示返回按钮 |
| | | shouye.pathPreviewReturnButton.setVisible(true); |
| | | if (shouye.floatingButtonPanel != null) { |
| | | shouye.floatingButtonPanel.setVisible(true); |
| | | if (shouye.floatingButtonPanel.getParent() != shouye.visualizationPanel) { |
| | | shouye.visualizationPanel.add(shouye.floatingButtonPanel, BorderLayout.SOUTH); |
| | | } |
| | | } |
| | | shouye.rebuildFloatingButtonColumn(); |
| | | |
| | | shouye.visualizationPanel.revalidate(); |
| | | shouye.visualizationPanel.repaint(); |
| | | } |
| | | |
| | | /** |
| | | * 处理边界预览返回 |
| | | */ |
| | | private void handleBoundaryPreviewReturn() { |
| | | Runnable callback = pathPreviewReturnAction; |
| | | exitBoundaryPreview(); |
| | | if (callback != null) { |
| | | callback.run(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 退出边界预览 |
| | | */ |
| | | private void exitBoundaryPreview() { |
| | | pathPreviewActive = false; |
| | | |
| | | // 恢复绘制割草机实时拖尾 |
| | | if (mapRenderer != null) { |
| | | mapRenderer.setIdleTrailSuppressed(false); |
| | | } |
| | | |
| | | // 清除边界预览 |
| | | if (mapRenderer != null) { |
| | | mapRenderer.clearBoundaryPreview(); |
| | | } |
| | | |
| | | // 隐藏返回按钮 |
| | | if (pathPreviewReturnButton != null) { |
| | | pathPreviewReturnButton.setVisible(false); |
| | | } |
| | | |
| | | // 隐藏悬浮面板 |
| | | if (floatingButtonPanel != null) { |
| | | floatingButtonPanel.setVisible(false); |
| | | } |
| | | |
| | | visualizationPanel.revalidate(); |
| | | visualizationPanel.repaint(); |
| | | } |
| | | |
| | | // 测试方法 |
| | | public static void main(String[] args) { |