zsh_root
2025-01-06 7857a444de69124e9f7fb45f98b0ae818b107f23
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
package index.JPanelMoudle;
 
import baowen.WriteReadAnchor;
import gnu.io.SerialPort;
import tools.ChuanKou.SerialPortUtil;
import tools.CustomProgressBar;
import tools.ShowMessage;
import ymodem.getFilesDataBytes;
import ymodem.YModemUpgradeThread;
 
import javax.swing.*;
import javax.swing.border.TitledBorder;
 
 
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
 
 
public class versionUpgradeComp {
    private static JPanel  versionUpdate;//版本升级面板
    private static JLabel label9;//空标签,进行占位
    private static JTextField readVersion_jf;//读取扫描器版本文本框
    private static JTextField chooseVersion_jf;//选择固件文本框
 
    private static JButton chooseVersion;//选择固件按钮
    private static JButton write_saoMiao;//写入扫描器按钮
    public static boolean write=false;//写入扫描器按钮
    public static boolean xinBiaoUp=false;//升级信标版本按钮
    private static JButton readVersion_jb;//读取版本按钮
    private static JButton  updateAnchor_jb;//升级信标版本按钮
 
    private static CustomProgressBar jindu_SaoMiaoQi;//写入扫描器进度条
    private static CustomProgressBar updateAnchor_jf;//升级信标版本进度条
 
    private static YModemUpgradeThread upgradeThread; // 保存线程引用
    private static Color color = new Color(6, 176, 37);; // 保存线程引用
 
    // 获取版本升级面板,若为空则创建
    public static JPanel getVersionUpdate() {
        if (versionUpdate == null) {
            versionUpdate = new JPanel();  // 初始化该面板
            versionUpdate.setBorder(new TitledBorder("固件升级"));
        }
        return versionUpdate;
    }
 
    // 获取空标签,若为空则创建
    public static JLabel getLabel9() {
        if (label9 == null) {
            label9 = new JLabel();  // 初始化该标签
        }
        return label9;
    }
 
 
 
