package com.mes.plcTaskThread;
|
|
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 com.mes.service.ModbusTcp;
|
import com.mes.service.PlcAgreement;
|
import com.mes.service.PlcParameter;
|
import com.mes.utils.HexUtil;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
@Slf4j
|
public class MachineDispensing extends Thread {
|
|
|
private MachineMapper machineMapper;
|
private TaskingService taskingService;
|
public static Long machineId = 16L;
|
public List<Map> sendRecords = new ArrayList<>();
|
public Machine thisMachine;
|
|
public MachineDispensing(Machine machine, MachineMapper machineMapper, TaskingService taskingService) {
|
this.thisMachine = machine;
|
this.machineMapper = machineMapper;
|
this.taskingService = taskingService;
|
}
|
|
public void plcStart() {
|
Machine machine = machineMapper.selectById(thisMachine.getId());
|
String fileName = machine.getFileName();
|
ModbusTcp modbusTcp1 = new ModbusTcp(machine.getIp(), machine.getPort(), fileName);
|
modbusTcp1.connect();
|
PlcAgreement plcAgreement = modbusTcp1.getPlcAgreement(fileName);
|
try {
|
if (!modbusTcp1.read(plcAgreement)) {
|
log.info("通讯读取新数据失败");
|
modbusTcp1.close();
|
return;
|
}
|
PlcParameter plcRequest = plcAgreement.getPlcParameter("plcRequest");//请求字
|
PlcParameter plcRequestID = plcAgreement.getPlcParameter("plcRequestID");//请求ID
|
PlcParameter mesSend = plcAgreement.getPlcParameter("mesSend");//发送字
|
PlcParameter mesSendID = plcAgreement.getPlcParameter("mesSendID");//发送ID
|
// PlcParameterInfo drawingMark =plcParameterObject.getPlcParameter("drawingGlue");//点胶图纸地址
|
PlcParameter JPZX = plcAgreement.getPlcParameter("JPZX");//类型
|
PlcParameter thickness = plcAgreement.getPlcParameter("thickness");//厚
|
PlcParameter side1 = plcAgreement.getPlcParameter("side1");//下边
|
PlcParameter side2 = plcAgreement.getPlcParameter("side2");//右边
|
PlcParameter side3 = plcAgreement.getPlcParameter("side3");//上边
|
PlcParameter side4 = plcAgreement.getPlcParameter("side4");//左边
|
PlcParameter R_angle1_1 = plcAgreement.getPlcParameter("R_angle1_1");//R角1-1
|
PlcParameter R_angle1_2 = plcAgreement.getPlcParameter("R_angle1_2");//R角1-2
|
PlcParameter R_angle2_1 = plcAgreement.getPlcParameter("R_angle2_1");//R角2-1
|
PlcParameter R_angle2_2 = plcAgreement.getPlcParameter("R_angle2_2");//R角2-2
|
PlcParameter R_angle3_1 = plcAgreement.getPlcParameter("R_angle3_1");//R角3-1
|
PlcParameter R_angle3_2 = plcAgreement.getPlcParameter("R_angle3_2");//R角3-2
|
PlcParameter R_angle4_1 = plcAgreement.getPlcParameter("R_angle4_1");//R角4-1
|
PlcParameter R_angle4_2 = plcAgreement.getPlcParameter("R_angle4_2");//R角4-2
|
|
PlcParameter plcReport = plcAgreement.getPlcParameter("plcReport");//汇报字
|
PlcParameter plcReportID = plcAgreement.getPlcParameter("plcReportID");//汇报ID
|
PlcParameter mesConfirm = plcAgreement.getPlcParameter("mesConfirm");//确认字
|
PlcParameter mesConfirmID = plcAgreement.getPlcParameter("mesConfirmID");//确认ID
|
PlcParameter machineStatusWord = plcAgreement.getPlcParameter("machineStatusWord");
|
if (machine.getIsLog() > 0) {
|
modbusTcp1.consoleLogInfo(plcAgreement);
|
}
|
|
if (0 == plcRequest.getValueInt() && 1 == mesSend.getValueInt()) {
|
log.info("1.发送字置零");
|
//发送字置0
|
String send = HexUtil.intTo2ByteHex(0);
|
modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesSend.getAddressStart() / 2));
|
log.info("发送字 清空成功;内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesSend.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}
|
if (0 == plcReport.getValueInt() && 0 != mesConfirm.getValueInt()) {
|
log.info("2.确认字置零");
|
//发送字置0
|
String send = HexUtil.intTo2ByteHex(0);
|
modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart() / 2));
|
log.info("任务完成 清空成功;内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}
|
if (1 == plcReport.getValueInt() && 0 == mesConfirm.getValueInt()) {
|
int finishCount = taskingService.finishMachineTask(machine);
|
log.info("3、任务完成");
|
if (finishCount > 0) {//有任务
|
String send = HexUtil.intTo2ByteHex(1);
|
modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart() / 2));
|
log.info("任务完成 成功 内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}
|
String send = HexUtil.intTo2ByteHex(1);
|
modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart() / 2));
|
log.info("任务完成 异常成功 内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}
|
if (4 == plcReport.getValueInt() && 0 == mesConfirm.getValueInt()) {
|
int finishCount = taskingService.glassDownLineOne(machine);
|
if (finishCount > 0) {//有任务
|
log.info("4、人工拿走");
|
String send = HexUtil.intTo2ByteHex(4);
|
modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart() / 2));
|
log.info("人工拿走发送报文内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}
|
String send = HexUtil.intTo2ByteHex(4);
|
modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart() / 2));
|
log.info("人工拿走异常 发送报文内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}
|
if (2 == plcReport.getValueInt() && 0 == mesConfirm.getValueInt()) {
|
int finishCount = taskingService.loseMachineTask(machine);
|
if (finishCount > 0) {//有任务
|
log.info("2、重发");
|
String send = HexUtil.intTo2ByteHex(2);
|
modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart() / 2));
|
log.info("重发发送报文内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
} else {
|
log.info("2、异常重发");
|
String send = HexUtil.intTo2ByteHex(2);
|
modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart() / 2));
|
log.info("异常重发发送报文内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}
|
}
|
|
if (3 == plcReport.getValueInt() && 0 == mesConfirm.getValueInt()) {
|
int finishCount = taskingService.finishMachineTask(machine);
|
if (finishCount > 0) {//有任务
|
log.info("3、设备上人工点破损");
|
String send = HexUtil.intTo2ByteHex(3);
|
modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart() / 2));
|
log.info("人工点破损发送报文内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
} else {
|
//是否自动清除破损
|
log.info("3、无效破损 (没有找到此ID的玻璃)");
|
String send = HexUtil.intTo2ByteHex(3);
|
modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart() / 2));
|
log.info("发送报文内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
|
modbusTcp1.close();
|
//modbusTcp1.close();
|
return;
|
}
|
}
|
List<Tasking> taskingCount = taskingService.findMachineWorkStateTask(machine, "正在工作");
|
if ((1 == plcRequest.getValueInt() && 0 == mesSend.getValueInt())
|
&& "开工".equals(machine.getState())
|
&& taskingCount.size() < machine.getMaxTaskCount()) {
|
Tasking tasking = taskingService.startMachineTask(machine);
|
if (tasking != null) {
|
log.info("4、发送任务 任务数据:{}, 设备状态:{}, ", machine, tasking);
|
String send =
|
HexUtil.intTo2ByteHex(1) +
|
HexUtil.intTo2ByteHex(tasking.getTaskSequence().intValue())+
|
HexUtil.intTo2ByteHex(tasking.getJPZX()) +
|
HexUtil.intTo2ByteHex(tasking.getThickness().intValue()) +
|
HexUtil.intTo2ByteHex(tasking.getWidth().intValue()) + // side1 下边 = 宽度
|
HexUtil.intTo2ByteHex(tasking.getLength().intValue()) + // side2 右边 = 长度
|
HexUtil.intTo2ByteHex(tasking.getWidth().intValue()) + // side3 上边 = 宽度
|
HexUtil.intTo2ByteHex(tasking.getLength().intValue()) + // side4 左边 = 长度
|
HexUtil.intTo2ByteHex((int) ((tasking.getR_1_1() != null ? tasking.getR_1_1() : 0.0) * 10)) +
|
HexUtil.intTo2ByteHex((int) ((tasking.getR_1_2() != null ? tasking.getR_1_2() : 0.0) * 10)) +
|
HexUtil.intTo2ByteHex((int) ((tasking.getR_2_1() != null ? tasking.getR_2_1() : 0.0) * 10)) +
|
HexUtil.intTo2ByteHex((int) ((tasking.getR_2_2() != null ? tasking.getR_2_2() : 0.0) * 10)) +
|
HexUtil.intTo2ByteHex((int) ((tasking.getR_3_1() != null ? tasking.getR_3_1() : 0.0) * 10)) +
|
HexUtil.intTo2ByteHex((int) ((tasking.getR_3_2() != null ? tasking.getR_3_2() : 0.0) * 10)) +
|
HexUtil.intTo2ByteHex((int) ((tasking.getR_4_1() != null ? tasking.getR_4_1() : 0.0) * 10)) +
|
HexUtil.intTo2ByteHex((int) ((tasking.getR_4_2() != null ? tasking.getR_4_2() : 0.0) * 10)) ;
|
modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesSend.getAddressStart() / 2));
|
log.info("发送任务发送报文内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesSend.getAddressStart()));
|
modbusTcp1.close();
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
String sendRecord = df.format(new Date()) + " " +
|
tasking.getGlassId() + "-" +
|
tasking.getLength() + "-" +
|
tasking.getWidth() + "-" +
|
tasking.getThickness() + "-" +
|
tasking.getR_1_1() + "-" +
|
tasking.getR_1_2() + "-" +
|
tasking.getR_2_1() + "-" +
|
tasking.getR_2_2() + "-" +
|
tasking.getR_3_1() + "-" +
|
tasking.getR_3_2() + "-" +
|
tasking.getR_4_1() + "-" +
|
tasking.getR_4_2() + "-" +
|
tasking.getJPZX();
|
if (sendRecords.size() > 7) {
|
sendRecords.remove(0);
|
}
|
Map sendContentMap = new HashMap();
|
sendContentMap.put("sendContent", sendRecord);
|
sendRecords.add(sendContentMap);
|
return;
|
}
|
}
|
//}
|
|
} catch (Exception e) {
|
log.info("交互逻辑错误");
|
}
|
|
modbusTcp1.close();
|
//log.info("无可执行的条件");
|
}
|
|
|
@Override
|
public void run() {
|
while (this != null) {
|
try {
|
Thread.sleep(1000);
|
plcStart();
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|