package com.zd.netty.sdk; import com.gg.reader.api.dal.GClient; import com.gg.reader.api.dal.HandlerTagEpcLog; import com.gg.reader.api.dal.HandlerTagEpcOver; import com.gg.reader.api.protocol.gx.*; import com.zd.common.core.exception.ServiceException; 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.Hashtable; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @Slf4j @Service public class DeJuRFIDService implements IService { @Resource private ISendService sendService; private final Map clientMap = new ConcurrentHashMap<>(); @Override public void start(RemoteLabHardware hardware) { GClient client; String ipAddress = hardware.getIpAddress(); if (clientMap.containsKey(ipAddress)){ client=clientMap.get(ipAddress); }else { client = new GClient(); clientMap.put(ipAddress,client); } Integer port = hardware.getPort(); if (client.openTcp(ipAddress+":8160", 2000)) { // 订阅标签上报事件 subscribeHandler(client); // 停止指令,空闲态 MsgBaseStop msgBaseStop = new MsgBaseStop(); client.sendSynMsg(msgBaseStop); if (0 == msgBaseStop.getRtCode()) { log.info("Stop successful."); } else { log.info("Stop error."); } // 功率配置, 将4个天线功率都设置为30dBm. //todo 持久化配置,断电保存 ,请勿做重复配置操作,需要频繁配置时,请先查询校验 MsgBaseSetPower msgBaseSetPower = new MsgBaseSetPower(); Hashtable hashtable = new Hashtable<>(); Integer uniformPower = hardware.getUniformPower(); hashtable.put(1, uniformPower); hashtable.put(2, uniformPower); hashtable.put(3, uniformPower); hashtable.put(4, uniformPower); msgBaseSetPower.setDicPower(hashtable); client.sendSynMsg(msgBaseSetPower); if (0 == msgBaseSetPower.getRtCode()) { log.info("Power configuration successful."); } else { log.info("Power configuration error."); } // 4个天线读卡, 读取EPC数据区以及TID数据区 MsgBaseInventoryEpc msgBaseInventoryEpc = new MsgBaseInventoryEpc(); msgBaseInventoryEpc.setAntennaEnable(EnumG.AntennaNo_1 | EnumG.AntennaNo_2 | EnumG.AntennaNo_3 | EnumG.AntennaNo_4); msgBaseInventoryEpc.setInventoryMode(EnumG.InventoryMode_Inventory); //ParamEpcReadTid tid = new ParamEpcReadTid(); //tid.setMode(EnumG.ParamTidMode_Auto); //tid.setLen(6); //msgBaseInventoryEpc.setReadTid(tid); client.sendSynMsg(msgBaseInventoryEpc); if (0 == msgBaseInventoryEpc.getRtCode()) { log.info("Inventory epc successful."); } else { log.info("Inventory epc error."); } } else { throw new ServiceException("Connect failure."); } } @Override public void disconnect(RemoteLabHardware hardware) { String ipAddress = hardware.getIpAddress(); if (clientMap.containsKey(ipAddress)){ GClient client = clientMap.get(ipAddress); MsgBaseStop msg = new MsgBaseStop(); // 停止读卡,空闲态 client.sendSynMsg(msg); if (0 == msg.getRtCode()) { log.info("Stop successful."); } else { log.info("Stop error."); } log.info("Close the connection"); client.close(); clientMap.remove(ipAddress); } } //订阅6c标签信息上报 private static void subscribeHandler(GClient client) { client.onTagEpcLog = new HandlerTagEpcLog() { @Override public void log(String s, LogBaseEpcInfo logBaseEpcInfo) { log.error("=======================>"+logBaseEpcInfo.getEpc()); if (logBaseEpcInfo.getResult() == 0) { System.out.println(logBaseEpcInfo); } } }; client.onTagEpcOver = new HandlerTagEpcOver() { @Override public void log(String s, LogBaseEpcOver logBaseEpcOver) { System.out.println("HandlerTagEpcOver"); } }; } }