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());
|
}
|
}));
|
}
|
}
|