| | |
| | | package com.mes.job; |
| | | |
| | | import cn.hutool.json.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.github.yulichang.wrapper.MPJLambdaWrapper; |
| | | import com.github.s7connector.api.DaveArea; |
| | | import com.github.s7connector.api.S7Connector; |
| | | import com.github.s7connector.api.factory.S7ConnectorFactory; |
| | | import com.github.s7connector.impl.serializer.converter.StringConverter; |
| | | import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType; |
| | | import com.mes.common.S7object; |
| | | import com.mes.common.S7objectFlipSlice; |
| | | import com.mes.common.S7objectMachine; |
| | | import com.mes.common.S7objectMarking; |
| | | import com.mes.device.PlcParameterInfo; |
| | | import com.mes.device.PlcParameterObject; |
| | | import com.mes.md.entity.GlassInfo; |
| | | import com.mes.md.entity.Machine; |
| | | import com.mes.md.entity.Tasking; |
| | | import com.mes.md.entity.WorkTaskDetail; |
| | | import com.mes.md.mapper.GlassInfoMapper; |
| | | import com.mes.md.mapper.MachineMapper; |
| | | import com.mes.md.mapper.TaskingMapper; |
| | | import com.mes.md.mapper.WorkTaskDetailMapper; |
| | | import com.mes.md.service.TaskingService; |
| | | import com.mes.service.ModbusTcp; |
| | | import com.mes.service.PlcAgreement; |
| | | import com.mes.tools.WebSocketServer; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | import sun.nio.cs.ext.GBK; |
| | | |
| | | import java.net.Socket; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.nio.charset.Charset; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 自动打标机 |
| | | * @Author : yanzhxiin |
| | | * @Date: 2024/8/20 11:19 |
| | | * @Description: |
| | |
| | | public class MarkingTask { |
| | | |
| | | public static String engineerId = ""; |
| | | |
| | | @Autowired |
| | | TaskingMapper taskingMapper; |
| | | @Autowired |
| | | MachineMapper machineMapper; |
| | | @Autowired |
| | | TaskingService taskingService; |
| | | @Autowired |
| | | private GlassInfoMapper glassInfoMapper; |
| | | |
| | | // @Scheduled(fixedDelay = 1000) |
| | | public void plcMarkingTask(Long machineId) { |
| | | public S7objectMachine s7objectMachine; |
| | | |
| | | public List<Map> sendRecords = new ArrayList<>(); |
| | | |
| | | //@Scheduled(fixedDelay = 1000) |
| | | public void plcMarkingTask() { |
| | | Long machineId=12L; |
| | | Machine machine=machineMapper.selectById(machineId); |
| | | PlcParameterObject plcParameterObject = S7object.getinstance().PlcMesObject; |
| | | String taskRequestTypeValue = plcParameterObject.getPlcParameter("A06_request_word").getValue(); |
| | | String glassIdeValue = plcParameterObject.getPlcParameter("A05_scanning_ID").getValue(); |
| | | String confirmationWrodValue = plcParameterObject.getPlcParameter("MES_confirmation_word").getValue(); |
| | | String confirmationWrodAddress = plcParameterObject.getPlcParameter("MES_confirmation_word").getAddress(); |
| | | String currentSlot = plcParameterObject.getPlcParameter("Current_slot").getValue(); |
| | | if ("0".equals(taskRequestTypeValue)) { |
| | | if ("0".equals(confirmationWrodValue)) { |
| | | log.info("2、获取到的请求字为0,且发送字为0,不执行任务"); |
| | | if (s7objectMachine==null){ |
| | | s7objectMachine=new S7objectMachine(machine.getIp(),machine.getPort(),"PlcMarking", EPlcType.S1500); |
| | | s7objectMachine.start(); |
| | | } |
| | | //PlcParameterObject plcParameterObject = S7objectMarking.getinstance().PlcMesObject; |
| | | PlcParameterObject plcParameterObject = s7objectMachine.PlcMesObject; |
| | | PlcParameterInfo plcRequest =plcParameterObject.getPlcParameter("plcRequest");//请求字 |
| | | PlcParameterInfo plcRequestID =plcParameterObject.getPlcParameter("plcRequestID");//请求ID |
| | | PlcParameterInfo mesSend =plcParameterObject.getPlcParameter("mesSend");//发送字 |
| | | PlcParameterInfo isMark =plcParameterObject.getPlcParameter("isMark");//发送ID【0 不打标,1标准模式,2定制模式】 |
| | | PlcParameterInfo drawingMark =plcParameterObject.getPlcParameter("drawingMark");//打标图纸地址 |
| | | PlcParameterInfo lengthOutOfService =plcParameterObject.getPlcParameter("length(OutOfService)");//长 |
| | | PlcParameterInfo widthOutOfService =plcParameterObject.getPlcParameter("width(OutOfService)");//宽 |
| | | PlcParameterInfo markingMode =plcParameterObject.getPlcParameter("markingMode");// |
| | | |
| | | PlcParameterInfo plcReport =plcParameterObject.getPlcParameter("plcReport");//汇报字 |
| | | PlcParameterInfo plcReportID =plcParameterObject.getPlcParameter("plcReportID");//汇报ID |
| | | PlcParameterInfo mesConfirm =plcParameterObject.getPlcParameter("mesConfirm");//确认字 |
| | | PlcParameterInfo mesConfirmID =plcParameterObject.getPlcParameter("mesConfirmID");//确认ID |
| | | if (machine.getIsLog()>0){ |
| | | s7objectMachine.consoleLogInfo(); |
| | | } |
| | | if ("0".equals(plcRequest.getValue())&&"1".equals(mesSend.getValue())) { |
| | | //发送字置0 |
| | | log.info("1.发送字置零,清除上次发送的数据{},{},{},{},{}" |
| | | ,markingMode.getValue() |
| | | ,isMark.getValue() |
| | | ,lengthOutOfService.getValue() |
| | | ,widthOutOfService.getValue() |
| | | ,mesSend.getValue()); |
| | | //S7objectMarking.getinstance().plccontrol.writeString(drawingMark.getAddress(),""); |
| | | s7objectMachine.plccontrol.writeWord(markingMode.getAddress(), 0); |
| | | s7objectMachine.plccontrol.writeWord(lengthOutOfService.getAddress(), 0); |
| | | s7objectMachine.plccontrol.writeWord(widthOutOfService.getAddress(), 0); |
| | | s7objectMachine.plccontrol.writeWord(isMark.getAddress(), 0); |
| | | s7objectMachine.plccontrol.writeWord(mesSend.getAddress(), 0); |
| | | return; |
| | | } |
| | | if ("0".equals(plcReport.getValue())&&"1".equals(mesConfirm.getValue())) { |
| | | log.info("2.确认字置零"); |
| | | //发送字置0 |
| | | s7objectMachine.plccontrol.writeWord(mesConfirm.getAddress(), 0); |
| | | return; |
| | | } |
| | | if("1".equals(plcReport.getValue())&&"0".equals(mesConfirm.getValue())){ |
| | | int finishCount=taskingService.finishMachineTask(machine); |
| | | log.info("3、任务完成"); |
| | | if(finishCount>0){//有任务 |
| | | s7objectMachine.plccontrol.writeWord(mesConfirm.getAddress(), 1); |
| | | return; |
| | | } |
| | | log.info("2、获取到的请求字为0,将发送字改为0"); |
| | | S7object.getinstance().plccontrol.writeWord(confirmationWrodAddress, 0); |
| | | return; |
| | | } |
| | | if (!"0".equals(confirmationWrodValue)) { |
| | | log.info("2、获取到的请求字不为0,将发送字不为0,直接结束"); |
| | | return; |
| | | List<Tasking> taskingCount=taskingService.findMachineWorkStateTask(machine,"正在工作"); |
| | | if("1".equals(plcRequest.getValue())&&"0".equals(mesSend.getValue())&& !plcRequestID.getValue().isEmpty() |
| | | &&"开工".equals(machine.getState()) |
| | | &&taskingCount.size()<machine.getMaxTaskCount()){ |
| | | Tasking tasking=taskingService.startMachineTask(machine); |
| | | if(tasking!=null){ |
| | | //模式:1定制 0 批量 是否打标:0不打标 1打标 |
| | | int isMark_=(tasking.getDrawingMarking()==null||tasking.getDrawingMarking().isEmpty())?0:1; |
| | | int isMarkingMode_="定制".equals(tasking.getTaskType()) ?1:0; |
| | | log.info("4、发送数据:{},{},{},{},{},{}" |
| | | ,tasking.getProgramId() |
| | | ,tasking.getLength().intValue(),tasking.getWidth().intValue(),isMark_,isMarkingMode_,1); |
| | | if(isMark_==1){ |
| | | s7objectMachine.plccontrol.writeString(drawingMark.getAddress(), tasking.getProgramId()); |
| | | } |
| | | //S7objectMarking.getinstance().plccontrol.writeUInt32 (mesSendID.getAddress(), 2); |
| | | s7objectMachine.plccontrol.writeWord(lengthOutOfService.getAddress(), tasking.getLength().intValue()); |
| | | s7objectMachine.plccontrol.writeWord(widthOutOfService.getAddress(), tasking.getWidth().intValue()); |
| | | s7objectMachine.plccontrol.writeWord(isMark.getAddress(), isMark_); |
| | | s7objectMachine.plccontrol.writeWord(markingMode.getAddress(), isMarkingMode_); |
| | | s7objectMachine.plccontrol.writeWord(mesSend.getAddress(), 1); |
| | | |
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String sendRecord=df.format(new Date())+" "+ |
| | | tasking.getScanId()+"-"+ |
| | | tasking.getProgramId()+"-"+ |
| | | tasking.getLength()+"-"+ |
| | | tasking.getWidth()+"-"+ |
| | | isMark_+"-"+ |
| | | isMarkingMode_; |
| | | if (sendRecords.size()>7){ |
| | | sendRecords.remove(0); |
| | | } |
| | | Map sendContentMap=new HashMap(); |
| | | sendContentMap.put("sendContent",sendRecord); |
| | | sendRecords.add(sendContentMap); |
| | | return; |
| | | } |
| | | } |
| | | if ("1".equals(taskRequestTypeValue)) { |
| | | log.info("2、进片请求,且发送字为0,执行打标任务"); |
| | | plcRequest(glassIdeValue, confirmationWrodAddress, currentSlot,machine); |
| | | }else if ("3".equals(taskRequestTypeValue)) { |
| | | log.info("2、完成请求,执行完成任务"); |
| | | plcReport(glassIdeValue, confirmationWrodAddress, currentSlot,machine); |
| | | } |
| | | } |
| | | public void plcRequest(String glassIdeValue, String confirmationWrodAddress, String currentSlot, Machine machine) { |
| | | //查找打标机任务 |
| | | Tasking tasking=taskingService.startMachineTask(machine); |
| | | if(tasking!=null&&"开工".equals(machine.getState())){//有任务 |
| | | log.info("发送任务:{}",tasking); |
| | | }else{ |
| | | log.info("不满足发送条件:{}",tasking); |
| | | } |
| | | //log.info("无可执行的条件"); |
| | | |
| | | } |
| | | public void plcReport(String glassIdeValue, String confirmationWrodAddress, String currentSlot, Machine machine) { |
| | | //查找打标机工作的任务 |
| | | //查找打标机任务 |
| | | int finishCount=taskingService.finishMachineTask(machine); |
| | | if(finishCount>0){//数据已标记完成 |
| | | log.info("正常汇报:"); |
| | | |
| | | }else{ |
| | | log.info("当前无共工作的任务,无效汇报完成!"); |
| | | } |
| | | } |
| | | //@Scheduled(fixedDelay = 1000) |
| | | @Scheduled(fixedDelay = 500) |
| | | public void markingTasks() { |
| | | JSONObject jsonObject = new JSONObject(); |
| | | Machine machine=machineMapper.selectById(11L); |
| | | Machine machine=machineMapper.selectById(12L); |
| | | List<Tasking> taskingList=taskingService.findMachineTask(machine); |
| | | jsonObject.append("taskingList", taskingList); |
| | | |
| | | jsonObject.append("machine", machine); |
| | | jsonObject.append("sendRecords", sendRecords); |
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("marking"); |
| | | if (sendwServer != null) { |
| | | for (WebSocketServer webserver : sendwServer) { |