|
|
|
|
@ -0,0 +1,215 @@
|
|
|
|
|
package com.zhehekeji.web.service.client;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.zhehekeji.web.pojo.Cmd;
|
|
|
|
|
import com.zhehekeji.web.service.GoodsActionTimes;
|
|
|
|
|
import com.zhehekeji.web.service.PlcCmdInfo;
|
|
|
|
|
import com.zhehekeji.web.service.PlcService;
|
|
|
|
|
import com.zhehekeji.web.service.ksec.KsecDataInfo;
|
|
|
|
|
import com.zhehekeji.web.service.ksec.KsecInfo;
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
|
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 客户端解码器
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class ClientDecoder extends DelimiterBasedFrameDecoder {
|
|
|
|
|
|
|
|
|
|
private static final Logger tcpLogger = LoggerFactory.getLogger("client_tcp");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(7,21,30, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(20000));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ClientDecoder(ByteBuf byteBuf) {
|
|
|
|
|
super(1000, byteBuf);
|
|
|
|
|
//ByteBuf byteBuf = Unpooled.copiedBuffer("AM".getBytes());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
|
|
|
|
|
in = (ByteBuf) super.decode(ctx, in);
|
|
|
|
|
if(in == null){
|
|
|
|
|
log.debug("no data");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
ClientRunnable kescRunnable = new ClientRunnable(in,ctx);
|
|
|
|
|
threadPoolExecutor.execute(kescRunnable);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 要找到对应的客户端
|
|
|
|
|
*/
|
|
|
|
|
public static class ClientRunnable implements Runnable{
|
|
|
|
|
|
|
|
|
|
private ByteBuf in;
|
|
|
|
|
|
|
|
|
|
private ChannelHandlerContext ctx;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ClientRunnable(ByteBuf body,ChannelHandlerContext ctx){
|
|
|
|
|
this.in = body;
|
|
|
|
|
this.ctx = ctx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
String body = in.toString(Charset.forName("UTF-8"));
|
|
|
|
|
tcpLogger.info(body);
|
|
|
|
|
if (body.startsWith("AM")){
|
|
|
|
|
// 去掉首尾标识符
|
|
|
|
|
body = body.substring(1, body.length());
|
|
|
|
|
KsecInfo ksecInfo = JSONObject.parseObject(body, KsecInfo.class);
|
|
|
|
|
if (Cmd.A.name().equals(ksecInfo.getType())) {
|
|
|
|
|
in.release();
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
KsecDataInfo dataInfo = ksecInfo.getData();
|
|
|
|
|
String lotnum = dataInfo.getLotnum();
|
|
|
|
|
PlcCmdInfo plcCmdInfo = null;
|
|
|
|
|
String srmNumber = null;
|
|
|
|
|
String cmdName = null;
|
|
|
|
|
if(dataInfo != null){
|
|
|
|
|
//左右换过来
|
|
|
|
|
if(dataInfo.getFromDirection() == 1){
|
|
|
|
|
dataInfo.setFromDirection(2);
|
|
|
|
|
}else {
|
|
|
|
|
dataInfo.setFromDirection(1);
|
|
|
|
|
}
|
|
|
|
|
if(dataInfo.getToDirection() != null && dataInfo.getToDirection() == 1){
|
|
|
|
|
dataInfo.setToDirection(2);
|
|
|
|
|
}else {
|
|
|
|
|
dataInfo.setToDirection(1);
|
|
|
|
|
}
|
|
|
|
|
plcCmdInfo = new PlcCmdInfo(dataInfo.getSRMNumber(), dataInfo.getTaskId(), dataInfo.getFromSide(), dataInfo.getFromDirection(), dataInfo.getFromColumn(), dataInfo.getFromRow(), dataInfo.getFromSeparation(),dataInfo.getToSide(), dataInfo.getToDirection(), dataInfo.getToColumn(), dataInfo.getToRow(),dataInfo.getToSeparation(),lotnum);
|
|
|
|
|
|
|
|
|
|
srmNumber = dataInfo.getSRMNumber();
|
|
|
|
|
cmdName = dataInfo.getCmdName();
|
|
|
|
|
}
|
|
|
|
|
if (Cmd.A.name().equals(ksecInfo.getType())) {
|
|
|
|
|
//心跳
|
|
|
|
|
log.debug("receieve heart ");
|
|
|
|
|
} else if (Cmd.B.name().equals(ksecInfo.getType())) {
|
|
|
|
|
|
|
|
|
|
//任务
|
|
|
|
|
if (Cmd.B1.name().equals(cmdName)) {
|
|
|
|
|
//昆船盘点模式下也会发B1 ,但是不会发送B2
|
|
|
|
|
//这里判断下,是否存在盘点批次号 若存在,既是盘点的B1,无需处理;若不存在lotnum,则是随行的B1
|
|
|
|
|
if(StringUtils.isEmpty(dataInfo.getLotnum())){
|
|
|
|
|
//任务开始 旋转到原点位
|
|
|
|
|
plcService.gyrateCamera(plcCmdInfo,Cmd.C5.name());
|
|
|
|
|
plcService.orderStart(plcCmdInfo);
|
|
|
|
|
}else {
|
|
|
|
|
log.info("check move");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (Cmd.B2.name().equals(cmdName)) {
|
|
|
|
|
//B2 C4 一起发的,需要停止等B2
|
|
|
|
|
|
|
|
|
|
//这里判断是不是双伸
|
|
|
|
|
if(plcCmdInfo.getSeparation2() == 2){
|
|
|
|
|
//深测货架延迟
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(plcService.getConfigProperties().getCameraConfig().getB2OutDelayTime());
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
//浅侧延迟
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(plcService.getConfigProperties().getCameraConfig().getB2DelayTime());
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
plcService.gyrateCamera(plcCmdInfo,Cmd.C5.name());
|
|
|
|
|
plcService.orderStop(plcCmdInfo);
|
|
|
|
|
}
|
|
|
|
|
} else if (Cmd.C.name().equals(ksecInfo.getType())) {
|
|
|
|
|
|
|
|
|
|
//动作
|
|
|
|
|
String code = dataInfo.getCmdName();
|
|
|
|
|
log.info("action code,{},orderInfo:{}", code, plcCmdInfo.toString());
|
|
|
|
|
if (Cmd.isBaseAction(code)) {
|
|
|
|
|
//执行动作,需要保存执行到第几步了
|
|
|
|
|
Integer times = GoodsActionTimes.put(plcCmdInfo.getOrderNum());
|
|
|
|
|
plcCmdInfo.setTimes(times);
|
|
|
|
|
code = code + "-" + plcCmdInfo.getLeftRightStr(times) + plcCmdInfo.getInOutStr(times);
|
|
|
|
|
//执行动作
|
|
|
|
|
try {
|
|
|
|
|
plcService.action(plcCmdInfo, times, code);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
log.info("other C code :{}",code);
|
|
|
|
|
}
|
|
|
|
|
} else if (Cmd.D.name().equals(ksecInfo.getType())) {
|
|
|
|
|
|
|
|
|
|
//柳州去掉告警
|
|
|
|
|
// String code = dataInfo.getCmdName();
|
|
|
|
|
// if(code.equals(Cmd.D1.name())){
|
|
|
|
|
// log.info("plcId:{},warn start",plcCmdInfo.getPlcId());
|
|
|
|
|
// //根据告警code转动camera
|
|
|
|
|
// String warnCode = dataInfo.getWarnCode();
|
|
|
|
|
// if(!StringUtils.isEmpty(warnCode)){
|
|
|
|
|
// String warnCode0 = Cmd.D1.name()+"-"+warnCode.split(",")[0];
|
|
|
|
|
// plcService.warnAction(plcCmdInfo,warnCode0);
|
|
|
|
|
// }
|
|
|
|
|
// plcService.warnStart(plcCmdInfo.getPlcId(),dataInfo.getWarnCode());
|
|
|
|
|
// }else if(code.equals(Cmd.D2.name())){
|
|
|
|
|
// log.info("plcId:{},warn stop",plcCmdInfo.getPlcId());
|
|
|
|
|
// plcService.warnStop(plcCmdInfo.getPlcId());
|
|
|
|
|
// }else {
|
|
|
|
|
// log.info("other D code :{}",code);
|
|
|
|
|
// }
|
|
|
|
|
} else if (Cmd.E.name().equals(ksecInfo.getType())) {
|
|
|
|
|
|
|
|
|
|
//盘点
|
|
|
|
|
//转球机到盘点位 然后拍照
|
|
|
|
|
|
|
|
|
|
// if(!StringUtils.isEmpty(lotnum) && !lotnum.equals(lastLotnum)){
|
|
|
|
|
// //需要把stock表truncate
|
|
|
|
|
// FileUtil.save(lotnum,"lastLotnum");
|
|
|
|
|
// tcpLogger.info("truncate table ,last lotnum:{},new lotnum:{}",lastLotnum,lotnum);
|
|
|
|
|
// plcService.truncateStock();
|
|
|
|
|
// lastLotnum = lotnum;
|
|
|
|
|
// }
|
|
|
|
|
// plcCmdInfo.setTimes(1);
|
|
|
|
|
// Boolean ok = plcService.check(plcCmdInfo,ksecInfo.getData().getCmdName(), dataInfo.getCode(), dataInfo.getTrayCode());
|
|
|
|
|
// if(ok){
|
|
|
|
|
// ksecInfo.getData().setAckStatus(1);
|
|
|
|
|
// }else {
|
|
|
|
|
// ksecInfo.getData().setAckStatus(0);
|
|
|
|
|
// }
|
|
|
|
|
// ctx.channel().writeAndFlush(ksecInfo);
|
|
|
|
|
//rfid的逻辑
|
|
|
|
|
String code = dataInfo.getCmdName();
|
|
|
|
|
if("E1".equals(code)){
|
|
|
|
|
plcService.RFIDCheck(plcCmdInfo);
|
|
|
|
|
}else {
|
|
|
|
|
plcService.RFIDStop(plcCmdInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//找到该货位的最后一张照片与现在的照片比照
|
|
|
|
|
//plcService.recordStock(plcCmdInfo, dataInfo.getCode(), 0, 0);
|
|
|
|
|
}
|
|
|
|
|
in.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|