package com.dayu.general.bean.card
|
|
import com.dayu.baselibrary.tools.HexUtil
|
import com.dayu.general.bean.db.CardData
|
import com.dayu.general.dao.AppDataBase
|
import com.dayu.general.tool.CardCommon
|
import com.tencent.bugly.crashreport.CrashReport
|
|
open class BaseCard {
|
var cardData: String? = null //标识码
|
|
companion object {
|
|
const val IDENTIFY_CODE_A0 = 0xA0.toByte() // 识别码A0
|
const val IDENTIFY_CODE_B1 = 0xB1.toByte() // 识别码B1
|
const val IDENTIFY_CODE_C2 = 0xC2.toByte() // 识别码C2
|
const val IDENTIFY_CODE_89 = 0x89.toByte() // 识别码89
|
}
|
|
|
fun setCardData(baseDao: AppDataBase, cardType: String?) {
|
try {
|
val cardDataBean: CardData =
|
baseDao.cardDataDao().findFirst(cardType)
|
cardData = if (cardDataBean != null) {
|
cardDataBean.cardIdentifying
|
} else {
|
CardCommon.getDefaultCardData(cardType)
|
}
|
} catch (e: Exception) {
|
CrashReport.postCatchedException(e)
|
}
|
}
|
|
|
/**
|
* 前15个字节算术累加和 不含进位
|
*
|
* @param data 源数据
|
* @return 16进制
|
*/
|
fun getByteSum(data: ByteArray?): Byte {
|
if (data != null) {
|
var sum = 0
|
for (b in data) {
|
sum += b.toInt() and 0xFF // & 0xFF 可以将字节扩展为正整数,避免符号位的影响
|
}
|
var hex = HexUtil.get10to16CompleteHex(sum)
|
hex = HexUtil.spaceHex(hex)
|
val hexArr = hex.split(" ".toRegex()).dropLastWhile { it.isEmpty() }
|
.toTypedArray()
|
return HexUtil.hexToByte(hexArr[hexArr.size - 1])
|
}
|
return 0
|
}
|
|
}
|