package chushihua; import java.io.File; import java.util.List; import home.MachineConfig; /** * 系统初始化类 * 负责从配置文件加载系统默认参数并初始化系统属性 */ public class Chushihua { // 系统属性键名常量 public static final String PROP_MACHINE_ID = "card.machine.id"; public static final String PROP_SERVER_ADDRESS = "card.machine.server.address"; public static final String PROP_SERVER_PORT = "card.machine.server.port"; public static final String PROP_ADMIN_PASSWORD = "card.machine.admin.password"; public static final String PROP_FETCH_CARD_PASSWORD = "card.machine.fetch.card.password"; public static final String PROP_SYSTEM_LANGUAGE = "card.machine.system.language"; public static final String PROP_CURRENT_VERSION = "card.machine.current.version"; public static final String PROP_TOTAL_SLOTS = "card.machine.total.slots"; public static final String PROP_READ_CARD_MODE = "card.machine.read.card.mode"; public static final String PROP_DEFAULT_SERIAL_PORT = "card.machine.default.serial.port"; public static final String PROP_POLLING_INTERVAL = "card.machine.polling.interval"; public static final String PROP_BAUDRATE = "card.machine.baudrate"; // 默认配置文件路径 private static final String DEFAULT_CONFIG_PATH = "config.properties"; // 单例实例 private static Chushihua instance; // 配置对象 private MachineConfig machineConfig; // 初始化状态 private boolean initialized = false; /** * 私有构造函数 */ private Chushihua() { // 防止外部实例化 } /** * 获取单例实例 */ public static synchronized Chushihua getInstance() { if (instance == null) { instance = new Chushihua(); } return instance; } /** * 初始化系统配置 - 修改版本:移除自动启动轮询 * 从根目录的config.properties文件加载配置并设置系统属性 */ public boolean initialize() { try { initialize(DEFAULT_CONFIG_PATH); // 移除自动启动轮询的逻辑,由Homein统一管理 ////System.out.println("系统配置初始化完成,轮询将由主启动类统一管理"); return true; } catch (Exception e) { System.err.println("系统初始化异常: " + e.getMessage()); e.printStackTrace(); return false; } } /** * 系统关闭时停止轮询 */ public void shutdown() { try { // 停止轮询查询 if (lunxun.isPolling()) { lunxun.stopPolling(); ////System.out.println("系统关闭:轮询查询已停止"); } } catch (Exception e) { System.err.println("系统关闭异常: " + e.getMessage()); } } /** * 初始化系统配置 * @param configFilePath 配置文件路径 */ public boolean initialize(String configFilePath) { try { // 检查配置文件是否存在 File configFile = new File(configFilePath); if (!configFile.exists()) { System.err.println("配置文件不存在: " + configFilePath); return false; } // 加载配置 machineConfig = new MachineConfig(configFilePath); // 设置系统属性 setSystemProperties(); // 标记初始化完成 initialized = true; ////System.out.println("系统初始化完成,配置已加载"); ////System.out.println("设备编号: " + machineConfig.getMachineId()); ////System.out.println("服务器地址: " + machineConfig.getServerAddress() + ":" + machineConfig.getServerPort()); ////System.out.println("卡槽总数: " + machineConfig.getTotalSlots()); ////System.out.println("轮询间隔: " + machineConfig.getPollingInterval() + "ms"); ////System.out.println("波特率: " + machineConfig.getBaudrate()); return true; } catch (Exception e) { System.err.println("系统初始化失败: " + e.getMessage()); e.printStackTrace(); return false; } } /** * 将配置值设置为系统属性 */ private void setSystemProperties() { if (machineConfig == null) { throw new IllegalStateException("配置未加载,无法设置系统属性"); } // 设置系统属性 System.setProperty(PROP_MACHINE_ID, machineConfig.getMachineId()); System.setProperty(PROP_SERVER_ADDRESS, machineConfig.getServerAddress()); System.setProperty(PROP_SERVER_PORT, String.valueOf(machineConfig.getServerPort())); System.setProperty(PROP_ADMIN_PASSWORD, machineConfig.getAdminPassword()); System.setProperty(PROP_FETCH_CARD_PASSWORD, machineConfig.getFetchCardPassword()); System.setProperty(PROP_SYSTEM_LANGUAGE, machineConfig.getSystemLanguage()); System.setProperty(PROP_CURRENT_VERSION, machineConfig.getCurrentVersion()); System.setProperty(PROP_TOTAL_SLOTS, String.valueOf(machineConfig.getTotalSlots())); System.setProperty(PROP_READ_CARD_MODE, machineConfig.getReadCardMode()); System.setProperty(PROP_DEFAULT_SERIAL_PORT, machineConfig.getDefaultSerialPort()); System.setProperty(PROP_POLLING_INTERVAL, String.valueOf(machineConfig.getPollingInterval())); System.setProperty(PROP_BAUDRATE, String.valueOf(machineConfig.getBaudrate())); } /** * 重新加载配置 */ public boolean reload() { initialized = false; return initialize(); } /** * 重新加载指定路径的配置 */ public boolean reload(String configFilePath) { initialized = false; return initialize(configFilePath); } /** * 保存当前配置到文件 */ public boolean saveConfig() { return saveConfig(DEFAULT_CONFIG_PATH); } /** * 保存当前配置到指定文件 */ public boolean saveConfig(String configFilePath) { if (machineConfig == null) { System.err.println("配置未加载,无法保存"); return false; } try { machineConfig.saveToFile(configFilePath); ////System.out.println("配置已保存到: " + configFilePath); return true; } catch (Exception e) { System.err.println("保存配置失败: " + e.getMessage()); return false; } } /** * 获取配置对象 */ public MachineConfig getMachineConfig() { if (!initialized) { throw new IllegalStateException("系统未初始化,请先调用initialize()方法"); } return machineConfig; } /** * 检查是否已初始化 */ public boolean isInitialized() { return initialized; } /** * 获取系统属性值 */ public String getSystemProperty(String key) { return System.getProperty(key); } /** * 获取波特率配置值 */ public int getBaudrate() { if (!initialized) { throw new IllegalStateException("系统未初始化,请先调用initialize()方法"); } return machineConfig.getBaudrate(); } /** * 统计卡槽状态数量 * @param slotList 卡槽状态列表,1表示有卡,0表示没有卡,-1表示未知 * @return 格式为"有卡数量/无卡数量"的字符串 */ public static String getCardSlotStatistics(List slotList) { if (slotList == null || slotList.isEmpty()) { return "0/0"; } int hasCardCount = 0; int noCardCount = 0; for (Integer status : slotList) { if (status != null) { if (status == 1) { hasCardCount++; } else if (status == 0) { noCardCount++; } // -1 表示未知,不进行统计 } } return hasCardCount + "/" + noCardCount; } /** * 统计卡槽状态数量(包含未知状态) * @param slotList 卡槽状态列表,1表示有卡,0表示没有卡,-1表示未知 * @return 格式为"有卡数量/无卡数量/未知数量"的字符串 */ public static String getCardSlotStatisticsWithUnknown(List slotList) { if (slotList == null || slotList.isEmpty()) { return "0/0/0"; } int hasCardCount = 0; int noCardCount = 0; int unknownCount = 0; for (Integer status : slotList) { if (status != null) { if (status == 1) { hasCardCount++; } else if (status == 0) { noCardCount++; } else if (status == -1) { unknownCount++; } } } return hasCardCount + "/" + noCardCount + "/" + unknownCount; } }