You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.5 KiB

1 year ago
  1. package com.iflytop.a800.device;
  2. import com.iflytop.a800.model.MdbIdChip;
  3. import com.iflytop.uf.UfActuatorCmdExecutor;
  4. import com.iflytop.uf.UfEventBus;
  5. import com.iflytop.uf.model.UfMdbNotification;
  6. import com.iflytop.uf.util.UfCommon;
  7. import org.springframework.util.DigestUtils;
  8. import java.nio.ByteBuffer;
  9. import java.nio.ByteOrder;
  10. import java.util.ArrayList;
  11. import java.util.Base64;
  12. import java.util.List;
  13. public class IdChipReader {
  14. // constructor
  15. public IdChipReader() {
  16. UfEventBus.getInstance().on("ZCanCmderWebSocketEvent", this::handleZCanCmderWebSocketEvent);
  17. }
  18. // 事件处理
  19. public void handleZCanCmderWebSocketEvent(Object ... args) {
  20. Integer eventId = (Integer)args[0];
  21. if ( 0x71C8 == eventId ) { // ID 卡插入
  22. UfMdbNotification.action("IdChipInsert");
  23. }
  24. // if ( 0x71C9 == eventId ) { // ID 拔出
  25. // UfMdbNotification.action("IdChipPullout");
  26. // }
  27. }
  28. // 读取数据
  29. public MdbIdChip read() {
  30. var buffer = this.readToByteBuffer();
  31. // parse to model
  32. var chip = new MdbIdChip();
  33. // item name
  34. byte[] itemNameBytes = new byte[15];
  35. buffer.get(0x0001, itemNameBytes);
  36. for (int i = 0; i < itemNameBytes.length; i++) {
  37. itemNameBytes[i] = -1 == itemNameBytes[i] ? 0 : itemNameBytes[i];
  38. }
  39. chip.itemName = new String(itemNameBytes);
  40. chip.itemName = chip.itemName.trim();
  41. // lot code
  42. byte[] lotCodeBytes = new byte[12];
  43. buffer.get(0x0010, lotCodeBytes);
  44. for (int i = 0; i < lotCodeBytes.length; i++) {
  45. lotCodeBytes[i] = -1 == lotCodeBytes[i] ? 0 : lotCodeBytes[i];
  46. }
  47. chip.lotCode = new String(lotCodeBytes);
  48. chip.lotCode = chip.lotCode.trim();
  49. // expire date
  50. byte expYear = buffer.get(0x001C);
  51. byte expMon = buffer.get(0x001D);
  52. byte expDay = buffer.get(0x001E);
  53. chip.expiredDate = String.format("20%02x-%02x-%02x", expYear, expMon, expDay);
  54. // version
  55. chip.version = Integer.toString(buffer.get(0x0021));
  56. // hash
  57. byte itemCode = buffer.get(0x001F);
  58. chip.hash = String.format("%d-%s-%s-%s", itemCode, chip.lotCode, chip.expiredDate, chip.version);
  59. chip.hash = DigestUtils.md5DigestAsHex(chip.hash.getBytes());
  60. return chip;
  61. }
  62. // read id chip data to buffer
  63. private ByteBuffer readToByteBuffer() {
  64. var sectorSizeStr = UfActuatorCmdExecutor.execute("IdCardReader", "module_get_reg", "10301");
  65. var sectorSize = Integer.parseInt(sectorSizeStr);
  66. var sectorNumStr = UfActuatorCmdExecutor.execute("IdCardReader", "module_get_reg", "10302");
  67. var sectorNum = Integer.parseInt(sectorNumStr);
  68. // read bytes
  69. List<Byte> data = new ArrayList<>();
  70. for (int i = 0; i<sectorNum; i++ ) {
  71. String readParam = Integer.toString(i);
  72. String response = UfActuatorCmdExecutor.execute("IdCardReader","a8000_idcard_reader_read_raw", readParam);
  73. Base64.Decoder decoder = Base64.getDecoder();
  74. byte[] bytes = decoder.decode(response);
  75. for (byte aByte : bytes) {
  76. data.add(aByte);
  77. }
  78. }
  79. // convert to buffer
  80. byte[] result = new byte[data.size()];
  81. for ( int i = 0; i < data.size(); i++ ) {
  82. result[i] = data.get(i);
  83. }
  84. var buffer = ByteBuffer.wrap(result);
  85. buffer.order(ByteOrder.LITTLE_ENDIAN);
  86. return buffer;
  87. }
  88. }