From 14b15539a8fbbce1145197993e0c062400174ef1 Mon Sep 17 00:00:00 2001
From: zuoxiao <lf_zuo@163.com>
Date: 星期四, 26 六月 2025 16:51:12 +0800
Subject: [PATCH] docs(baselibrary): 更新 NativeNfcReadHelper 类文档
---
generallibrary/src/main/java/com/dayu/general/bean/card/UserCard.kt | 122 ++++++++++++++++++++++++----------------
1 files changed, 74 insertions(+), 48 deletions(-)
diff --git a/generallibrary/src/main/java/com/dayu/general/bean/card/UserCard.kt b/generallibrary/src/main/java/com/dayu/general/bean/card/UserCard.kt
index 7aa0eed..3d7168e 100644
--- a/generallibrary/src/main/java/com/dayu/general/bean/card/UserCard.kt
+++ b/generallibrary/src/main/java/com/dayu/general/bean/card/UserCard.kt
@@ -3,23 +3,24 @@
import com.dayu.baselibrary.bean.BaseUserCardCard
import com.dayu.baselibrary.tools.BcdUtil
import com.dayu.baselibrary.tools.HexUtil
+import com.dayu.general.tool.CardCommon.Companion.USER_CARD_TYPE_1
import java.io.Serializable
import java.util.*
/**
* 閫氱敤椤圭洰鐢ㄦ埛鍗$粨鏋�
*/
-class UserCard : BaseCard(), Serializable {
- var cardType: String = "A1" // 鍗$被鍨嬶細A1缁堢鍐欏崱 A8鍒峰崱寮�娉靛悗鍊� A2鍙犲姞鍏呭��
- var areaNumber: Int = 0 // 鍥藉琛屾斂鍖哄煙鍙�(12浣岯CD,绮剧‘鍒版潙)
+class UserCard : BaseUserCardCard(), Serializable {
+ var cardType: String = USER_CARD_TYPE_1 // 鍗$被鍨嬶細A1缁堢鍐欏崱 A8鍒峰崱寮�娉靛悗鍊� A2鍙犲姞鍏呭��
+ var areaNumber: String = "" // 鍥藉琛屾斂鍖哄煙鍙�(12浣岯CD,绮剧‘鍒版潙)
var userCode: String = "" // 鐢ㄦ埛缂栧彿BCD
var userCodeNumber: Int = 0 // 鐢ㄦ埛鍗$紪鍙�(HEX)
var phoneNumber: String = "" // 鎵嬫満鍙�(BCD)
var projectCode: Int = 0 // 椤圭洰缂栫爜(HEX 1-255)
- var balance: Int = 0 // 鍓╀綑閲戦(2浣嶅皬鏁扮偣锛屽崟浣嶅厓)
+ var balance: Int = 0 // 鍓╀綑閲戦(2浣嶅皬鏁扮偣锛屽崟浣嶅垎)
var surplusWater: Int = 0 // 鍓╀綑姘撮噺(2浣嶅皬鏁扮偣锛屽崟浣嶇珛鏂圭背)
- var waterPrice: Float = 0f // 姘撮噺鍗曚环(鏈�澶�655.35锛�2浣嶅皬鏁扮偣銆傚崟浣嶏細m3/鍏�)
- var electricPrice: Float = 0f // 鐢甸噺鍗曚环(鏈�澶�655.35锛�2浣嶅皬鏁扮偣銆傚崟浣�: 鍏�/搴�)
+ var waterPrice: Int = 0 // 姘撮噺鍗曚环(2浣嶅皬鏁扮偣锛屽崟浣嶅垎/m3)
+ var electricPrice: Int = 0 // 鐢甸噺鍗曚环(2浣嶅皬鏁扮偣锛屽崟浣嶅垎/搴�)
var rechargeDate: Calendar? = null // 鍏呭�兼椂闂�
/**
@@ -29,10 +30,10 @@
if (data == null || data.size < 3) {
return false
}
-
+
val expectedBytes = arrayOf(getZeroBytes(), getOneBytes(), getTwoBytes())
- return data.zip(expectedBytes.toList()).all { (actual, expected) ->
- actual.contentEquals(expected)
+ return data.zip(expectedBytes.toList()).all { (actual, expected) ->
+ actual.contentEquals(expected)
}
}
@@ -46,7 +47,7 @@
/**
* 閫氳繃byte杞琤ean
*/
- fun getBean(data: List<ByteArray>): UserCard? {
+ override fun getBean(data: List<ByteArray>): UserCard? {
try {
val userCard = UserCard()
// 瑙f瀽绗�0鍧�
@@ -54,7 +55,7 @@
userCard.apply {
// 瑙f瀽鍥藉琛屾斂鍖哄煙鍙�(0-5浣�)
val areaCodeBytes = zero.copyOfRange(0, 6)
- areaNumber = BcdUtil.bcdToStr(areaCodeBytes).toInt()
+ areaNumber = BcdUtil.bcdToStr(areaCodeBytes)
// 瑙f瀽鐢ㄦ埛鍗$紪鍙�(6-7浣�)
val userCodeNumberBytes = zero.copyOfRange(6, 8)
@@ -73,8 +74,8 @@
projectCode = HexUtil.get16To10LowHightByBytes(byteArrayOf(one[0]))
balance = HexUtil.get16To10LowHightByBytes(one.copyOfRange(1, 5))
surplusWater = HexUtil.get16To10LowHightByBytes(one.copyOfRange(5, 9))
- electricPrice = HexUtil.hexToFloatLowHigh(one.copyOfRange(9, 11))
-
+ electricPrice = HexUtil.get16To10LowHightByBytes(one.copyOfRange(9, 11))
+
// 瑙f瀽鍏呭�兼椂闂�
val year = HexUtil.getBcdToInt(one[11])
val month = HexUtil.getBcdToInt(one[12])
@@ -89,7 +90,7 @@
// 瑙f瀽绗�2鍧�
val two = data[2]
userCard.apply {
- waterPrice = HexUtil.hexToFloatLowHigh(two.copyOfRange(9, 11))
+ waterPrice = HexUtil.get16To10LowHightByBytes(two.copyOfRange(9, 11))
}
return userCard
@@ -105,19 +106,26 @@
val data = ByteArray(16)
try {
// 璁剧疆鍥藉琛屾斂鍖哄煙鍙�(BCD鏍煎紡锛�6瀛楄妭锛�0-5浣�)
- val areaCodeBytes = BcdUtil.strToBcd(String.format("%012d", areaNumber))
- System.arraycopy(areaCodeBytes, 0, data, 0, 6)
+ val areaNumberStr = if (areaNumber.isBlank() || !areaNumber.all { it.isDigit() }) {
+ "000000000000"
+ } else {
+ areaNumber.padStart(12, '0')
+ }
+ val areaCodeBytes = BcdUtil.strToBcd(areaNumberStr)
+ System.arraycopy(areaCodeBytes, 0, data, 0, minOf(areaCodeBytes.size, 6))
- // 璁剧疆鐢ㄦ埛鍗$紪鍙�(HEX鏍煎紡锛�2瀛楄妭锛�6-7浣�)
+ // 璁剧疆鐢ㄦ埛鍗$紪鍙�(HEX鏍煎紡锛�2瀛楄妭锛�6-7浣�) - 淇锛氱‘淇濇暟缁勯暱搴︽纭�
val userCodeBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(userCodeNumber))
- System.arraycopy(userCodeBytes, 0, data, 6, 2)
+ val userCodePadded = ByteArray(2)
+ System.arraycopy(userCodeBytes, 0, userCodePadded, 0, minOf(userCodeBytes.size, 2))
+ System.arraycopy(userCodePadded, 0, data, 6, 2)
// 璁剧疆鍗$被鍨�(8浣�)
data[8] = HexUtil.hexToByte(cardType)
// 璁剧疆鎵嬫満鍙�(BCD鏍煎紡锛�6瀛楄妭锛�9-14浣�)
val phoneBytes = BcdUtil.strToBcd(phoneNumber.padStart(12, '0'))
- System.arraycopy(phoneBytes, 0, data, 9, 6)
+ System.arraycopy(phoneBytes, 0, data, 9, minOf(phoneBytes.size, 6))
// 璁剧疆鏍¢獙鍜�(15浣�)
data[15] = getByteSum(data)
@@ -133,19 +141,28 @@
val data = ByteArray(16)
try {
data[0] = projectCode.toByte()
-
- // 璁剧疆浣欓
- val balanceBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(balance))
- System.arraycopy(balanceBytes, 0, data, 1, 4)
-
- // 璁剧疆鍓╀綑姘撮噺
- val waterBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(surplusWater))
- System.arraycopy(waterBytes, 0, data, 5, 4)
-
- // 璁剧疆鐢典环
- val priceBytes = HexUtil.hexToByteArray(HexUtil.floatToHexLowHigh(electricPrice))
- System.arraycopy(priceBytes, 0, data, 9, 2)
-
+
+ // 璁剧疆浣欓 - 淇锛氱‘淇濇暟缁勯暱搴︽纭�
+ val balanceHex = HexUtil.get10To16LowHigh(balance)
+ val balanceBytes = HexUtil.hexToByteArray(balanceHex)
+ val balancePadded = ByteArray(4)
+ System.arraycopy(balanceBytes, 0, balancePadded, 0, minOf(balanceBytes.size, 4))
+ System.arraycopy(balancePadded, 0, data, 1, 4)
+
+ // 璁剧疆鍓╀綑姘撮噺 - 淇锛氱‘淇濇暟缁勯暱搴︽纭�
+ val waterHex = HexUtil.get10To16LowHigh(surplusWater)
+ val waterBytes = HexUtil.hexToByteArray(waterHex)
+ val waterPadded = ByteArray(4)
+ System.arraycopy(waterBytes, 0, waterPadded, 0, minOf(waterBytes.size, 4))
+ System.arraycopy(waterPadded, 0, data, 5, 4)
+
+ // 璁剧疆鐢典环 - 淇锛氱‘淇濇暟缁勯暱搴︽纭�
+ val electricPriceHex = HexUtil.get10To16LowHigh(electricPrice)
+ val electricPriceBytes = HexUtil.hexToByteArray(electricPriceHex)
+ val electricPricePadded = ByteArray(2)
+ System.arraycopy(electricPriceBytes, 0, electricPricePadded, 0, minOf(electricPriceBytes.size, 2))
+ System.arraycopy(electricPricePadded, 0, data, 9, 2)
+
// 璁剧疆鍏呭�兼椂闂�
rechargeDate?.let {
data[11] = HexUtil.getIntToBCD(it.get(Calendar.YEAR) % 100)[0]
@@ -153,7 +170,7 @@
data[13] = HexUtil.getIntToBCD(it.get(Calendar.DAY_OF_MONTH))[0]
data[14] = HexUtil.getIntToBCD(it.get(Calendar.HOUR_OF_DAY))[0]
}
-
+
data[15] = getByteSum(data)
} catch (e: Exception) {
e.printStackTrace()
@@ -166,17 +183,26 @@
fun toBytes(): ByteArray {
val data = ByteArray(16)
try {
- // 澶囦唤浣欓鍜屾按閲忔暟鎹�
- val balanceBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(balance))
- System.arraycopy(balanceBytes, 0, data, 1, 4)
-
- val waterBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(surplusWater))
- System.arraycopy(waterBytes, 0, data, 5, 4)
-
- // 璁剧疆姘翠环
- val priceBytes = HexUtil.hexToByteArray(HexUtil.floatToHexLowHigh(waterPrice))
- System.arraycopy(priceBytes, 0, data, 9, 2)
-
+ // 澶囦唤浣欓鍜屾按閲忔暟鎹� - 淇锛氱‘淇濇暟缁勯暱搴︽纭�
+ val balanceHex = HexUtil.get10To16LowHigh(balance)
+ val balanceBytes = HexUtil.hexToByteArray(balanceHex)
+ val balancePadded = ByteArray(4)
+ System.arraycopy(balanceBytes, 0, balancePadded, 0, minOf(balanceBytes.size, 4))
+ System.arraycopy(balancePadded, 0, data, 1, 4)
+
+ val waterHex = HexUtil.get10To16LowHigh(surplusWater)
+ val waterBytes = HexUtil.hexToByteArray(waterHex)
+ val waterPadded = ByteArray(4)
+ System.arraycopy(waterBytes, 0, waterPadded, 0, minOf(waterBytes.size, 4))
+ System.arraycopy(waterPadded, 0, data, 5, 4)
+
+ // 璁剧疆姘翠环 - 淇锛氱‘淇濇暟缁勯暱搴︽纭�
+ val waterPriceHex = HexUtil.get10To16LowHigh(waterPrice)
+ val waterPriceBytes = HexUtil.hexToByteArray(waterPriceHex)
+ val waterPricePadded = ByteArray(2)
+ System.arraycopy(waterPriceBytes, 0, waterPricePadded, 0, minOf(waterPriceBytes.size, 2))
+ System.arraycopy(waterPricePadded, 0, data, 9, 2)
+
// 璁剧疆鍏呭�兼椂闂�
rechargeDate?.let {
data[11] = HexUtil.getIntToBCD(it.get(Calendar.YEAR) % 100)[0]
@@ -184,7 +210,7 @@
data[13] = HexUtil.getIntToBCD(it.get(Calendar.DAY_OF_MONTH))[0]
data[14] = HexUtil.getIntToBCD(it.get(Calendar.HOUR_OF_DAY))[0]
}
-
+
data[15] = getByteSum(data)
} catch (e: Exception) {
e.printStackTrace()
@@ -193,7 +219,7 @@
}
}
- fun getZeroBytes(): ByteArray = Zero().toBytes()
- fun getOneBytes(): ByteArray = One().toBytes()
- fun getTwoBytes(): ByteArray = Two().toBytes()
+ override fun getZeroBytes(): ByteArray = Zero().toBytes()
+ override fun getOneBytes(): ByteArray = One().toBytes()
+ override fun getTwoBytes(): ByteArray = Two().toBytes()
}
--
Gitblit v1.8.0