张世豪
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package chuankou;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
 
import javax.swing.SwingWorker;
 
import chushihua.lunxun;
import dialog.Charulog;
import dialog.Dingshidialog;
import dialog.Errlog;
import publicway.OpenDoor;
import xitongshezhi.SystemDebugDialog;
 
/**
 * 串口消息发送工具类
 * 提供高性能的串口消息发送功能,适合高频调用
 * 优化内存使用,避免长时间运行内存泄漏
 */
public class Sendmsg {
    // 静态串口服务实例
    private static volatile SerialPortService serialService = null;
    private static final AtomicBoolean isPortOpen = new AtomicBoolean(false);
    
    // 改为非final,支持动态控制
    private static volatile boolean DEBUG_MODE = false;
    
    // 使用ThreadLocal保证SimpleDateFormat线程安全
    private static final ThreadLocal<SimpleDateFormat> TIME_FORMATTER = 
        ThreadLocal.withInitial(() -> new SimpleDateFormat("HH:mm:ss.SSS"));
    
    // 缓存字符串构建器,减少对象创建
    private static final ThreadLocal<StringBuilder> STRING_BUILDER_CACHE = 
        ThreadLocal.withInitial(() -> new StringBuilder(128));
    
    // 记录活跃的SwingWorker,便于管理
    private static final ConcurrentHashMap<String, SwingWorker<?, ?>> ACTIVE_WORKERS = 
        new ConcurrentHashMap<>();
   
    
    /**发卡服务器控制打开某个柜门调用指令
     * @param int slotId柜门编号1-60
     * @param int type 1是服务器发卡,2是管理员发卡*/
    public static boolean opendoorzhiling(int slotId,int type) {
        lunxun.setSendChaxunzhiling(false);//暂停查询指令
        
        try {
            // 调用OpenDoor生成开门指令
            String command = OpenDoor.openOneDoor(slotId, type);
            boolean sendResult = Sendmsg.sendMessage(command);
            
            StringBuilder mesBuilder = STRING_BUILDER_CACHE.get();
            mesBuilder.setLength(0); // 清空重用
            mesBuilder.append(command).append(";type").append(type).append("控制打开").append(slotId).append("柜门");
            String mes = mesBuilder.toString();
            
            Charulog.logOperation(mes);
            if (lunxun.DEBUG_ENABLED) {
                SystemDebugDialog.appendAsciiData(mes);
            }
            
            return sendResult;
        } finally {
            // 确保恢复查询指令
            lunxun.setSendChaxunzhiling(true);
        }
    }
    
    /**
     * 打开全部卡槽的公用静态方法
     * @param type 操作类型:1-服务器发卡,2-管理员发卡
     */
    public static void openAllSlots(int type) {
        lunxun.setSendChaxunzhiling(false);//暂停查询指令
        
        String workerKey = "openAllSlots_" + System.currentTimeMillis();
        
        // 使用SwingWorker在后台执行,避免阻塞UI
        SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() throws Exception {
                // 遍历所有卡槽(1-60)
                for (int slotId = 1; slotId <= 60 && !isCancelled(); slotId++) {
                    try {
                        // 生成开门指令
                        String command = OpenDoor.openOneDoor(slotId, type);
                        
                        // 发送串口指令
                        boolean sent = Sendmsg.sendMessage(command);
                        
                        if (!sent) {
                            Errlog.logOperation("发送指令到卡槽 " + slotId + " 失败");
                        }
                        
                        // 间隔100ms,但检查是否被取消
                        Thread.sleep(100);
                        
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        break; // 被中断时退出循环
                    } catch (Exception e) {
                        Errlog.logOperation("处理卡槽 " + slotId + " 时发生错误: " + e.getMessage());
                        // 继续处理下一个卡槽,不中断循环
                    }
                }
                return null;
            }
            
            @Override
            protected void done() {
                try {
                    // 清理worker引用
                    ACTIVE_WORKERS.remove(workerKey);
                    
                    // 可选:完成后可以添加回调处理
                    if (!isCancelled()) {
                        StringBuilder messageBuilder = STRING_BUILDER_CACHE.get();
                        messageBuilder.setLength(0);
                        
                        String types = "管理员";
                        if(type == 1) {
                            types = "服务器指令";
                        }
                        messageBuilder.append(types).append("已将全部卡槽已经打开请取卡");
                        String message = messageBuilder.toString();
                        
                        Dingshidialog.showTimedDialog(null, 5, message);
                        Charulog.logOperation(message);
                    }
                } finally {
                    // 确保恢复查询指令
                    lunxun.setSendChaxunzhiling(true);
                }
            }
        };
        
