From 8d662de2fd262b3a485f16e197cb4d0ca2a61cdf Mon Sep 17 00:00:00 2001
From: zsh_root <979909237@qq.com>
Date: 星期三, 10 十二月 2025 17:03:47 +0800
Subject: [PATCH] 发布版V1.0

---
 src/PublicPannel/UpgradePanel.java |  689 ++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 606 insertions(+), 83 deletions(-)

diff --git a/src/PublicPannel/UpgradePanel.java b/src/PublicPannel/UpgradePanel.java
index 29d6f52..0541a2a 100644
--- a/src/PublicPannel/UpgradePanel.java
+++ b/src/PublicPannel/UpgradePanel.java
@@ -1,155 +1,678 @@
 package PublicPannel;
+
 import javax.swing.*;
-import home.ButtonUtils;
-import home.MainFrame;
+
+import Ymodem.YModem;
+import home.*;
+import jiexi.DellTag55AA03;
+
 import java.awt.*;
 import java.awt.event.ActionListener;
+import java.io.*;
+import java.net.Socket;
 
 public class UpgradePanel extends JPanel {
     /**
-	 * 
-	 */
-	private static final long serialVersionUID = 1L;
-	private MainFrame mainFrame;
+     *
+     */
+    private static final long serialVersionUID = 1L;
+    private MainFrame mainFrame;
     private JButton upgradeBtn;
     private JTextField filePathField;
     private JButton selectBinBtn;
     private JProgressBar progressBar;
-    private JLabel selectFileLabel; // 新增:用于文件选择标签
-    private JLabel progressLabel;   // 新增:用于进度标签
-    
+    private JLabel selectFileLabel;
+    private JLabel progressLabel;
+
+    // 鏂板锛氶�氫俊鏂瑰紡閫夋嫨
+    private JComboBox<String> communicationTypeCombo;
+    private JLabel communicationTypeLabel;
+
+    // 涓插彛鏈嶅姟瀹炰緥
+    private SerialPortService serialService;
+    // 缃戠粶閫氫俊闈㈡澘寮曠敤
+    private NetworkCommunicationPanel networkPanel;
+
+    // 鏂板锛氬崌绾х浉鍏崇姸鎬�
+    private volatile boolean upgradeInProgress = false;
+    private volatile boolean cancelUpgrade = false;
+
     public UpgradePanel(MainFrame mainFrame) {
         this.mainFrame = mainFrame;
+        this.serialService = SerialCommunicationPanel.getSerialService();
         initializeUI();
     }
-    
+
     private void initializeUI() {
         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
-        
-        // 内容面板 - 使用垂直布局,与BaseParameterPanel一致
+
+        // 鍐呭闈㈡澘
         JPanel contentPanel = new JPanel();
         contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
-        
-        // 文件选择面板
+
+        // 閫氫俊鏂瑰紡閫夋嫨闈㈡澘
+        JPanel communicationPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
+        communicationTypeLabel = new JLabel(mainFrame.getString("communication.type"));
+        communicationTypeCombo = new JComboBox<>(new String[]{
+                mainFrame.getString("serial"),
+                mainFrame.getString("tcp.client"),
+                mainFrame.getString("tcp.server"),
+                mainFrame.getString("udp")
+        });
+        communicationPanel.add(communicationTypeLabel);
+        communicationPanel.add(communicationTypeCombo);
+
+        // 鏂囦欢閫夋嫨闈㈡澘
         JPanel filePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
-        
         filePathField = new JTextField();
         filePathField.setEditable(false);
-        filePathField.setPreferredSize(new Dimension(200, 25)); // 设置文件路径宽度
-        
+        filePathField.setPreferredSize(new Dimension(200, 25));
+
         selectBinBtn = ButtonUtils.createBlueButton(mainFrame.getString("select.bin.file"));
         selectBinBtn.addActionListener(e -> selectBinFile());
-        
-        // 使用变量引用标签,以便后续更新
+
         selectFileLabel = new JLabel(mainFrame.getString("select.bin.file"));
-        
+
         filePanel.add(selectFileLabel);
         filePanel.add(filePathField);
         filePanel.add(selectBinBtn);
-        
-        // 进度条和升级按钮面板 - 放在同一行
+
+        // 杩涘害鏉″拰鍗囩骇鎸夐挳闈㈡澘
         JPanel progressButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
-        
-        // 进度条
         progressBar = new JProgressBar(0, 100);
         progressBar.setStringPainted(true);
         progressBar.setVisible(false);
-        progressBar.setPreferredSize(new Dimension(200, 25)); // 设置进度条宽度
-        
-        // 升级按钮
+        progressBar.setPreferredSize(new Dimension(200, 25));
+
         upgradeBtn = ButtonUtils.createBlueButton(mainFrame.getString("start.upgrade"));
         upgradeBtn.addActionListener(e -> startUpgrade());
-        
-        // 使用变量引用进度标签,以便后续更新
-        progressLabel = new JLabel(mainFrame.getString("upgrade.progress")); // 需要在语言资源文件中添加"upgrade.progress"键
-        
+
+        progressLabel = new JLabel(mainFrame.getString("upgrade.progress"));
+
         progressButtonPanel.add(progressLabel);
         progressButtonPanel.add(progressBar);
         progressButtonPanel.add(upgradeBtn);
-        
-        // 设置固定高度
+
+        // 璁剧疆鍥哄畾楂樺害
+        communicationPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
         filePanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
         progressButtonPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
-        
-        // 将所有组件添加到内容面板
-        contentPanel.add(Box.createVerticalStrut(10)); // 顶部10像素间距
+
+        // 灏嗘墍鏈夌粍浠舵坊鍔犲埌鍐呭闈㈡澘
+        contentPanel.add(Box.createVerticalStrut(10));
+        contentPanel.add(communicationPanel);
         contentPanel.add(filePanel);
         contentPanel.add(progressButtonPanel);
-        
-        // 添加内容面板,不添加滚动面板
+
         add(contentPanel);
     }
-    
+
     private void selectBinFile() {
         JFileChooser fileChooser = new JFileChooser();
         fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
         fileChooser.setDialogTitle(mainFrame.getString("select.bin.file"));
         fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("BIN files (*.bin)", "bin"));
-        
+
         int result = fileChooser.showOpenDialog(this);
         if (result == JFileChooser.APPROVE_OPTION) {
-            // 将选择的文件路径显示在文件路径框中
             String selectedFile = fileChooser.getSelectedFile().getAbsolutePath();
             filePathField.setText(selectedFile);
-            
-            JOptionPane.showMessageDialog(this, 
-                mainFrame.getString("selected.file") + ": " + selectedFile,
-                mainFrame.getString("file.selected"),
-                JOptionPane.INFORMATION_MESSAGE);
+
+            JOptionPane.showMessageDialog(this,
+                    mainFrame.getString("selected.file") + ": " + selectedFile,
+                    mainFrame.getString("file.selected"),
+                    JOptionPane.INFORMATION_MESSAGE);
         }
     }
