package com.mes.job;
|
|
import cn.hutool.json.JSONObject;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.mes.md.entity.*;
|
import com.mes.md.mapper.MachineMapper;
|
import com.mes.md.mapper.PrimitiveTaskMapper;
|
import com.mes.md.mapper.TaskingMapper;
|
import com.mes.md.service.*;
|
import com.mes.service.ModbusTcp;
|
import com.mes.service.PlcAgreement;
|
import com.mes.service.PlcParameter;
|
import com.mes.tools.WebSocketServer;
|
import com.mes.utils.HexUtil;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.net.Socket;
|
import java.nio.charset.StandardCharsets;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
/**
|
* @author SNG-010
|
*/
|
@Component
|
@Slf4j
|
public class PLCLoad {
|
|
@Autowired
|
TaskingMapper taskingMapper;
|
@Autowired
|
MachineMapper machineMapper;
|
@Autowired
|
TaskingService taskingService;
|
@Autowired
|
ProjectService projectService;
|
@Autowired
|
PrimitiveTaskService primitiveTaskService;
|
@Autowired
|
KBBTJPDrawingBPService kBBTJPDrawingBPService;
|
@Autowired
|
LineConfigurationService lineConfigurationService;
|
|
public static Long machineId1 = 1L;
|
public static Long machineId2 = 2L;
|
public ModbusTcp modbusTcp2 = new ModbusTcp();
|
public List<Map> sendRecords = new ArrayList<>();
|
@Autowired
|
private PrimitiveTaskMapper primitiveTaskMapper;
|
|
|
//@Scheduled(fixedDelay = 1000)
|
public void plcLoad() {
|
Machine machine=machineMapper.selectById(machineId2);
|
ModbusTcp modbusTcp1 =new ModbusTcp(machine.getIp(),machine.getPort(),"PlcLoad");
|
modbusTcp1.connect();
|
PlcAgreement plcAgreement=modbusTcp1.getPlcAgreement("PlcLoad");
|
try {
|
if(!modbusTcp1.read(plcAgreement)){
|
log.info("通讯读取新数据失败");
|
modbusTcp1.close();
|
return;
|
}
|
PlcParameter plcRequest =plcAgreement.getPlcParameter("plcRequest");//请求字
|
PlcParameter mesSend =plcAgreement.getPlcParameter("mesSend");//发送字
|
PlcParameter mesSendCount =plcAgreement.getPlcParameter("mesSendCount");//发送数量
|
|
PlcParameter plcReport =plcAgreement.getPlcParameter("plcReport");//汇报字
|
PlcParameter plcReportID =plcAgreement.getPlcParameter("plcReportID");//汇报ID
|
PlcParameter mesConfirm =plcAgreement.getPlcParameter("mesConfirm");//确认字
|
PlcParameter mesConfirmID =plcAgreement.getPlcParameter("mesConfirmID");//确认ID
|
if (machine.getIsLog()>0){
|
modbusTcp1.consoleLogInfo(plcAgreement);
|
}
|
if (0==plcRequest.getValueInt()&&1==mesSend.getValueInt()) {
|
//发送字置0
|
String send= HexUtil.intTo2ByteHex(0);
|
modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()/2));
|
log.info("1.发送字置零 发送字 清空成功;内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}
|
if (0==plcReport.getValueInt()&&0!=mesConfirm.getValueInt()) {
|
//确认字置零
|
String send=HexUtil.intTo2ByteHex(0);
|
modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()/2));
|
log.info("2.确认字置零 任务完成 清空成功;内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}
|
LineConfiguration lineConfiguration=lineConfigurationService.machineLineConfiguration(machine);
|
List<PrimitiveTask> findPrimitiveTasking=primitiveTaskService.findPrimitiveTasking(machine,lineConfiguration.getLineId());
|
|
if(1==plcReport.getValueInt()&&0==mesConfirm.getValueInt()){
|
for(PrimitiveTask primitiveTask:findPrimitiveTasking){
|
log.info("机械手任务完成 原始数量:{} 本次数量:{}", primitiveTask.getReportCount(), plcReportID.getValueInt());
|
if (plcReportID.getValueInt()>0){
|
primitiveTask.setReportCount(primitiveTask.getReportCount()+ plcReportID.getValueInt());
|
primitiveTaskMapper.updateById(primitiveTask);
|
}
|
String send = HexUtil.intTo2ByteHex(1);
|
modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()/ 2));
|
log.info("任务完成 成功 内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}
|
log.info("3、任务完成:"+plcReportID.getValueString());
|
String send = HexUtil.intTo2ByteHex(1);
|
modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart() / 2));
|
log.info("任务完成 异常成功 内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
|
modbusTcp1.close();
|
return;
|
}
|
if((1==plcRequest.getValueInt()&&0==mesSend.getValueInt())
|
&&"开工".equals(machine.getState())){
|
int loadCount=0;
|
if(machine.getMode()==1){
|
//标准模式 找已领取未完成的第一条任务
|
for(PrimitiveTask primitiveTask:findPrimitiveTasking){
|
// //任务数>已上数量
|
// List<Tasking> taskingTopList=taskingMapper.selectList(new QueryWrapper<Tasking>().lambda()
|
// .eq(Tasking::getScanId,primitiveTask.getScanId())
|
// );
|
loadCount=primitiveTask.getTaskQuantity()-primitiveTask.getReportCount();
|
loadCount=(loadCount>0?loadCount:0);
|
|
break;
|
}
|
//loadCount=taskingCount.size();
|
}else{
|
//定制模式
|
loadCount=1;
|
}
|
if(loadCount>0){
|
log.info("4、发送任务 任务数据:{}, 设备状态:{}, ",loadCount,machine);
|
String send=HexUtil.intTo2ByteHex(1)+ HexUtil.intTo2ByteHex(loadCount);
|
modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()/2));
|
log.info("发送任务发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()/2));
|
modbusTcp1.close();
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
String sendRecord=df.format(new Date())+" "+"1 "+ loadCount;
|
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("无可执行的条件");
|
}
|
@Scheduled(fixedDelay = 1000)
|
public void loadTasks() {
|
JSONObject jsonObject = new JSONObject();
|
Machine machine=machineMapper.selectById(machineId2);
|
|
|
//标准模式 找已领取未完成的第一条任务
|
LineConfiguration lineConfiguration=lineConfigurationService.machineLineConfiguration(machine);
|
//此线路已领取的任务
|
List<PrimitiveTask> findPrimitiveTasking=primitiveTaskService.findPrimitiveTasking(machine,lineConfiguration.getLineId());
|
//此线路未领取的任务 本地
|
//List<PrimitiveTask> findPrimitiveTask=primitiveTaskService.findPrimitiveTaskNotFinish();
|
//此线路未领取的任务 客户数据库
|
List<KBBTJPDrawingBP> list =kBBTJPDrawingBPService.notReceiveKBBTJPDrawingBP();
|
jsonObject.append("findPrimitiveTask", list);
|
jsonObject.append("findPrimitiveTasking", findPrimitiveTasking);
|
//jsonObject.append("findPrimitiveTask", findPrimitiveTask);
|
jsonObject.append("machine", machine);
|
jsonObject.append("sendRecords", sendRecords);
|
ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("loadTask");
|
if (sendwServer != null) {
|
for (WebSocketServer webserver : sendwServer) {
|
if (webserver != null) {
|
webserver.sendMessage(jsonObject.toString());
|
List<String> messages = webserver.getMessages();
|
if (!messages.isEmpty()) {
|
// // 将最后一个消息转换为整数类型的列表
|
webserver.clearMessages();
|
}
|
} else {
|
log.info("Home is closed");
|
}
|
}
|
}
|
}
|
|
}
|