我构建了以下函数来执行 CardTerminal 以读取 SIM 卡的 ICCID。
目标:
这是我的代码。当终端未连接时,它会等待。连接终端后,它会等待卡片。存在卡后,它读取 iccid 并将其写入文件,但仅一次(期望它会无限循环,直到终端断开连接或应用程序停止)。当我取出卡或终端时,应用程序结束(这是不需要的)。
public void run() {
Mono.defer(cardTerminalFactory::getTerminal)
.retry()
.doOnSuccess(t -> {
System.out.println("connected with terminal: " + t.getName());
Mono.defer(() -> cardTerminalFactory.waitForCard(t))
.doOnSuccess(x -> {
System.out.println("connected with card");
try {
// wait for card insert
if (t.waitForCardPresent(0)) {
// just some logic here (read out iccid and print to file)
var iccid = cardTerminalFactory.readIccid(x.getBasicChannel()).blockOptional();
iccid.ifPresent(bigInteger -> Writer.write(bigInteger.toString()));
//continue when a card removed
t.waitForCardAbsent(0);
}
} catch (CardException e) {
Mono.error(Exception::new);
}
}).block();
}
)
.onErrorResume(error -> Mono.empty())
.block();
}
你能帮帮我吗?