| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- package com.zd.netty.sdk;
- import com.gg.reader.api.dal.GClient;
- import com.gg.reader.api.protocol.gx.*;
- import com.zd.common.core.exception.ServiceException;
- 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 java.util.*;
- 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 DeJuRFIDService implements IService {
- private static final ISendService sendService = SpringUtils.getBean("sendService");
- private static final Map<String, GClient> clientMap = new ConcurrentHashMap<>();
- static AtomicBoolean isAlarm = new AtomicBoolean();
- public static final Integer PORT=8160;
- private static final ScheduledExecutorService scheduledExecutorService = SpringUtils.getBean("scheduledExecutorService");
- @Override
- public void start(RemoteLabHardware hardware) {
- open(hardware);
- }
- private static void open(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 + ":"+PORT, 2000)) {
- // 订阅标签上报事件
- subscribeHandler(client,hardware);
- // 功率配置, 将4个天线功率都设置为30dBm.
- MsgBaseSetPower msgBaseSetPower = setPower(hardware, client);
- if (0 != msgBaseSetPower.getRtCode()) {
- log.error("Power configuration error.");
- reset(client);
- close(hardware);
- open(hardware);
- }
- //天线读卡, 读取EPC数据区以及TID数据区
- MsgBaseInventoryEpc msgBaseInventoryEpc = setInventory(hardware, client);
- if (0 != msgBaseInventoryEpc.getRtCode()) {
- log.error("Inventory epc error.");
- reset(client);
- close(hardware);
- open(hardware);
- }
- } else {
- throw new ServiceException("Connect failure.");
- }
- }
- public static MsgBaseInventoryEpc setInventory(RemoteLabHardware hardware, GClient client) {
- 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);
- return msgBaseInventoryEpc;
- }
- public static MsgBaseSetPower setPower(RemoteLabHardware hardware, GClient client) {
- Integer uniformPower = hardware.getUniformPower();
- MsgBaseGetPower msgBaseGetPower = new MsgBaseGetPower();
- client.sendSynMsg(msgBaseGetPower);
- MsgBaseSetPower msgBaseSetPower = new MsgBaseSetPower();
- if (0 == msgBaseGetPower.getRtCode()) {
- Hashtable<Integer, Integer> dicPower = msgBaseGetPower.getDicPower();
- Integer integer = dicPower.get(0);
- if (!Objects.equals(integer, uniformPower)) {
- Hashtable<Integer, Integer> hashtable = new Hashtable<>();
- Integer channels = hardware.getChannels();
- for (int i = 1; i <= channels; i++) {
- hashtable.put(i, uniformPower);
- }
- msgBaseSetPower.setDicPower(hashtable);
- client.sendSynMsg(msgBaseSetPower);
- } else {
- msgBaseSetPower.setRtCode((byte) 0);
- }
- }
- return msgBaseSetPower;
- }
- public static void reset(GClient client) {
- MsgAppReset msgAppReset = new MsgAppReset();
- client.sendSynMsg(msgAppReset);
- if (0 != msgAppReset.getRtCode()) {
- log.error("Reset epc error.");
- }
- }
- @Override
- public void disconnect(RemoteLabHardware hardware) {
- close(hardware);
- }
- @Override
- public void alarm(RemoteLabHardware hardware) {
- //灯带设置
- String ipAddress = hardware.getIpAddress();
- if (clientMap.containsKey(ipAddress)){
- GClient client = clientMap.get(ipAddress);
- if (!isAlarm.get()) {
- scheduledExecutorService.execute(() -> {
- isAlarm.set(true);
- changeGpo(1, client, 10);
- });
- }
- }
- }
- private static void close(RemoteLabHardware hardware) {
- String ipAddress = hardware.getIpAddress();
- if (clientMap.containsKey(ipAddress)) {
- GClient client = clientMap.get(ipAddress);
- changeGpo(0, client, 0);
- MsgBaseStop msg = new MsgBaseStop();
- // 停止读卡,空闲态
- client.sendSynMsg(msg);
- client.close();
- clientMap.remove(ipAddress);
- }
- }
- private static 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 static void stopGpo(GClient client, int delayTime) {
- scheduledExecutorService.schedule(new TimerTask() {
- @Override
- public void run() {
- changeGpo(0, client, 0);
- }
- }, delayTime, TimeUnit.SECONDS);
- }
- /**
- * 订阅6c标签信息上报
- *
- * @param client 客户端
- * @param hardware 设备数据
- */
- public static void subscribeHandler(GClient client, RemoteLabHardware hardware) {
- String ipAddress = hardware.getIpAddress();
- if (!clientMap.containsKey(ipAddress)){
- clientMap.put(ipAddress,client);
- }
- client.onTagEpcLog = (s, logBaseEpcInfo) -> {
- if (logBaseEpcInfo.getResult() == 0) {
- log.info("===========》{}", logBaseEpcInfo.getbEpc());
- InventoryTag tag = new InventoryTag();
- BeanUtils.copyProperties(logBaseEpcInfo, tag);
- sendService.send(tag,hardware);
- }
- };
- client.onTagEpcOver = (s, logBaseEpcOver) -> log.info("HandlerTagEpcOver");
- }
- }
|