左晓为主开发手持机充值管理机
zuoxiao
2024-06-13 d1573513f0eada6a915c2163ce6c84451c8c1070
henanlibrary/src/main/java/com/dayu/henanlibrary/card/DomainCard.java
@@ -1,8 +1,10 @@
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.MyCommon;
import com.dayu.henanlibrary.utils.CardCommon;
import com.tencent.bugly.crashreport.CrashReport;
import java.io.Serializable;
@@ -19,13 +21,23 @@
public class DomainCard implements Serializable {
    public String cardType = MyCommon.DOMAIN_CARD_TYPE;//卡类型
    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;
@@ -71,34 +83,80 @@
    public static DomainCard toBean(List<byte[]> data) {
        try {
            DomainCard domainCard = new DomainCard();
            DomainCard domainXinXiangCard = new DomainCard();
            String domainNumber = BcdUtil.bcdToStr(data.get(0)[1]);
            domainCard.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++;
            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();
                    }
                }
            }
            String domainStr = new String(domainData, StandardCharsets.US_ASCII);
            String domainName = domainStr.split(",")[0];
            String port = domainStr.split(",")[1].replace("#", "");
            domainCard.setDomainName(domainName);
            domainCard.setPort(Integer.valueOf(port));
            return domainCard;
            return domainXinXiangCard;
        } catch (Exception e) {
            e.printStackTrace();
            CrashReport.postCatchedException(e);
@@ -114,24 +172,45 @@
        public byte[] toByte() {
            byte[] data = new byte[16];
            data[0] = HexUtil.hexToByte(cardType);
            if (domainNumber != null) {
                byte[] domainNumbers = BcdUtil.strToBcd(domainNumber);
                data[1] = domainNumbers[0];
            }
            domainLength = domainName.length();
            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();
            if (asciiByte.length < 12) {
                System.arraycopy(asciiByte, 0, data, 3, asciiByte.length);
//            域名卡新乡C1后有01==0,鹿邑没有01==1
            if (type == 0) {
                if (domainNumber != null) {
                    byte[] domainNumbers = BcdUtil.strToBcd(domainNumber);
                    data[1] = domainNumbers[0];
                }
                domainLength = domainName.length();
                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();
                if (asciiByte.length < 12) {
                    System.arraycopy(asciiByte, 0, data, 3, asciiByte.length);
                } else {
                    System.arraycopy(asciiByte, 0, data, 3, 12);
                }
            } else {
                System.arraycopy(asciiByte, 0, data, 3, 12);
                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();
                if (asciiByte.length < 13) {
                    System.arraycopy(asciiByte, 0, data, 1, asciiByte.length);
                } else {
                    System.arraycopy(asciiByte, 0, data, 1, 13);
                }
            }
            data[15] = getByteSum(data);
            return data;
        }
@@ -145,13 +224,24 @@
        public byte[] toBytes() {
            byte[] data = new byte[16];
            if (asciiByte.length > 12) {
                if (asciiByte.length - 12 < 15) {
                    System.arraycopy(asciiByte, 12, data, 0, asciiByte.length - 12);
                } else {
                    System.arraycopy(asciiByte, 12, data, 0, 15);
            if (type==0){
                if (asciiByte.length > 12) {
                    if (asciiByte.length - 12 < 15) {
                        System.arraycopy(asciiByte, 12, data, 0, asciiByte.length - 12);
                    } else {
                        System.arraycopy(asciiByte, 12, data, 0, 15);
                    }
                }
            }else {
                if (asciiByte.length > 13) {
                    if (asciiByte.length - 13 < 15) {
                        System.arraycopy(asciiByte, 13, data, 0, asciiByte.length - 12);
                    } else {
                        System.arraycopy(asciiByte, 13, data, 0, 15);
                    }
                }
            }
            data[15] = getByteSum(data);
            return data;
        }
@@ -165,13 +255,24 @@
    public class Two extends BaseCard {
        public byte[] toBytes() {
            byte[] data = new byte[16];
            if (asciiByte.length > (12 + 15)) {
                if (asciiByte.length - (12 + 15) < 15) {
                    System.arraycopy(asciiByte, 12, data, 0, asciiByte.length - (12 + 15));
                } else {
                    System.arraycopy(asciiByte, 12, data, 0, 15);
            if (type==0){
                if (asciiByte.length > (12 + 15)) {
                    if (asciiByte.length - (12 + 15) < 15) {
                        System.arraycopy(asciiByte, 12 + 15, data, 0, asciiByte.length - (12 + 15));
                    } else {
                        System.arraycopy(asciiByte, 12 + 15, data, 0, 15);
                    }
                }
            }else {
                if (asciiByte.length > (13 + 15)) {
                    if (asciiByte.length - (13 + 15) < 15) {
                        System.arraycopy(asciiByte, 13 + 15, data, 0, asciiByte.length - (12 + 15));
                    } else {
                        System.arraycopy(asciiByte, 13 + 15, data, 0, 15);
                    }
                }
            }
            data[15] = getByteSum(data);
            return data;
        }