|
|
|
|
@ -0,0 +1,118 @@
|
|
|
|
|
package com.zhehekeji.web.service;
|
|
|
|
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
|
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class MyProtocolDecoder extends LengthFieldBasedFrameDecoder {
|
|
|
|
|
|
|
|
|
|
private ThreadPoolExecutor threadPoolExecutor;
|
|
|
|
|
|
|
|
|
|
private OrderService orderService;
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @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, OrderService orderService) {
|
|
|
|
|
|
|
|
|
|
super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip, failFast);
|
|
|
|
|
this.threadPoolExecutor = threadPoolExecutor;
|
|
|
|
|
this.orderService = orderService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
|
|
|
|
|
in = (ByteBuf) super.decode(ctx,in);
|
|
|
|
|
|
|
|
|
|
if(in == null){
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
//包头 `L` `P`
|
|
|
|
|
char l = in.readChar();
|
|
|
|
|
char p = in.readChar();
|
|
|
|
|
if(l != 76 && p != 80){
|
|
|
|
|
//不是包头 丢
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
short length = in.readShort();
|
|
|
|
|
log.debug("length:{}",length);
|
|
|
|
|
CharSequence charSequence = in.readCharSequence(6, Charset.defaultCharset());
|
|
|
|
|
String plcId = charSequence.toString();
|
|
|
|
|
log.debug("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);
|
|
|
|
|
|
|
|
|
|
// //读取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();
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|