|
@@ -4,25 +4,34 @@ import com.zd.common.core.alert.DingTalkAlert;
|
|
|
import com.zd.common.core.redis.RedisService;
|
|
import com.zd.common.core.redis.RedisService;
|
|
|
import com.zd.common.core.utils.DateUtils;
|
|
import com.zd.common.core.utils.DateUtils;
|
|
|
import com.zd.common.core.utils.SpringUtils;
|
|
import com.zd.common.core.utils.SpringUtils;
|
|
|
|
|
+import com.zd.common.core.utils.StringUtils;
|
|
|
|
|
+import com.zd.laboratory.constant.RelayConstants;
|
|
|
import com.zd.laboratory.domain.LabAbnormal;
|
|
import com.zd.laboratory.domain.LabAbnormal;
|
|
|
import com.zd.laboratory.domain.LabHardware;
|
|
import com.zd.laboratory.domain.LabHardware;
|
|
|
|
|
+import com.zd.laboratory.domain.LabRelay;
|
|
|
import com.zd.laboratory.domain.vo.LabHardwareVO;
|
|
import com.zd.laboratory.domain.vo.LabHardwareVO;
|
|
|
|
|
+import com.zd.laboratory.event.LabRelayNrStatusEvent;
|
|
|
|
|
+import com.zd.laboratory.event.LabRelayRegisterEvent;
|
|
|
import com.zd.laboratory.mapper.LabAbnormalMapper;
|
|
import com.zd.laboratory.mapper.LabAbnormalMapper;
|
|
|
import com.zd.laboratory.mapper.LabHardwareMapper;
|
|
import com.zd.laboratory.mapper.LabHardwareMapper;
|
|
|
|
|
+import com.zd.laboratory.mqtt.entiy.EquipmentStatus;
|
|
|
|
|
+import com.zd.laboratory.mqtt.entiy.LabRelayStatus;
|
|
|
|
|
+import com.zd.laboratory.service.ILabHardwareService;
|
|
|
import com.zd.laboratory.socket.constant.JXCTPacket;
|
|
import com.zd.laboratory.socket.constant.JXCTPacket;
|
|
|
import com.zd.laboratory.socket.constant.SocketTypes;
|
|
import com.zd.laboratory.socket.constant.SocketTypes;
|
|
|
import com.zd.laboratory.socket.runner.TCPServer;
|
|
import com.zd.laboratory.socket.runner.TCPServer;
|
|
|
import com.zd.laboratory.socket.service.BaseRouter;
|
|
import com.zd.laboratory.socket.service.BaseRouter;
|
|
|
import com.zd.laboratory.utils.CRCCHECK;
|
|
import com.zd.laboratory.utils.CRCCHECK;
|
|
|
|
|
+import com.zd.laboratory.utils.RelayUtils;
|
|
|
|
|
+import com.zd.model.enums.HardwareOperate;
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
import io.netty.channel.ChannelInboundHandler;
|
|
import io.netty.channel.ChannelInboundHandler;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
|
|
+import java.net.Socket;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
-import java.util.concurrent.Executors;
|
|
|
|
|
-import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
+import java.util.concurrent.*;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 服务器端handler
|
|
* 服务器端handler
|
|
@@ -37,10 +46,9 @@ public class NettyServerHandler implements ChannelInboundHandler {
|
|
|
public void channelRead(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
|
|
public void channelRead(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
|
|
|
byte[] msg = (byte[]) o;
|
|
byte[] msg = (byte[]) o;
|
|
|
String data = TCPServer.bytesToHexString(msg).toUpperCase();
|
|
String data = TCPServer.bytesToHexString(msg).toUpperCase();
|
|
|
- log.info("netty服务端接受消息为:" + data);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ String dataStr=TCPServer.hexStr2Str(data);
|
|
|
|
|
+ log.info("**************************************"+dataStr+"****************************************");
|
|
|
if(data.startsWith(SocketTypes.RELAY_PREFIX.replaceAll(" ", "").toUpperCase())){
|
|
if(data.startsWith(SocketTypes.RELAY_PREFIX.replaceAll(" ", "").toUpperCase())){
|
|
|
-
|
|
|
|
|
analyticRelayData(data, channelHandlerContext);
|
|
analyticRelayData(data, channelHandlerContext);
|
|
|
}else if(data.startsWith(SocketTypes.SZZQ_PREFIX)){
|
|
}else if(data.startsWith(SocketTypes.SZZQ_PREFIX)){
|
|
|
// 采集器规范定义 hex发送 结构 31 11 01 01 后两位递增方式增加
|
|
// 采集器规范定义 hex发送 结构 31 11 01 01 后两位递增方式增加
|
|
@@ -48,25 +56,31 @@ public class NettyServerHandler implements ChannelInboundHandler {
|
|
|
// 将通道加入ChannelMap
|
|
// 将通道加入ChannelMap
|
|
|
if(relayCode.length() == data.length()){
|
|
if(relayCode.length() == data.length()){
|
|
|
ChannelMap.getChannelMap().put(data, channelHandlerContext);
|
|
ChannelMap.getChannelMap().put(data, channelHandlerContext);
|
|
|
-
|
|
|
|
|
}else if(data.length() > relayCode.length()) {
|
|
}else if(data.length() > relayCode.length()) {
|
|
|
data = data.replaceAll(relayCode, "");
|
|
data = data.replaceAll(relayCode, "");
|
|
|
// 柜锁bit 位
|
|
// 柜锁bit 位
|
|
|
long bit = CRCCHECK.getBitByCommand(data);
|
|
long bit = CRCCHECK.getBitByCommand(data);
|
|
|
// 1开锁状态 0关锁状态
|
|
// 1开锁状态 0关锁状态
|
|
|
int status = CRCCHECK.getLockStatus(data);
|
|
int status = CRCCHECK.getLockStatus(data);
|
|
|
-
|
|
|
|
|
String openData = CRCCHECK.getOpenLockOrder((int)bit).replace(" ", "").toUpperCase();
|
|
String openData = CRCCHECK.getOpenLockOrder((int)bit).replace(" ", "").toUpperCase();
|
|
|
if(data.equals(openData)){
|
|
if(data.equals(openData)){
|
|
|
log.info("netty柜锁指令回调:" + relayCode + ":" + bit + ",指令:" + data);
|
|
log.info("netty柜锁指令回调:" + relayCode + ":" + bit + ",指令:" + data);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
log.info("netty柜锁状态回调:" + relayCode + ":" + bit + ",回调结果" + (status == 1 ? "开启": "关闭") + ",指令:" + data);
|
|
log.info("netty柜锁状态回调:" + relayCode + ":" + bit + ",回调结果" + (status == 1 ? "开启": "关闭") + ",指令:" + data);
|
|
|
-
|
|
|
|
|
RedisService redisService = SpringUtils.getBean(RedisService.class);
|
|
RedisService redisService = SpringUtils.getBean(RedisService.class);
|
|
|
redisService.setCacheObject(relayCode + ":" + bit, status, 3 * 60L, TimeUnit.SECONDS);
|
|
redisService.setCacheObject(relayCode + ":" + bit, status, 3 * 60L, TimeUnit.SECONDS);
|
|
|
}
|
|
}
|
|
|
|
|
+ }else if(dataStr.endsWith(SocketTypes.NRKJ_PREFIX) || dataStr.contains(SocketTypes.NRKJ_IDENTIFY)){
|
|
|
|
|
+ String codeStr=dataStr.substring(0,17);
|
|
|
|
|
+ String relayCode=codeStr.replace(":","");
|
|
|
|
|
+ //回复ack
|
|
|
|
|
+ ChannelMap.getChannelMap().put(relayCode, channelHandlerContext);
|
|
|
|
|
+ NettyPushMsgService.push(relayCode,RelayConstants.AT_ACK.getBytes());
|
|
|
|
|
+ //注册状态更新
|
|
|
|
|
+ RelayUtils.relayRefreshStatus(dataStr,relayCode,codeStr);
|
|
|
|
|
+ //继电器开关
|
|
|
|
|
+ RelayUtils.relayOpenClose(dataStr,relayCode);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -77,7 +91,6 @@ public class NettyServerHandler implements ChannelInboundHandler {
|
|
|
log.info("netty继电器上报不识别指令:" + packet.order);
|
|
log.info("netty继电器上报不识别指令:" + packet.order);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
log.info("netty继电器状态上报:" + packet.toString());
|
|
log.info("netty继电器状态上报:" + packet.toString());
|
|
|
ChannelMap.getChannelMap().put(packet.deviceNumber, channelHandlerContext);
|
|
ChannelMap.getChannelMap().put(packet.deviceNumber, channelHandlerContext);
|
|
|
baseRouter.routePacket(packet);
|
|
baseRouter.routePacket(packet);
|
|
@@ -152,34 +165,24 @@ public class NettyServerHandler implements ChannelInboundHandler {
|
|
|
@Override
|
|
@Override
|
|
|
public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception {
|
|
public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception {
|
|
|
log.info("netty客户端断开连接" + channelHandlerContext.toString());
|
|
log.info("netty客户端断开连接" + channelHandlerContext.toString());
|
|
|
-
|
|
|
|
|
for (Map.Entry<String, ChannelHandlerContext> a : ChannelMap.getChannelMap().entrySet()) {
|
|
for (Map.Entry<String, ChannelHandlerContext> a : ChannelMap.getChannelMap().entrySet()) {
|
|
|
-
|
|
|
|
|
if(a.getValue() == channelHandlerContext){
|
|
if(a.getValue() == channelHandlerContext){
|
|
|
ChannelMap.getChannelMap().remove(a.getKey());
|
|
ChannelMap.getChannelMap().remove(a.getKey());
|
|
|
log.info("netty客户端断开连接清除key: " + a.getKey());
|
|
log.info("netty客户端断开连接清除key: " + a.getKey());
|
|
|
-
|
|
|
|
|
executorService.schedule(new Runnable() {
|
|
executorService.schedule(new Runnable() {
|
|
|
@Override
|
|
@Override
|
|
|
public void run() {
|
|
public void run() {
|
|
|
-
|
|
|
|
|
String relayCode = a.getKey();
|
|
String relayCode = a.getKey();
|
|
|
-
|
|
|
|
|
if(ChannelMap.getChannelMap().get(relayCode) != null){
|
|
if(ChannelMap.getChannelMap().get(relayCode) != null){
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
LabHardwareMapper labHardwareMapper = SpringUtils.getBean(LabHardwareMapper.class);
|
|
LabHardwareMapper labHardwareMapper = SpringUtils.getBean(LabHardwareMapper.class);
|
|
|
-
|
|
|
|
|
LabHardware labHardware = new LabHardware();
|
|
LabHardware labHardware = new LabHardware();
|
|
|
labHardware.setRelayCode(relayCode);
|
|
labHardware.setRelayCode(relayCode);
|
|
|
-
|
|
|
|
|
List<LabHardwareVO> list = labHardwareMapper.selectHardwareAndSubInfoByRelay(labHardware);
|
|
List<LabHardwareVO> list = labHardwareMapper.selectHardwareAndSubInfoByRelay(labHardware);
|
|
|
-
|
|
|
|
|
if(list.size() == 0){
|
|
if(list.size() == 0){
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
LabAbnormalMapper labAbnormalMapper = SpringUtils.getBean(LabAbnormalMapper.class);
|
|
LabAbnormalMapper labAbnormalMapper = SpringUtils.getBean(LabAbnormalMapper.class);
|
|
|
for (LabHardwareVO hardwareVO : list) {
|
|
for (LabHardwareVO hardwareVO : list) {
|
|
|
LabAbnormal labAbnormal = new LabAbnormal();
|
|
LabAbnormal labAbnormal = new LabAbnormal();
|
|
@@ -192,20 +195,14 @@ public class NettyServerHandler implements ChannelInboundHandler {
|
|
|
labAbnormal.setCreateBy("系统");
|
|
labAbnormal.setCreateBy("系统");
|
|
|
labAbnormal.setCreateTime(DateUtils.getNowDate());
|
|
labAbnormal.setCreateTime(DateUtils.getNowDate());
|
|
|
labAbnormalMapper.insertLabAbnormal(labAbnormal);
|
|
labAbnormalMapper.insertLabAbnormal(labAbnormal);
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
String content = hardwareVO.getDeptName() + hardwareVO.getSubjectName() + hardwareVO.getName()
|
|
String content = hardwareVO.getDeptName() + hardwareVO.getSubjectName() + hardwareVO.getName()
|
|
|
+ "netty连接已掉线三分钟未连接!请确认处理!";
|
|
+ "netty连接已掉线三分钟未连接!请确认处理!";
|
|
|
DingTalkAlert.sendAlert(content);
|
|
DingTalkAlert.sendAlert(content);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
}, 180, TimeUnit.SECONDS);
|
|
}, 180, TimeUnit.SECONDS);
|
|
|
-
|
|
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
}
|
|
}
|