|
|
|
|
@ -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;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|