| | |
| | | package xitongshezhi; |
| | | |
| | | import javax.swing.*; |
| | | import javax.swing.border.EmptyBorder; |
| | | import java.awt.*; |
| | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | // 新增导入 |
| | | import publicway.OpenDoor; |
| | | import chuankou.Sendmsg; |
| | | import chushihua.SlotManager; |
| | | import home.Fkj; |
| | | |
| | | @SuppressWarnings("serial") |
| | | public class kuaisuquka extends JDialog { |
| | | // 屏幕尺寸常量 - 适配7寸竖屏 |
| | | private static final int SCREEN_WIDTH = 600; |
| | |
| | | private static final Color PRIMARY_COLOR = new Color(52, 152, 219); |
| | | private static final Color PRIMARY_DARK_COLOR = new Color(41, 128, 185); |
| | | private static final Color SECONDARY_COLOR = new Color(46, 204, 113); |
| | | private static final Color DANGER_COLOR = new Color(231, 76, 60); |
| | | private static final Color WARNING_COLOR = new Color(243, 156, 18); |
| | | private static final Color DARK_COLOR = new Color(15, 28, 48); |
| | | private static final Color DARK_LIGHT_COLOR = new Color(26, 43, 68); |
| | | private static final Color TEXT_COLOR = new Color(224, 224, 224); |
| | | private static final Color TEXT_LIGHT_COLOR = new Color(160, 200, 255); |
| | | private static final Color CARD_BG_COLOR = new Color(30, 60, 114, 178); |
| | | |
| | | // 优化的颜色常量 |
| | | private static final Color BRIGHT_GREEN = new Color(46, 204, 113); // 有卡 - 绿色 |
| | | private static final Color DARK_GRAY = new Color(93, 109, 126); // 无卡 - 深灰色 |
| | |
| | | HAS_CARD("{卡号}", BRIGHT_GREEN), // 占位符,实际显示时会替换为具体卡号 |
| | | NO_CARD("无卡", DARK_GRAY); |
| | | |
| | | private final String displayName; |
| | | private final Color color; |
| | | |
| | | SlotStatus(String displayName, Color color) { |
| | | this.displayName = displayName; |
| | | this.color = color; |
| | | } |
| | | |
| | | public String getDisplayName() { |
| | | return displayName; |
| | | } |
| | | |
| | | public Color getColor() { |
| | |
| | | |
| | | // 优化的对话框管理 |
| | | private JDialog progressDialog; |
| | | private JProgressBar progressBar; |
| | | private JLabel progressLabel; |
| | | private JDialog resultDialog; |
| | | |
| | | // 共享的事件监听器 |
| | |
| | | private void pausePollingWhenEntering() { |
| | | if (chushihua.lunxun.isPolling() && !chushihua.lunxun.isPaused()) { |
| | | chushihua.lunxun.pausePolling(); |
| | | //System.out.println("进入快速取卡页面,轮询已暂停"); |
| | | System.out.println("进入快速取卡页面,轮询已暂停"); |
| | | } |
| | | } |
| | | |
| | |
| | | if (wasPollingRunning && !wasPollingPaused) { |
| | | if (chushihua.lunxun.isPolling() && chushihua.lunxun.isPaused()) { |
| | | chushihua.lunxun.resumePolling(); |
| | | //System.out.println("退出快速取卡页面,轮询已恢复"); |
| | | System.out.println("退出快速取卡页面,轮询已恢复"); |
| | | } |
| | | } else { |
| | | //System.out.println("退出快速取卡页面,保持原有轮询状态 - 运行: " + wasPollingRunning + ", 暂停: " + wasPollingPaused); |
| | |
| | | openAllButton.setForeground(Color.WHITE); |
| | | openAllButton.setFocusPainted(false); |
| | | openAllButton.setBorder(BorderFactory.createEmptyBorder(12, 24, 12, 24)); |
| | | openAllButton.setIcon(getCachedIcon("🚪", 20)); |
| | | openAllButton.addActionListener(e -> openAllSlots()); |
| | | |
| | | // 添加悬停效果 |
| | |
| | | JLabel label = (JLabel) comp; |
| | | try { |
| | | int slotId = Integer.parseInt(label.getText()); |
| | | SlotStatus status = slotStatuses.get(slotId - 1); |
| | | |
| | | // 如果是有卡状态,执行取卡操作 |
| | | if (status == SlotStatus.HAS_CARD) { |
| | | takeCard(slotId); |
| | | } |
| | | // 发送开门指令(不管是否有卡) |
| | | sendOpenDoorCommand(slotId); |
| | | Sendmsg.opendoorzhiling(slotId,2); |
| | | break; |
| | | } catch (NumberFormatException ex) { |
| | | // 忽略非数字标签 |
| | |
| | | } |
| | | } |
| | | |
| | | // 新增方法:发送单个卡槽开门指令 |
| | | private void sendOpenDoorCommand(int slotId) { |
| | | try { |
| | | // 生成开门指令 |
| | | String command = OpenDoor.openOneDoor(slotId, OpenDoor.TYPE_ADMIN); |
| | | |
| | | // 发送串口指令 |
| | | boolean sent = Sendmsg.sendMessage(command); |
| | | |
| | | if (sent) { |
| | | //System.out.println("成功发送开门指令到卡槽 " + slotId); |
| | | } else { |
| | | System.err.println("发送开门指令到卡槽 " + slotId + " 失败"); |
| | | } |
| | | } catch (Exception e) { |
| | | System.err.println("生成开门指令失败: " + e.getMessage()); |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | private void takeCard(int slotId) { |
| | | int index = slotId - 1; |
| | | |
| | | // 更新卡槽状态为无卡 |
| | | slotStatuses.set(index, SlotStatus.NO_CARD); |
| | | |
| | | // 调用 SlotManager 的changgehaska方法更新状态 |
| | | SlotManager.changgehaska(slotId, "1"); // "1"表示管理员操作 |
| | | |
| | | updateCardSlotsDisplay(); |
| | | updateStatistics(); |
| | | } |
| | | |
| | | // 优化的卡槽显示更新 |
| | | // 修改更新卡槽显示的方法 |
| | |
| | | } |
| | | |
| | | private void openAllSlots() { |
| | | int openedCount = 0; |
| | | |
| | | // 更新所有有卡卡槽为无卡 |
| | | for (int i = 0; i < slotStatuses.size(); i++) { |
| | | if (slotStatuses.get(i) == SlotStatus.HAS_CARD) { |
| | | slotStatuses.set(i, SlotStatus.NO_CARD); |
| | | // 调用 SlotManager 的changgehaska方法更新状态 |
| | | SlotManager.changgehaska(i + 1, "1"); // "1"表示管理员操作 |
| | | openedCount++; |
| | | } |
| | | } |
| | | |
| | | if (openedCount > 0) { |
| | | updateCardSlotsDisplay(); |
| | | updateStatistics(); |
| | | } |
| | | |
| | | // 修改:直接调用串口发送方法,不显示进度对话框 |
| | | openAllSlotsWithSerialCommands(openedCount); |
| | | } |
| | | |
| | | // 新增方法:通过串口发送开门指令 |
| | | private void openAllSlotsWithSerialCommands(int openedCount) { |
| | | // 修改:直接调用OpenDoor的异步开门方法,不显示进度对话框 |
| | | OpenDoor.openAllSlotsAsync(60, 250, OpenDoor.TYPE_ADMIN, new OpenDoor.OpenDoorCallback() { |
| | | @Override |
| | | public void onProgress(int currentSlot, int totalSlots, String command) { |
| | | // 发送串口指令 |
| | | boolean sent = Sendmsg.sendMessage(command); |
| | | if (!sent) { |
| | | System.err.println("发送指令失败: " + command); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onComplete(String[] commands) { |
| | | SwingUtilities.invokeLater(() -> { |
| | | String message; |
| | | if (openedCount > 0) { |
| | | message = "已成功打开全部 " + openedCount + " 个有卡卡槽\n发送了 " + commands.length + " 条开门指令"; |
| | | } else { |
| | | message = "已发送 " + commands.length + " 条开门指令到所有卡槽"; |
| | | } |
| | | showResultDialog("success", "操作成功", message); |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | public void onError(Exception error) { |
| | | SwingUtilities.invokeLater(() -> { |
| | | showResultDialog("error", "操作失败", |
| | | "发送开门指令时发生错误: " + error.getMessage()); |
| | | error.printStackTrace(); |
| | | }); |
| | | } |
| | | }); |
| | | Sendmsg.openAllSlots(2); |
| | | } |
| | | |
| | | // 优化的统计计算 |
| | |
| | | } |
| | | |
| | | cardsCountLabel.setText("有卡: " + hasCardCount + "/60"); |
| | | } |
| | | |
| | | // 优化的结果对话框显示 |
| | | private void showResultDialog(String type, String title, String message) { |
| | | if (resultDialog == null) { |
| | | createResultDialog(); |
| | | } |
| | | updateResultDialog(type, title, message); |
| | | resultDialog.setVisible(true); |
| | | } |
| | | |
| | | private void createResultDialog() { |
| | | resultDialog = new JDialog(this, "", true); |
| | | resultDialog.setSize(400, 250); |
| | | resultDialog.setLocationRelativeTo(this); |
| | | resultDialog.setResizable(false); |
| | | resultDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); |
| | | |
| | | JPanel contentPanel = new JPanel(); |
| | | contentPanel.setLayout(new BorderLayout()); |
| | | contentPanel.setBorder(new EmptyBorder(25, 25, 25, 25)); |
| | | contentPanel.setBackground(DARK_LIGHT_COLOR); |
| | | |
| | | // 图标和标题 |
| | | JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
| | | headerPanel.setOpaque(false); |
| | | |
| | | JLabel iconLabel = new JLabel(); |
| | | iconLabel.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 32)); |
| | | |
| | | JLabel titleLabel = new JLabel(); |
| | | titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 20)); |
| | | titleLabel.setForeground(TEXT_COLOR); |
| | | |
| | | headerPanel.add(iconLabel); |
| | | headerPanel.add(Box.createRigidArea(new Dimension(10, 0))); |
| | | headerPanel.add(titleLabel); |
| | | |
| | | // 消息内容 |
| | | JLabel messageLabel = new JLabel(); |
| | | messageLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14)); |
| | | messageLabel.setForeground(TEXT_LIGHT_COLOR); |
| | | messageLabel.setHorizontalAlignment(SwingConstants.CENTER); |
| | | messageLabel.setBorder(new EmptyBorder(15, 0, 25, 0)); |
| | | |
| | | // 确定按钮 |
| | | JButton confirmButton = new JButton("确定"); |
| | | confirmButton.setFont(new Font("Microsoft YaHei", Font.BOLD, 14)); |
| | | confirmButton.setBackground(SECONDARY_COLOR); |
| | | confirmButton.setForeground(Color.WHITE); |
| | | confirmButton.setFocusPainted(false); |
| | | confirmButton.setBorder(BorderFactory.createEmptyBorder(10, 30, 10, 30)); |
| | | confirmButton.addActionListener(e -> resultDialog.dispose()); |
| | | |
| | | // 添加悬停效果 |
| | | confirmButton.addMouseListener(new java.awt.event.MouseAdapter() { |
| | | public void mouseEntered(java.awt.event.MouseEvent evt) { |
| | | confirmButton.setBackground(brighterColor(SECONDARY_COLOR)); |
| | | } |
| | | |
| | | public void mouseExited(java.awt.event.MouseEvent evt) { |
| | | confirmButton.setBackground(SECONDARY_COLOR); |
| | | } |
| | | }); |
| | | |
| | | JPanel buttonPanel = new JPanel(); |
| | | buttonPanel.setOpaque(false); |
| | | buttonPanel.add(confirmButton); |
| | | |
| | | contentPanel.add(headerPanel, BorderLayout.NORTH); |
| | | contentPanel.add(messageLabel, BorderLayout.CENTER); |
| | | contentPanel.add(buttonPanel, BorderLayout.SOUTH); |
| | | |
| | | resultDialog.add(contentPanel); |
| | | } |
| | | |
| | | private void updateResultDialog(String type, String title, String message) { |
| | | resultDialog.setTitle(title); |
| | | |
| | | Component[] components = ((JPanel)resultDialog.getContentPane().getComponent(0)).getComponents(); |
| | | |
| | | // 更新图标和标题 |
| | | JPanel headerPanel = (JPanel) components[0]; |
| | | JLabel iconLabel = (JLabel) headerPanel.getComponent(0); |
| | | JLabel titleLabel = (JLabel) headerPanel.getComponent(2); |
| | | |
| | | if ("success".equals(type)) { |
| | | iconLabel.setText("✅"); |
| | | } else if ("warning".equals(type)) { |
| | | iconLabel.setText("⚠️"); |
| | | } else if ("error".equals(type)) { |
| | | iconLabel.setText("❌"); |
| | | } else { |
| | | iconLabel.setText("ℹ️"); |
| | | } |
| | | |
| | | titleLabel.setText(title); |
| | | |
| | | // 更新消息 |
| | | JLabel messageLabel = (JLabel) components[1]; |
| | | messageLabel.setText("<html><div style='text-align: center;'>" + message.replace("\n", "<br>") + "</div></html>"); |
| | | } |
| | | |
| | | private Color brighterColor(Color color) { |