张世豪
昨天 ef78717c5b956a26b360de44f774fc2b804296c2
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
package publicway;
 
public class QueryData {
    // 常量定义
    private static final String HEADER = "DDCC000BF0";
    private static final String FOOTER = "015AA55AA5";
    
    // 参数范围常量
    private static final int MIN_SLOT_NUMBER = 0;
    private static final int MAX_SLOT_NUMBER = 255;
    
    /**
     * 查询数据
     * 
     * @param slotNumber 卡槽编号 (0-255)
     * @return 生成的查询指令字符串
     * @throws IllegalArgumentException 当参数超出范围时抛出
     * @throws RuntimeException 当生成查询内容过程中发生错误时抛出
     */
    public static String queryData(int slotNumber) {
        // 参数校验
        validateSlotNumber(slotNumber);
        
        try {
            String baseContent = buildBaseContent(slotNumber);
            String contentWithTime = appendTimestamp(baseContent);
            return buildFinalCommand(contentWithTime);
        } catch (Exception e) {
            throw new RuntimeException("生成查询内容时发生错误", e);
        }
    }
    
    /**
     * 验证卡槽编号参数
     */
    private static void validateSlotNumber(int slotNumber) {
        if (slotNumber < MIN_SLOT_NUMBER || slotNumber > MAX_SLOT_NUMBER) {
            throw new IllegalArgumentException(
                String.format("卡槽编号必须在%d-%d范围内", MIN_SLOT_NUMBER, MAX_SLOT_NUMBER));
        }
    }
    
    /**
     * 构建基础内容
     */
    private static String buildBaseContent(int slotNumber) {
        String slotHex = decimalToHex(slotNumber, 2);
        return HEADER + slotHex + FOOTER;
    }
    
    /**
     * 添加时间戳
     */
    private static String appendTimestamp(String baseContent) {
        long timestamp = System.currentTimeMillis() / 1000;
        String timeHex = Long.toHexString(timestamp).toUpperCase();
        return baseContent + timeHex;
    }
    
    /**
     * 构建最终命令(添加CRC校验)
     */
    private static String buildFinalCommand(String content) {
        byte[] cmdBytes = HexUtil.hexStringToBytes(content);
        String crc = HexUtil.calculate(cmdBytes);
        String finalContent = content + crc;        
        return finalContent;
    }
    
    /**
     * 十进制转16进制
     * 
     * @param decimal 十进制数字
     * @param minLength 最小长度,不足时前面补0
     * @return 16进制字符串(大写)
     * @throws IllegalArgumentException 当十进制数为负数时抛出
     */
    public static String decimalToHex(int decimal, int minLength) {
        if (decimal < 0) {
            throw new IllegalArgumentException("十进制数不能为负数: " + decimal);
        }
        
        String hex = Integer.toHexString(decimal).toUpperCase();
        
        // 如果指定了最小长度且当前长度不足,则前面补0
        if (minLength > 0 && hex.length() < minLength) {
            hex = String.format("%0" + minLength + "d", 0).replace("0", "0") + hex;
            hex = hex.substring(hex.length() - minLength);
        }
        
        return hex;
    }
    
    /**
     * 十进制转16进制(不指定最小长度)
     * 
     * @param decimal 十进制数字
     * @return 16进制字符串(大写)
     */
    public static String decimalToHex(int decimal) {
        return decimalToHex(decimal, 0);
    }
    
 
}