package com.iflytop.a800.device; import com.iflytop.a800.model.MdbIdChip; import com.iflytop.uf.UfActuatorCmdExecutor; import com.iflytop.uf.UfEventBus; import com.iflytop.uf.model.UfMdbNotification; import com.iflytop.uf.util.UfCommon; import org.springframework.util.DigestUtils; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayList; import java.util.Base64; import java.util.List; public class IdChipReader { // constructor public IdChipReader() { UfEventBus.getInstance().on("ZCanCmderWebSocketEvent", this::handleZCanCmderWebSocketEvent); } // 事件处理 public void handleZCanCmderWebSocketEvent(Object ... args) { Integer eventId = (Integer)args[0]; if ( 0x71C8 == eventId ) { // ID 卡插入 UfMdbNotification.action("IdChipInsert"); } // if ( 0x71C9 == eventId ) { // ID 拔出 // UfMdbNotification.action("IdChipPullout"); // } } // 读取数据 public MdbIdChip read() { var buffer = this.readToByteBuffer(); // parse to model var chip = new MdbIdChip(); // item name byte[] itemNameBytes = new byte[15]; buffer.get(0x0001, itemNameBytes); for (int i = 0; i < itemNameBytes.length; i++) { itemNameBytes[i] = -1 == itemNameBytes[i] ? 0 : itemNameBytes[i]; } chip.itemName = new String(itemNameBytes); chip.itemName = chip.itemName.trim(); // lot code byte[] lotCodeBytes = new byte[12]; buffer.get(0x0010, lotCodeBytes); for (int i = 0; i < lotCodeBytes.length; i++) { lotCodeBytes[i] = -1 == lotCodeBytes[i] ? 0 : lotCodeBytes[i]; } chip.lotCode = new String(lotCodeBytes); chip.lotCode = chip.lotCode.trim(); // expire date byte expYear = buffer.get(0x001C); byte expMon = buffer.get(0x001D); byte expDay = buffer.get(0x001E); chip.expiredDate = String.format("20%02x-%02x-%02x", expYear, expMon, expDay); // version chip.version = Integer.toString(buffer.get(0x0021)); // hash byte itemCode = buffer.get(0x001F); chip.hash = String.format("%d-%s-%s-%s", itemCode, chip.lotCode, chip.expiredDate, chip.version); chip.hash = DigestUtils.md5DigestAsHex(chip.hash.getBytes()); return chip; } // read id chip data to buffer private ByteBuffer readToByteBuffer() { var sectorSizeStr = UfActuatorCmdExecutor.execute("IdCardReader", "module_get_reg", "10301"); var sectorSize = Integer.parseInt(sectorSizeStr); var sectorNumStr = UfActuatorCmdExecutor.execute("IdCardReader", "module_get_reg", "10302"); var sectorNum = Integer.parseInt(sectorNumStr); // read bytes List data = new ArrayList<>(); for (int i = 0; i