camera_merge
王一鸣 5 years ago
parent 9e359b329c
commit f64804cf58

@ -7,6 +7,8 @@ import io.netty.handler.timeout.IdleStateHandler;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static java.nio.ByteOrder.BIG_ENDIAN;
/** /**
* *
* *
@ -32,7 +34,7 @@ public class HeartNettyClientFilter extends ChannelInitializer<SocketChannel> {
ChannelPipeline ph = ch.pipeline(); ChannelPipeline ph = ch.pipeline();
//因为服务端设置的超时时间是5秒所以客户端设置4秒 //因为服务端设置的超时时间是5秒所以客户端设置4秒
ph.addLast(new IdleStateHandler(0, 4, 0, TimeUnit.SECONDS)); ph.addLast(new IdleStateHandler(0, 4, 0, TimeUnit.SECONDS));
ph.addLast(new MyProtocolDecoder(1000*10000,6,4,0,0,false,null,plcService)); ph.addLast(new MyProtocolDecoder(null,plcService));
ph.addLast(new MyProtocolEncoder()); ph.addLast(new MyProtocolEncoder());
//处理客户端的业务逻辑 //处理客户端的业务逻辑
ph.addLast(new HeartNettyClientHandler(streetId,sendHeart)); ph.addLast(new HeartNettyClientHandler(streetId,sendHeart));

@ -2,6 +2,7 @@ package com.zhehekeji.web.service;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -9,93 +10,93 @@ import lombok.extern.slf4j.Slf4j;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import static java.nio.ByteOrder.LITTLE_ENDIAN;
@Slf4j @Slf4j
public class MyProtocolDecoder extends LengthFieldBasedFrameDecoder { public class MyProtocolDecoder extends FixedLengthFrameDecoder {
private ThreadPoolExecutor threadPoolExecutor; private ThreadPoolExecutor threadPoolExecutor;
private PlcService plcService; private PlcService plcService;
/**
*
* @param maxFrameLength
* @param lengthFieldOffset length
* @param lengthFieldLength length
* @param lengthAdjustment ,,
* @param initialBytesToStrip
* @param failFast trueframemaxFrameLengthTooLongFrameExceptionfalse
*/
public MyProtocolDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast, ThreadPoolExecutor threadPoolExecutor, PlcService plcService) {
super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip, failFast);
this.threadPoolExecutor = threadPoolExecutor;
this.plcService = plcService;
}
@Override public MyProtocolDecoder(ThreadPoolExecutor threadPoolExecutor, PlcService plcService) {
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
//in = (ByteBuf) super.decode(ctx,in);
byte l = in.readByte();
byte p = in.readByte();
if(l != 76 && p != 80){
//不是包头 丢
log.error("??");
}else {
log.info("baotou come");
}
byte len1 = in.readByte();
byte len2 = in.readByte();
log.info("len1:{},len2:{}",len1,len2);
CharSequence plc = in.readCharSequence(6,Charset.defaultCharset());
String plcId = plc.toString();
log.info("plcId:{}",plcId);
// OA=心跳 OB=工单 OC=任务 OD=告警
CharSequence typeChar = in.readCharSequence(2,Charset.defaultCharset());
String type = typeChar.toString();
int q =in.readInt();
log.info("type:{}",type);
short short1 = in.readShort();
short short2 = in.readShort();
short short3 = in.readShort();
short short4 = in.readShort();
short short5 = in.readShort();
short short6 = in.readShort();
short short7 = in.readShort();
short short8 = in.readShort();
log.info("int:{},1:{},2:{},3:{},4:{},5:{},6:{},7:{},8:{}",q,short1,short2,short3,short4,short5,short6,short7,short8);
if(type.equals("0A")){
//心跳
log.info("recieve plc heart");
}else if(type.equals("0B")){
String orderNum = ""+q+short1+short2+short3+short4+short5+short6+short7+short8;
log.info("order:{}",orderNum);
if(q ==1){
log.info("order start");
plcService.orderStart(orderNum,plcId);
}else if(q == 2){
log.info("order end");
plcService.orderStop(orderNum,null,plcId);
}
}else if(type.equals("0C")){
}else {
} super(43);
byte maohao = in.readByte(); this.threadPoolExecutor = threadPoolExecutor;
log.info(":{}",maohao); this.plcService = plcService;
byte leixing = in.readByte(); }
log.info("---leixing{}",leixing);
byte w = in.readByte();
log.info("---w{}",w);
in.readBytes(8);
return null;
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
in = (ByteBuf) super.decode(ctx, in);
if (in == null) {
return null;
}
byte l = in.readByte();
byte p = in.readByte();
byte len1 = in.readByte();
byte len2 = in.readByte();
if (l != 76 && p != 80 && len1 != 4 && len2 != 3) {
//不是包头 丢
return null;
} }
CharSequence plc = in.readCharSequence(6, Charset.defaultCharset());
String plcId = plc.toString();
log.debug("plcId:{}", plcId);
// OA=心跳 OB=工单 OC=任务 OD=告警
CharSequence typeChar = in.readCharSequence(2, Charset.defaultCharset());
String type = typeChar.toString();
int q = in.readInt();
log.info("type:{}", type);
short short1 = in.readShort();
short short2 = in.readShort();
short short3 = in.readShort();
short short4 = in.readShort();
short short5 = in.readShort();
short short6 = in.readShort();
short short7 = in.readShort();
short short8 = in.readShort();
log.info("int:{},1:{},2:{},3:{},4:{},5:{},6:{},7:{},8:{}", q, short1, short2, short3, short4, short5, short6, short7, short8);
if (type.equals("0A")) {
//心跳
log.info("recieve plc heart");
} else if (type.equals("0B")) {
String orderNum = "" + q + short1 + short2 + short3 + short4 + short5 + short6 + short7 + short8;
log.info("order:{}", orderNum);
if (q == 1) {
log.info("order start");
plcService.orderStart(orderNum, plcId);
} else if (q == 2) {
log.info("order end");
plcService.orderStop(orderNum, null, plcId);
}
} else if (type.equals("0C")) {
} else {
}
byte maohao = in.readByte();
log.info(":{}", maohao);
byte leixing = in.readByte();
log.info("---leixing:{}", leixing);
byte w = in.readByte();
log.info("---w:{}", w);
byte b1 = in.readByte();
byte b2 = in.readByte();
byte b3 = in.readByte();
byte b4 = in.readByte();
byte b5 = in.readByte();
byte b6 = in.readByte();
byte b7 = in.readByte();
byte b8 = in.readByte();
log.info("b1:{},b2:{},b3:{},b4:{},b5:{},b6:{},b7:{},b8:{}", b1, b2, b3, b4, b5, b6, b7, b8);
in.release();
return null;
}
} }

@ -150,13 +150,15 @@ public class PlcService {
//是否有告警 //是否有告警
Long warnId = OrderRealtime.getWarnId(street.getId()); Long warnId = OrderRealtime.getWarnId(street.getId());
if(warnId != null){ if(warnId != null){
OrderRealtime.cleanWarn(street.getId());
Warn warn = warnMapper.selectById(warnId); Warn warn = warnMapper.selectById(warnId);
if(warn != null){ if(warn != null){
warn.setEndTime(LocalDateTime.now()); warn.setEndTime(LocalDateTime.now());
} }
OrderRealtime.cleanWarn(street.getId());
} }
} }
} }

@ -36,7 +36,7 @@ public class SendHeart {
} }
id = 1; id = 1;
for (int j = 0; j < 8; j++) { for (int j = 0; j < 8; j++) {
emp[j] = (byte) 1; emp[j] = (byte) j;
} }
} }
} }

Loading…
Cancel
Save