-    
+
     private void startUpgrade() {
-        // 检查升级条件
+        // 妫�鏌ュ崌绾ф潯浠�
         if (filePathField.getText().isEmpty()) {
             JOptionPane.showMessageDialog(this,
-                mainFrame.getString("please.select.file.first"),
-                mainFrame.getString("warning"),
-                JOptionPane.WARNING_MESSAGE);
+                    mainFrame.getString("please.select.file.first"),
+                    mainFrame.getString("warning"),
+                    JOptionPane.WARNING_MESSAGE);
             return;
         }
-        
-        progressBar.setVisible(true);
+
+        // 妫�鏌ラ�氫俊杩炴帴鐘舵��
+        String communicationType = (String) communicationTypeCombo.getSelectedItem();
+        if (!checkCommunicationStatus(communicationType)) {
+            return;
+        }
+
+        // 瀵逛簬缃戠粶鍗囩骇锛屾鏌ユ槸鍚﹂�夋嫨浜嗗鎴风
+        if (!communicationType.equals(mainFrame.getString("serial"))) {
+            if (!checkSelectedClient()) {
+                return;
+            }
+        }
+
+        // 纭鍗囩骇
+        int result = JOptionPane.showConfirmDialog(this,
+                mainFrame.getString("upgrade.confirm.message"),
+                mainFrame.getString("confirm.upgrade"),
+                JOptionPane.YES_NO_OPTION,
+                JOptionPane.QUESTION_MESSAGE);
+
+        if (result != JOptionPane.YES_OPTION) {
+            return;
+        }
+
         upgradeBtn.setEnabled(false);
