Improve NFC history readers and prepare production build

This commit is contained in:
2026-05-08 05:40:52 +07:00
parent 1dc293c697
commit bd34467ddc
14 changed files with 688 additions and 182 deletions

View File

@ -14,6 +14,7 @@ public class TapCashApi : UnifiedNfcApi {
var riwayatList: [RiwayatCard] = []
var start = 0
var totalLog = 0
private var historyRetryCount = 0
public override init() {}
@ -22,15 +23,22 @@ public class TapCashApi : UnifiedNfcApi {
if (response.sw1 == 0x90 && response.sw2 == 0x00){
self.emoney.setCardLabel("BNI TapCash")
self.tapCashData.setPurseData(response.getData().bytes)
let balance = self.tapCashData.getPurseBalance()?.hexString().hex2decimal()
self.emoney.setBalance(balance!)
self.emoney.setCardNumber(self.tapCashData.getCAN()!.hexString())
self.totalLog = (self.tapCashData.getTotalRecords()?.hexString().hex2decimal())!
let balance = self.tapCashData.getPurseBalance()?.hexString().hex2decimal() ?? 0
guard let can = self.tapCashData.getCAN()?.hexString() else {
self.emoney.setTampilRiwayat(false)
self.updateScreen()
self.apduRunner.invalidateSession(msg: "readFailed".localizeString(string: self.langCode!))
return
}
self.emoney.setBalance(balance)
self.emoney.setCardNumber(can)
self.totalLog = self.tapCashData.getTotalRecords()?.hexString().hex2decimal() ?? 0
if (self.totalLog > 10){
self.totalLog = 10
}
debugLog("total log " + String(self.totalLog))
self.getHistory(index: 0)
self.historyRetryCount = 0
self.startHistoryRead()
} else {
self.emoney.setTampilRiwayat(false)
self.updateScreen()
@ -39,29 +47,58 @@ public class TapCashApi : UnifiedNfcApi {
}
})
}
private func startHistoryRead() {
start = 0
riwayatList.removeAll()
getHistory(index: 0)
}
private func retryHistoryRead(reason: String) {
if historyRetryCount < 1 {
historyRetryCount += 1
debugLog("Retrying TapCash history read: \(reason)")
startHistoryRead()
} else {
finalizeHistoryRead()
}
}
private func finalizeHistoryRead() {
self.updateScreen()
self.riwayatList = self.riwayatList.sorted {
($0.getTransationTime() ?? Date.distantPast) > ($1.getTransationTime() ?? Date.distantPast)
}
if (self.riwayatList.count > 0){
self.emoney.setRiwayatList(self.riwayatList)
self.emoney.setTampilRiwayat(true)
}
self.apduRunner.sessionEx?.alertMessage = "readFinish".localizeString(string: self.langCode!)
self.apduRunner.invalidateSession()
}
private func getHistory(index : Int){
let st = String(index).leftPad(with: "0", length: 2)
guard let payload = st.stringToBytes() else {
debugLog("Invalid TapCash history index payload: \(st)")
return
}
//90 32 03 00 01 00 10
let TAPCASH_LOG = NFCISO7816APDU(instructionClass : 0x90, instructionCode : 0x32, p1Parameter : 0x03, p2Parameter : 0x00, data : Data(_ : st.stringToBytes()!), expectedResponseLength : 16)
let TAPCASH_LOG = NFCISO7816APDU(instructionClass : 0x90, instructionCode : 0x32, p1Parameter : 0x03, p2Parameter : 0x00, data : Data(_ : payload), expectedResponseLength : 16)
debugLog(TAPCASH_LOG.toHexString())
apduRunner.exchangeApdu(apduCommand: TAPCASH_LOG, completionHandler: {response in
if (response.sw1 == 0x90 && response.sw2 == 0x00){
self.addRiwayatTransaksi(data: response.getData().bytes)
} else {
self.retryHistoryRead(reason: "status \(response.sw1)-\(response.sw2) at index \(index)")
return
}
self.start+=1
if (self.start < (self.totalLog)){
self.getHistory(index: self.start)
} else {
self.updateScreen()
self.riwayatList = self.riwayatList.sorted(by: { $0.getTransationTime()?.compare($1.getTransationTime()!) == .orderedDescending })
if (self.riwayatList.count > 0){
self.emoney.setRiwayatList(self.riwayatList)
self.emoney.setTampilRiwayat(true)
}
self.apduRunner.sessionEx?.alertMessage = "readFinish".localizeString(string: self.langCode!)
self.apduRunner.invalidateSession()
self.finalizeHistoryRead()
}
})
}
@ -73,6 +110,10 @@ public class TapCashApi : UnifiedNfcApi {
}
private func addRiwayatTransaksi(data: [UInt8]) {
guard data.count >= 8 else {
debugLog("TapCash history data too short: \(data.count)")
return
}
let trxType = Array(data[0..<1])
let trxAmount = Array(data[1..<4])
let trxDateTimes = Array(data[4..<8])