|
|
@@ -32,17 +32,20 @@ import java.util.concurrent.TimeUnit;
|
|
|
@Service
|
|
|
public class RfidClientImpl implements IService {
|
|
|
private static final ISendService sendService = SpringUtils.getBean("sendService");
|
|
|
-
|
|
|
private static final Map<String, GClient> clientMap = new ConcurrentHashMap<>();
|
|
|
private static final Map<String, GServer> serverMap = new ConcurrentHashMap<>();
|
|
|
- //传送方向
|
|
|
- private static final Map<String, Integer> directionMap = new ConcurrentHashMap<>();
|
|
|
private static final ScheduledExecutorService scheduledExecutorService = SpringUtils.getBean("scheduledExecutorService");
|
|
|
@Resource
|
|
|
private RedisTemplate<String, HardwareRfid> redisTemplate;
|
|
|
@Autowired
|
|
|
private HardwareRfidService hardwareRfidService;
|
|
|
|
|
|
+ /**
|
|
|
+ * 启动读写器
|
|
|
+ *
|
|
|
+ * @Param [hardwareRfid 硬件信息]
|
|
|
+ * @Return void
|
|
|
+ **/
|
|
|
@Override
|
|
|
public void start(HardwareRfid hardwareRfid) {
|
|
|
GServer server = new GServer();
|
|
|
@@ -51,7 +54,7 @@ public class RfidClientImpl implements IService {
|
|
|
log.info("【RFID门禁机】开始监听");
|
|
|
subscribeServerHandler(hardwareRfid, server);
|
|
|
} else {
|
|
|
- log.info("【RFID门禁机】监听失败");
|
|
|
+ log.info("【RFID门禁机】监听失败,请检查端口号是否被占用!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -63,51 +66,82 @@ public class RfidClientImpl implements IService {
|
|
|
server.onGClientConnected = new HandlerGClientConnected() {
|
|
|
@Override
|
|
|
public void log(GClient client, String serialNumber) {
|
|
|
- String ipAddress = getIp(hardwareRfid);
|
|
|
- if (clientMap.containsKey(ipAddress)) {
|
|
|
- log.info("此ip已存在客户端!");
|
|
|
- return;
|
|
|
+ try {
|
|
|
+ log.info("监听到远程读写设备主动连接...");
|
|
|
+ //初始化服务map
|
|
|
+ initServicesMap(client, server, hardwareRfid);
|
|
|
+ //开启心跳检测Tcp连接状态
|
|
|
+ client.setSendHeartBeat(true);
|
|
|
+ client.setPrint(true);
|
|
|
+ //订阅Tcp断连上报
|
|
|
+ subscribeTcpHandler(client, hardwareRfid);
|
|
|
+ //设置rfid初始化参数
|
|
|
+ setConfig(client, hardwareRfid);
|
|
|
+ //订阅报告启动事件
|
|
|
+ subscribeReportedStartEvents(client, hardwareRfid);
|
|
|
+ //订阅报告结束事件
|
|
|
+ subscribeReportedOverEvents(client, hardwareRfid);
|
|
|
+ //修改设备状态
|
|
|
+ changStatus(hardwareRfid, DeviceStatusEnum.ON_LINE);
|
|
|
+ log.info("【RFID门禁机】监听成功!{}", client.getName());
|
|
|
+ } catch (Exception e) {
|
|
|
+ changStatus(hardwareRfid, DeviceStatusEnum.EXCEPTION);
|
|
|
+ log.error("远程读写设备主动连接报错,请联系管理员!{}", e);
|
|
|
}
|
|
|
- //存储客户端
|
|
|
- clientMap.put(ipAddress, client);
|
|
|
- //储存服务端
|
|
|
- serverMap.put(ipAddress, server);
|
|
|
- //开启心跳检测Tcp连接状态
|
|
|
- client.setSendHeartBeat(true);
|
|
|
- client.setPrint(true);
|
|
|
- //订阅Tcp断连上报
|
|
|
- subscribeTcpHandler(client,hardwareRfid);
|
|
|
- //设置rfid初始化参数
|
|
|
- setConfig(client, hardwareRfid);
|
|
|
- //订阅报告启动事件
|
|
|
- subscribeReportedStartEvents(client, hardwareRfid);
|
|
|
- //订阅报告结束事件
|
|
|
- subscribeReportedOverEvents(client, hardwareRfid);
|
|
|
- //修改设备状态
|
|
|
- changStatus(hardwareRfid,DeviceStatusEnum.ON_LINE.getCode());
|
|
|
- log.info("【RFID门禁机】监听成功!{}", client.getName());
|
|
|
}
|
|
|
};
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 初始化保存客户端/服务端
|
|
|
+ *
|
|
|
+ * @param client 客户端
|
|
|
+ * @param server 服务端
|
|
|
+ * @param hardwareRfid 设备信息
|
|
|
+ */
|
|
|
+ private void initServicesMap(GClient client, GServer server, HardwareRfid hardwareRfid) {
|
|
|
+ String hardwareNum = hardwareRfid.getHardwareNum();
|
|
|
+ if (StringUtils.isBlank(hardwareNum)) {
|
|
|
+ log.info("设备编码为空!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (clientMap.containsKey(hardwareNum)) {
|
|
|
+ log.info("此序列编号已存在客户端,删除已存在客户端!");
|
|
|
+ clientMap.remove(hardwareNum);
|
|
|
+ }
|
|
|
+ if (serverMap.containsKey(hardwareNum)) {
|
|
|
+ log.info("此序列编号已存在服务端,删除已存在服务端!");
|
|
|
+ serverMap.remove(hardwareNum);
|
|
|
+ }
|
|
|
+ //存储客户端
|
|
|
+ clientMap.put(hardwareNum, client);
|
|
|
+ //储存服务端
|
|
|
+ serverMap.put(hardwareNum, server);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 订阅TCP断开连接上报
|
|
|
*/
|
|
|
- private void subscribeTcpHandler(GClient client,HardwareRfid hardwareRfid) {
|
|
|
+ private void subscribeTcpHandler(GClient client, HardwareRfid hardwareRfid) {
|
|
|
client.onDisconnected = new HandlerTcpDisconnected() {
|
|
|
@Override
|
|
|
public void log(String ipPort) {
|
|
|
- log.warn("【RFID门禁机】连接已断开:{}", ipPort);
|
|
|
- changStatus(hardwareRfid,DeviceStatusEnum.OFF_LINE.getCode());
|
|
|
+ log.info("【RFID门禁机】连接已断开:{}", ipPort);
|
|
|
+ changStatus(hardwareRfid, DeviceStatusEnum.OFF_LINE);
|
|
|
//释放当前连接资源
|
|
|
client.close();
|
|
|
//删除客户端map数据
|
|
|
- String[] split = ipPort.split(":");
|
|
|
- clientMap.remove(split[0]);
|
|
|
+ clientMap.remove(hardwareRfid.getHardwareNum());
|
|
|
}
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 订阅报告启动事件
|
|
|
+ *
|
|
|
+ * @param client 客户端
|
|
|
+ * @param hardwareRfid 硬件信息
|
|
|
+ */
|
|
|
private void subscribeReportedStartEvents(GClient client, HardwareRfid hardwareRfid) {
|
|
|
// 订阅标签上报事件
|
|
|
client.onTagEpcLog = new HandlerTagEpcLog() {
|
|
|
@@ -140,6 +174,12 @@ public class RfidClientImpl implements IService {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 订阅报告结束事件
|
|
|
+ *
|
|
|
+ * @param client 客户端
|
|
|
+ * @param hardwareRfid 硬件信息
|
|
|
+ */
|
|
|
private void subscribeReportedOverEvents(GClient client, HardwareRfid hardwareRfid) {
|
|
|
client.onTagEpcOver = new HandlerTagEpcOver() {
|
|
|
@Override
|
|
|
@@ -151,6 +191,12 @@ public class RfidClientImpl implements IService {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 设置rfid初始化参数
|
|
|
+ *
|
|
|
+ * @param client 客户顿
|
|
|
+ * @param hardwareRfid 硬件信息
|
|
|
+ */
|
|
|
private void setConfig(GClient client, HardwareRfid hardwareRfid) {
|
|
|
try {
|
|
|
// 读写器停止读写卡
|
|
|
@@ -169,17 +215,21 @@ public class RfidClientImpl implements IService {
|
|
|
}
|
|
|
//天线读卡, 读取EPC数据区以及TID数据区
|
|
|
setInventory(client, hardwareRfid);
|
|
|
- //读写器传输方向
|
|
|
- //directionMap.put("direction",1);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- changStatus(hardwareRfid,DeviceStatusEnum.EXCEPTION.getCode());
|
|
|
- log.error("设置门禁机异常,请联系管理员!");
|
|
|
+ changStatus(hardwareRfid, DeviceStatusEnum.EXCEPTION);
|
|
|
+ log.error("设置门禁机参数异常,请联系管理员!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void changStatus(HardwareRfid hardwareRfid,Integer code) {
|
|
|
- hardwareRfid.setDeviceStatus(code);
|
|
|
+ /**
|
|
|
+ * 修改RFID硬件状态
|
|
|
+ *
|
|
|
+ * @param hardwareRfid 硬件信息
|
|
|
+ * @param deviceStatusEnum 设备状态枚举类
|
|
|
+ */
|
|
|
+ private void changStatus(HardwareRfid hardwareRfid, DeviceStatusEnum deviceStatusEnum) {
|
|
|
+ hardwareRfid.setDeviceStatus(deviceStatusEnum.getCode());
|
|
|
hardwareRfidService.updateHardwareRfid(hardwareRfid);
|
|
|
}
|
|
|
|
|
|
@@ -311,7 +361,14 @@ public class RfidClientImpl implements IService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private boolean changeGpo(String ipAddress, int state, GClient client, int delayTime) {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param client 客户端
|
|
|
+ * @param state 报警状态(0不报警,1报警)
|
|
|
+ * @param delayTime 报警持续时间(单位:秒)
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private boolean changeGpo(GClient client, int state, int delayTime) {
|
|
|
log.info("进入控制报警方法!是否开启={}", state);
|
|
|
MsgAppSetGpo msgAppSetGpo = new MsgAppSetGpo();
|
|
|
msgAppSetGpo.setGpo1(state);
|
|
|
@@ -323,7 +380,7 @@ public class RfidClientImpl implements IService {
|
|
|
if (0 == msgAppSetGpo.getRtCode()) {
|
|
|
log.info("【RFID门禁机】灯带报警{}成功!", status);
|
|
|
if (state == 1) {
|
|
|
- stopGpo(ipAddress, client, delayTime);
|
|
|
+ stopGpo(client, delayTime);
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
@@ -332,14 +389,19 @@ public class RfidClientImpl implements IService {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 断开设备链接
|
|
|
+ *
|
|
|
+ * @param hardwareRfid 设备数据
|
|
|
+ */
|
|
|
@Override
|
|
|
public void disconnect(HardwareRfid hardwareRfid) {
|
|
|
- String ip = getIp(hardwareRfid);
|
|
|
- if (clientMap.containsKey(ip)) {
|
|
|
- GClient client = clientMap.get(ip);
|
|
|
- GServer server = serverMap.get(ip);
|
|
|
+ String hardwareNum = hardwareRfid.getHardwareNum();
|
|
|
+ if (clientMap.containsKey(hardwareNum)) {
|
|
|
+ GClient client = clientMap.get(hardwareNum);
|
|
|
+ GServer server = serverMap.get(hardwareNum);
|
|
|
//关闭报警
|
|
|
- changeGpo(ip, 0, client, 0);
|
|
|
+ changeGpo(client, 0, 0);
|
|
|
//停止读写卡
|
|
|
stopMsg(client);
|
|
|
//客户端资源释放
|
|
|
@@ -347,9 +409,9 @@ public class RfidClientImpl implements IService {
|
|
|
//服务端接口释放
|
|
|
server.close();
|
|
|
//客户端清除
|
|
|
- clientMap.remove(ip);
|
|
|
+ clientMap.remove(hardwareNum);
|
|
|
//服务端缓存清除
|
|
|
- serverMap.remove(ip);
|
|
|
+ serverMap.remove(hardwareNum);
|
|
|
log.info("【RFID门禁机】关闭成功!");
|
|
|
}
|
|
|
|
|
|
@@ -357,6 +419,9 @@ public class RfidClientImpl implements IService {
|
|
|
|
|
|
/**
|
|
|
* 停止读写卡
|
|
|
+ *
|
|
|
+ * @param client 客户端
|
|
|
+ * @return
|
|
|
*/
|
|
|
public boolean stopMsg(GClient client) {
|
|
|
// 停止指令,空闲态
|
|
|
@@ -371,26 +436,36 @@ public class RfidClientImpl implements IService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 报警实现
|
|
|
+ *
|
|
|
+ * @param hardwareRfid 硬件信息
|
|
|
+ * @return
|
|
|
+ */
|
|
|
@Override
|
|
|
public boolean alarm(HardwareRfid hardwareRfid) {
|
|
|
log.info("收到调用报警!");
|
|
|
//灯带设置
|
|
|
- String ipAddress = getIp(hardwareRfid);
|
|
|
- if (clientMap.containsKey(ipAddress)) {
|
|
|
- GClient client = clientMap.get(ipAddress);
|
|
|
+ String hardwareNum = hardwareRfid.getHardwareNum();
|
|
|
+ if (clientMap.containsKey(hardwareNum)) {
|
|
|
+ GClient client = clientMap.get(hardwareNum);
|
|
|
synchronized (this) {
|
|
|
//设置开始报警、报警时长
|
|
|
- return changeGpo(ipAddress, 1, client, 1);
|
|
|
+ return changeGpo(client, 1, 1);
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- public void stopGpo(String ipAddress, GClient client, int delayTime) {
|
|
|
+ /**
|
|
|
+ * @param client 客户端
|
|
|
+ * @param delayTime 报警持续时间(单位:秒)
|
|
|
+ */
|
|
|
+ public void stopGpo(GClient client, int delayTime) {
|
|
|
scheduledExecutorService.schedule(new TimerTask() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
- changeGpo(ipAddress, 0, client, 0);
|
|
|
+ changeGpo(client, 0, 0);
|
|
|
}
|
|
|
}, delayTime, TimeUnit.SECONDS);
|
|
|
}
|
|
|
@@ -414,7 +489,10 @@ public class RfidClientImpl implements IService {
|
|
|
|
|
|
/**
|
|
|
* 获取端口号 未配置端口号默认给到8160
|
|
|
- */
|
|
|
+ *
|
|
|
+ * @Param [ipPort]
|
|
|
+ * @Return int
|
|
|
+ **/
|
|
|
private static int getPort(String ipPort) {
|
|
|
//默认给到8160
|
|
|
int port = 8160;
|