423 lines
17 KiB
Java
423 lines
17 KiB
Java
package com.cnbm.dispatch.line1;
|
||
|
||
import cn.hutool.core.date.DateUtil;
|
||
import com.cnbm.dispatch.enums.kuka.step1.Step1Mes2PlcVar;
|
||
import com.cnbm.dispatch.enums.kuka.step1.Step1Plc2MesVar;
|
||
import com.cnbm.dispatch.enums.kuka.step2.Step2Mes2PlcVar;
|
||
import com.cnbm.dispatch.enums.kuka.step2.Step2Plc2MesVar;
|
||
import com.cnbm.dispatch.enums.kuka.step3.Step3Mes2PlcVar;
|
||
import com.cnbm.dispatch.enums.kuka.step3.Step3Plc2MesVar;
|
||
import com.cnbm.packing.dto.CamlineSubIdDTO;
|
||
import com.cnbm.packing.entity.WoPackagingBox;
|
||
import com.cnbm.packing.entity.WoPackagingBoxSubstrate;
|
||
import com.cnbm.packing.service.*;
|
||
import com.cnbm.s7.s7connector.enmuc.S7Client;
|
||
import com.cnbm.s7.s7connector.type.PlcVar;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.boot.ApplicationArguments;
|
||
import org.springframework.boot.ApplicationRunner;
|
||
import org.springframework.core.annotation.Order;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
import java.text.DecimalFormat;
|
||
import java.text.Format;
|
||
import java.time.LocalDateTime;
|
||
import java.util.Arrays;
|
||
import java.util.Date;
|
||
import java.util.concurrent.Executors;
|
||
import java.util.concurrent.ScheduledExecutorService;
|
||
import java.util.concurrent.TimeUnit;
|
||
|
||
/**
|
||
* @Desc: ""
|
||
* @Author: caixiang
|
||
* @DATE: 2022/12/23 12:33
|
||
*/
|
||
@Component
|
||
@Order(value = 1)
|
||
public class KukaEvent2ThreadLine1 implements ApplicationRunner {
|
||
|
||
private static final Logger logger = LoggerFactory.getLogger("EVENT2Line1");
|
||
|
||
|
||
@Autowired
|
||
DynamicDataSourceService service;
|
||
@Autowired
|
||
WoPowerLevelServiceBiz levelServiceBiz;
|
||
|
||
@Autowired
|
||
WoCompensationPowerServiceBiz compensationPowerServiceBiz;
|
||
|
||
@Autowired
|
||
WoPackagingBoxSubstrateServiceBiz substrateServiceBiz;
|
||
|
||
@Autowired
|
||
WoPackagingBoxServiceBiz boxServiceBiz;
|
||
|
||
public KukaEvent2ThreadLine1(){
|
||
|
||
this.kukaStep2 = Executors.newScheduledThreadPool(1);
|
||
|
||
}
|
||
|
||
|
||
private ScheduledExecutorService kukaStep2;
|
||
|
||
|
||
|
||
|
||
|
||
/**
|
||
* return
|
||
* 成功: 返回相应的object对象
|
||
* 失败: 返回null
|
||
* */
|
||
private Object read(S7Client s7Client,Step1Plc2MesVar var) {
|
||
try {
|
||
return s7Client.read(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getLength(), var.getStrSize(), var.getType());
|
||
}catch (Throwable e){
|
||
logger.info("host:"+s7Client.getHost()+" ; read 操作出现问题: {}",e.getMessage(),e);
|
||
return null;
|
||
}
|
||
}
|
||
private void write(S7Client s7Client, Step1Mes2PlcVar var, Object newValue) {
|
||
if(var.getType().equals(PlcVar.STRING_Array)){
|
||
String[] s = (String[])newValue;
|
||
String[] ss = (String[])newValue;
|
||
if(s.length > var.getLength() ){
|
||
ss = new String[var.getLength()];
|
||
for(int i=0;i< var.getLength();i++){
|
||
ss[i] = s[i];
|
||
}
|
||
}
|
||
s7Client.write(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getStrSize(), var.getType(),ss);
|
||
}else {
|
||
s7Client.write(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getStrSize(), var.getType(),newValue);
|
||
}
|
||
}
|
||
|
||
private Object read(S7Client s7Client, Step2Plc2MesVar var) {
|
||
try {
|
||
return s7Client.read(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getLength(), var.getStrSize(), var.getType());
|
||
}catch (Throwable e){
|
||
logger.info("host:"+s7Client.getHost()+" ; read 操作出现问题: {}",e.getMessage(),e);
|
||
return null;
|
||
}
|
||
}
|
||
private void write(S7Client s7Client, Step2Mes2PlcVar var, Object newValue) {
|
||
if(var.getType().equals(PlcVar.STRING_Array)){
|
||
String[] s = (String[])newValue;
|
||
String[] ss = (String[])newValue;
|
||
if(s.length > var.getLength() ){
|
||
ss = new String[var.getLength()];
|
||
for(int i=0;i< var.getLength();i++){
|
||
ss[i] = s[i];
|
||
}
|
||
}
|
||
s7Client.write(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getStrSize(), var.getType(),ss);
|
||
}else {
|
||
s7Client.write(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getStrSize(), var.getType(),newValue);
|
||
}
|
||
}
|
||
|
||
private Object read(S7Client s7Client, Step3Plc2MesVar var) {
|
||
try {
|
||
return s7Client.read(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getLength(), var.getStrSize(), var.getType());
|
||
}catch (Throwable e){
|
||
logger.info("host:"+s7Client.getHost()+" ; read 操作出现问题: {}",e.getMessage(),e);
|
||
return null;
|
||
}
|
||
}
|
||
private void write(S7Client s7Client, Step3Mes2PlcVar var, Object newValue) {
|
||
if(var.getType().equals(PlcVar.STRING_Array)){
|
||
String[] s = (String[])newValue;
|
||
String[] ss = (String[])newValue;
|
||
if(s.length > var.getLength() ){
|
||
ss = new String[var.getLength()];
|
||
for(int i=0;i< var.getLength();i++){
|
||
ss[i] = s[i];
|
||
}
|
||
}
|
||
s7Client.write(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getStrSize(), var.getType(),ss);
|
||
}else {
|
||
s7Client.write(var.getArea(), var.getAreaNumber(), var.getByteOffset(), var.getBitOffset(), var.getStrSize(), var.getType(),newValue);
|
||
}
|
||
}
|
||
|
||
|
||
public String getBoxId(String sapMaterial){
|
||
Integer todayBoxNum = service.getTodayBoxNum()+1;
|
||
String nowTime = DateUtil.format(new Date(), "yyMMdd");
|
||
|
||
todayBoxNum+=500;
|
||
logger.info("BOXID生成Event-------现在生成boxId ,sapMaterial: "+sapMaterial+",nowTime:"+nowTime+",todayBoxNum:"+formateString(todayBoxNum));
|
||
String res = "301"+sapMaterial+nowTime+formateString(todayBoxNum);
|
||
return res;
|
||
}
|
||
|
||
|
||
public String getErrorBoxId(String sapMaterial){
|
||
|
||
String nowTime = DateUtil.format(new Date(), "yyMMdd");
|
||
String mixBoxError = "001";
|
||
String res = "444"+sapMaterial+nowTime+mixBoxError;
|
||
logger.info("BOXID生成Event-------现在生成errBoxId ,sapMaterial: "+sapMaterial+",nowTime:"+nowTime);
|
||
|
||
return res;
|
||
}
|
||
|
||
public String removeKG(String str){
|
||
return str.substring(0,17);
|
||
}
|
||
|
||
public static void main(String[] args) {
|
||
Integer a = 102;
|
||
Format f1 = new DecimalFormat("000");
|
||
System.out.println(f1.format(a));
|
||
}
|
||
public String formateString(Integer a){
|
||
Format f1 = new DecimalFormat("000");
|
||
return f1.format(a);
|
||
}
|
||
|
||
//return true 出现混档了,return false 没有出现混档
|
||
private boolean isMixLevel(String[] subIds,Integer size){
|
||
String correctLevel = substrateServiceBiz.getBySubId(subIds[0]).getPowerLevel();
|
||
String errMsg = "以下基板ID出现混档:";
|
||
boolean res = false;
|
||
for(int i=0;i<size;i++){
|
||
String powerLevel = substrateServiceBiz.getBySubId(subIds[i]).getPowerLevel();
|
||
|
||
if(!powerLevel.equals(correctLevel)){
|
||
errMsg += "基板ID:"+subIds[i]+" 档位:"+powerLevel+", ";
|
||
res = true;
|
||
}
|
||
}
|
||
if(res){
|
||
logger.error(errMsg);
|
||
}
|
||
|
||
return res;
|
||
}
|
||
|
||
|
||
|
||
|
||
@Override
|
||
public void run(ApplicationArguments args) throws Exception {
|
||
kukaStep2.scheduleAtFixedRate(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
try {
|
||
|
||
|
||
//调度开始
|
||
Integer shelfIsFull = waitingForTarget(Step2Plc2MesVar.ShelfIsFull, true);
|
||
logger.info("");
|
||
logger.info("================================== 现在开始执行 过程二 任务( 开始 ) ===================================");
|
||
if(shelfIsFull != 1){
|
||
logger.info("BOXID生成Event-------失败"+" --- "+"MES监听 Step2Plc2MesVar.ShelfIsFull 是否 等于 1 失败");
|
||
}else if(shelfIsFull == 2){
|
||
throw new Exception("MES 监听值出现异常。");
|
||
}
|
||
|
||
//一. 从plc 中获取 subIdList 和 lineNum
|
||
String[] subIdList = (String[])read(S7Client.S7_DDJ1,Step2Plc2MesVar.SubIdList);
|
||
Integer listSize = (Integer)read(S7Client.S7_DDJ1,Step2Plc2MesVar.PackageNumberSet)-1;
|
||
|
||
|
||
//二. MES 业务
|
||
//=============== MES 业务 开始 =================
|
||
//1.MES 生成BoxId
|
||
if(subIdList.length<=0){
|
||
logger.info("BOXID生成Event-------失败"+" --- "+"从plc里面获取到的基板列表 为空");
|
||
throw new Exception("BOXID生成Event失败-------kuka机械手 取到了空箱");
|
||
}
|
||
if(listSize == null){
|
||
logger.info("BOXID生成Event-------失败"+" --- "+"从plc里面获取到的listSize 为null");
|
||
throw new Exception("BOXID生成Event失败-------listSize 为null");
|
||
}
|
||
|
||
//真实的基板ID 就是17位的,plc读取过来,有空格,要把后面3位空格去掉就行了。
|
||
for(int i=0;i<subIdList.length;i++){
|
||
subIdList[i] = removeKG(subIdList[i]);
|
||
}
|
||
logger.info("BOXID生成Event-------获取到基板列表:"+ Arrays.toString(subIdList)+",基板数量:"+listSize);
|
||
|
||
WoPackagingBoxSubstrate bySubId = substrateServiceBiz.getBySubId(subIdList[0]);
|
||
logger.info("BOXID生成Event-------依据subId"+subIdList[0]+"去qgs数据库 查询数据,数据内容:"+bySubId);
|
||
|
||
|
||
//如果我们系统中找不到这块基板那么就去camline里面找
|
||
if(bySubId == null){
|
||
logger.info("BOXID生成Event-------数据库中 无subId:"+ subIdList[0]+"现在从camline 系统获取这个基板相关信息");
|
||
//todo
|
||
CamlineSubIdDTO subIdByCamline = service.getSubIdByCamline(subIdList[0]);
|
||
bySubId = new WoPackagingBoxSubstrate();
|
||
bySubId.setPowerLevel(subIdByCamline.getPowerLevel());
|
||
bySubId.setSapMaterial(subIdByCamline.getSapMaterial());
|
||
bySubId.setLineBody(1);
|
||
}
|
||
|
||
|
||
//校验是否存在混档位情况
|
||
String boxId = "";
|
||
if(isMixLevel(subIdList,listSize)){
|
||
//出现混档
|
||
logger.info("BOXID生成Event-------subIdList :"+subIdList+"出现混档");
|
||
boxId = getErrorBoxId(bySubId.getSapMaterial());
|
||
|
||
}else {
|
||
//正常情况
|
||
logger.info("BOXID生成Event-------subIdList :"+subIdList+"没有出现混档");
|
||
boxId = getBoxId(bySubId.getSapMaterial());
|
||
}
|
||
logger.info("BOXID生成Event-------subIdList :"+subIdList+"isMixLevel 校验结束");
|
||
|
||
logger.info("BOXID生成Event-------mes 生成的boxId :"+ boxId);
|
||
//2. 依次update 基板表,把boxId 赋予这些基板
|
||
for(int i=0;i<listSize;i++){
|
||
substrateServiceBiz.updatePackagingBoxIdAndSlotByWoSubstrateId(boxId,(i+1),subIdList[i]);
|
||
}
|
||
//3.去camline数据库 查询此基板ID 对应的订单
|
||
String orderNameBySubId = service.getOrderNameBySubId(subIdList[0]);
|
||
//4. box信息录入到box表
|
||
WoPackagingBox woPackagingBox = new WoPackagingBox();
|
||
woPackagingBox.setBoxNo(boxId);
|
||
woPackagingBox.setSubstrateQuantity(listSize);
|
||
woPackagingBox.setPackagingTime(LocalDateTime.now());
|
||
woPackagingBox.setPowerLevel(bySubId.getPowerLevel());
|
||
woPackagingBox.setLineBody(bySubId.getLineBody());
|
||
woPackagingBox.setSapMaterial(bySubId.getSapMaterial());
|
||
woPackagingBox.setOrderNum(orderNameBySubId);
|
||
//1-手动模式,2-自动模式
|
||
woPackagingBox.setModel(2);
|
||
woPackagingBox.setCreateTime(LocalDateTime.now());
|
||
woPackagingBox.setPackagingTime(LocalDateTime.now());
|
||
boxServiceBiz.insert(woPackagingBox);
|
||
logger.info("BOXID生成Event-------boxId :"+ boxId+"写入数据库成功");
|
||
//=============== MES 业务 结束 =================
|
||
|
||
//三. 把生成的BoxId 告诉kuka
|
||
write(S7Client.S7_DDJ1,Step2Mes2PlcVar.BoxId,boxId);
|
||
logger.info("BOXID生成Event-------boxId :"+ boxId+"传给PLC 成功");
|
||
|
||
//四. 当MES完成任务后,把MesToPlc.ShelfIsFullFinish变量置为true,告诉plc,我操作完成了
|
||
write(S7Client.S7_DDJ1,Step2Mes2PlcVar.ShelfIsFullFinish,true);
|
||
logger.info("BOXID生成Event-------boxId :"+ boxId+"ShelfIsFullFinish 置为true 成功");
|
||
//四. mes 监控 到PlcToMes.SubArrived==false,就把MesToPlc.SubArrivedFinish置为false
|
||
Integer subArrived2 = waitingForTarget(Step2Plc2MesVar.ShelfIsFull, false);
|
||
if(subArrived2 != 1){
|
||
logger.info("BOXID生成Event-------失败"+" --- "+"步骤1. MES监听 Step1Plc2MesVar.SubArrivedToMes 是否 等于 1 失败");
|
||
}else if(subArrived2 == 2){
|
||
throw new Exception("MES 监听值出现异常。");
|
||
}
|
||
write(S7Client.S7_DDJ1,Step2Mes2PlcVar.ShelfIsFullFinish,false);
|
||
logger.info("BOXID生成Event-------boxId :"+ boxId+"ShelfIsFullFinish 置为false 成功,Event2 成功结束");
|
||
logger.info("================================== 现在开始执行 过程二 任务( 结束 ) ===================================");
|
||
logger.info("");
|
||
|
||
}catch (Throwable e){
|
||
// logger.info("BOXID生成Event------- 出现异常:" + e.toString());
|
||
logger.info("BOXID生成Event--- 出现异常 : {}",e.getMessage(),e);
|
||
}
|
||
}
|
||
},1,1, TimeUnit.SECONDS);
|
||
|
||
}
|
||
|
||
/**
|
||
*
|
||
* arg:
|
||
* Step1Plc2MesVar 参数类型
|
||
* targetValue 目的值
|
||
* return:
|
||
* 2 s7 访问异常;
|
||
* 1 成功;
|
||
* */
|
||
private Integer waitingForTarget(Step1Plc2MesVar var, Boolean targetValue) {
|
||
Integer res = 0;
|
||
while (true){
|
||
try {
|
||
|
||
Boolean now = (Boolean) read(S7Client.S7_DDJ1,var);
|
||
//注意:即使now为null ,这里也不会抛出异常,这里很容易产生 空轮询。
|
||
if(now == null){
|
||
throw new Exception("");
|
||
}
|
||
if(targetValue.equals(now)){
|
||
res = 1;
|
||
break;
|
||
}
|
||
Thread.sleep(200);
|
||
}catch (Throwable e){
|
||
logger.info("S7 数据采集 异常. 监听 变量 :"+var.getName()+" 为目标值:"+targetValue+"失败");
|
||
res = 2;
|
||
break;
|
||
}
|
||
}
|
||
return res;
|
||
}
|
||
/**
|
||
*
|
||
* arg:
|
||
* Step2Plc2MesVar 参数类型
|
||
* targetValue 目的值
|
||
* return:
|
||
* 2 s7 访问异常;
|
||
* 1 成功;
|
||
* */
|
||
private Integer waitingForTarget(Step2Plc2MesVar var, Boolean targetValue) {
|
||
Integer res = 0;
|
||
while (true){
|
||
try {
|
||
Boolean now = (Boolean) read(S7Client.S7_DDJ1,var);
|
||
if(now == null){
|
||
throw new Exception("");
|
||
}
|
||
if(targetValue.equals(now)){
|
||
res = 1;
|
||
break;
|
||
}
|
||
Thread.sleep(200);
|
||
}catch (Throwable e){
|
||
logger.info("S7 数据采集 异常. 监听 变量 :"+var.getName()+" 为目标值:"+targetValue+"失败");
|
||
res = 2;
|
||
break;
|
||
}
|
||
}
|
||
return res;
|
||
}
|
||
/**
|
||
*
|
||
* arg:
|
||
* Step3Plc2MesVar 参数类型
|
||
* targetValue 目的值
|
||
* return:
|
||
* 2 s7 访问异常;
|
||
* 1 成功;
|
||
* */
|
||
private Integer waitingForTarget(Step3Plc2MesVar var, Boolean targetValue) {
|
||
Integer res = 0;
|
||
while (true){
|
||
try {
|
||
Boolean now = (Boolean) read(S7Client.S7_DDJ1,var);
|
||
if(now == null){
|
||
throw new Exception("");
|
||
}
|
||
if(targetValue.equals(now)){
|
||
res = 1;
|
||
break;
|
||
}
|
||
Thread.sleep(200);
|
||
}catch (Throwable e){
|
||
logger.info("S7 数据采集 异常. 监听 变量 :"+var.getName()+" 为目标值:"+targetValue+"失败");
|
||
res = 2;
|
||
break;
|
||
}
|
||
}
|
||
return res;
|
||
}
|
||
} |