ZengTao
2025-06-20 f07fd673fb0977a370178f27655bf39cc574de6e
hangzhoumesParent/moduleService/hollowGlassModule/src/main/java/com/mes/job/OpcHollowRemoveTask.java
@@ -1,5 +1,9 @@
package com.mes.job;
import com.github.xingshuangs.iot.common.buff.ByteReadBuff;
import com.github.xingshuangs.iot.common.buff.EByteBuffFormat;
import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
import com.github.xingshuangs.iot.utils.ByteUtil;
import com.kangaroohy.milo.model.ReadWriteEntity;
import com.kangaroohy.milo.service.MiloService;
import com.mes.hollow.entity.vo.HollowGlassFormulaVO;
@@ -7,10 +11,12 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
@@ -28,6 +34,43 @@
    @Autowired(required = false)
    MiloService miloService;
    @Autowired
    @Qualifier("CMJ1ModbusTcp")
    ModbusTcp CMJ1ModbusTcp;
    @Autowired
    @Qualifier("CMJ2ModbusTcp")
    ModbusTcp CMJ2ModbusTcp;
    private Integer offset = 40001;
    /**
     * 除膜机联机状态
     *
     * @throws Exception
     */
    @Scheduled(fixedDelay = 3000)
    public void filmRemoveStateOne() throws Exception {
        ModbusTcp modbusTcp = CMJ1ModbusTcp;
        try {
            modbusTcp.readUInt16(42001 - offset);
            log.info("除膜机1联机状态正常");
        } catch (Exception e) {
            log.error("除膜机1联机状态异常", e);
        }
    }
    @Scheduled(fixedDelay = 3000)
    public void filmRemoveStateTwo() throws Exception {
        ModbusTcp modbusTcp = CMJ2ModbusTcp;
        try {
            modbusTcp.readUInt16(42001 - offset);
            log.info("除膜机2联机状态正常");
        } catch (Exception e) {
            log.error("除膜机2联机状态异常", e);
        }
    }
    /**
     * 收到除膜机请求及玻璃id
     *
@@ -43,58 +86,123 @@
        hollowRemoveChildTask("CMJ2.CMJ2.");
    }
    public String readString(ModbusTcp modbusTcp, int address) {
        //
        byte[] bytes = new byte[20];
        for (int i = 0; i < 10; i++) {
            int res = modbusTcp.readUInt16(address + i);
            bytes[i * 2 + 1] = (byte) ((res >> 8) & 0xFF);
            bytes[i * 2] = (byte) (res & 0xFF);
        }
        int endIndex = 0;
        while (endIndex < bytes.length) {
            if (bytes[endIndex] == 0) {
                break;
            }
            endIndex++;
        }
        // 截取有效部分
        return new String(bytes, 0, endIndex);
    }
    private void hollowRemoveChildTask(String cell) throws Exception {
        ReadWriteEntity requestEntity = miloService.readFromOpcUa(cell + "mesControl");
        if ("0".equals(requestEntity.getValue() + "")) {
        ModbusTcp modbusTcp = null;
        if ("CMJ1.CMJ1.".equals(cell)) {
            modbusTcp = CMJ1ModbusTcp;
        } else {
            modbusTcp = CMJ2ModbusTcp;
        }
        if (!modbusTcp.checkConnected()) {
            log.info("除膜机{}未连接", cell);
            return;
        }
        String glassIdEntitys = readString(modbusTcp, 42003 - offset);
        log.info("除膜{}获取当前的玻璃id:{}", cell, glassIdEntitys);
//        byte[] res = CMJ1ModbusTcp.readHoldRegister(1, 42003, 14);
//        String glassId1 = ByteReadBuff.newInstance(res, 0, false, EByteBuffFormat.CD_AB).getString(10);
//        String glassId2 = ByteReadBuff.newInstance(res, 0, false, EByteBuffFormat.AB_CD).getString(10);
//        String glassId3 = ByteReadBuff.newInstance(res, 0, false, EByteBuffFormat.BA_DC).getString(10);
//        String glassId4 = ByteReadBuff.newInstance(res, 0, false, EByteBuffFormat.DC_BA).getString(10);
//        log.info("获取{}当前的玻璃id1:{}", cell, glassId1);
//        log.info("获取{}当前的玻璃id2:{}", cell, glassId2);
//        log.info("获取{}当前的玻璃id3:{}", cell, glassId3);
//        log.info("获取{}当前的玻璃id4:{}", cell, glassId4);
        Integer requestEntity = modbusTcp.readUInt16(42001 - offset);
//        ReadWriteEntity requestEntity = miloService.readFromOpcUa(cell + "mesControl");
//        if ("0".equals(requestEntity.getValue() + "")) {
        if (0 == requestEntity) {
            log.info("当前除膜机为单机状态");
            return;
        }
        int request = Integer.parseInt(requestEntity.getValue() + "");
//        int request = Integer.parseInt(requestEntity.getValue() + "");
        int request = requestEntity;
        int flagRequest = request & 3;
        ReadWriteEntity glassIdEntity = miloService.readFromOpcUa(cell + "glassId");
        log.info("除膜{}获取当前的信号为:{},玻璃id:{}", cell, flagRequest, glassIdEntity);
        if (flagRequest != 3) {
            log.info("当前未收到请求玻璃id数据,结束任务");
            return;
        }
        log.info("除膜{}获取当前的玻璃id:{}", cell, glassIdEntity);
        if (null == glassIdEntity.getValue() || StringUtils.isBlank(glassIdEntity.getValue() + "")) {
//        String glassIdEntity = modbusTcp.readString(42003 - offset, 20);
        String glassIdEntity = readString(modbusTcp, 42003 - offset);
//        String glassIdEntity = ByteReadBuff.newInstance(res, 0, false, EByteBuffFormat.CD_AB).getString(10);
        //你删了?
        log.info("除膜{}获取当前的信号为:{},玻璃id:{}", cell, flagRequest, glassIdEntity);
        if (null == glassIdEntity || StringUtils.isBlank(glassIdEntity)) {
//        ReadWriteEntity glassIdEntity = miloService.readFromOpcUa(cell + "glassId");
//        if (null == glassIdEntity.getValue() || StringUtils.isBlank(glassIdEntity.getValue() + "")) {
            log.info("当前未收到玻璃id数据,结束任务");
            return;
        }
        log.info("当前需要除膜的玻璃id为:{}", glassIdEntity.getValue() + "");
//        log.info("除膜{}获取当前的玻璃id:{}", cell, glassIdEntity);
//        if (null == glassIdEntity.getValue() || StringUtils.isBlank(glassIdEntity.getValue() + "")) {
//            log.info("当前未收到玻璃id数据,结束任务");
//            return;
//        }
        log.info("当前需要除膜的玻璃id为:{}", glassIdEntity);
        //按照玻璃id获取对应的任务id
        HollowGlassFormulaVO detailsVO = hollowFormulaDetailsService.queryFormulaDetailsByGlassId(glassIdEntity.getValue() + "", null, null);
        HollowGlassFormulaVO detailsVO = hollowFormulaDetailsService.queryFormulaDetailsByGlassId(glassIdEntity, null, null);
        if (null == detailsVO) {
            log.info("玻璃id不存在,请检查数据");
            return;
        }
        //按照任务id获取对应的配方信息
        int Id = 0;
        List<ReadWriteEntity> ualist = new ArrayList<>();
        List<ReadWriteEntity> wordlist = new ArrayList<>();
        log.info("除膜{}获取玻璃{},是否除膜:{}", cell, glassIdEntity.getValue(), detailsVO.getFilmRemove());
        log.info("除膜{}获取玻璃{},是否除膜:{}", cell, requestEntity, detailsVO.getFilmRemove());
        if (detailsVO.getFilmRemove() == 0) {
            // 10111011
            int sendId = request & 63;
            sendId = sendId + 4;
            wordlist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "mesControl", sendId));
            Id = sendId + 4;
//            wordlist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "mesControl", sendId));
            modbusTcp.writeUInt16(42001 - offset, sendId);
        } else {
            // 先将对应的位置为0,获得请求的玻璃数据, 然后将对应位值     110111011
            int sendId = request & 63;
            sendId = sendId + 4 + 64;
            wordlist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "mesControl", sendId));
            ualist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "glassTop", detailsVO.getTopRemove()));
            ualist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "glassBottom", detailsVO.getBottomRemove()));
            ualist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "glassLeft", detailsVO.getLeftRemove()));
            ualist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "glassRight", detailsVO.getRightRemove()));
            Id = sendId + 4 + 64;
            modbusTcp.writeUInt32(42019 - offset, detailsVO.getTopRemove());
            modbusTcp.writeUInt32(42021 - offset, detailsVO.getTopRemove());
            modbusTcp.writeUInt32(42023 - offset, detailsVO.getTopRemove());
            modbusTcp.writeUInt32(42025 - offset, detailsVO.getTopRemove());
//            wordlist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "mesControl", sendId));
//            ualist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "glassTop", detailsVO.getTopRemove()));
//            ualist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "glassBottom", detailsVO.getBottomRemove()));
//            ualist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "glassLeft", detailsVO.getLeftRemove()));
//            ualist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "glassRight", detailsVO.getRightRemove()));
        }
        ualist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "thickness", (int) (detailsVO.getThickness() * 10)));
        ualist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "firstLength", (int) (Math.max(detailsVO.getWidth(), detailsVO.getHeight())) * 10));
        ualist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "secondLength", (int) (Math.min(detailsVO.getWidth(), detailsVO.getHeight())) * 10));
        miloService.writeToOpcWord(wordlist);
        miloService.writeToOpcUa(ualist);
        modbusTcp.writeUInt32(42013 - offset, (int) (detailsVO.getThickness() * 10));
        modbusTcp.writeUInt32(42015 - offset, (int) (Math.max(detailsVO.getWidth(), detailsVO.getHeight())) * 10);
        modbusTcp.writeUInt32(42017 - offset, (int) (Math.min(detailsVO.getWidth(), detailsVO.getHeight())) * 10);
        modbusTcp.writeUInt16(42001 - offset, Id);
//        ualist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "thickness", (int) (detailsVO.getThickness() * 10)));
//        ualist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "firstLength", (int) (Math.max(detailsVO.getWidth(), detailsVO.getHeight())) * 10));
//        ualist.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "secondLength", (int) (Math.min(detailsVO.getWidth(), detailsVO.getHeight())) * 10));
//        miloService.writeToOpcWord(wordlist);
//        miloService.writeToOpcUa(ualist);
        return;
    }
@@ -114,8 +222,19 @@
    }
    public void finishHollowRemoveChildTask(String cell) throws Exception {
        ReadWriteEntity requestEntity = miloService.readFromOpcUa(cell + "mesControl");
        int request = Integer.parseInt(requestEntity.getValue() + "");
//        ReadWriteEntity requestEntity = miloService.readFromOpcUa(cell + "mesControl");
        ModbusTcp modbusTcp = null;
        if ("CMJ1.CMJ1.".equals(cell)) {
            modbusTcp = CMJ1ModbusTcp;
        } else {
            modbusTcp = CMJ2ModbusTcp;
        }
        if (!modbusTcp.checkConnected()) {
            log.info("除膜机{}未连接", cell);
            return;
        }
        int requestEntity = modbusTcp.readUInt16(42001 - offset);
        int request = requestEntity;
        int flagRequest = request & 9;
        if (flagRequest != 9) {
            log.info("当前未收到除膜机ID接收完成信号,结束任务");
@@ -124,9 +243,10 @@
        //向电气发送清楚信号
        int sendId = request & 251;
        log.info("{}收到除膜机ID接收完成信号,将写入数据完成置0,发送数据为:{}", cell, sendId);
        List<ReadWriteEntity> list = new ArrayList<>();
        list.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "mesControl", sendId));
        miloService.writeToOpcWord(list);
//        List<ReadWriteEntity> list = new ArrayList<>();
//        list.add(opcPlcStorageCageHollowTask.generateReadWriteEntity(cell + "mesControl", sendId));
//        miloService.writeToOpcWord(list);
        modbusTcp.writeUInt16(42001 - offset, sendId);
    }
}