826220679@qq.com
5 天以前 352da282b6c21700eb454407b92cabcf169a448e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package home;
 
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
 
public class ConfigPanel extends JPanel {
    private JTextField deviceIdField;
    private JTextField groupField;
    private JTextField frequencyField;
    private JButton readConfigBtn;
    private JButton saveConfigBtn;
    private JTextField filePathField;
    private JButton selectFileBtn;
    private JButton upgradeBtn;
    private JProgressBar progressBar;
    private SerialPortService serialService;
    private FirmwareUpgrader firmwareUpgrader;
    private AOAFollowSystem parentFrame;
    
    // ÓÃÓÚ½çÃæÎı¾µÄ±êÇ©
    private JLabel deviceIdLabel;
    private JLabel groupLabel;
    private JLabel frequencyLabel;
    private JLabel selectFileLabel;
    
    public ConfigPanel(SerialPortService serialService, AOAFollowSystem parentFrame) {
        this.serialService = serialService;
        this.parentFrame = parentFrame;
        this.firmwareUpgrader = new FirmwareUpgrader(serialService);
        setLayout(new GridBagLayout());
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        
        // É豸±àºÅ - Ê¹ÓÃgetString()·½·¨
        gbc.gridx = 0;
        gbc.gridy = 0;
        deviceIdLabel = new JLabel(getString("device_id") + ":");
        add(deviceIdLabel, gbc);
        
        gbc.gridx = 1;
        deviceIdField = new JTextField(15);
        deviceIdField.setText("2548");
        add(deviceIdField, gbc);
        
        // Í¨ÐÅС×é - Ê¹ÓÃgetString()·½·¨
        gbc.gridx = 0;
        gbc.gridy = 1;
        groupLabel = new JLabel(getString("group") + ":");
        add(groupLabel, gbc);
        
        gbc.gridx = 1;
        groupField = new JTextField(15);
        groupField.setText("2");
        add(groupField, gbc);
        
        // Í¨ÐÅÆµÂÊ - Ê¹ÓÃgetString()·½·¨
        gbc.gridx = 0;
        gbc.gridy = 2;
        frequencyLabel = new JLabel(getString("frequency") + ":");
        add(frequencyLabel, gbc);
        
        gbc.gridx = 1;
        frequencyField = new JTextField(15);
        frequencyField.setText("1");
        add(frequencyField, gbc);
        
        // Ñ¡ÔñÎļþ - Ê¹ÓÃgetString()·½·¨
        gbc.gridx = 0;
        gbc.gridy = 3;
        selectFileLabel = new JLabel(getString("select_file") + ":");
        add(selectFileLabel, gbc);
        
        gbc.gridx = 1;
        JPanel filePanel = new JPanel(new BorderLayout(5, 0));
        filePathField = new JTextField();
        filePathField.setEditable(false);
        selectFileBtn = new JButton("...");
        selectFileBtn.setPreferredSize(new Dimension(30, 25));
        selectFileBtn.addActionListener(e -> selectFile());
        filePanel.add(filePathField, BorderLayout.CENTER);
        filePanel.add(selectFileBtn, BorderLayout.EAST);
        add(filePanel, gbc);
        
        // ¹Ì¼þÉý¼¶
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 2;
        upgradeBtn = createColoredButton(getString("upgrade"), new Color(70, 130, 180));
        upgradeBtn.setPreferredSize(new Dimension(100, 30));
        upgradeBtn.addActionListener(e -> upgradeFirmware());
        add(upgradeBtn, gbc);
        
        // ½ø¶ÈÌõ
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.gridwidth = 2;
        progressBar = new JProgressBar(0, 100);
        progressBar.setStringPainted(true);
        progressBar.setVisible(false);
        add(progressBar, gbc);
        
        // ²Ù×÷°´Å¥
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.gridwidth = 2;
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        readConfigBtn = createColoredButton(getString("read_config"), new Color(70, 130, 180));
        readConfigBtn.setPreferredSize(new Dimension(100, 30));
        saveConfigBtn = createColoredButton(getString("save_config"), new Color(50, 205, 50));
        saveConfigBtn.setPreferredSize(new Dimension(100, 30));
        
        readConfigBtn.addActionListener(e -> readConfig());
        saveConfigBtn.addActionListener(e -> saveConfig());
        
        buttonPanel.add(readConfigBtn);
        buttonPanel.add(saveConfigBtn);
        
        add(buttonPanel, gbc);
    }
    