    // 获取 选择固件 按钮,若为空则创建
    public static JButton getChooseVersion() {
        if (chooseVersion == null) {
            chooseVersion = new JButton("选择固件");  // 初始化该按钮
            chooseVersion.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // 打开文件选择对话框
                    String currentDirectory = System.getProperty("user.dir");
                    JFileChooser fileChooser = new JFileChooser();
                    // 设置文件选择对话框为可选择文件
                    fileChooser.setDialogTitle("选择固件");
                    fileChooser.setCurrentDirectory(new File(currentDirectory));
                    int result = fileChooser.showOpenDialog(readParamsComp.getReadAncParam_jp());
                    if (result == JFileChooser.APPROVE_OPTION) {
                        // 如果用户选择了文件
                        File selectedFile = fileChooser.getSelectedFile();
                        // 显示选择的文件路径
                        getChooseVersion_jf().setText(selectedFile.getPath());//文件名称);
                    }
                }
            });
        }
        return chooseVersion;
    }
 
    // 获取 写入扫描器 按钮,若为空则创建
    public static JButton getWrite_saoMiao() {
        if (write_saoMiao == null) {
            write_saoMiao = new JButton("写入扫描器");  // 初始化该按钮
            write_saoMiao.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(!write) {
                        String filePath = getChooseVersion_jf().getText();
                        if (!ComMoudleComp.isChuankou()) {
                            ShowMessage.zidingyi("串口未识别");
                            return;
                        }
                        if (filePath == null || filePath.length() < 10) {
                            ShowMessage.zidingyi("固件不正确");
                            return;
                        }
                        SerialPort serialPort = ComMoudleComp.getSerialPort();
                        write = true;
                        byte[] bytes = WriteReadAnchor.write_lora((byte) 0x10, (byte) 0x2, 1);
                        YModemUpgradeThread.shengji(serialPort,filePath,bytes);
                        write_saoMiao.setText("取消写入");
                        jindu_SaoMiaoQi.setStringPainted(true); // 显示百分比文字
                    }else {
                       YModemUpgradeThread.setSendDataFlag(true);
                        exitUpgrade();
                    }
                }
            });
        }
        return write_saoMiao;
    }
 
 
 
    // 获取 读取扫描器版本 按钮,若为空则创建
    public static JButton getReadVersion_jb() {
        if (readVersion_jb == null) {
            readVersion_jb = new JButton("读取扫描器版本");  // 初始化该按钮
            readVersion_jb.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
 
                        if (!ComMoudleComp.isChuankou()) {
                            ShowMessage.zidingyi("串口未识别");
                            return;
                        }
                        ShowMessage.zidingyi("读取扫描器版本指令发送成功");
                        byte[] byt = WriteReadAnchor.ReadData((byte) 0x2, (byte) 0x50);
                        WriteReadAnchor.sendData(ComMoudleComp.getSerialPort(), byt);
                }
 
            });
        }
        return readVersion_jb;
    }
 
    // 获取 升级信标版本 按钮,若为空则创建
    public static JButton getUpdateAnchor_jb() {
        if (updateAnchor_jb == null) {
            updateAnchor_jb = new JButton("升级信标版本");  // 初始化该按钮
            updateAnchor_jb.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!xinBiaoUp) {
 
                        if (!ComMoudleComp.isChuankou()) {
                            ShowMessage.zidingyi("串口未识别");
                            return;
                        }
                        String text = readParamsComp.getAnchorId_jf().getText();
                        if (text.length() != 4) {
                            ShowMessage.zidingyi("信标ID不正确,请先读取信标参数");
                            return;
                        }
                        SerialPort serialPort = ComMoudleComp.getSerialPort();
                        xinBiaoUp=true;
                        updateAnchor_jb.setText("退出信标升级");
                        WriteReadAnchor.updateLora("0", (byte) 0xAA,(byte)  0x06, text,serialPort,(byte) 0x02);
                    }else {
                        exitUpgradeXinBiao();
                    }
                }
            });
        }
        return updateAnchor_jb;
    }
 
 
    // 获取 读取扫描器版本 文本框,若为空则创建
    public static JTextField getReadVersion_jf() {
        if (readVersion_jf == null) {
            readVersion_jf = new JTextField();  // 初始化该文本框
        }
        return readVersion_jf;
    }
 
    // 获取 选择固件 文本框,若为空则创建
    public static JTextField getChooseVersion_jf() {
        if (chooseVersion_jf == null) {
            chooseVersion_jf = new JTextField();  // 初始化该文本框
            chooseVersion_jf.setEnabled(false);
        }
        return chooseVersion_jf;
    }
 
    // 获取 写入扫描器 进度条,若为空则创建
    public static CustomProgressBar  getJindu_SaoMiaoQi() {
        if (jindu_SaoMiaoQi == null) {
            jindu_SaoMiaoQi = new CustomProgressBar();  // 初始化该进度条
            jindu_SaoMiaoQi.setForeground(color); // 设置进度条前景色为绿色
            // 自定义UI来修改文本颜色
        }
        return jindu_SaoMiaoQi;
    }
 
 
    // 获取 升级信标版本 进度条,若为空则创建
    public static CustomProgressBar getUpdateAnchor_jf() {
        if (updateAnchor_jf == null) {
            updateAnchor_jf = new CustomProgressBar();  // 初始化该进度条
            updateAnchor_jf.setForeground(color); // 设置进度条前景色为绿色
        }
        return updateAnchor_jf;
    }
 
 
    public static boolean isWrite() {
        return write;
    }
 
    public static void exitUpgrade() {
        write=false;
        write_saoMiao.setText("写入扫描器");
        jindu_SaoMiaoQi.setValue(0); // 显示百分比文字
    }
 
 
    public static void exitUpgradeXinBiao() {
        xinBiaoUp=false;
        updateAnchor_jb.setText("升级信标版本");
        updateAnchor_jf.setValue(0); // 显示百分比文字
    }
}