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