Browse Source

ID卡读取

tags/v0
sige 1 year ago
parent
commit
b750c2abcb
  1. 58
      src/main/java/com/iflytop/a800/controller/IdChipController.java
  2. 2
      src/main/java/com/iflytop/a800/device/Device.java
  3. 101
      src/main/java/com/iflytop/a800/device/IdChipReader.java
  4. 24
      src/main/java/com/iflytop/a800/model/MdbIdChip.java

58
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<String, Object> 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<String, Object> params ) {
List<String> ids = (List<String>) params.get("ids");
for ( var id : ids ) {
var chip = UfActiveRecord.findOne(MdbIdChip.class, id);
if ( null != chip ) {
chip.delete();
}
}
return this.success();
}
}

2
src/main/java/com/iflytop/a800/device/Device.java

@ -23,6 +23,8 @@ public class Device {
public final TestCardManager testCard = new TestCardManager(); public final TestCardManager testCard = new TestCardManager();
// 废料箱 // 废料箱
public final TrashBox trashBox = new TrashBox(); public final TrashBox trashBox = new TrashBox();
// id卡读卡器
public final IdChipReader idChipReader = new IdChipReader();
// get instance // get instance
public static Device getInstance() { public static Device getInstance() {

101
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<Byte> data = new ArrayList<>();
for (int i = 0; i<sectorNum; i++ ) {
String readParam = Integer.toString(i);
String response = UfActuatorCmdExecutor.execute("IdCardReader","a8000_idcard_reader_read_raw", readParam);
Base64.Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(response);
for (byte aByte : bytes) {
data.add(aByte);
}
}
// convert to buffer
byte[] result = new byte[data.size()];
for ( int i = 0; i < data.size(); i++ ) {
result[i] = data.get(i);
}
var buffer = ByteBuffer.wrap(result);
buffer.order(ByteOrder.LITTLE_ENDIAN);
return buffer;
}
}

24
src/main/java/com/iflytop/a800/model/MdbIdChip.java

@ -1,5 +1,27 @@
package com.iflytop.a800.model; package com.iflytop.a800.model;
public class MdbIdChip {
import com.iflytop.uf.UfActiveRecord;
import com.iflytop.uf.UfActiveRecordField;
public class MdbIdChip extends UfActiveRecord {
@UfActiveRecordField
public String itemName;
@UfActiveRecordField
public String lotCode;
@UfActiveRecordField
public String expiredDate;
@UfActiveRecordField
public String version;
@UfActiveRecordField
public String hash;
// get table name
public static String getTableName() {
return "app_id_chips";
}
public Integer peakCount; public Integer peakCount;
public Integer itemCount; public Integer itemCount;
public String items; public String items;

Loading…
Cancel
Save