DeJuRFIDService.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package com.zd.netty.sdk;
  2. import com.gg.reader.api.dal.GClient;
  3. import com.gg.reader.api.protocol.gx.*;
  4. import com.zd.common.core.exception.ServiceException;
  5. import com.zd.common.core.utils.SpringUtils;
  6. import com.zd.netty.service.ISendService;
  7. import com.zd.netty.service.IService;
  8. import com.zd.system.api.domain.InventoryTag;
  9. import com.zd.system.api.laboratory.domain.RemoteLabHardware;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.beans.BeanUtils;
  12. import org.springframework.stereotype.Service;
  13. import java.util.*;
  14. import java.util.concurrent.ConcurrentHashMap;
  15. import java.util.concurrent.ScheduledExecutorService;
  16. import java.util.concurrent.TimeUnit;
  17. import java.util.concurrent.atomic.AtomicBoolean;
  18. @Slf4j
  19. @Service
  20. public class DeJuRFIDService implements IService {
  21. private static final ISendService sendService = SpringUtils.getBean("sendService");
  22. private static final Map<String, GClient> clientMap = new ConcurrentHashMap<>();
  23. static AtomicBoolean isAlarm = new AtomicBoolean();
  24. public static final Integer PORT=8160;
  25. private static final ScheduledExecutorService scheduledExecutorService = SpringUtils.getBean("scheduledExecutorService");
  26. @Override
  27. public void start(RemoteLabHardware hardware) {
  28. open(hardware);
  29. }
  30. private static void open(RemoteLabHardware hardware) {
  31. GClient client;
  32. String ipAddress = hardware.getIpAddress();
  33. if (clientMap.containsKey(ipAddress)) {
  34. client = clientMap.get(ipAddress);
  35. } else {
  36. client = new GClient();
  37. clientMap.put(ipAddress, client);
  38. }
  39. if (client.openTcp(ipAddress + ":"+PORT, 2000)) {
  40. // 订阅标签上报事件
  41. subscribeHandler(client,hardware);
  42. // 功率配置, 将4个天线功率都设置为30dBm.
  43. MsgBaseSetPower msgBaseSetPower = setPower(hardware, client);
  44. if (0 != msgBaseSetPower.getRtCode()) {
  45. log.error("Power configuration error.");
  46. reset(client);
  47. close(hardware);
  48. open(hardware);
  49. }
  50. //天线读卡, 读取EPC数据区以及TID数据区
  51. MsgBaseInventoryEpc msgBaseInventoryEpc = setInventory(hardware, client);
  52. if (0 != msgBaseInventoryEpc.getRtCode()) {
  53. log.error("Inventory epc error.");
  54. reset(client);
  55. close(hardware);
  56. open(hardware);
  57. }
  58. } else {
  59. throw new ServiceException("Connect failure.");
  60. }
  61. }
  62. public static MsgBaseInventoryEpc setInventory(RemoteLabHardware hardware, GClient client) {
  63. MsgBaseInventoryEpc msgBaseInventoryEpc = new MsgBaseInventoryEpc();
  64. switch (hardware.getChannels()) {
  65. case 4:
  66. msgBaseInventoryEpc.setAntennaEnable(EnumG.AntennaNo_1 | EnumG.AntennaNo_2 | EnumG.AntennaNo_3 | EnumG.AntennaNo_4);
  67. break;
  68. case 8:
  69. msgBaseInventoryEpc.setAntennaEnable(EnumG.AntennaNo_1 | EnumG.AntennaNo_2 | EnumG.AntennaNo_3 | EnumG.AntennaNo_4 | EnumG.AntennaNo_5 | EnumG.AntennaNo_6 | EnumG.AntennaNo_7 | EnumG.AntennaNo_8);
  70. break;
  71. case 16:
  72. msgBaseInventoryEpc.setAntennaEnable(EnumG.AntennaNo_1 | EnumG.AntennaNo_2 | EnumG.AntennaNo_3 | EnumG.AntennaNo_4 | EnumG.AntennaNo_5 | EnumG.AntennaNo_6 | EnumG.AntennaNo_7 | EnumG.AntennaNo_8 | EnumG.AntennaNo_9 | EnumG.AntennaNo_10 | EnumG.AntennaNo_11 | EnumG.AntennaNo_12 | EnumG.AntennaNo_13 | EnumG.AntennaNo_14 | EnumG.AntennaNo_15 | EnumG.AntennaNo_16);
  73. break;
  74. case 1:
  75. default:
  76. msgBaseInventoryEpc.setAntennaEnable(EnumG.AntennaNo_1);
  77. }
  78. msgBaseInventoryEpc.setInventoryMode(EnumG.InventoryMode_Inventory);
  79. client.sendSynMsg(msgBaseInventoryEpc);
  80. return msgBaseInventoryEpc;
  81. }
  82. public static MsgBaseSetPower setPower(RemoteLabHardware hardware, GClient client) {
  83. Integer uniformPower = hardware.getUniformPower();
  84. MsgBaseGetPower msgBaseGetPower = new MsgBaseGetPower();
  85. client.sendSynMsg(msgBaseGetPower);
  86. MsgBaseSetPower msgBaseSetPower = new MsgBaseSetPower();
  87. if (0 == msgBaseGetPower.getRtCode()) {
  88. Hashtable<Integer, Integer> dicPower = msgBaseGetPower.getDicPower();
  89. Integer integer = dicPower.get(0);
  90. if (!Objects.equals(integer, uniformPower)) {
  91. Hashtable<Integer, Integer> hashtable = new Hashtable<>();
  92. Integer channels = hardware.getChannels();
  93. for (int i = 1; i <= channels; i++) {
  94. hashtable.put(i, uniformPower);
  95. }
  96. msgBaseSetPower.setDicPower(hashtable);
  97. client.sendSynMsg(msgBaseSetPower);
  98. } else {
  99. msgBaseSetPower.setRtCode((byte) 0);
  100. }
  101. }
  102. return msgBaseSetPower;
  103. }
  104. public static void reset(GClient client) {
  105. MsgAppReset msgAppReset = new MsgAppReset();
  106. client.sendSynMsg(msgAppReset);
  107. if (0 != msgAppReset.getRtCode()) {
  108. log.error("Reset epc error.");
  109. }
  110. }
  111. @Override
  112. public void disconnect(RemoteLabHardware hardware) {
  113. close(hardware);
  114. }
  115. @Override
  116. public void alarm(RemoteLabHardware hardware) {
  117. //灯带设置
  118. String ipAddress = hardware.getIpAddress();
  119. if (clientMap.containsKey(ipAddress)){
  120. GClient client = clientMap.get(ipAddress);
  121. if (!isAlarm.get()) {
  122. scheduledExecutorService.execute(() -> {
  123. isAlarm.set(true);
  124. changeGpo(1, client, 10);
  125. });
  126. }
  127. }
  128. }
  129. private static void close(RemoteLabHardware hardware) {
  130. String ipAddress = hardware.getIpAddress();
  131. if (clientMap.containsKey(ipAddress)) {
  132. GClient client = clientMap.get(ipAddress);
  133. changeGpo(0, client, 0);
  134. MsgBaseStop msg = new MsgBaseStop();
  135. // 停止读卡,空闲态
  136. client.sendSynMsg(msg);
  137. client.close();
  138. clientMap.remove(ipAddress);
  139. }
  140. }
  141. private static void changeGpo(int state, GClient client, int delayTime) {
  142. MsgAppSetGpo msgAppSetGpo = new MsgAppSetGpo();
  143. msgAppSetGpo.setGpo1(state);
  144. msgAppSetGpo.setGpo2(state);
  145. client.sendSynMsg(msgAppSetGpo);
  146. String status = state == 1 ? "start" : "stop";
  147. if (0 == msgAppSetGpo.getRtCode()) {
  148. if (state == 1) {
  149. stopGpo(client, delayTime);
  150. } else {
  151. isAlarm.set(false);
  152. }
  153. } else {
  154. log.error("Gpo epc {} error.", status);
  155. if (state == 1) {
  156. stopGpo(client, delayTime);
  157. }
  158. }
  159. }
  160. public static void stopGpo(GClient client, int delayTime) {
  161. scheduledExecutorService.schedule(new TimerTask() {
  162. @Override
  163. public void run() {
  164. changeGpo(0, client, 0);
  165. }
  166. }, delayTime, TimeUnit.SECONDS);
  167. }
  168. /**
  169. * 订阅6c标签信息上报
  170. *
  171. * @param client 客户端
  172. * @param hardware 设备数据
  173. */
  174. public static void subscribeHandler(GClient client, RemoteLabHardware hardware) {
  175. String ipAddress = hardware.getIpAddress();
  176. if (!clientMap.containsKey(ipAddress)){
  177. clientMap.put(ipAddress,client);
  178. }
  179. client.onTagEpcLog = (s, logBaseEpcInfo) -> {
  180. if (logBaseEpcInfo.getResult() == 0) {
  181. log.info("===========》{}", logBaseEpcInfo.getbEpc());
  182. InventoryTag tag = new InventoryTag();
  183. BeanUtils.copyProperties(logBaseEpcInfo, tag);
  184. sendService.send(tag,hardware);
  185. }
  186. };
  187. client.onTagEpcOver = (s, logBaseEpcOver) -> log.info("HandlerTagEpcOver");
  188. }
  189. }