左晓为主开发手持机充值管理机
zuoxiao
2024-06-26 ddafb0f0951e68b4e56bfceef43cf78559f5161f
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
package com.dayu.henanlibrary.card;
 
import android.text.TextUtils;
 
import com.dayu.baselibrary.tools.BcdUtil;
import com.dayu.baselibrary.tools.HexUtil;
import com.dayu.henanlibrary.utils.CardCommon;
import com.tencent.bugly.crashreport.CrashReport;
 
import java.io.Serializable;
import java.net.IDN;
import java.nio.charset.StandardCharsets;
import java.util.List;
 
/**
 * Copyright (C), 2023,
 * Author: zuo
 * Date: 2023-11-08 11:47
 * Description:设置域名卡:
 */
public class DomainCard implements Serializable {
 
 
    public String cardType = CardCommon.DOMAIN_CARD_TYPE;//卡类型
    public String domainNumber;//域名序号 (BCD格式)
    public int domainLength;//域名长度
 
    //端口号为5个字节,不足5位高位补0。端口号与域名之间用”,”隔开。端口号最后以”#”结束
    public String domainName;//域名
    public int port;//端口
 
    public int type;//域名卡新乡C1后有01==0,鹿邑没有01==1
 
    public int getType() {
        return type;
    }
 
    public void setType(int type) {
        this.type = type;
    }
 
    public String getDomainName() {
        return domainName;
    }
 
    public void setDomainName(String domainName) {
        this.domainName = domainName;
    }
 
    public String getDomainNumber() {
        return domainNumber;
    }
 
    public void setDomainNumber(String domainNumber) {
        this.domainNumber = domainNumber;
    }
 
    public int getPort() {
        return port;
    }
 
    public void setPort(int port) {
        this.port = port;
    }
 
    private byte[] asciiByte;//当前域名和端口号数组
 
    public byte[] getZeroByte() {
        Zero zero = new Zero();
        return zero.toByte();
    }
 
    public byte[] getOneByte() {
        One one = new One();
        return one.toBytes();
    }
 
    public byte[] getTwoByte() {
        Two two = new Two();
        return two.toBytes();
    }
 
 
    public static DomainCard toBean(List<byte[]> data) {
        try {
            DomainCard domainXinXiangCard = new DomainCard();
            String domainNumber = BcdUtil.bcdToStr(data.get(0)[1]);
            if ("00".equals(domainNumber)||"01".equals(domainNumber)||"02".equals(domainNumber)){//假如序号小于等于2就认为是新乡的
                domainXinXiangCard.setType(0);
                domainXinXiangCard.domainNumber = domainNumber;
                int domainLength = HexUtil.get16to10(HexUtil.byteToHex(data.get(0)[2]));
                byte[] domainData = new byte[domainLength + 7];
                int index = 0;
                for (int i = 0; i < 3; i++) {
                    int jIndex;
                    if (i == 0) {
                        jIndex = 3;
                    } else {
                        jIndex = 0;
                    }
                    for (int j = jIndex; j < 15; j++) {
                        if (index < domainData.length) {
                            domainData[index] = data.get(i)[j];
                            index++;
                        }
                    }
                }
                String domainStr = new String(domainData, StandardCharsets.US_ASCII);
                if (!TextUtils.isEmpty(domainStr)){
                    try {
                        String[] parts = domainStr.split("#")[0].split(",");
                        if (parts.length >= 2) {
                            String domainName = parts[0];
                            String port = parts[1].replace("#", "");
                            domainXinXiangCard.setDomainName(domainName);
                            domainXinXiangCard.setPort(Integer.valueOf(port));
                        }
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                }
 
            }else {
                domainXinXiangCard.setType(1);
                int domainLength = HexUtil.get16to10(HexUtil.byteToHex(data.get(0)[1]));
                byte[] domainData = new byte[domainLength + 7];
                int index = 0;
                for (int i = 0; i < 3; i++) {
                    int jIndex;
                    if (i == 0) {
                        jIndex = 2;
                    } else {
                        jIndex = 0;
                    }
                    for (int j = jIndex; j < 15; j++) {
                        if (index < domainData.length) {
                            domainData[index] = data.get(i)[j];
                            index++;
                        }
                    }
                }
                String domainStr = new String(domainData, StandardCharsets.US_ASCII);
 
                if (!TextUtils.isEmpty(domainStr)){
                    try {
                        String[] parts = domainStr.split("#")[0].split(",");
                        if (parts.length >= 2) {
                            String domainName = parts[0];
                            String port = parts[1].replace("#", "");
                            domainXinXiangCard.setDomainName(domainName);
                            domainXinXiangCard.setPort(Integer.valueOf(port));
                        }
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                }
            }
 
            return domainXinXiangCard;
        } catch (Exception e) {
            e.printStackTrace();
            CrashReport.postCatchedException(e);
        }
        return null;
    }
 
 
    /**
     * 用户卡0块
     */
    public class Zero extends BaseCard {
        public byte[] toByte() {
            byte[] data = new byte[16];
            data[0] = HexUtil.hexToByte(cardType);
            domainLength = domainName.length();
//            域名卡新乡C1后有01==0,鹿邑没有01==1
            if (type == 0) {
                if (domainNumber != null) {
                    byte[] domainNumbers = BcdUtil.strToBcd(domainNumber);
                    data[1] = domainNumbers[0];
                }
                data[2] = HexUtil.hexToByte(HexUtil.get10to16(domainLength));
                String portStr = String.valueOf(port);
                int lenght = portStr.length();
                for (int i = 0; i < 5 - lenght; i++) {
                    portStr = "0" + portStr;
                }
                String ascii = IDN.toASCII(domainName + "," + portStr + "#");
                asciiByte = ascii.getBytes();
                System.arraycopy(asciiByte, 0, data, 3, Math.min(asciiByte.length, 12));
            } else {
                data[1] = HexUtil.hexToByte(HexUtil.get10to16(domainLength));
                String portStr = String.valueOf(port);
                int lenght = portStr.length();
                for (int i = 0; i < 5 - lenght; i++) {
                    portStr = "0" + portStr;
                }
                String ascii = IDN.toASCII(domainName + "," + portStr + "#");
                asciiByte = ascii.getBytes();
                System.arraycopy(asciiByte, 0, data, 2, Math.min(asciiByte.length, 13));
 
            }
 
            data[15] = getByteSum(data);
            return data;
        }
 
    }
 
    /**
     * 用户卡1块
     */
    public class One extends BaseCard {
 
        public byte[] toBytes() {
            byte[] data = new byte[16];
            int offset = (type == 0) ? 12 : 13;
 
            if (asciiByte.length > offset) {
                System.arraycopy(asciiByte, offset, data, 0, Math.min(asciiByte.length - offset, 15));
            }
 
            data[15] = getByteSum(data);
            return data;
        }
 
 
    }
 
    /**
     * 用户卡2块
     */
    public class Two extends BaseCard {
        public byte[] toBytes() {
            byte[] data = new byte[16];
            int offset = (type == 0) ? 27 : 28; // 12 + 15 and 13 + 15
            if (asciiByte.length > offset) {
                int lengthToCopy = Math.min(asciiByte.length - offset, 15);
                System.arraycopy(asciiByte, offset, data, 0, lengthToCopy);
            }
            data[15] = getByteSum(data);
            return data;
        }
    }
}