package com.mes.plcTaskThread;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.mes.md.entity.LineConfiguration;
|
import com.mes.md.entity.Machine;
|
import com.mes.md.entity.PrimitiveTask;
|
import com.mes.md.entity.Tasking;
|
import com.mes.md.mapper.MachineMapper;
|
import com.mes.md.mapper.TaskingMapper;
|
import com.mes.md.service.LineConfigurationService;
|
import com.mes.md.service.PrimitiveTaskService;
|
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.io.InputStream;
|
import java.io.OutputStream;
|
import java.net.Socket;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
@Slf4j
|
public class MachineEdging extends Thread{
|
|
TaskingMapper taskingMapper;
|
|
MachineMapper machineMapper;
|
|
TaskingService taskingService;
|
|
PrimitiveTaskService primitiveTaskService;
|
|
LineConfigurationService lineConfigurationService;
|
public static int thinness = 40;
|
public static int edgSpeed = 40;
|
//public static Long machineId = 6L;
|
public List<Map> sendRecords = new ArrayList<>();
|
public Machine thisMachine;
|
public MachineEdging(Machine machine,TaskingMapper taskingMapper, MachineMapper machineMapper, TaskingService taskingService, PrimitiveTaskService primitiveTaskService, LineConfigurationService lineConfigurationService){
|
this.thisMachine = machine;
|
this.taskingMapper = taskingMapper;
|
this.machineMapper = machineMapper;
|
this.taskingService = taskingService;
|
this.primitiveTaskService = primitiveTaskService;
|
this.lineConfigurationService = lineConfigurationService;
|
}
|
|
public void plcStart() {
|
Machine machine=machineMapper.selectById(thisMachine.getId());
|
ModbusTcp modbusTcp1 =new ModbusTcp(machine.getIp(),machine.getPort(),machine.getFileName());
|
try {
|
modbusTcp1.connect();
|
PlcAgreement plcAgreement=modbusTcp1.getPlcAgreement("PlcEdging");
|
if(!modbusTcp1.read(plcAgreement)){
|
log.info("通讯读取新数据失败");
|
modbusTcp1.close();
|
return;
|
}
|
PlcParameter inputOrOut2 =plcAgreement.getPlcParameter("inputOrOut2");
|
edgSpeed=inputOrOut2.getValueInt();
|
|
PlcParameter plcRequest =plcAgreement.getPlcParameter("plcRequest");//请求字
|
PlcParameter mesSend =plcAgreement.getPlcParameter("mesSend");//发送字
|
|
PlcParameter plcReport =plcAgreement.getPlcParameter("plcReport");//汇报字
|
PlcParameter plcReportID =plcAgreement.getPlcParameter("plcReportID");//汇报ID
|
PlcParameter plcReport2 =plcAgreement.getPlcParameter("plcReport2");//手动汇报字
|
PlcParameter plcReportID2 =plcAgreement.getPlcParameter("plcReportID2");//手动汇报ID
|
|
PlcParameter mesConfirm =plcAgreement.getPlcParameter("mesConfirm");//确认字
|
PlcParameter mesConfirmID =plcAgreement.getPlcParameter("mesConfirmID");//确认ID
|
PlcParameter mesConfirm2 =plcAgreement.getPlcParameter("mesConfirm2");//手动确认字
|
PlcParameter mesConfirmID2 =plcAgreement.getPlcParameter("mesConfirmID2");//手动确认ID
|
|
PlcParameter alarmStatus =plcAgreement.getPlcParameter("alarmStatus");//报警字
|
PlcParameter machineStatusWord =plcAgreement.getPlcParameter("machineStatusWord");//设备状态字
|
|
// if (alarmStatus.getValueInt()>0&&!"报警".equals(machine.getStatePowerOn())){
|
// machine.setStatePowerOn("报警");
|
// }else if(alarmStatus.getValueInt()==0&&!"报警解除".equals(machine.getStatePowerOn())){
|
// machine.setStatePowerOn("报警解除");
|
// }
|
// if (machineStatusWord.getValueInt()!=2&&!"非联机".equals(machine.getStatePowerOn())){
|
// machine.setStatePowerOn("非联机");
|
// }else if(machineStatusWord.getValueInt()==2&&!"联机".equals(machine.getStatePowerOn())){
|
// machine.setStatePowerOn("联机");
|
// }
|
|
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()));
|
errorMachine(machine,"");
|
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 (0==plcReport2.getValueInt()&&0!=mesConfirm2.getValueInt()) {
|
log.info("2.确认字置零");
|
//发送字置0
|
String send=HexUtil.intTo2ByteHex(0);
|
modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()/2));
|
log.info("任务完成 清空成功;内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesConfirm2.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==plcReport2.getValueInt()&&0==mesConfirm2.getValueInt()){
|
int finishCount=taskingService.glassDownLineOne(machine);
|
if(finishCount>0){//有任务
|
log.info("4、人工拿走");
|
String send=HexUtil.intTo2ByteHex(4);
|
modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()/2));
|
log.info("人工拿走发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}
|
String send=HexUtil.intTo2ByteHex(4);
|
modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()/2));
|
log.info("人工拿走异常 发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}
|
if(2==plcReport2.getValueInt()&&0==mesConfirm2.getValueInt()){
|
int finishCount=taskingService.loseMachineTask(machine);
|
if(finishCount>0){//有任务
|
log.info("2、重发");
|
String send=HexUtil.intTo2ByteHex(2);
|
modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()/2));
|
log.info("重发发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}else{
|
log.info("2、异常重发");
|
String send=HexUtil.intTo2ByteHex(2);
|
modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()/2));
|
log.info("异常重发发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}
|
}
|
|
if(3==plcReport2.getValueInt()&&0==mesConfirm2.getValueInt()){
|
int finishCount=taskingService.finishMachineTask(machine);
|
if(finishCount>0){//有任务
|
log.info("3、设备上人工点破损");
|
String send=HexUtil.intTo2ByteHex(3);
|
modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()/2));
|
log.info("人工点破损发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}else{
|
//是否自动清除破损
|
log.info("3、无效破损 (没有找到此ID的玻璃)");
|
String send=HexUtil.intTo2ByteHex(3);
|
modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()/2));
|
log.info("发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}
|
}
|
List<Tasking> taskingCount=taskingService.findMachineWorkStateTask(machine,"正在工作");
|
|
if(((1==plcRequest.getValueInt()&&0==mesSend.getValueInt())
|
&&"开工".equals(machine.getState())
|
&&taskingCount.size()<machine.getMaxTaskCount())){
|
//Machine loadMachine=machineMapper.selectById(2L);
|
LineConfiguration lineConfigurationBefore=lineConfigurationService.machineLineConfigurationBefore(machine);
|
Machine loadMachine=machineMapper.selectById(lineConfigurationBefore.getMachineId());
|
Tasking tasking;
|
String scan_id="";
|
if(loadMachine.getMode()==1){
|
//标准模式 找已领取未完成的第一条任务
|
//LineConfiguration lineConfiguration=lineConfigurationService.machineLineConfiguration(machine);
|
List<PrimitiveTask> findPrimitiveTasking=primitiveTaskService.findPrimitiveTasking(machine,lineConfigurationBefore.getLineId());
|
//添加数据:
|
for(PrimitiveTask primitiveTask:findPrimitiveTasking){
|
List<Tasking> taskingTopList=taskingMapper.selectList(new QueryWrapper<Tasking>().lambda()
|
.eq(Tasking::getScanId,primitiveTask.getScanId())
|
);
|
//任务数>已上数量
|
if(taskingTopList.size()<primitiveTask.getTaskQuantity()){
|
LineConfiguration lineConfiguration=lineConfigurationService.machineLineConfiguration(machine);
|
Tasking taskingAdd=primitiveTaskService.convertListTasking(primitiveTask,lineConfiguration.getId());
|
Integer taskSequence=1;
|
if (machine.getTodayCount()>0&&machine.getTodayCount()<5000){
|
taskSequence=machine.getTodayCount()+1;
|
}else{
|
taskSequence=1;
|
}
|
machine.setTodayCount(taskSequence);
|
taskingAdd.setTaskSequence(taskSequence);
|
machineMapper.updateById(machine);
|
taskingMapper.insert(taskingAdd);
|
}
|
break;
|
}
|
tasking=taskingService.startMachineTask(machine);
|
}else{
|
//定制模式
|
LineConfiguration lineConfigurationAfter=lineConfigurationService.machineLineConfigurationBefore(machine);
|
Machine scanMachine=machineMapper.selectById(lineConfigurationAfter.getMachineId());
|
String PlcRequestID_=clientTCPSocket(scanMachine);
|
//String PlcRequestID_=",,46408889";
|
if (PlcRequestID_==null||PlcRequestID_.lastIndexOf(",")<0){
|
modbusTcp1.close();
|
errorMachine(machine,("扫不到码:"+PlcRequestID_));
|
return;
|
}
|
scan_id= PlcRequestID_.substring(PlcRequestID_.lastIndexOf(",")+1);
|
tasking=taskingService.startMachineTask(machine,scan_id);
|
}
|
if(tasking!=null){
|
log.info("4、发送任务 任务数据:{}, 设备状态:{}, ",machine,tasking);
|
String send=
|
HexUtil.intTo2ByteHex(1)+
|
HexUtil.intTo2ByteHex(tasking.getTaskSequence().intValue())+
|
HexUtil.intTo2ByteHex(((Double)(tasking.getLength()*10)).intValue())+
|
HexUtil.intTo2ByteHex(((Double)(tasking.getWidth()*10)).intValue())+
|
HexUtil.intTo2ByteHex(((Double)(tasking.getThickness()*10)).intValue());
|
modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()/2));
|
log.info("发送任务发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()));
|
modbusTcp1.close();
|
errorMachine(machine,"");
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
String sendRecord=df.format(new Date())+" "+
|
tasking.getGlassId()+"-"+
|
tasking.getTaskSequence()+"-"+
|
tasking.getWidth()+"-"+
|
tasking.getLength()+"-"+
|
tasking.getThickness();
|
if (sendRecords.size()>7){
|
sendRecords.remove(0);
|
}
|
Map sendContentMap=new HashMap();
|
sendContentMap.put("sendContent",sendRecord);
|
sendRecords.add(sendContentMap);
|
return;
|
}else if(!scan_id.isEmpty()){
|
taskingService.scanMachineAdd(machine,scan_id);
|
modbusTcp1.close();
|
return;
|
}
|
}else if(0==plcRequest.getValueInt()){
|
errorMachine(machine,"");
|
}
|
//}
|
|
} catch (Exception e) {
|
log.info("交互逻辑错误");
|
}
|
modbusTcp1.close();
|
//log.info("无可执行的条件");
|
}
|
|
|
public void errorMachine(Machine machine,String error) {
|
if (!error.equals(machine.getRemark())){
|
machine.setRemark(error);
|
machineMapper.updateById(machine);
|
}
|
}
|
public String clientTCPSocket(Machine machine) {
|
try {
|
//Machine machine=machineMapper.selectById(4L);
|
Socket clientSocket = new Socket(machine.getIp(), machine.getPort());
|
try {
|
clientSocket.setSoTimeout(1000);
|
// 创建输入流和输出流
|
InputStream inputStream = clientSocket.getInputStream();
|
OutputStream outputStream = clientSocket.getOutputStream();
|
// 发送启动命令
|
String message = "start";
|
outputStream.write(message.getBytes());
|
// 读取服务器响应的数据
|
byte[] buffer = new byte[1024];
|
int bytesRead = inputStream.read(buffer);
|
String receivedMessage = new String(buffer, 0, bytesRead);
|
System.out.println("收到扫码枪返回内容:" + receivedMessage);
|
// 关闭连接
|
String message2 = "stop";
|
outputStream.write(message2.getBytes());
|
clientSocket.close();
|
return receivedMessage;
|
} catch (Exception e) {
|
log.info("扫码异常");
|
//e.printStackTrace();
|
}
|
clientSocket.close();
|
} catch (Exception e) {
|
log.info("ip或端口异常");
|
}
|
return "";
|
}
|
@Override
|
public void run() {
|
while (this != null) {
|
try {
|
Thread.sleep(1000);
|
plcStart();
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|