springboot-vue3/src/main/java/com/example/springboot/component/PlcHoldNew.java
@@ -1,4 +1,7 @@
package com.example.springboot.component;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.springboot.service.SpianServiceNew;
import com.example.springboot.service.StorageCageService;
@@ -6,118 +9,167 @@
import com.example.springboot.entity.device.PlcParameterObject;
import com.example.springboot.mapper.AlbaniaMapper;
public class PlcHoldNew extends Thread {
  @Autowired
  private AlbaniaMapper albaniaMapper;
  private SpianServiceNew spianService;
  private StorageCageService storageCageService;
  int aaa;
  String huibao = "";
  String qidong = "";
  @Override
  public void run() {
    while (this != null) {
      try {
        Thread.sleep(500);
        Thread.sleep(300);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      storageCageService = WebSocketServer.applicationContext.getBean(StorageCageService.class);
      spianService = WebSocketServer.applicationContext.getBean(SpianServiceNew.class);
      albaniaMapper = WebSocketServer.applicationContext.getBean(AlbaniaMapper.class);
      //读取DB14区文件
      PlcParameterObject plcmes=PLCAutoMes.PlcMesObject;
      if(plcmes.getPlcParameter("PLCToMES").getValue()!=null){
      // spianService.Mestast("NG0001-1",40,1,0,"MESID1",1);
      // spianService.Mestast("NG0001-2",40,2,0,"MESID2",2);
      // spianService.Mestast("NG0001-3",40,2,1,"MESID3",3);
      String ExportTOMES1=plcmes.getPlcParameter("ExportTOMES1").getValue();//出片请求一线
      String ExportToMES2=plcmes.getPlcParameter("ExportToMES2").getValue();//出片请求二线
      String PLCToMES=plcmes.getPlcParameter("PLCToMES").getValue();//运送车请求
      String IDStatus1=plcmes.getPlcParameter("IDStatus1").getValue();//1号任务完成
      String MESToPLCStatus1=plcmes.getPlcParameter("MESToPLCStatus1").getValue();//1号确认字
      String MESToPLC=plcmes.getPlcParameter("MESToPLC").getValue();//发送任务字
      // String scan=plcmes.getPlcParameter("scan").getValue();//发送扫码信息
      // if(scan.equals("1")==true){
      //   spianService.scan(scan);
      // }
      //一号线请求
      if(ExportTOMES1!=null&&MESToPLC!=null){
      if (ExportTOMES1.equals("1") == true&&MESToPLC.equals("0")==true) {
        spianService.selectout(1);
      }
    }
      // //二线号请求时
    //   if(ExportToMES2!=null &&MESToPLC!=null){
    //   if (ExportToMES2.equals("1") == true&&MESToPLC.equals("0")==true) {
    //     spianService.selectout(2);
    //   }
    // }
      //进片请求时
      GlassInfo glass=albaniaMapper.SelectGlass();
      if(PLCToMES!=null && glass!=null&&MESToPLC!=null){
      if(PLCToMES.equals("1")==true&&MESToPLC.equals("0")==true){
  
        spianService.selectAll(glass);
      }
    }
    // System.out.println("IDStatus1:"+IDStatus1+"MESToPLCStatus1:"+MESToPLCStatus1);
      //任务完成时
      if(IDStatus1.equals("1")==true && MESToPLCStatus1.equals("0")==true){
        //完成确认字
        for(int i=1;i<7;i++){
          String Tastover=plcmes.getPlcParameter("IDStatus"+i).getValue();//PLC理片任务状态汇报
          String Glassid=plcmes.getPlcParameter("MESID"+i).getValueString();//MES玻璃ID
          GlassInfo id=albaniaMapper.SelectTaskId(Glassid);
            if(id!=null&&Tastover.equals("1")){
              //调用完成任务 Glassid
              storageCageService.FinishTask(id.getId());
            }
              S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLCStatus"+i).getAddress(), (short) 1);//完成确认字
              System.out.println("完成确认字MESToPLCStatus");
            }
              S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLC").getAddress(), (short) 0);//清除任务发送字
              System.out.println("清除任务发送字MESToPLC");
          }
      // spianService.selectout(1);
      // spianService.mateOut();
      // 读取DB14区文件
      PlcParameterObject plcmes = PLCAutoMes.PlcMesObject;
      if (plcmes.getPlcParameter("PLCToMES").getValue() != null) {
        String ExportTOMES1 = plcmes.getPlcParameter("ExportTOMES1").getValue();// 出片请求一线
        String PLCToMES = plcmes.getPlcParameter("PLCToMES").getValue();// 运送车请求
        String IDStatus1 = plcmes.getPlcParameter("IDStatus1").getValue();// 1号任务完成
        String MESToPLCStatus1 = plcmes.getPlcParameter("MESToPLCStatus1").getValue();// 1号确认字
        String MESToPLC = plcmes.getPlcParameter("MESToPLC").getValue();// 发送任务字
        String GaToMES = plcmes.getPlcParameter("GaToMES").getValue();// 测量成功信号
        
      //汇报取消时
      if(IDStatus1.equals("0")==true &&MESToPLCStatus1.equals("1")){
        //取消确认字
        for(int i=1;i<7;i++){
          String Tastover=plcmes.getPlcParameter("IDStatus"+i).getValue();//依次获取任务汇报
          if(Tastover.equals("0")){
            S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLCStatus"+i).getAddress(), (short) 0);//清除确认字
            System.out.println("清除确认字MESToPLCStatus");
        int taskcont = albaniaMapper.SelectTaskcount(); // 正在进行的任务数量
        int outtask = albaniaMapper.SelectOutTaskCount(); // 等待补发的出片任务数量
        if (ExportTOMES1 != null && MESToPLC != null && taskcont == 0) {
          //当可以发任务并且出片队列有任务时
          if (ExportTOMES1.equals("1") == true && MESToPLC.equals("0") == true && PLCToMES.equals("1") == true&&outtask>0 && MESToPLCStatus1.equals("0")) {
             spianService.mateOut();
          }
        }
        // String scan=plcmes.getPlcParameter("scan").getValue();//发送扫码信息
        // if(scan.equals("1")==true){
        // spianService.scan(scan);
        // }
        // 获取当前时间
        String currentTime = new Date().toString();
        if (!huibao.equals(IDStatus1)) {
          System.out.println("汇报记录:" + IDStatus1 + "时间" + currentTime);
        }
        huibao = IDStatus1;
        if (!qidong.equals(MESToPLC)) {
          System.out.println("启动记录:" + MESToPLC + "时间" + currentTime);
        }
        qidong = MESToPLC;
        PLCToMES = plcmes.getPlcParameter("PLCToMES").getValue();// 运送车请求
        IDStatus1 = plcmes.getPlcParameter("IDStatus1").getValue();// 1号任务完成
        MESToPLCStatus1 = plcmes.getPlcParameter("MESToPLCStatus1").getValue();// 1号确认字
        MESToPLC = plcmes.getPlcParameter("MESToPLC").getValue();// 发送任务字
        // 一号线请求
        if (ExportTOMES1 != null && MESToPLC != null && taskcont == 0) {
          if (ExportTOMES1.equals("1") == true && MESToPLC.equals("0") == true && PLCToMES.equals("1") == true
              && MESToPLCStatus1.equals("0") == true&&outtask == 0) {
            // System.out
            //     .println("出片状态:MESToPLC:" + MESToPLC + "PLCToMES:" + PLCToMES + "MESToPLCStatus1:" + MESToPLCStatus1);
            spianService.selectout(1);
            try {
              Thread.sleep(100); // 每100毫秒检查一次
            } catch (InterruptedException e) {
              Thread.currentThread().interrupt(); // 处理线程中断
            }
          }
        }
        PLCToMES = plcmes.getPlcParameter("PLCToMES").getValue();// 运送车请求
        IDStatus1 = plcmes.getPlcParameter("IDStatus1").getValue();// 1号任务完成
        MESToPLCStatus1 = plcmes.getPlcParameter("MESToPLCStatus1").getValue();// 1号确认字
        MESToPLC = plcmes.getPlcParameter("MESToPLC").getValue();// 发送任务字
        // 运送车请求和测量完成请求
        GlassInfo glass = albaniaMapper.SelectGlass();
        if (PLCToMES != null && glass != null && MESToPLC != null) {
          if (PLCToMES.equals("1")&& MESToPLC.equals("0")&&("1".equals(GaToMES)||"2".equals(GaToMES))&& taskcont == 0) {
            try {
              Thread.sleep(100); // 每100毫秒检查一次
            } catch (InterruptedException e) {
              Thread.currentThread().interrupt(); // 处理线程中断
            }
            spianService.selectAll(glass);
          }
        }
        // 任务完成时
        if ((IDStatus1.equals("1") && MESToPLCStatus1.equals("0")) ||
            (IDStatus1.equals("4") && MESToPLCStatus1.equals("0"))) {
          // 完成确认字
          for (int i = 1; i < 7; i++) {
            String Tastover = plcmes.getPlcParameter("IDStatus" + i).getValue();// PLC理片任务状态汇报
            String Glassid = plcmes.getPlcParameter("MESID" + i).getValueString();// MES玻璃ID
            GlassInfo id = albaniaMapper.SelectTaskId(Glassid);
            if (Tastover.equals("1")) {
              // 调用完成任务 Glassid
              storageCageService.FinishTask();
              System.out.println("完成任务ID:" + id);
            } else if (Tastover.equals("4")) {
              storageCageService.FinishTask();
              System.out.println("完成失败任务ID:" + id);
            }
            S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLCStatus" + i).getAddress(), (short) 1);// 完成确认字
          }
          System.out.println("完成确认字MESToPLCStatus");
          // 清除任务地址
          for (int i = 1; i < 7; i++) {
            S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLCStart" + i).getAddress(), (short) 0);// 清除任务地址
            S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLCTarget" + i).getAddress(), (short) 0);// 清除任务地址
          }
          System.out.println("清除任务地址");
          while (true) {
            String currentValue = plcmes.getPlcParameter("MESToPLC").getValue();
            // 检查 MESToPLC 的值是否为 0
            if (currentValue.equals("0")) {
              System.out.println("MESToPLC 已变为 0,停止循环写入");
              break; // 退出循环
            }
            // 如果不为 0,继续写入
            S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLC").getAddress(),
                (short) 0);// 清除任务发送字
            System.out.println("继续写入 MESToPLC,当前值:" + currentValue);
            // 加上延时,避免占用过多 CPU 资源
            try {
              Thread.sleep(100); // 例如每秒检查一次
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        }
        // 汇报取消时
        if ((IDStatus1.equals("0")) && MESToPLCStatus1.equals("1")) {
          // 取消确认字
          for (int i = 1; i < 7; i++) {
            String Tastover = plcmes.getPlcParameter("IDStatus" + i).getValue();// 依次获取任务汇报
            if (Tastover.equals("0")) {
              S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLCStatus" + i).getAddress(), (short) 0);// 清除确认字
            }
          }
          System.out.println("清除确认字MESToPLCStatus");
        }
      }
  }
      // 查询数据库
      // 推送到前端
    }
  }