diff --git a/src/main/java/com/iflytop/a800/controller/IdChipController.java b/src/main/java/com/iflytop/a800/controller/IdChipController.java new file mode 100644 index 0000000..fba1cf3 --- /dev/null +++ b/src/main/java/com/iflytop/a800/controller/IdChipController.java @@ -0,0 +1,58 @@ +package com.iflytop.a800.controller; +import com.iflytop.a800.device.Device; +import com.iflytop.a800.model.MdbIdChip; +import com.iflytop.uf.UfActiveRecord; +import com.iflytop.uf.UfActiveRecordCriteria; +import com.iflytop.uf.controller.UfApiControllerBase; +import com.iflytop.uf.controller.UfApiResponse; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.ResponseBody; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +@Controller +public class IdChipController extends UfApiControllerBase { + @PostMapping("/api/id-chip/search") + @ResponseBody + public UfApiResponse search( @RequestBody Map params ) { + var criteria = new UfActiveRecordCriteria(); + criteria.offset = (Integer)params.get("offset"); + criteria.limit = (Integer)params.get("limit"); + String orderBy = (String)params.get("orderBy"); + String orderDirection = (String)params.get("orderDir"); + if ( null != orderBy && null != orderDirection ) { + criteria.orders = new HashMap<>(); + criteria.orders.put(orderBy, orderDirection); + } + var list = UfActiveRecord.find(MdbIdChip.class, criteria); + var totalCount = UfActiveRecord.count(MdbIdChip.class, criteria); + return this.success(Map.of("list", list, "totalCount", totalCount)); + } + + @PostMapping("/api/id-chip/read-and-save") + @ResponseBody + public UfApiResponse readAndSave() { + var reader = Device.getInstance().idChipReader; + var chip = reader.read(); + var existsCount = UfActiveRecord.count(MdbIdChip.class, Map.of("hash", chip.hash)); + if ( 0 == existsCount ) { + chip.save(); + } + return this.success(); + } + + @PostMapping("/api/id-chip/batch-delete") + @ResponseBody + public UfApiResponse batchDelete( @RequestBody Map params ) { + List ids = (List) params.get("ids"); + for ( var id : ids ) { + var chip = UfActiveRecord.findOne(MdbIdChip.class, id); + if ( null != chip ) { + chip.delete(); + } + } + return this.success(); + } +} diff --git a/src/main/java/com/iflytop/a800/device/Device.java b/src/main/java/com/iflytop/a800/device/Device.java index f2791aa..142a7aa 100644 --- a/src/main/java/com/iflytop/a800/device/Device.java +++ b/src/main/java/com/iflytop/a800/device/Device.java @@ -23,6 +23,8 @@ public class Device { public final TestCardManager testCard = new TestCardManager(); // 废料箱 public final TrashBox trashBox = new TrashBox(); + // id卡读卡器 + public final IdChipReader idChipReader = new IdChipReader(); // get instance public static Device getInstance() { diff --git a/src/main/java/com/iflytop/a800/device/IdChipReader.java b/src/main/java/com/iflytop/a800/device/IdChipReader.java new file mode 100644 index 0000000..d8d5a24 --- /dev/null +++ b/src/main/java/com/iflytop/a800/device/IdChipReader.java @@ -0,0 +1,101 @@ +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