DeJuRFIDService.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.zd.netty.sdk;
  2. import com.gg.reader.api.dal.GClient;
  3. import com.gg.reader.api.dal.HandlerTagEpcLog;
  4. import com.gg.reader.api.dal.HandlerTagEpcOver;
  5. import com.gg.reader.api.protocol.gx.*;
  6. import com.zd.common.core.exception.ServiceException;
  7. import com.zd.netty.service.ISendService;
  8. import com.zd.netty.service.IService;
  9. import com.zd.system.api.domain.InventoryTag;
  10. import com.zd.system.api.laboratory.domain.RemoteLabHardware;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.springframework.beans.BeanUtils;
  13. import org.springframework.stereotype.Service;
  14. import javax.annotation.Resource;
  15. import java.util.Hashtable;
  16. import java.util.Map;
  17. import java.util.concurrent.ConcurrentHashMap;
  18. @Slf4j
  19. @Service
  20. public class DeJuRFIDService implements IService {
  21. @Resource
  22. private ISendService sendService;
  23. private final Map<String, GClient> clientMap = new ConcurrentHashMap<>();
  24. @Override
  25. public void start(RemoteLabHardware hardware) {
  26. GClient client;
  27. String ipAddress = hardware.getIpAddress();
  28. if (clientMap.containsKey(ipAddress)){
  29. client=clientMap.get(ipAddress);
  30. }else {
  31. client = new GClient();
  32. clientMap.put(ipAddress,client);
  33. }
  34. Integer port = hardware.getPort();
  35. if (client.openTcp(ipAddress+":8160", 2000)) {
  36. // 订阅标签上报事件
  37. subscribeHandler(client);
  38. // 停止指令,空闲态
  39. MsgBaseStop msgBaseStop = new MsgBaseStop();
  40. client.sendSynMsg(msgBaseStop);
  41. if (0 == msgBaseStop.getRtCode()) {
  42. log.info("Stop successful.");
  43. } else {
  44. log.info("Stop error.");
  45. }
  46. // 功率配置, 将4个天线功率都设置为30dBm.
  47. //todo 持久化配置,断电保存 ,请勿做重复配置操作,需要频繁配置时,请先查询校验
  48. MsgBaseSetPower msgBaseSetPower = new MsgBaseSetPower();
  49. Hashtable<Integer, Integer> hashtable = new Hashtable<>();
  50. Integer uniformPower = hardware.getUniformPower();
  51. hashtable.put(1, uniformPower);
  52. hashtable.put(2, uniformPower);
  53. hashtable.put(3, uniformPower);
  54. hashtable.put(4, uniformPower);
  55. msgBaseSetPower.setDicPower(hashtable);
  56. client.sendSynMsg(msgBaseSetPower);
  57. if (0 == msgBaseSetPower.getRtCode()) {
  58. log.info("Power configuration successful.");
  59. } else {
  60. log.info("Power configuration error.");
  61. }
  62. // 4个天线读卡, 读取EPC数据区以及TID数据区
  63. MsgBaseInventoryEpc msgBaseInventoryEpc = new MsgBaseInventoryEpc();
  64. msgBaseInventoryEpc.setAntennaEnable(EnumG.AntennaNo_1 | EnumG.AntennaNo_2 | EnumG.AntennaNo_3 | EnumG.AntennaNo_4);
  65. msgBaseInventoryEpc.setInventoryMode(EnumG.InventoryMode_Inventory);
  66. //ParamEpcReadTid tid = new ParamEpcReadTid();
  67. //tid.setMode(EnumG.ParamTidMode_Auto);
  68. //tid.setLen(6);
  69. //msgBaseInventoryEpc.setReadTid(tid);
  70. client.sendSynMsg(msgBaseInventoryEpc);
  71. if (0 == msgBaseInventoryEpc.getRtCode()) {
  72. log.info("Inventory epc successful.");
  73. } else {
  74. log.info("Inventory epc error.");
  75. }
  76. } else {
  77. throw new ServiceException("Connect failure.");
  78. }
  79. }
  80. @Override
  81. public void disconnect(RemoteLabHardware hardware) {
  82. String ipAddress = hardware.getIpAddress();
  83. if (clientMap.containsKey(ipAddress)){
  84. GClient client = clientMap.get(ipAddress);
  85. MsgBaseStop msg = new MsgBaseStop();
  86. // 停止读卡,空闲态
  87. client.sendSynMsg(msg);
  88. if (0 == msg.getRtCode()) {
  89. log.info("Stop successful.");
  90. } else {
  91. log.info("Stop error.");
  92. }
  93. log.info("Close the connection");
  94. client.close();
  95. clientMap.remove(ipAddress);
  96. }
  97. }
  98. //订阅6c标签信息上报
  99. private static void subscribeHandler(GClient client) {
  100. client.onTagEpcLog = new HandlerTagEpcLog() {
  101. @Override
  102. public void log(String s, LogBaseEpcInfo logBaseEpcInfo) {
  103. log.error("=======================>"+logBaseEpcInfo.getEpc());
  104. if (logBaseEpcInfo.getResult() == 0) {
  105. System.out.println(logBaseEpcInfo);
  106. }
  107. }
  108. };
  109. client.onTagEpcOver = new HandlerTagEpcOver() {
  110. @Override
  111. public void log(String s, LogBaseEpcOver logBaseEpcOver) {
  112. System.out.println("HandlerTagEpcOver");
  113. }
  114. };
  115. }
  116. }