    private JButton createColoredButton(String text, Color color) {
        JButton button = new JButton(text);
        button.setBackground(color);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createEmptyBorder(8, 15, 8, 15));
        
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                button.setBackground(color.darker());
            }
            
            @Override
            public void mouseExited(MouseEvent e) {
                button.setBackground(color);
            }
        });
        
        return button;
    }
    
    private void selectFile() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("BIN Files", "bin"));
        int result = fileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            filePathField.setText(selectedFile.getAbsolutePath());
        }
    }
    
    private void upgradeFirmware() {
        String filePath = filePathField.getText();
        if (filePath.isEmpty()) {
            JOptionPane.showMessageDialog(this, getString("select_bin_file"), getString("error"), JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if (!serialService.isOpen()) {
            JOptionPane.showMessageDialog(this, getString("open_serial_first"), getString("error"), JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        // ½øÐÐÉý¼¶£¬ÏÔʾ½ø¶ÈÌõ
        upgradeBtn.setEnabled(false);
        selectFileBtn.setEnabled(false);
        progressBar.setVisible(true);
        progressBar.setValue(0);
        
        // ÔÚÐÂÏß³ÌÖÐÖ´ÐÐÉý¼¶²Ù×÷
        new Thread(() -> {
            try {
                firmwareUpgrader.upgradeFirmware(filePath, progress -> {
                    SwingUtilities.invokeLater(() -> {
                        progressBar.setValue(progress);
                    });
                });
                
                SwingUtilities.invokeLater(() -> {
                    JOptionPane.showMessageDialog(this, getString("upgrade_success"), getString("info"), JOptionPane.INFORMATION_MESSAGE);
                    upgradeBtn.setEnabled(true);
                    selectFileBtn.setEnabled(true);
                });
            } catch (Exception e) {
                SwingUtilities.invokeLater(() -> {
                    JOptionPane.showMessageDialog(this, getString("upgrade_failed") + ": " + e.getMessage(), getString("error"), JOptionPane.ERROR_MESSAGE);
                    upgradeBtn.setEnabled(true);
                    selectFileBtn.setEnabled(true);
                });
            }
        }).start();
    }
    
    private void readConfig() {
        // Ä£Äâ¶ÁÈ¡ÅäÖÃ
        readConfigBtn.setBackground(Color.GREEN);
        Timer timer = new Timer(500, e -> {
            readConfigBtn.setBackground(new Color(70, 130, 180));
        });
        timer.setRepeats(false);
        timer.start();
        
        JOptionPane.showMessageDialog(this, getString("config_read_success"), getString("info"), JOptionPane.INFORMATION_MESSAGE);
    }
    
    private void saveConfig() {
        // ÑéÖ¤ÊäÈë
        try {
            Integer.parseInt(deviceIdField.getText());
            Integer.parseInt(groupField.getText());
            Integer.parseInt(frequencyField.getText());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, getString("input_valid_number"), getString("error"), JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        // Ä£Äâ±£´æÅäÖÃ
        saveConfigBtn.setBackground(Color.GREEN);
        Timer timer = new Timer(500, e -> {
            saveConfigBtn.setBackground(new Color(50, 205, 50));
        });
        timer.setRepeats(false);
        timer.start();
        
        JOptionPane.showMessageDialog(this, getString("config_save_success"), getString("info"), JOptionPane.INFORMATION_MESSAGE);
    }
    
    public void updateLanguage() {
        // ¸üа´Å¥Îı¾
        readConfigBtn.setText(getString("read_config"));
        saveConfigBtn.setText(getString("save_config"));
        upgradeBtn.setText(getString("upgrade"));
        
        // ¸üбêÇ©Îı¾
        deviceIdLabel.setText(getString("device_id") + ":");
        groupLabel.setText(getString("group") + ":");
        frequencyLabel.setText(getString("frequency") + ":");
        selectFileLabel.setText(getString("select_file") + ":");
    }
    
    private String getString(String key) {
        return parentFrame.getString(key);
    }
}