From f64804cf5870c0aa454eabe71fec746d642b421b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=80=E9=B8=A3?= Date: Tue, 19 Jan 2021 13:29:44 +0800 Subject: [PATCH] warn --- .../web/service/HeartNettyClientFilter.java | 4 +- .../web/service/MyProtocolDecoder.java | 155 +++++++++--------- .../com/zhehekeji/web/service/PlcService.java | 6 +- .../com/zhehekeji/web/service/SendHeart.java | 2 +- 4 files changed, 86 insertions(+), 81 deletions(-) diff --git a/web/src/main/java/com/zhehekeji/web/service/HeartNettyClientFilter.java b/web/src/main/java/com/zhehekeji/web/service/HeartNettyClientFilter.java index 88c5f9c..769317d 100644 --- a/web/src/main/java/com/zhehekeji/web/service/HeartNettyClientFilter.java +++ b/web/src/main/java/com/zhehekeji/web/service/HeartNettyClientFilter.java @@ -7,6 +7,8 @@ import io.netty.handler.timeout.IdleStateHandler; import java.util.concurrent.TimeUnit; +import static java.nio.ByteOrder.BIG_ENDIAN; + /** * 客户端过滤器,如编解码和心跳的设置 * @@ -32,7 +34,7 @@ public class HeartNettyClientFilter extends ChannelInitializer { ChannelPipeline ph = ch.pipeline(); //因为服务端设置的超时时间是5秒,所以客户端设置4秒 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 HeartNettyClientHandler(streetId,sendHeart)); diff --git a/web/src/main/java/com/zhehekeji/web/service/MyProtocolDecoder.java b/web/src/main/java/com/zhehekeji/web/service/MyProtocolDecoder.java index 9fd8b43..d51b854 100644 --- a/web/src/main/java/com/zhehekeji/web/service/MyProtocolDecoder.java +++ b/web/src/main/java/com/zhehekeji/web/service/MyProtocolDecoder.java @@ -2,6 +2,7 @@ package com.zhehekeji.web.service; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.FixedLengthFrameDecoder; import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import io.netty.util.CharsetUtil; import lombok.extern.slf4j.Slf4j; @@ -9,93 +10,93 @@ import lombok.extern.slf4j.Slf4j; import java.nio.charset.Charset; import java.util.concurrent.ThreadPoolExecutor; +import static java.nio.ByteOrder.LITTLE_ENDIAN; + + @Slf4j -public class MyProtocolDecoder extends LengthFieldBasedFrameDecoder { +public class MyProtocolDecoder extends FixedLengthFrameDecoder { private ThreadPoolExecutor threadPoolExecutor; private PlcService plcService; - /** - * - * @param maxFrameLength 帧的最大长度 - * @param lengthFieldOffset length字段偏移的地址 - * @param lengthFieldLength length字段所占的字节长 - * @param lengthAdjustment 修改帧数据长度字段中定义的值,可以为负数 因为有时候我们习惯把头部记入长度,若为负数,则说明要推后多少个字段 - * @param initialBytesToStrip 解析时候跳过多少个长度 - * @param failFast 为true,当frame长度超过maxFrameLength时立即报TooLongFrameException异常,为false,读取完整个帧再报异 - */ - - 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 - 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 { + public MyProtocolDecoder(ThreadPoolExecutor threadPoolExecutor, PlcService plcService) { - } - byte maohao = in.readByte(); - log.info(":{}",maohao); - byte leixing = in.readByte(); - log.info("---leixing{}",leixing); - byte w = in.readByte(); - log.info("---w{}",w); - in.readBytes(8); - return null; + super(43); + this.threadPoolExecutor = threadPoolExecutor; + this.plcService = plcService; + } + @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; + + } } diff --git a/web/src/main/java/com/zhehekeji/web/service/PlcService.java b/web/src/main/java/com/zhehekeji/web/service/PlcService.java index 78f7eb5..6fc46d2 100644 --- a/web/src/main/java/com/zhehekeji/web/service/PlcService.java +++ b/web/src/main/java/com/zhehekeji/web/service/PlcService.java @@ -150,13 +150,15 @@ public class PlcService { //是否有告警 Long warnId = OrderRealtime.getWarnId(street.getId()); if(warnId != null){ + OrderRealtime.cleanWarn(street.getId()); Warn warn = warnMapper.selectById(warnId); if(warn != null){ - warn.setEndTime(LocalDateTime.now()); + + } - OrderRealtime.cleanWarn(street.getId()); + } } } diff --git a/web/src/main/java/com/zhehekeji/web/service/SendHeart.java b/web/src/main/java/com/zhehekeji/web/service/SendHeart.java index 0181127..afacdd8 100644 --- a/web/src/main/java/com/zhehekeji/web/service/SendHeart.java +++ b/web/src/main/java/com/zhehekeji/web/service/SendHeart.java @@ -36,7 +36,7 @@ public class SendHeart { } id = 1; for (int j = 0; j < 8; j++) { - emp[j] = (byte) 1; + emp[j] = (byte) j; } } }