|
|
|
|
@ -3,6 +3,7 @@ package com.zhehekeji.web.service;
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
|
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
|
|
|
|
|
import io.netty.util.CharsetUtil;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
@ -13,7 +14,7 @@ public class MyProtocolDecoder extends LengthFieldBasedFrameDecoder {
|
|
|
|
|
|
|
|
|
|
private ThreadPoolExecutor threadPoolExecutor;
|
|
|
|
|
|
|
|
|
|
private OrderService orderService;
|
|
|
|
|
private PlcService plcService;
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param maxFrameLength 帧的最大长度
|
|
|
|
|
@ -24,91 +25,73 @@ public class MyProtocolDecoder extends LengthFieldBasedFrameDecoder {
|
|
|
|
|
* @param failFast 为true,当frame长度超过maxFrameLength时立即报TooLongFrameException异常,为false,读取完整个帧再报异
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public MyProtocolDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast, ThreadPoolExecutor threadPoolExecutor, OrderService orderService) {
|
|
|
|
|
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.orderService = orderService;
|
|
|
|
|
this.plcService = plcService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
|
|
|
|
|
in = (ByteBuf) super.decode(ctx,in);
|
|
|
|
|
//in = (ByteBuf) super.decode(ctx,in);
|
|
|
|
|
|
|
|
|
|
if(in == null){
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
//包头 `L` `P`
|
|
|
|
|
char l = in.readChar();
|
|
|
|
|
char p = in.readChar();
|
|
|
|
|
byte l = in.readByte();
|
|
|
|
|
byte p = in.readByte();
|
|
|
|
|
if(l != 76 && p != 80){
|
|
|
|
|
//不是包头 丢
|
|
|
|
|
return null;
|
|
|
|
|
log.error("??");
|
|
|
|
|
}else {
|
|
|
|
|
log.info("baotou come");
|
|
|
|
|
}
|
|
|
|
|
short length = in.readShort();
|
|
|
|
|
log.debug("length:{}",length);
|
|
|
|
|
CharSequence charSequence = in.readCharSequence(6, Charset.defaultCharset());
|
|
|
|
|
String plcId = charSequence.toString();
|
|
|
|
|
log.debug("plcId:{}",plcId);
|
|
|
|
|
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 charSequence1 = in.readCharSequence(2,Charset.defaultCharset());
|
|
|
|
|
String type = charSequence1.toString();
|
|
|
|
|
log.debug("type:{}",type);
|
|
|
|
|
CharSequence charSequence2 = in.readCharSequence(20,Charset.defaultCharset());
|
|
|
|
|
String orderNum = charSequence2.toString();
|
|
|
|
|
log.debug("订单号:{}",orderNum);
|
|
|
|
|
char maohao = in.readChar();
|
|
|
|
|
log.debug("冒号:{}",maohao);
|
|
|
|
|
char leixing = in.readChar();
|
|
|
|
|
log.debug("---{}",leixing);
|
|
|
|
|
in.readBytes(8);
|
|
|
|
|
CharSequence typeChar = in.readCharSequence(2,Charset.defaultCharset());
|
|
|
|
|
String type = typeChar.toString();
|
|
|
|
|
int q =in.readInt();
|
|
|
|
|
|
|
|
|
|
log.info("type:{}",type);
|
|
|
|
|
|
|
|
|
|
// //读取type字段
|
|
|
|
|
// byte header1 = in.readByte();
|
|
|
|
|
// byte header2 = in.readByte();
|
|
|
|
|
// byte header3 = in.readByte();
|
|
|
|
|
// byte header4 = in.readByte();
|
|
|
|
|
// byte header5 = in.readByte();
|
|
|
|
|
// byte header6 = in.readByte();
|
|
|
|
|
// if(header1 != 20 && header2 != 20 && header3 != 9 && header4 != 7 && header5 != 11 && header6 != 30){
|
|
|
|
|
// //不是包头 直接丢弃
|
|
|
|
|
// return null;
|
|
|
|
|
// }
|
|
|
|
|
// long length = in.readUnsignedInt();
|
|
|
|
|
// if(in.readableBytes()!=length){
|
|
|
|
|
// throw new Exception("标记的长度不符合实际长度");
|
|
|
|
|
// }
|
|
|
|
|
// //读取body
|
|
|
|
|
// byte []bytes = new byte[in.readableBytes()];
|
|
|
|
|
// in.readBytes(bytes);
|
|
|
|
|
// String string = new String(bytes,"UTF-8");
|
|
|
|
|
// String substring = string.substring(string.indexOf("{"), string.lastIndexOf("}") + 1);
|
|
|
|
|
// JSONObject jsonObject = JSONObject.parseObject(substring);
|
|
|
|
|
// String deviceId = jsonObject.getString("device_id");
|
|
|
|
|
// log.debug("tcp deviceId:{}",deviceId);
|
|
|
|
|
// String dataLength = jsonObject.getString("data_length");
|
|
|
|
|
// Integer flawType = jsonObject.getInteger("msg");
|
|
|
|
|
// String detectStatus = "";
|
|
|
|
|
// if(flawType > 0){
|
|
|
|
|
// detectStatus = "NG";
|
|
|
|
|
// }else {
|
|
|
|
|
// detectStatus = "OK";
|
|
|
|
|
// }
|
|
|
|
|
// String result = jsonObject.getString("result");
|
|
|
|
|
// if(!StringUtils.isEmpty(result) && !"{}".equals(result)){
|
|
|
|
|
// log.debug(result);
|
|
|
|
|
// AdaptData adaptData = JSONObject.parseObject(result,AdaptData.class);
|
|
|
|
|
// if(adaptData != null){
|
|
|
|
|
// FlawRunnable flawRunnable = new FlawRunnable(jdbcTemplate,deviceId,"flaw",adaptData);
|
|
|
|
|
// threadPoolExecutor.execute(flawRunnable);
|
|
|
|
|
// }
|
|
|
|
|
// }else {
|
|
|
|
|
// log.warn("result为空:{}",result);
|
|
|
|
|
// }
|
|
|
|
|
// String s2 = string.substring(string.lastIndexOf("}") + 1, string.length());
|
|
|
|
|
// WebSocketMapUtil.send(deviceId,s2,detectStatus);
|
|
|
|
|
in.release();
|
|
|
|
|
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);
|
|
|
|
|
in.readBytes(8);
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|