张世豪
3 小时以前 d22349714c8d199c02f336f90fba841ef8f5cd39
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package home;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import chuankou.SerialPortConnectionDialog;
import chushihua.Chushihua;
import chushihua.SlotManager;
import chushihua.lunxun;
 
public class Homein {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                // 设置系统外观
                UIManager.setLookAndFeel(UIManager.getLookAndFeel());
                
                // 执行系统初始化流程
                boolean initialized = initializeSystem();
                
                if (initialized) {
                    // 系统初始化成功,继续后续流程
                    startApplication();
                } else {
                    // 系统初始化失败,退出程序
                    JOptionPane.showMessageDialog(null, 
                        "系统初始化失败,无法启动应用程序", 
                        "错误", 
                        JOptionPane.ERROR_MESSAGE);
                    System.exit(1);
                }
                
            } catch (Exception e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(null, 
                    "程序启动失败: " + e.getMessage(), 
                    "错误", 
                    JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }
        });        
    }
    
    /**
     * 系统初始化流程
     * @return 初始化成功返回true,失败返回false
     */
    private static boolean initializeSystem() {
        try {
            //System.out.println("开始系统初始化流程...");
            
            // 1. 运行系统初始化类 Chushihua
            //System.out.println("步骤1: 初始化系统配置...");
            if (!initializeChushihua()) {
                System.err.println("系统配置初始化失败");
                return false;
            }
            
            // 2. 初始化 SlotManager 类
            //System.out.println("步骤2: 初始化卡槽管理器...");
            if (!initializeSlotManager()) {
                System.err.println("卡槽管理器初始化失败");
                return false;
            }
            
            //System.out.println("系统初始化完成");
            return true;
            
        } catch (Exception e) {
            System.err.println("系统初始化过程中发生异常: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 初始化系统配置
     */
    private static boolean initializeChushihua() {
        try {
            Chushihua configSystem = Chushihua.getInstance();
            boolean success = configSystem.initialize();
            
            if (success) {
                //System.out.println("✓ 系统配置初始化成功");
                //System.out.println("  设备编号: " + configSystem.getMachineConfig().getMachineId());
                //System.out.println("  卡槽总数: " + configSystem.getMachineConfig().getTotalSlots());
                //System.out.println("  轮询间隔: " + configSystem.getMachineConfig().getPollingInterval() + "ms");
                //System.out.println("  波特率: " + configSystem.getMachineConfig().getBaudrate());
            } else {
                System.err.println("✗ 系统配置初始化失败");
            }
            
            return success;
            
        } catch (Exception e) {
            System.err.println("初始化系统配置时发生异常: " + e.getMessage());
            return false;
        }
    }
    
    /**
     * 初始化卡槽管理器
     */
    private static boolean initializeSlotManager() {
        try {
            // SlotManager 会在构造函数中自动初始化所有卡槽
            new SlotManager();
            //System.out.println("✓ 卡槽管理器初始化成功");
            //System.out.println("  总卡槽数: " + slotManager.getTotalSlots());
            
            return true;
            
        } catch (Exception e) {
            System.err.println("初始化卡槽管理器时发生异常: " + e.getMessage());
            return false;
        }
    }
    
    /**
     * 启动应用程序主流程
     */
    private static void startApplication() {
        try {                       
            // 3. 运行串口连接对话框,等待串口连接成功
            boolean serialConnected = initializeSerialPort();
            
            if (serialConnected) {
                // 4. 串口连接成功后,启动轮询
                startPollingService();
                showMainInterface();                
                
            } else {
                System.err.println("串口连接失败");
                // 串口连接失败已经由SerialPortConnectionDialog处理,直接退出
                System.exit(0);
            }
            
        } catch (Exception e) {
            System.err.println("应用程序主流程执行过程中发生异常: " + e.getMessage());
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, 
                "应用程序启动过程中发生错误: " + e.getMessage(), 
                "错误", 
                JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
    }
    
    /**
     * 初始化串口连接 - 修改:显示串口连接对话框,等待用户点击连接
     */
    private static boolean initializeSerialPort() {
        try {
            //System.out.println("正在打开串口连接对话框...");
            
            // 显示串口连接对话框(模态对话框,会阻塞直到用户操作)
            // 用户必须点击"连接串口"按钮才会实际连接
            boolean connected = SerialPortConnectionDialog.showConnectionDialog(null);
            
            if (connected) {
                //System.out.println("✓ 串口连接成功");
                return true;
            } else {
                //System.out.println("✗ 串口连接失败或用户取消");
                // 添加详细错误信息
                JOptionPane.showMessageDialog(null, 
                    "串口连接失败,请检查串口设备是否可用", 
                    "连接失败", 
                    JOptionPane.WARNING_MESSAGE);
                return false;
            }
            
        } catch (Exception e) {
            System.err.println("串口连接过程中发生异常: " + e.getMessage());
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, 
                "串口连接失败: " + e.getMessage(), 
                "串口错误", 
                JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }
    
    /**
     * 启动轮询服务
     */
    private static boolean startPollingService() {
        try {
            // 检查串口连接状态
            if (!lunxun.checkSerialConnection()) {
                System.err.println("串口未连接,无法启动轮询");
                return false;
            }
            
            // 启动轮询查询
            boolean started = lunxun.startPolling();
            
            if (started) {
                //System.out.println("✓ 轮询查询启动成功");
                return true;
            } else {
                System.err.println("✗ 轮询查询启动失败");
                return false;
            }
            
        } catch (Exception e) {
            System.err.println("启动轮询服务时发生异常: " + e.getMessage());
            return false;
        }
    }
    
    /**
     * 显示主界面
     */
    private static void showMainInterface() {
        try {
            //System.out.println("正在创建主界面...");
            
            // 确保轮询服务已启动
            if (!lunxun.isPolling()) {
                //System.out.println("启动轮询服务...");
                startPollingService();
            }
            
            // 创建并显示主界面
            CardMachineUI mainUI = new CardMachineUI();
            mainUI.setVisible(true);
            
            //System.out.println("✓ 主界面启动成功");
            //System.out.println("应用程序启动完成,进入正常运行状态");
            
            // 添加关闭钩子,确保应用程序退出时正确清理资源
            addShutdownHook(mainUI);
            
        } catch (Exception e) {
            System.err.println("显示主界面时发生异常: " + e.getMessage());
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, 
                "打开主界面失败: " + e.getMessage(), 
                "错误", 
                JOptionPane.ERROR_MESSAGE);
            throw new RuntimeException("主界面启动失败", e);
        }
    }
    
    /**
     * 添加关闭钩子,确保应用程序退出时正确清理资源
     */
    private static void addShutdownHook(CardMachineUI mainUI) {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            //System.out.println("应用程序正在关闭,执行清理操作...");
            
            try {
                // 停止轮询查询
                if (lunxun.isPolling()) {
                    //System.out.println("停止轮询查询...");
                    lunxun.stopPolling();
                }
                
                // 关闭系统配置
                //System.out.println("关闭系统配置...");
                Chushihua.getInstance().shutdown();
                
                // 关闭主界面资源
                if (mainUI != null) {
                    //System.out.println("关闭主界面资源...");
                    mainUI.dispose();
                }
                
                //System.out.println("应用程序关闭完成");
                
            } catch (Exception e) {
                System.err.println("应用程序关闭过程中发生异常: " + e.getMessage());
            }
        }));
    }
}