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.netty.thread.ThreadPoolTaskConfig; 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.Objects; import java.util.concurrent.ConcurrentHashMap; @Slf4j @Service public class DeJuRFIDService implements IService { @Resource private ISendService sendService; private final Map clientMap = new ConcurrentHashMap<>(); @Resource private ThreadPoolTaskConfig threadPoolTaskConfig; @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); } if (client.openTcp(ipAddress + ":8160", 2000)) { // 订阅标签上报事件 subscribeHandler(client); // 功率配置, 将4个天线功率都设置为30dBm. Integer uniformPower = hardware.getUniformPower(); MsgBaseGetPower msgBaseGetPower = new MsgBaseGetPower(); client.sendSynMsg(msgBaseGetPower); if (0 == msgBaseGetPower.getRtCode()) { Hashtable dicPower = msgBaseGetPower.getDicPower(); Integer integer = dicPower.get(0); if (!Objects.equals(integer, uniformPower)) { MsgBaseSetPower msgBaseSetPower = new MsgBaseSetPower(); Hashtable hashtable = new Hashtable<>(); Integer channels = hardware.getChannels(); for (int i = 1; i <= channels; i++) { hashtable.put(i, uniformPower); } msgBaseSetPower.setDicPower(hashtable); client.sendSynMsg(msgBaseSetPower); if (0 == msgBaseSetPower.getRtCode()) { log.info("Power configuration successful."); } else { log.info("Power configuration error."); } } } else { log.info("Power configuration error."); } //蜂鸣器设置 // MsgAppSetBeep msgAppSetBeep = new MsgAppSetBeep(); // msgAppSetBeep.setBeepMode(hardware.getSessionIndex() > 1 ? 1 : 0); // msgAppSetBeep.setBeepStatus(1); // client.sendSynMsg(msgAppSetBeep); // // if (0 == msgAppSetBeep.getRtCode()) { // log.info("Beep epc successful."); // } else { // log.info("Beep epc error."); // } //天线读卡, 读取EPC数据区以及TID数据区 MsgBaseInventoryEpc msgBaseInventoryEpc = new MsgBaseInventoryEpc(); switch (hardware.getChannels()) { case 4: msgBaseInventoryEpc.setAntennaEnable(EnumG.AntennaNo_1 | EnumG.AntennaNo_2 | EnumG.AntennaNo_3 | EnumG.AntennaNo_4); break; case 8: 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); break; case 16: 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); break; case 1: default: msgBaseInventoryEpc.setAntennaEnable(EnumG.AntennaNo_1); } msgBaseInventoryEpc.setInventoryMode(EnumG.InventoryMode_Inventory); 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); MsgAppSetGpo msgAppSetGpo=new MsgAppSetGpo(); msgAppSetGpo.setGpo1(0); msgAppSetGpo.setGpo2(0); msgAppSetGpo.setGpo3(0); msgAppSetGpo.setGpo4(0); msgAppSetGpo.setGpo5(0); msgAppSetGpo.setGpo6(0); msgAppSetGpo.setGpo7(0); msgAppSetGpo.setGpo8(0); client.sendSynMsg(msgAppSetGpo); if (0 == msgAppSetGpo.getRtCode()) { log.info("Gpo epc stop successful."); } else { log.error("Gpo epc stop error."); } 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标签信息上报 * * @param client 客户端 */ private void subscribeHandler(GClient client) { client.onTagEpcLog = (s, logBaseEpcInfo) -> { if (logBaseEpcInfo.getResult() == 0) { //灯带设置 MsgAppSetGpo msgAppSetGpo=new MsgAppSetGpo(); msgAppSetGpo.setGpo1(1); msgAppSetGpo.setGpo2(1); msgAppSetGpo.setGpo3(1); msgAppSetGpo.setGpo4(1); msgAppSetGpo.setGpo5(1); msgAppSetGpo.setGpo6(1); msgAppSetGpo.setGpo7(1); msgAppSetGpo.setGpo8(1); client.sendSynMsg(msgAppSetGpo); if (0 == msgAppSetGpo.getRtCode()) { log.info("Gpo epc successful."); } else { log.error("Gpo epc error."); } threadPoolTaskConfig.getAsyncExecutor().execute(() -> { msgAppSetGpo.setGpo1(0); msgAppSetGpo.setGpo2(0); msgAppSetGpo.setGpo3(0); msgAppSetGpo.setGpo4(0); msgAppSetGpo.setGpo5(0); msgAppSetGpo.setGpo6(0); msgAppSetGpo.setGpo7(0); msgAppSetGpo.setGpo8(0); client.sendSynMsg(msgAppSetGpo); if (0 == msgAppSetGpo.getRtCode()) { log.info("Gpo epc stop successful."); } else { log.error("Gpo epc stop error."); } },8*1000); InventoryTag tag = new InventoryTag(); BeanUtils.copyProperties(logBaseEpcInfo, tag); sendService.send(tag); } }; client.onTagEpcOver = (s, logBaseEpcOver) -> log.info("HandlerTagEpcOver"); } }