| | |
| | | /** |
| | | * 通用项目用户卡结构 |
| | | */ |
| | | class UserCard : BaseCard(), Serializable { |
| | | class UserCard : BaseUserCardCard(), Serializable { |
| | | var cardType: String = USER_CARD_TYPE_1 // 卡类型:A1终端写卡 A8刷卡开泵后值 A2叠加充值 |
| | | var areaNumber: Int = 0 // 国家行政区域号(12位BCD,精确到村) |
| | | var userCode: String = "" // 用户编号BCD |
| | |
| | | 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) |
| | | } |
| | | } |
| | | |
| | |
| | | /** |
| | | * 通过byte转bean |
| | | */ |
| | | fun getBean(data: List<ByteArray>): UserCard? { |
| | | override fun getBean(data: List<ByteArray>): UserCard? { |
| | | try { |
| | | val userCard = UserCard() |
| | | // 解析第0块 |
| | |
| | | balance = HexUtil.get16To10LowHightByBytes(one.copyOfRange(1, 5)) |
| | | surplusWater = HexUtil.get16To10LowHightByBytes(one.copyOfRange(5, 9)) |
| | | electricPrice = HexUtil.hexToFloatLowHigh(one.copyOfRange(9, 11)) |
| | | |
| | | |
| | | // 解析充值时间 |
| | | val year = HexUtil.getBcdToInt(one[11]) |
| | | val month = HexUtil.getBcdToInt(one[12]) |
| | |
| | | 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) |
| | | |
| | | |
| | | // 设置充值时间 |
| | | rechargeDate?.let { |
| | | data[11] = HexUtil.getIntToBCD(it.get(Calendar.YEAR) % 100)[0] |
| | |
| | | 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() |
| | |
| | | // 备份余额和水量数据 |
| | | 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) |
| | | |
| | | |
| | | // 设置充值时间 |
| | | rechargeDate?.let { |
| | | data[11] = HexUtil.getIntToBCD(it.get(Calendar.YEAR) % 100)[0] |
| | |
| | | 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() |
| | |
| | | } |
| | | } |
| | | |
| | | 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() |
| | | } |