-        
-        // 模拟升级进度
-        Timer timer = new Timer(100, null);
-        ActionListener actionListener = new ActionListener() {
-            int progress = 0;
-            
-            @Override
-            public void actionPerformed(java.awt.event.ActionEvent e) {
-                progress += 5;
-                progressBar.setValue(progress);
-                
-                if (progress >= 100) {
-                    ((Timer) e.getSource()).stop();
-                    upgradeBtn.setEnabled(true);
-                    JOptionPane.showMessageDialog(UpgradePanel.this,
-                        mainFrame.getString("upgrade.completed"),
-                        mainFrame.getString("success"),
-                        JOptionPane.INFORMATION_MESSAGE);
+        selectBinBtn.setEnabled(false);
+        progressBar.setVisible(true);
+        progressBar.setValue(0);
+        upgradeInProgress = true;
+        cancelUpgrade = false;
+
+        String filePath = filePathField.getText();
+
+        new Thread(() -> {
+            int retryCount = 0;
+            final int maxRetries = 3;
+
+            try {
+                // 鍙戦�佸崌绾у懡浠�
+                boolean success = sendUpgradeCommand(communicationType);
+                if (!success) {
+                    throw new IOException("Failed to send upgrade command");
                 }
+
+                // 绛夊緟璁惧鍑嗗鍗囩骇
+                Thread.sleep(1000);
+
+                // 鎵ц鍗囩骇
+                if (communicationType.equals(mainFrame.getString("serial"))) {
+                    performSerialUpgrade(filePath);
+                } else {
+                    performNetworkUpgrade(filePath, communicationType);
+                }
+
+            } catch (Exception e) {
+                if (retryCount < maxRetries && !cancelUpgrade) {
+                    retryCount++;
+                    try {
+                        Thread.sleep(1000);
+                        // 鍙互鍦ㄨ繖閲屾坊鍔犻噸璇曢�昏緫
+                    } catch (InterruptedException ie) {
+                        Thread.currentThread().interrupt();
+                    }
+                } else {
+                    SwingUtilities.invokeLater(() -> {
+                        if (!cancelUpgrade) {
+                            JOptionPane.showMessageDialog(this,
+                                    "鍗囩骇澶辫触" + ": " + e.getMessage(),
+                                    "閿欒",
+                                    JOptionPane.ERROR_MESSAGE);
+                        }
+                        restoreUIAfterUpgrade();
+                    });
+                }
+            } finally {
+                restoreUIAfterUpgrade();
+            }
+        }).start();
+    }
+
+    /**
+     * 妫�鏌ラ�氫俊杩炴帴鐘舵��
+     */
+    private boolean checkCommunicationStatus(String communicationType) {
+        if (communicationType.equals(mainFrame.getString("serial"))) {
+            if (!serialService.isOpen()) {
+                JOptionPane.showMessageDialog(this,
+                        "璇峰厛鎵撳紑涓插彛杩炴帴",
+                        "閿欒",
+                        JOptionPane.ERROR_MESSAGE);
+                return false;
+            }
+        } else {
+            // 鑾峰彇缃戠粶閫氫俊闈㈡澘
+            if (networkPanel == null) {
+                networkPanel = mainFrame.getNetworkCommunicationPanel();
+            }
+            if (networkPanel == null || !networkPanel.isOpen()) {
+                JOptionPane.showMessageDialog(this,
+                        "璇峰厛鎵撳紑缃戠粶杩炴帴",
+                        "閿欒",
+                        JOptionPane.ERROR_MESSAGE);
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * 妫�鏌ユ槸鍚﹂�夋嫨浜嗗鎴风锛堥拡瀵圭綉缁滃崌绾э級
+     */
+    private boolean checkSelectedClient() {
+        if (networkPanel == null) {
+            networkPanel = mainFrame.getNetworkCommunicationPanel();
+        }
+
+        // 鑾峰彇褰撳墠閫変腑鐨勫鎴风
+        String selectedClient = networkPanel.getSelectedClient();
+        if (selectedClient == null || selectedClient.isEmpty()) {
+            JOptionPane.showMessageDialog(this,
+                    "璇峰厛閫夋嫨瑕佸崌绾х殑瀹㈡埛绔�",
+                    "閿欒",
+                    JOptionPane.ERROR_MESSAGE);
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * 鍙戦�佸崌绾у懡浠�
+     */
+    private boolean sendUpgradeCommand(String communicationType) {
+        byte[] upgradeCommand = DellTag55AA03.shengjizhiling();
+
+        if (communicationType.equals(mainFrame.getString("serial"))) {
+            // 涓插彛鍗囩骇鏈熼棿鍋滄鏁版嵁鎹曡幏
+            serialService.stopCapture();
+            return serialService.send(upgradeCommand);
+        } else {
+            // 缃戠粶鍗囩骇
+            return sendNetworkData(upgradeCommand, "鍙戦�佸崌绾ф寚浠�");
+        }
+    }
+
+    /**
+     * 鎵ц涓插彛鍗囩骇
+     */
+    private void performSerialUpgrade(String filePath) throws Exception {
+        InputStream inputStream = serialService.getInputStream();
+        OutputStream outputStream = serialService.getOutputStream();
+
+        if (inputStream == null || outputStream == null) {
+            throw new IOException("涓插彛涓嶅彲鐢�");
+        }
+
+        YModem ymodem = new YModem(inputStream, outputStream);
+        File file = new File(filePath);
+
+        YModem.ProgressCallback progressCallback = new YModem.ProgressCallback() {
+            @Override
+            public void onProgress(int currentBlock, int totalBlocks) {
+                int progress = (int) ((currentBlock * 100.0) / totalBlocks);
+                SwingUtilities.invokeLater(() -> {
+                    progressBar.setValue(progress);
+                });
             }
         };
-        
-        timer.addActionListener(actionListener);
-        timer.start();
+
+        ymodem.send(file.toPath(), progressCallback);
+
+        SwingUtilities.invokeLater(() -> {
+            JOptionPane.showMessageDialog(this, "鍗囩骇鎴愬姛", "鎴愬姛", JOptionPane.INFORMATION_MESSAGE);
+        });
     }
-    
+
+    /**
+     * 鎵ц缃戠粶鍗囩骇
+     */
+    private void performNetworkUpgrade(String filePath, String communicationType) throws Exception {
+        File file = new File(filePath);
+
+        // 鑾峰彇閫変腑鐨勫鎴风淇℃伅
+        String selectedClient = networkPanel.getSelectedClient();
+        if (selectedClient == null || selectedClient.isEmpty()) {
+            throw new IOException("鏈�夋嫨瀹㈡埛绔�");
+        }
+
+        // 瑙f瀽瀹㈡埛绔湴鍧�
+        String[] clientParts = selectedClient.split(":");
+        if (clientParts.length != 2) {
+            throw new IOException("瀹㈡埛绔湴鍧�鏍煎紡閿欒: " + selectedClient);
+        }
+
+        String clientIp = clientParts[0];
+        int clientPort;
+        try {
+            clientPort = Integer.parseInt(clientParts[1]);
+        } catch (NumberFormatException e) {
+            throw new IOException("瀹㈡埛绔鍙f牸寮忛敊璇�: " + clientParts[1]);
+        }
+
+        // 鍒涘缓缃戠粶杈撳叆杈撳嚭娴侀�傞厤鍣�
+        NetworkStreamAdapter streamAdapter = new NetworkStreamAdapter(networkPanel, communicationType, clientIp, clientPort);
+        InputStream inputStream = streamAdapter.getInputStream();
+        OutputStream outputStream = streamAdapter.getOutputStream();
+
+        if (inputStream == null || outputStream == null) {
+            throw new IOException("缃戠粶娴佷笉鍙敤");
+        }
+
+        YModem ymodem = new YModem(inputStream, outputStream);
+
+        YModem.ProgressCallback progressCallback = new YModem.ProgressCallback() {
+            @Override
+            public void onProgress(int currentBlock, int totalBlocks) {
+                int progress = (int) ((currentBlock * 100.0) / totalBlocks);
+                SwingUtilities.invokeLater(() -> {
+                    progressBar.setValue(progress);
+                });
+            }
+        };
+
+        try {
+            ymodem.send(file.toPath(), progressCallback);
+
+            if (!cancelUpgrade) {
+                SwingUtilities.invokeLater(() -> {
+                    JOptionPane.showMessageDialog(UpgradePanel.this,
+                            "缃戠粶鍗囩骇鎴愬姛", "鎴愬姛", JOptionPane.INFORMATION_MESSAGE);
+                });
+            }
+        } catch (Exception e) {
+            if (!cancelUpgrade) {
+                throw e;
+            }
+        } finally {
+            streamAdapter.close();
+        }
+    }
+
+    /**
+     * 鍙戦�佺綉缁滄暟鎹�
+     */
+    private boolean sendNetworkData(byte[] data, String description) {
+        if (networkPanel == null) {
+            networkPanel = mainFrame.getNetworkCommunicationPanel();
+        }
+
+        if (networkPanel != null) {
+            // 浣跨敤NetworkCommunicationPanel鐨勫彂閫佹柟娉�
+            return networkPanel.sendNetworkDataInternal(data, description);
+        }
+
+        return false;
+    }
+
+    /**
+     * 鎭㈠UI鐘舵��
+     */
+    private void restoreUIAfterUpgrade() {
+        SwingUtilities.invokeLater(() -> {
+            upgradeInProgress = false;
+            upgradeBtn.setEnabled(true);
+            selectBinBtn.setEnabled(true);
+            progressBar.setVisible(false);
+            progressBar.setValue(0);
+
+            // 閲嶆柊鍚姩涓插彛鏁版嵁鎹曡幏锛堝鏋滄槸涓插彛鍗囩骇锛�
+            if (serialService != null) {
+                serialService.startCapture();
+            }
+        });
+    }
+
+    /**
+     * 鍙栨秷鍗囩骇
+     */
+    public void cancelUpgrade() {
+        if (upgradeInProgress) {
+            cancelUpgrade = true;
+            upgradeInProgress = false;
+        }
+    }
+
+    /**
+     * 璁剧疆缃戠粶閫氫俊闈㈡澘寮曠敤
+     */
+    public void setNetworkCommunicationPanel(NetworkCommunicationPanel networkPanel) {
+        this.networkPanel = networkPanel;
+    }
+
     public void updateLanguage() {
-        // 更新按钮文本
+        // 鏇存柊閫氫俊鏂瑰紡閫夋嫨
+        communicationTypeLabel.setText(mainFrame.getString("communication.type"));
+        communicationTypeCombo.removeAllItems();
+        communicationTypeCombo.addItem(mainFrame.getString("serial"));
+        communicationTypeCombo.addItem(mainFrame.getString("tcp.client"));
+        communicationTypeCombo.addItem(mainFrame.getString("tcp.server"));
+        communicationTypeCombo.addItem(mainFrame.getString("udp"));
+
+        // 鏇存柊鎸夐挳鏂囨湰
         selectBinBtn.setText(mainFrame.getString("select.bin.file"));
         upgradeBtn.setText(mainFrame.getString("start.upgrade"));
-        
-        // 更新标签文本
+
+        // 鏇存柊鏍囩鏂囨湰
         selectFileLabel.setText(mainFrame.getString("select.bin.file"));
-        progressLabel.setText(mainFrame.getString("upgrade.progress")); // 需要在语言资源文件中添加"upgrade.progress"键
-        
-        // 更新文件选择器对话框标题
-        // 注意:这里无法直接更新已创建的JFileChooser,但下次打开时会使用新的文本
-        
+        progressLabel.setText(mainFrame.getString("upgrade.progress"));
+
         revalidate();
         repaint();
     }
+    /**
+     * 缃戠粶娴侀�傞厤鍣� - 鍐呴儴绫�
+     * 鐢ㄤ簬灏嗙綉缁滈�氫俊閫傞厤涓篩Modem鎵�闇�鐨処nputStream鍜孫utputStream
+     */
+    private class NetworkStreamAdapter {
+        private NetworkCommunicationPanel networkPanel;
+        private String communicationType;
+        private String clientIp;
+        private int clientPort;
+        private PipedInputStream inputStream;
+        private PipedOutputStream outputStream;
+        private boolean isConnected = false;
+
+        // 鐢ㄤ簬鏁版嵁鎺ユ敹鐨勭閬�
+        private PipedInputStream receiverInputStream;
+        private PipedOutputStream receiverOutputStream;
+
+        // 淇濆瓨鍘熷鐨勬暟鎹洃鍚櫒
+        private NetworkBase.NetworkDataListener originalDataListener;
+
+        public NetworkStreamAdapter(NetworkCommunicationPanel networkPanel, String communicationType,
+                                    String clientIp, int clientPort) {
+            this.networkPanel = networkPanel;
+            this.communicationType = communicationType;
+            this.clientIp = clientIp;
+            this.clientPort = clientPort;
+            initializeStreams();
+            setupNetworkDataListener();
+        }
+
+        private void initializeStreams() {
+            try {
+                // 鍒涘缓绠¢亾娴佺敤浜庢暟鎹彂閫�
+                inputStream = new PipedInputStream(8192); // 澧炲姞缂撳啿鍖哄ぇ灏�
+                outputStream = new PipedOutputStream(inputStream);
+
+                // 鍒涘缓绠¢亾娴佺敤浜庢暟鎹帴鏀�
+                receiverInputStream = new PipedInputStream(8192);
+                receiverOutputStream = new PipedOutputStream(receiverInputStream);
+
+                isConnected = true;
+                System.out.println("缃戠粶娴侀�傞厤鍣ㄥ垵濮嬪寲鎴愬姛");
+            } catch (Exception e) {
+                System.err.println("鍒濆鍖栫綉缁滄祦澶辫触: " + e.getMessage());
+                isConnected = false;
+                throw new RuntimeException("缃戠粶娴佸垵濮嬪寲澶辫触", e);
+            }
+        }
+
+        /**
+         * 璁剧疆缃戠粶鏁版嵁鐩戝惉鍣�
+         */
+        private void setupNetworkDataListener() {
+            try {
+                // 鑾峰彇缃戠粶閫氫俊闈㈡澘鐨勫綋鍓嶇綉缁滃疄渚�
+                NetworkBase currentNetwork = networkPanel.getCurrentNetwork();
+                if (currentNetwork == null) {
+                    System.err.println("缃戠粶杩炴帴鏈缓绔�");
+                    return;
+                }
+
+                // 淇濆瓨鍘熸潵鐨勬暟鎹洃鍚櫒
+                originalDataListener = currentNetwork.getDataListener();
+
+                // 鍒涘缓鍗囩骇涓撶敤鐨勬暟鎹洃鍚櫒
+                NetworkBase.NetworkDataListener upgradeDataListener = new NetworkBase.NetworkDataListener() {
+                    @Override
+                    public void onDataReceived(byte[] data, String fromAddress) {
+                        // 妫�鏌ユ暟鎹槸鍚︽潵鑷洰鏍囧鎴风
+                        String expectedAddress = clientIp + ":" + clientPort;
+                        if (expectedAddress.equals(fromAddress)) {
+                            try {
+                                // 灏嗘帴鏀跺埌鐨勬暟鎹啓鍏ユ帴鏀剁閬擄紝渚沋Modem璇诲彇
+                                if (receiverOutputStream != null && !cancelUpgrade) {
+                                    receiverOutputStream.write(data);
+                                    receiverOutputStream.flush();
+
+                                    // 璋冭瘯淇℃伅
+                                    System.out.println("鎺ユ敹鍒板崌绾у搷搴旀暟鎹紝闀垮害: " + data.length + "锛屾潵鑷�: " + fromAddress);
+                                }
+                            } catch (IOException e) {
+                                System.err.println("鍐欏叆鎺ユ敹绠¢亾澶辫触: " + e.getMessage());
+                                if (!cancelUpgrade) {
+                                    SwingUtilities.invokeLater(() -> {
+                                        JOptionPane.showMessageDialog(UpgradePanel.this,
+                                                "鍗囩骇鏁版嵁鎺ユ敹澶辫触: " + e.getMessage(),
+                                                "閿欒", JOptionPane.ERROR_MESSAGE);
+                                    });
+                                }
+                            }
+                        } else {
+                            // 闈炵洰鏍囧鎴风鐨勬暟鎹紝鍙互璁板綍鏃ュ織浣嗗拷鐣�
+                            System.out.println("蹇界暐闈炵洰鏍囧鎴风鏁版嵁锛屾潵鑷�: " + fromAddress + "锛屾湡鏈�: " + expectedAddress);
+                        }
+
+                        // 鍚屾椂璋冪敤鍘熸潵鐨勭洃鍚櫒锛屼繚鎸佸叾浠栧姛鑳芥甯�
+                        if (originalDataListener != null) {
+                            originalDataListener.onDataReceived(data, fromAddress);
+                        }
+                    }
+
+                    @Override
+                    public void onStatusChanged(String status, boolean isConnected) {
+                        // 浼犻�掔姸鎬佸彉鍖栫粰鍘熸潵鐨勭洃鍚櫒
+                        if (originalDataListener != null) {
+                            originalDataListener.onStatusChanged(status, isConnected);
+                        }
+
+                        // 濡傛灉杩炴帴鏂紑涓旀鍦ㄥ崌绾э紝闇�瑕佸鐞�
+                        if (!isConnected && upgradeInProgress) {
+                            System.err.println("缃戠粶杩炴帴鏂紑锛屽崌绾у彲鑳藉け璐�");
+                            if (!cancelUpgrade) {
+                                SwingUtilities.invokeLater(() -> {
+                                    JOptionPane.showMessageDialog(UpgradePanel.this,
+                                            "缃戠粶杩炴帴宸叉柇寮�锛屽崌绾уけ璐�",
+                                            "閿欒", JOptionPane.ERROR_MESSAGE);
+                                    cancelUpgrade = true;
+                                });
+                            }
+                        }
+                    }
+
+                    @Override
+                    public void onError(String errorMessage) {
+                        // 浼犻�掗敊璇粰鍘熸潵鐨勭洃鍚櫒
+                        if (originalDataListener != null) {
+                            originalDataListener.onError(errorMessage);
+                        }
+
+                        // 濡傛灉姝e湪鍗囩骇锛岄渶瑕佸鐞嗛敊璇�
+                        if (upgradeInProgress && !cancelUpgrade) {
+                            System.err.println("鍗囩骇杩囩▼涓彂鐢熺綉缁滈敊璇�: " + errorMessage);
+                            SwingUtilities.invokeLater(() -> {
+                                JOptionPane.showMessageDialog(UpgradePanel.this,
+                                        "缃戠粶閿欒: " + errorMessage,
+                                        "閿欒", JOptionPane.ERROR_MESSAGE);
+                            });
+                        }
+                    }
+                };
+
+                // 璁剧疆鏂扮殑鏁版嵁鐩戝惉鍣�
+                currentNetwork.setDataListener(upgradeDataListener);
+
+                System.out.println("鍗囩骇鏁版嵁鐩戝惉鍣ㄥ凡璁剧疆锛岀洰鏍囧鎴风: " + clientIp + ":" + clientPort);
+
+            } catch (Exception e) {
+                System.err.println("璁剧疆缃戠粶鏁版嵁鐩戝惉鍣ㄥけ璐�: " + e.getMessage());
+                throw new RuntimeException("缃戠粶鏁版嵁鐩戝惉鍣ㄥ垵濮嬪寲澶辫触", e);
+            }
+        }
+
+        public InputStream getInputStream() {
+            return receiverInputStream;
+        }
+
+        public OutputStream getOutputStream() {
+            return new NetworkOutputStream();
+        }
+
+        public void close() {
+            try {
+                // 鎭㈠鍘熷鐨勬暟鎹洃鍚櫒
+                if (networkPanel != null && networkPanel.getCurrentNetwork() != null && originalDataListener != null) {
+                    networkPanel.getCurrentNetwork().setDataListener(originalDataListener);
+                    System.out.println("宸叉仮澶嶅師濮嬫暟鎹洃鍚櫒");
+                }
+
+                if (inputStream != null) inputStream.close();
+                if (outputStream != null) outputStream.close();
+                if (receiverInputStream != null) receiverInputStream.close();
+                if (receiverOutputStream != null) receiverOutputStream.close();
+
+                System.out.println("缃戠粶娴侀�傞厤鍣ㄥ凡鍏抽棴");
+            } catch (IOException e) {
+                System.err.println("鍏抽棴缃戠粶娴佸け璐�: " + e.getMessage());
+            }
+        }
+
+        /**
+         * 缃戠粶杈撳嚭娴佸疄鐜�
+         * 灏哬Modem鍐欏叆鐨勬暟鎹�氳繃缃戠粶鍙戦�佸埌鎸囧畾瀹㈡埛绔�
+         */
+        private class NetworkOutputStream extends OutputStream {
+            @Override
+            public void write(int b) throws IOException {
+                write(new byte[]{(byte) b}, 0, 1);
+            }
+
+            @Override
+            public void write(byte[] b, int off, int len) throws IOException {
+                if (cancelUpgrade) {
+                    throw new IOException("鍗囩骇宸插彇娑�");
+                }
+
+                byte[] dataToSend = new byte[len];
+                System.arraycopy(b, off, dataToSend, 0, len);
+
+                boolean success = false;
+
+                if (communicationType.equals(mainFrame.getString("udp"))) {
+                    // UDP妯″紡鍙戦��
+                    success = networkPanel.sendUdpData(dataToSend, clientIp, clientPort, "鍗囩骇鏁版嵁");
+                } else if (communicationType.equals(mainFrame.getString("tcp.server"))) {
+                    // TCP Server妯″紡鍙戦�佸埌鎸囧畾瀹㈡埛绔�
+                    String clientKey = clientIp + ":" + clientPort;
+                    success = networkPanel.sendTcpDataToClient(clientKey, dataToSend, "鍗囩骇鏁版嵁");
+                }
+
+                if (!success) {
+                    throw new IOException("缃戠粶鍙戦�佸け璐�");
+                }
+
+                // 璋冭瘯淇℃伅
+                System.out.println("鍙戦�佸崌绾ф暟鎹紝闀垮害: " + len);
+            }
+
+            @Override
+            public void flush() throws IOException {
+                // 缃戠粶鍙戦�佺珛鍗崇敓鏁堬紝鏃犻渶鐗规畩澶勭悊
+            }
+
+            @Override
+            public void close() throws IOException {
+                // 涓嶅叧闂簳灞傜綉缁滆繛鎺�
+            }
+        }
+    }
+
 }
\ No newline at end of file

--
Gitblit v1.10.0