|
|
@@ -10,9 +10,11 @@ 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.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
@@ -28,9 +30,9 @@ public class DeJuRFIDServerImpl implements IService {
|
|
|
|
|
|
private static final ISendService sendService = SpringUtils.getBean("sendService");
|
|
|
|
|
|
- private static final Map<String, GClient> CLIENT_MAP = new ConcurrentHashMap<>();
|
|
|
+ private final Map<String, GClient> clientMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
- private static final Map<String, Boolean> ALARM_MAP = new ConcurrentHashMap<>();
|
|
|
+ private final Map<String, Boolean> alarmMap = new ConcurrentHashMap<>();
|
|
|
static AtomicInteger stopCount = new AtomicInteger();
|
|
|
|
|
|
public static final Integer PORT = 8160;
|
|
|
@@ -42,26 +44,29 @@ public class DeJuRFIDServerImpl implements IService {
|
|
|
|
|
|
private static final ScheduledExecutorService scheduledExecutorService = SpringUtils.getBean("scheduledExecutorService");
|
|
|
|
|
|
+ @Resource
|
|
|
+ private RedisTemplate<String, RemoteLabHardware> redisTemplate;
|
|
|
+
|
|
|
@Override
|
|
|
public void start(RemoteLabHardware hardware) {
|
|
|
open(hardware);
|
|
|
}
|
|
|
|
|
|
- private static void open(RemoteLabHardware hardware) {
|
|
|
+ private void open(RemoteLabHardware hardware) {
|
|
|
GClient client;
|
|
|
String ipAddress = hardware.getIpAddress();
|
|
|
- if (CLIENT_MAP.containsKey(ipAddress)) {
|
|
|
- client = CLIENT_MAP.get(ipAddress);
|
|
|
+ if (clientMap.containsKey(ipAddress)) {
|
|
|
+ client = clientMap.get(ipAddress);
|
|
|
} else {
|
|
|
client = new GClient();
|
|
|
- CLIENT_MAP.put(ipAddress, client);
|
|
|
+ clientMap.put(ipAddress, client);
|
|
|
}
|
|
|
if (client.openTcp(ipAddress + HTTP_SEPARATOR + PORT, TIME_OUT)) {
|
|
|
stopMsg(client);
|
|
|
// 功率配置, 将4个天线功率都设置为30dBm.
|
|
|
MsgBaseSetPower msgBaseSetPower = setPower(hardware, client);
|
|
|
if (0 != msgBaseSetPower.getRtCode()) {
|
|
|
- log.error("Power configuration error.");
|
|
|
+ log.info("Power configuration error.");
|
|
|
close(hardware);
|
|
|
open(hardware);
|
|
|
return;
|
|
|
@@ -69,7 +74,7 @@ public class DeJuRFIDServerImpl implements IService {
|
|
|
//天线读卡, 读取EPC数据区以及TID数据区
|
|
|
MsgBaseInventoryEpc msgBaseInventoryEpc = setInventory(hardware, client);
|
|
|
if (0 != msgBaseInventoryEpc.getRtCode()) {
|
|
|
- log.error("Inventory epc error.");
|
|
|
+ log.info("Inventory epc error.");
|
|
|
close(hardware);
|
|
|
open(hardware);
|
|
|
return;
|
|
|
@@ -77,13 +82,14 @@ public class DeJuRFIDServerImpl implements IService {
|
|
|
|
|
|
MsgBaseSetBaseband msgBaseSetBaseband = setSession(hardware, client);
|
|
|
if (0 != msgBaseSetBaseband.getRtCode()) {
|
|
|
- log.error("Session epc error.");
|
|
|
+ log.info("Session epc error.");
|
|
|
close(hardware);
|
|
|
open(hardware);
|
|
|
return;
|
|
|
}
|
|
|
// 订阅标签上报事件
|
|
|
subscribeHandler(client, hardware);
|
|
|
+ redisTemplate.opsForValue().set(client.getSerialNumber(), hardware);
|
|
|
} else {
|
|
|
throw new ServiceException("Connect failure.");
|
|
|
}
|
|
|
@@ -179,14 +185,6 @@ public class DeJuRFIDServerImpl implements IService {
|
|
|
return msgBaseSetBaseband;
|
|
|
}
|
|
|
|
|
|
- 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);
|
|
|
@@ -196,28 +194,28 @@ public class DeJuRFIDServerImpl implements IService {
|
|
|
public void alarm(RemoteLabHardware hardware) {
|
|
|
//灯带设置
|
|
|
String ipAddress = hardware.getIpAddress();
|
|
|
- if (CLIENT_MAP.containsKey(ipAddress)) {
|
|
|
- GClient client = CLIENT_MAP.get(ipAddress);
|
|
|
- alarm(hardware,client);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static void alarm(RemoteLabHardware hardware, GClient client) {
|
|
|
- String ipAddress = hardware.getIpAddress();
|
|
|
- if (!ALARM_MAP.containsKey(ipAddress)) {
|
|
|
- log.info("报警状态:==============》{},设备IP:{}", false, ipAddress);
|
|
|
- changeGpo(ipAddress,1, client, 10);
|
|
|
+ if (clientMap.containsKey(ipAddress)) {
|
|
|
+ GClient client = clientMap.get(ipAddress);
|
|
|
+ boolean alarm = false;
|
|
|
+ if (alarmMap.containsKey(ipAddress)) {
|
|
|
+ alarm = alarmMap.get(ipAddress);
|
|
|
+ }
|
|
|
+ log.info("报警状态:==============》{},设备IP:{}", alarm, ipAddress);
|
|
|
+ if (!alarm) {
|
|
|
+ alarmMap.putIfAbsent(ipAddress,true);
|
|
|
+ changeGpo(ipAddress, 1, client, 10);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void close(RemoteLabHardware hardware) {
|
|
|
+ public void close(RemoteLabHardware hardware) {
|
|
|
String ipAddress = hardware.getIpAddress();
|
|
|
- if (CLIENT_MAP.containsKey(ipAddress)) {
|
|
|
- GClient client = CLIENT_MAP.get(ipAddress);
|
|
|
+ if (clientMap.containsKey(ipAddress)) {
|
|
|
+ GClient client = clientMap.get(ipAddress);
|
|
|
changeGpo(ipAddress, 0, client, 0);
|
|
|
stopMsg(client);
|
|
|
client.close();
|
|
|
- CLIENT_MAP.remove(ipAddress);
|
|
|
+ clientMap.remove(ipAddress);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -229,7 +227,7 @@ public class DeJuRFIDServerImpl implements IService {
|
|
|
log.info("客户端停止读卡{}", result);
|
|
|
}
|
|
|
|
|
|
- private static void changeGpo(String ipAddress, int state, GClient client, int delayTime) {
|
|
|
+ private void changeGpo(String ipAddress, int state, GClient client, int delayTime) {
|
|
|
MsgAppSetGpo msgAppSetGpo = new MsgAppSetGpo();
|
|
|
msgAppSetGpo.setGpo1(state);
|
|
|
msgAppSetGpo.setGpo2(state);
|
|
|
@@ -240,20 +238,19 @@ public class DeJuRFIDServerImpl implements IService {
|
|
|
if (0 == msgAppSetGpo.getRtCode()) {
|
|
|
log.info("============》灯带{}成功.", status);
|
|
|
if (state == 1) {
|
|
|
- ALARM_MAP.put(ipAddress,true);
|
|
|
- stopGpo(ipAddress,client, delayTime);
|
|
|
- }else {
|
|
|
- ALARM_MAP.remove(ipAddress);
|
|
|
+ stopGpo(ipAddress, client, delayTime);
|
|
|
+ } else {
|
|
|
+ alarmMap.remove(ipAddress);
|
|
|
}
|
|
|
} else {
|
|
|
- log.error("Gpo epc {} error.", status);
|
|
|
+ log.info("Gpo epc {} error.", status);
|
|
|
if (stopCount.getAndIncrement() < MAX_RETRY) {
|
|
|
- stopGpo(ipAddress,client, delayTime);
|
|
|
+ stopGpo(ipAddress, client, delayTime);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void stopGpo(String ipAddress,GClient client, int delayTime) {
|
|
|
+ public void stopGpo(String ipAddress, GClient client, int delayTime) {
|
|
|
scheduledExecutorService.schedule(new TimerTask() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
@@ -268,19 +265,19 @@ public class DeJuRFIDServerImpl implements IService {
|
|
|
* @param client 客户端
|
|
|
* @param hardware 设备数据
|
|
|
*/
|
|
|
- public static void subscribeHandler(GClient client, RemoteLabHardware hardware) {
|
|
|
+ public void subscribeHandler(GClient client, RemoteLabHardware hardware) {
|
|
|
String ipAddress = hardware.getIpAddress();
|
|
|
- CLIENT_MAP.computeIfAbsent(ipAddress, f -> client);
|
|
|
+ clientMap.computeIfAbsent(ipAddress, f -> client);
|
|
|
client.onTagEpcLog = (s, logBaseEpcInfo) -> {
|
|
|
if (logBaseEpcInfo.getResult() == 0) {
|
|
|
log.info("===========》{},index: {}", logBaseEpcInfo.getEpc(), logBaseEpcInfo.getAntId());
|
|
|
InventoryTag tag = new InventoryTag();
|
|
|
BeanUtils.copyProperties(logBaseEpcInfo, tag);
|
|
|
- tag.setRemoteLabHardware(hardware);
|
|
|
- boolean send = sendService.send(tag);
|
|
|
- if (send){
|
|
|
- alarm(hardware,client);
|
|
|
- }
|
|
|
+ String readerSerialNumber = logBaseEpcInfo.getReaderSerialNumber();
|
|
|
+ Long replySerialNumber = logBaseEpcInfo.getReplySerialNumber();
|
|
|
+ log.info("===========》读取器设备编码:{},replySerialNumber: {}", readerSerialNumber, replySerialNumber);
|
|
|
+ tag.setSerialNumber(client.getSerialNumber());
|
|
|
+ sendService.send(tag);
|
|
|
}
|
|
|
};
|
|
|
client.onTagEpcOver = (s, logBaseEpcOver) -> log.info("HandlerTagEpcOver");
|