|
|
@@ -0,0 +1,148 @@
|
|
|
+package com.zd.netty.sdk;
|
|
|
+
|
|
|
+import com.gg.reader.api.dal.GClient;
|
|
|
+import com.gg.reader.api.dal.GServer;
|
|
|
+import com.gg.reader.api.protocol.gx.MsgAppSetGpo;
|
|
|
+import com.gg.reader.api.protocol.gx.MsgBaseStop;
|
|
|
+import com.zd.common.core.utils.SpringUtils;
|
|
|
+import com.zd.netty.service.ISendService;
|
|
|
+import com.zd.netty.service.IService;
|
|
|
+import com.zd.system.api.domain.InventoryTag;
|
|
|
+import com.zd.system.api.laboratory.domain.RemoteLabHardware;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.TimerTask;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.concurrent.ScheduledExecutorService;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
+
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class DeJuRFIDListenerService implements IService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ISendService sendService;
|
|
|
+
|
|
|
+ private final Map<String, GClient> clientMap = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ AtomicBoolean isAlarm = new AtomicBoolean();
|
|
|
+
|
|
|
+ static GClient client;
|
|
|
+
|
|
|
+ private final ScheduledExecutorService scheduledExecutorService = SpringUtils.getBean("scheduledExecutorService");
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void start(RemoteLabHardware hardware) {
|
|
|
+ String ipAddress = hardware.getIpAddress();
|
|
|
+ if (clientMap.containsKey(ipAddress)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ GServer server = new GServer();
|
|
|
+ if (server.open(9190)) {
|
|
|
+ subscribeServerHandler(server);
|
|
|
+ if (client!=null){
|
|
|
+ clientMap.put(ipAddress,client);
|
|
|
+ }
|
|
|
+ log.info("开始监听");
|
|
|
+ } else {
|
|
|
+ log.info("监听失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void disconnect(RemoteLabHardware hardware) {
|
|
|
+ stop();
|
|
|
+ clientMap.remove(hardware.getIpAddress());
|
|
|
+ }
|
|
|
+
|
|
|
+ //订阅监听上报
|
|
|
+ private void subscribeServerHandler(GServer server) {
|
|
|
+ server.onGClientConnected = (gClient, serialNumber) -> {
|
|
|
+ log.info(gClient.getName() + "---监听成功");
|
|
|
+ client = gClient;//切换连接对象
|
|
|
+ client.setSendHeartBeat(true);//开启心跳检测Tcp连接状态
|
|
|
+ client.setPrint(true);
|
|
|
+ subscribeTcpHandler();//订阅Tcp断连上报
|
|
|
+ subscribeHandler(client);
|
|
|
+ stop();//测试监听成功的连接是否通信正常
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ //订阅TCP断开连接上报
|
|
|
+ private static void subscribeTcpHandler() {
|
|
|
+ client.onDisconnected = s -> {
|
|
|
+ log.info("连接" + s + "已断开");
|
|
|
+ client.close();//释放当前连接资源
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void stop() {
|
|
|
+ MsgBaseStop msg = new MsgBaseStop();
|
|
|
+ client.sendSynMsg(msg);
|
|
|
+ if (0x00 == msg.getRtCode()) {
|
|
|
+ log.info("Stop success");
|
|
|
+ } else {
|
|
|
+ log.info(msg.getRtMsg());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 订阅6c标签信息上报
|
|
|
+ *
|
|
|
+ * @param client 客户端
|
|
|
+ */
|
|
|
+ private void subscribeHandler(GClient client) {
|
|
|
+ client.onTagEpcLog = (s, logBaseEpcInfo) -> {
|
|
|
+ if (logBaseEpcInfo.getResult() == 0) {
|
|
|
+ log.info("===========》{}", logBaseEpcInfo.getbEpc());
|
|
|
+ //灯带设置
|
|
|
+ if (!isAlarm.get()) {
|
|
|
+ scheduledExecutorService.execute(() -> {
|
|
|
+ isAlarm.set(true);
|
|
|
+ changeGpo(1, client, 10);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ InventoryTag tag = new InventoryTag();
|
|
|
+ BeanUtils.copyProperties(logBaseEpcInfo, tag);
|
|
|
+ sendService.send(tag);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ client.onTagEpcOver = (s, logBaseEpcOver) -> log.info("HandlerTagEpcOver");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void changeGpo(int state, GClient client, int delayTime) {
|
|
|
+ MsgAppSetGpo msgAppSetGpo = new MsgAppSetGpo();
|
|
|
+ msgAppSetGpo.setGpo1(state);
|
|
|
+ msgAppSetGpo.setGpo2(state);
|
|
|
+
|
|
|
+ client.sendSynMsg(msgAppSetGpo);
|
|
|
+ String status = state == 1 ? "start" : "stop";
|
|
|
+ if (0 == msgAppSetGpo.getRtCode()) {
|
|
|
+ if (state == 1) {
|
|
|
+ stopGpo(client, delayTime);
|
|
|
+ } else {
|
|
|
+ isAlarm.set(false);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.error("Gpo epc {} error.", status);
|
|
|
+ if (state == 1) {
|
|
|
+ stopGpo(client, delayTime);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void stopGpo(GClient client, int delayTime) {
|
|
|
+ scheduledExecutorService.schedule(new TimerTask() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ changeGpo(0, client, 0);
|
|
|
+ }
|
|
|
+ }, delayTime, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+}
|