hangzhoumesParent/moduleService/CacheGlassModule/src/main/java/com/mes/job/OpcCacheGlassTask.java
@@ -1,14 +1,27 @@
package com.mes.job;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kangaroohy.milo.model.ReadWriteEntity;
import com.kangaroohy.milo.runner.subscription.SubscriptionCallback;
import com.kangaroohy.milo.service.MiloService;
import com.mes.common.config.Const;
import com.mes.edgglasstaskqueueinfo.entity.EdgGlassTaskQueueInfo;
import com.mes.edgglasstaskqueueinfo.service.EdgGlassTaskQueueInfoService;
import com.mes.glassinfo.entity.GlassInfo;
import com.mes.glassinfo.service.GlassInfoService;
import com.mes.opctask.entity.EdgStorageDeviceTask;
import com.mes.opctask.service.EdgStorageDeviceTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * @Author : zhoush
@@ -19,16 +32,98 @@
@Slf4j
public class OpcCacheGlassTask {
    private static final String EDG_STORAGE_DEVICE_ONE_TASK = "edg_storage_device_one_task";
    private static final String EDG_STORAGE_DEVICE_TWO_TASK = "edg_storage_device_two_task";
    @Autowired(required = false)
    MiloService miloService;
    @Resource(name = "cacheGlassStartCallback")
    SubscriptionCallback cacheGlassStartCallback;
    @Resource(name = "cacheGlassTestCallback")
    SubscriptionCallback cacheGlassTestCallback;
    @Resource
    EdgStorageDeviceTaskService edgStorageDeviceTaskService;
    @Resource
    EdgGlassTaskQueueInfoService edgGlassTaskQueueInfoService;
    @Resource
    GlassInfoService glassInfoService;
    private String glassIdOne = "";
    private String glassIdTwo = "";
    @Scheduled(fixedDelay = Long.MAX_VALUE)
    public void startOpcTask() throws Exception {
//        miloService.subscriptionFromOpcUa(Arrays.asList("C101-WL.S7-1200.plc_task_state", "D101-WL.S7-1200.plc_task_state"), cacheGlassStartCallback);
        miloService.subscriptionFromOpcUa(Arrays.asList("mes.dec.edg_storage_device_one_task[1].task_state", "D101-WL.S7-1200.plc_task_state"), cacheGlassStartCallback);
        log.info("--------------------s---------------------------");
    public void startOneOpcTask() throws Exception {
        miloService.subscriptionFromOpcUa(Arrays.asList("mes.WL1.edg_storage_device_one_task[1].task_state"), cacheGlassStartCallback);
    }
    @Scheduled(fixedDelay = Long.MAX_VALUE)
    public void startTwoOpcTask() throws Exception {
        miloService.subscriptionFromOpcUa(Arrays.asList("mes.WL2.edg_storage_device_two_task[1].task_state"), cacheGlassStartCallback);
    }
    @Scheduled(fixedDelay = 1000)
    public void edgOneOpcTask() throws Exception {
        EdgStorageDeviceTask task = edgStorageDeviceTaskService.queryTaskMessage(EDG_STORAGE_DEVICE_ONE_TASK);
        String glassId = task.getGlassId();
        if (StringUtils.isBlank(glassId) || glassId.equals(glassIdOne)) {
            log.info("{}号线磨边前玻璃未就位,结束本次任务", 1);
            return;
        }
        edgTaskChild(glassId, 1);
    }
    @Scheduled(fixedDelay = 1000)
    public void edgTwoOpcTask() throws Exception {
        EdgStorageDeviceTask task = edgStorageDeviceTaskService.queryTaskMessage(EDG_STORAGE_DEVICE_TWO_TASK);
        String glassId = task.getGlassId();
        if (StringUtils.isBlank(glassId) || glassId.equals(glassIdTwo)) {
            log.info("{}号线磨边前玻璃未就位,结束本次任务", 2);
            return;
        }
        edgTaskChild(glassId, 2);
    }
    private void edgTaskChild(String glassId, int cell) throws Exception {
        GlassInfo glassInfo = glassInfoService.getOne(new LambdaQueryWrapper<GlassInfo>().eq(GlassInfo::getGlassId, glassId).last("limit 1"));
        if (glassInfo == null) {
            log.info("对列表中的玻璃id错误,请检查数据,玻璃id:{}", glassId);
            return;
        }
        String toEndingId = glassInfo.getTemperingLayoutId() + "" + glassInfo.getTemperingFeedSequence();
        List<ReadWriteEntity> list = new ArrayList<>();
        list.add(generateReadWriteEntity("PLC.MB" + cell + ".mesControl", true));
        list.add(generateReadWriteEntity("PLC.MB" + cell + ".glassId", Integer.parseInt(toEndingId)));
        list.add(generateReadWriteEntity("PLC.MB" + cell + ".toEdingId", Integer.parseInt(toEndingId)));
        list.add(generateReadWriteEntity("PLC.MB" + cell + ".width", (int) Math.max(glassInfo.getWidth() * 10, glassInfo.getHeight() * 10)));
        list.add(generateReadWriteEntity("PLC.MB" + cell + ".height", (int) Math.min(glassInfo.getWidth() * 10, glassInfo.getHeight() * 10)));
        miloService.writeToOpcUa(list);
        miloService.writeToOpcWord(generateReadWriteEntity("PLC.MB" + cell + ".thickness", (int) glassInfo.getThickness() * 10));
        //修改磨边对列中的磨边线路及状态
        edgGlassTaskQueueInfoService.update(new LambdaUpdateWrapper<EdgGlassTaskQueueInfo>()
                .set(EdgGlassTaskQueueInfo::getLine, cell)
                .set(EdgGlassTaskQueueInfo::getState, 1)
                .eq(EdgGlassTaskQueueInfo::getGlassId, glassId)
                .eq(EdgGlassTaskQueueInfo::getState, Const.GLASS_STATE_NEW));
        if (cell == 1) {
            glassIdOne = glassId;
        } else {
            glassIdTwo = glassId;
        }
    }
    private ReadWriteEntity generateReadWriteEntity(String identifier, Object value) {
        return ReadWriteEntity.builder()
                .identifier(identifier)
                //Kep中是Long类型,即:Int32,Java中的int类型
                .value(value)
                .build();
    }
}