张世豪
5 小时以前 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
278
279
280
281
282
283
284
285
package chushihua;
 
import java.io.File;
import java.util.List;
 
import dialog.Dingshidialog;
import dialog.Errlog;
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) {
            Errlog.logOperation("系统初始化异常: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 系统关闭时停止轮询
     */
    public void shutdown() {
        try {
            // 停止轮询查询
            if (lunxun.isPolling()) {
                lunxun.stopPolling();
                ////System.out.println("系统关闭:轮询查询已停止");
            }
        } catch (Exception e) {
            Errlog.logOperation("系统关闭异常: " + e.getMessage());
        }
    }
    
    /**
     * 初始化系统配置
     * @param configFilePath 配置文件路径
     */
    public boolean initialize(String configFilePath) {
        try {
            // 检查配置文件是否存在
            File configFile = new File(configFilePath);
            if (!configFile.exists()) {
                Errlog.logOperation("配置文件不存在: " + 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) {
            Errlog.logOperation("系统初始化失败: " + 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) {
            Errlog.logOperation("配置未加载,无法保存");
            return false;
        }
        
        try {
            machineConfig.saveToFile(configFilePath);
            ////System.out.println("配置已保存到: " + configFilePath);
            return true;
        } catch (Exception e) {
            Errlog.logOperation("保存配置失败: " + e.getMessage());
            return false;
        }
    }
    
    /**
     * 获取配置对象
     */
    public MachineConfig getMachineConfig() {
        if (!initialized) {
            Dingshidialog.showTimedDialog(null,10,"系统未初始化成功");
        }
        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<Integer> 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<Integer> 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;
    }
}