1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
 * @Date: 2024/10/10 8:05
 * @Description:
 */
@Component
@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 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", "PLC.WL2.plc_task_tate"), 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();
    }
}