        // 记录worker便于管理
        ACTIVE_WORKERS.put(workerKey, worker);
        worker.execute();
    }
    
    /**
     * 取消所有正在执行的任务
     */
    public static void cancelAllTasks() {
        ACTIVE_WORKERS.forEach((key, worker) -> {
            if (!worker.isDone()) {
                worker.cancel(true);
            }
        });
        ACTIVE_WORKERS.clear();
    }
    
    /**
     * 设置串口服务实例
     * @param service 串口服务实例
     * @param open 串口是否打开
     */
    public static void setSerialService(SerialPortService service, boolean open) {
        serialService = service;
        isPortOpen.set(open);
    }
    
    /**
     * 获取串口服务实例
     */
    public static SerialPortService getSerialService() {
        return serialService;
    }
    
    /**
     * 发送消息到串口(带重试机制)
     */
    public static boolean sendMessage(String message) {
        if (!isPortOpen.get() || serialService == null) {
            Errlog.logOperation("[" + getCurrentTime() + "] 串口未打开,无法发送数据");
            return false;
        }
        
        if (message == null || message.trim().isEmpty()) {
            Errlog.logOperation("[" + getCurrentTime() + "] 发送数据为空");
            return false;
        }
        
        String text = message.trim();
        int retryCount = 0;
        final int MAX_RETRY = 2;
        
        while (retryCount <= MAX_RETRY) {
            try {
                byte[] data = hexStringToByteArray(text);
                if (data == null) {
                    Errlog.logOperation("[" + getCurrentTime() + "] HEX转换失败,数据: " + text);
                    return false;
                }
                
                boolean sendResult = serialService.send(data);
                
                if (sendResult) {
                    if (DEBUG_MODE) {
                        System.out.println("[" + getCurrentTime() + "] 发送成功: " + text.toUpperCase());
                    }
                    return true;
                } else {
                    retryCount++;
                    if (retryCount <= MAX_RETRY) {
                        Errlog.logOperation("[" + getCurrentTime() + "] 发送失败,正在重试 (" + retryCount + "/" + MAX_RETRY + ")");
                        try {
                            Thread.sleep(50); // 重试前等待
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                            break;
                        }
                    } else {
                        if (DEBUG_MODE) {
                            Errlog.logOperation("[" + getCurrentTime() + "] 串口发送失败,指令: " + text.toUpperCase());
                            Errlog.logOperation("[" + getCurrentTime() + "] 串口状态 - 打开: " + isPortOpen.get() + ", 服务: " + (serialService != null));
                            if (serialService != null) {
                                Errlog.logOperation("[" + getCurrentTime() + "] 串口服务状态 - 是否打开: " + serialService.isOpen());
                            }
                        }
                        return false;
                    }
                }
            } catch (Exception e) {
                Errlog.logOperation("[" + getCurrentTime() + "] 发送异常,指令: " + text.toUpperCase());
                if (DEBUG_MODE) {
                    e.printStackTrace();
                }
                return false;
            }
        }
        
        return false;
    }
    
    /**
     * 检查串口是否已打开
     * @return 串口打开状态
     */
    public static boolean isPortOpen() {
        boolean open = isPortOpen.get() && serialService != null;
        if (!open && DEBUG_MODE) {
            Errlog.logOperation("[" + getCurrentTime() + "] 串口状态检查: 未打开");
        }
        return open;
    }
    
    /**
     * HEX字符串转字节数组
     * @param s HEX字符串
     * @return 字节数组
     */
    private static byte[] hexStringToByteArray(String s) {
        if (s == null || s.isEmpty()) {
            return new byte[0];
        }
        
        s = s.replaceAll("\\s+", ""); // 移除空格
        int len = s.length();
        if (len % 2 != 0) {
            throw new IllegalArgumentException("HEX字符串长度必须为偶数,当前长度: " + len + ", 数据: " + s);
        }
        
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            int high = Character.digit(s.charAt(i), 16);
            int low = Character.digit(s.charAt(i + 1), 16);
            
            if (high == -1 || low == -1) {
                throw new IllegalArgumentException("无效的HEX字符: '" + s.charAt(i) + s.charAt(i + 1) + "' 在位置 " + i + "-" + (i+1) + ", 完整数据: " + s);
            }
            
            data[i / 2] = (byte) ((high << 4) + low);
        }
        return data;
    }
    
    /**
     * 字节数组转HEX字符串
     * @param bytes 字节数组
     * @return HEX字符串
     */
    public static String bytesToHex(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return "";
        }
        
        StringBuilder sb = STRING_BUILDER_CACHE.get();
        sb.setLength(0);
        for (byte b : bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }
    
    /**
     * 获取当前时间字符串
     * @return 时间字符串
     */
    private static String getCurrentTime() {
        return TIME_FORMATTER.get().format(new Date());
    }
    
    /**
     * 启用调试模式
     */
    public static void enableDebugMode() {
        DEBUG_MODE = true;
        System.out.println("[" + getCurrentTime() + "] Sendmsg调试模式已启用");
    }
    
    /**
     * 禁用调试模式
     */
    public static void disableDebugMode() {
        DEBUG_MODE = false;
        System.out.println("[" + getCurrentTime() + "] Sendmsg调试模式已禁用");
    }
    
    /**
     * 设置调试模式
     */
    public static void setDebugMode(boolean debug) {
        DEBUG_MODE = debug;
        System.out.println("[" + getCurrentTime() + "] Sendmsg调试模式: " + (debug ? "启用" : "禁用"));
    }
    
    /**
     * 清理资源,防止内存泄漏
     */
    public static void cleanup() {
        cancelAllTasks();
        
        // 清理ThreadLocal资源
        TIME_FORMATTER.remove();
        STRING_BUILDER_CACHE.remove();
        
        if (DEBUG_MODE) {
            System.out.println("[" + getCurrentTime() + "] Sendmsg资源清理完成");
        }
    }
    
    /**
     * 获取活跃任务数量
     */
    public static int getActiveTaskCount() {
        return ACTIVE_WORKERS.size();
    }
}