|
|
|
@ -3,15 +3,11 @@ 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.FixedLengthFrameDecoder;
|
|
|
|
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
|
|
|
|
|
|
|
|
import io.netty.util.CharsetUtil;
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
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 FixedLengthFrameDecoder {
|
|
|
|
public class MyProtocolDecoder extends FixedLengthFrameDecoder {
|
|
|
|
@ -43,14 +39,10 @@ public class MyProtocolDecoder extends FixedLengthFrameDecoder {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CharSequence plc = in.readCharSequence(6, Charset.defaultCharset());
|
|
|
|
CharSequence plc = in.readCharSequence(6, Charset.defaultCharset());
|
|
|
|
String plcId = plc.toString();
|
|
|
|
String plcId = plc.toString();
|
|
|
|
log.info("plcId:{}", plcId);
|
|
|
|
|
|
|
|
// OA=心跳 OB=工单 OC=任务 OD=告警
|
|
|
|
// OA=心跳 OB=工单 OC=任务 OD=告警
|
|
|
|
CharSequence typeChar = in.readCharSequence(2, Charset.defaultCharset());
|
|
|
|
CharSequence typeChar = in.readCharSequence(2, Charset.defaultCharset());
|
|
|
|
String type = typeChar.toString();
|
|
|
|
String type = typeChar.toString();
|
|
|
|
int taskId = in.readInt();
|
|
|
|
int taskId = in.readInt();
|
|
|
|
|
|
|
|
|
|
|
|
log.debug("type:{}", type);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
short short1 = in.readShort();
|
|
|
|
short short1 = in.readShort();
|
|
|
|
short short2 = in.readShort();
|
|
|
|
short short2 = in.readShort();
|
|
|
|
short short3 = in.readShort();
|
|
|
|
short short3 = in.readShort();
|
|
|
|
@ -59,7 +51,7 @@ public class MyProtocolDecoder extends FixedLengthFrameDecoder {
|
|
|
|
short short6 = in.readShort();
|
|
|
|
short short6 = in.readShort();
|
|
|
|
short short7 = in.readShort();
|
|
|
|
short short7 = in.readShort();
|
|
|
|
short short8 = in.readShort();
|
|
|
|
short short8 = in.readShort();
|
|
|
|
PlcOrderInfo plcOrderInfo = new PlcOrderInfo(taskId,(int)short1,(int)short2,(int)short3,(int)short4,(int)short5,(int)short6,(int)short7,(int)short8);
|
|
|
|
PlcOrderInfo plcOrderInfo = new PlcOrderInfo(plcId,taskId,(int)short1,(int)short2,(int)short3,(int)short4,(int)short5,(int)short6,(int)short7,(int)short8);
|
|
|
|
|
|
|
|
|
|
|
|
byte maohao = in.readByte();
|
|
|
|
byte maohao = in.readByte();
|
|
|
|
byte leixing = in.readByte();
|
|
|
|
byte leixing = in.readByte();
|
|
|
|
@ -74,9 +66,10 @@ public class MyProtocolDecoder extends FixedLengthFrameDecoder {
|
|
|
|
byte b6 = in.readByte();
|
|
|
|
byte b6 = in.readByte();
|
|
|
|
byte b7 = in.readByte();
|
|
|
|
byte b7 = in.readByte();
|
|
|
|
byte b8 = in.readByte();
|
|
|
|
byte b8 = in.readByte();
|
|
|
|
|
|
|
|
byte[]bytes=new byte[]{b1,b2,b3,b4,b5,b6,b7,b8};
|
|
|
|
if (type.equals("0A")) {
|
|
|
|
if (type.equals("0A")) {
|
|
|
|
//心跳
|
|
|
|
//心跳
|
|
|
|
log.debug("receive plc heart");
|
|
|
|
log.debug("receive heart plcId:{}",plcId);
|
|
|
|
} else if (type.equals("0B")) {
|
|
|
|
} else if (type.equals("0B")) {
|
|
|
|
|
|
|
|
|
|
|
|
if ("B1".equals(code)) {
|
|
|
|
if ("B1".equals(code)) {
|
|
|
|
@ -93,24 +86,58 @@ public class MyProtocolDecoder extends FixedLengthFrameDecoder {
|
|
|
|
//执行动作
|
|
|
|
//执行动作
|
|
|
|
plcService.action(plcOrderInfo,plcId,times,code);
|
|
|
|
plcService.action(plcOrderInfo,plcId,times,code);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else if(type.equals("0D")){
|
|
|
|
} else if(type.equals("0D")){
|
|
|
|
log.info(" warn code:{}",code);
|
|
|
|
log.info(" warn code:{}",code);
|
|
|
|
if("D0".equals(code)){
|
|
|
|
if("D0".equals(code)){
|
|
|
|
log.info("warn start ,plcId:{}",plcId);
|
|
|
|
log.info("warn start ,plcId:{}",plcId);
|
|
|
|
plcService.warnStart(plcId);
|
|
|
|
plcService.warnStart(plcId);
|
|
|
|
//动作
|
|
|
|
//动作
|
|
|
|
|
|
|
|
//解析具体的告警 具体规则见文档
|
|
|
|
|
|
|
|
int n = 1;
|
|
|
|
|
|
|
|
int warn = 0;
|
|
|
|
|
|
|
|
for(byte b: bytes){
|
|
|
|
|
|
|
|
warn = warn(b,8*n);
|
|
|
|
|
|
|
|
if(warn > 0){
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
n++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(warn > 0){
|
|
|
|
|
|
|
|
//执行动作
|
|
|
|
|
|
|
|
//times = 1 只执行动作
|
|
|
|
|
|
|
|
//code 写作"D{warn}"
|
|
|
|
|
|
|
|
plcService.action(plcOrderInfo,plcId,1,"D"+warn);
|
|
|
|
|
|
|
|
}
|
|
|
|
}else if("D100".equals(code)){
|
|
|
|
}else if("D100".equals(code)){
|
|
|
|
log.info("warn end ,plcId:{}",plcId);
|
|
|
|
log.info("warn end ,plcId:{}",plcId);
|
|
|
|
plcService.warnStop(plcId);
|
|
|
|
plcService.warnStop(plcId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.debug("1:{},b1:{},b2:{},b3:{},b4:{},b5:{},b6:{},b7:{},b8:{}",w, b1, b2, b3, b4, b5, b6, b7, b8);
|
|
|
|
|
|
|
|
|
|
|
|
log.info("b1:{},b2:{},b3:{},b4:{},b5:{},b6:{},b7:{},b8:{}", b1, b2, b3, b4, b5, b6, b7, b8);
|
|
|
|
|
|
|
|
in.release();
|
|
|
|
in.release();
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Integer warn(byte b,Integer num){
|
|
|
|
|
|
|
|
String tString = Integer.toBinaryString((b & 0xFF) + 0x100).substring(1);
|
|
|
|
|
|
|
|
Integer warn = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
|
|
|
|
char item = tString.charAt(i);
|
|
|
|
|
|
|
|
if(String.valueOf(item).equals("1")){
|
|
|
|
|
|
|
|
warn = num-i;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return warn;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
byte tByte = 4;
|
|
|
|
|
|
|
|
String tString = Integer.toBinaryString((tByte & 0xFF) + 0x100).substring(1);
|
|
|
|
|
|
|
|
System.out.println(tString);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|