package com.mes.plcTaskThread;
|
|
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
|
import com.mes.common.S7objectMachine;
|
import com.mes.device.PlcParameterInfo;
|
import com.mes.device.PlcParameterObject;
|
import com.mes.md.entity.Machine;
|
import com.mes.md.entity.Tasking;
|
import com.mes.md.mapper.MachineMapper;
|
import com.mes.md.service.TaskingService;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
|
@Slf4j
|
public class MachineLamination extends Thread{
|
|
|
private MachineMapper machineMapper;
|
private TaskingService taskingService;
|
private S7objectMachine s7objectMachine;
|
public Machine thisMachine;
|
public MachineLamination(Machine machine, MachineMapper machineMapper, TaskingService taskingService){
|
this.thisMachine = machine;
|
this.machineMapper = machineMapper;
|
this.taskingService = taskingService;
|
}
|
|
public void plcStart() {
|
Machine machine=machineMapper.selectById(thisMachine.getId());
|
if (s7objectMachine==null){
|
s7objectMachine=new S7objectMachine(machine.getIp(),machine.getPort(),machine.getFileName(), EPlcType.S1200);
|
s7objectMachine.start();
|
}
|
PlcParameterObject plcParameterObject = s7objectMachine.PlcMesObject;
|
PlcParameterInfo plcRequest =plcParameterObject.getPlcParameter("plcRequest");//请求字
|
PlcParameterInfo mesSend =plcParameterObject.getPlcParameter("mesSend");//发送字
|
PlcParameterInfo lengthOutOfService =plcParameterObject.getPlcParameter("length");//长
|
PlcParameterInfo widthOutOfService =plcParameterObject.getPlcParameter("width");//宽
|
PlcParameterInfo markingMode =plcParameterObject.getPlcParameter("thickness");//厚度
|
|
PlcParameterInfo plcReport =plcParameterObject.getPlcParameter("plcReport");//汇报字
|
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.发送字置零,清除上次发送的数据{}",mesSend.getValue());
|
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;
|
}
|
return;
|
}
|
List<Tasking> taskingCount=taskingService.findMachineWorkStateTask(machine,"正在工作");
|
if("1".equals(plcRequest.getValue())&&"0".equals(mesSend.getValue())
|
&&"开工".equals(machine.getState())
|
&&taskingCount.size()<machine.getMaxTaskCount()){
|
|
Tasking tasking=taskingService.startMachineTask(machine);
|
if(tasking!=null){
|
log.info("4、发送数据:{},{},{}",tasking.getLength().intValue(),tasking.getWidth().intValue(),1);
|
s7objectMachine.plccontrol.writeWord(lengthOutOfService.getAddress(), tasking.getLength().intValue());//x
|
s7objectMachine.plccontrol.writeWord(widthOutOfService.getAddress(), tasking.getWidth().intValue());//y
|
|
s7objectMachine.plccontrol.writeWord(mesSend.getAddress(), 1);
|
return;
|
}
|
}
|
}
|
|
@Override
|
public void run() {
|
while (this != null) {
|
try {
|
Thread.sleep(1000);
|
plcStart();
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|