wang
2024-05-09 93e36c526fe445c14d5fb19f3653f8a8e55e3a6c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.mes.job;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mes.common.S7object;
import com.mes.common.config.Const;
import com.mes.device.PlcParameterObject;
import com.mes.edgstoragecage.entity.EdgStorageCage;
import com.mes.edgstoragecage.entity.EdgStorageCageDetails;
import com.mes.edgstoragecage.service.EdgStorageCageDetailsService;
import com.mes.edgstoragecage.service.EdgStorageCageService;
import com.mes.glassinfo.entity.GlassInfo;
import com.mes.glassinfo.service.GlassInfoService;
import com.mes.taskcache.entity.TaskCache;
import com.mes.taskcache.service.TaskCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.List;
 
/**
 * @Author : zhoush
 * @Date: 2024/5/8 8:17
 * @Description:
 */
@Component
@Slf4j
public class CacheGlassTask {
 
    @Autowired
    TaskCacheService taskCacheService;
    @Autowired
    GlassInfoService glassInfoService;
    @Autowired
    EdgStorageCageService edgStorageCageService;
    @Autowired
    EdgStorageCageDetailsService edgStorageCageDetailsService;
 
    @Value("${mes.threshold}")
    private int threshold;
 
    @Scheduled(fixedDelay = 10000)
    public void plcHomeEdgTask() {
        PlcParameterObject plcParameterObject = S7object.getinstance().PlcMesObject;
 
        String taskRequestTypeValue = plcParameterObject.getPlcParameter("A06_request_word").getValue();
        String glassIdeValue = plcParameterObject.getPlcParameter("A05_scanning_ID").getValue();
        String confirmationWrodValue = plcParameterObject.getPlcParameter("MES_confirmation_word").getValue();
//        1为A09空闲,2为A10空闲,3为A09A10都空闲,0为A09A10都有玻璃
        String outGlassstate = plcParameterObject.getPlcParameter("A09_glass_status").getValue();
 
        String confirmationWrodAddress = plcParameterObject.getPlcParameter("MES_confirmation_word").getAddress();
 
        if ("0".equals(taskRequestTypeValue)) {
            log.info("1、获取到的请求字为0,将确认字改为0");
            S7object.getinstance().plccontrol.WriteWord(confirmationWrodAddress, (short) 0);
            return;
        }
        if (!"0".equals(confirmationWrodValue)) {
            log.info("1、获取到的请求字不为0,将确认字改为0");
            S7object.getinstance().plccontrol.WriteWord(confirmationWrodAddress, (short) 0);
            return;
        }
        if ("1".equals(taskRequestTypeValue)) {
            log.info("2、进片请求,且确认字为0,执行进片任务");
            inTo(glassIdeValue, confirmationWrodAddress);
        } else if ("2".equals(taskRequestTypeValue)) {
            //09空闲 :1      10空闲 :2        都空闲:3    其他0
            log.info("2、出片请求,且确认字为0,执行进片任务");
            outTo(Integer.parseInt(outGlassstate), confirmationWrodAddress);
        } else if ("3".equals(taskRequestTypeValue)) {
            log.info("2、进片和出片都空闲,执行出片任务");
            if ("0".equals(outGlassstate)) {
                inTo(glassIdeValue, confirmationWrodAddress);
            } else {
                outTo(Integer.parseInt(outGlassstate), confirmationWrodAddress);
            }
        }
    }
 
    private void inTo(String glassId, String confirmationWrodAddress) {
        log.info("1、按照玻璃id:{}获取玻璃小片信息", glassId);
        GlassInfo glassInfo = glassInfoService.getOne(new LambdaQueryWrapper<GlassInfo>().eq(GlassInfo::getGlassId, glassId));
        if (glassInfo == null) {
            log.info("2、此玻璃编号不存在");
            return;
        }
        log.info("2、获取到的玻璃信息为{}", glassInfo);
        //添加进片任务  查找空格
        List<EdgStorageCage> list = edgStorageCageService.selectCacheEmpty();
        log.info("3、查询卧式理片笼里面的空格:{}", list);
        if (CollectionUtil.isEmpty(list)) {
            log.info("4、不存在空格");
            return;
        }
        log.info("4、将玻璃信息插入卧式理片笼:{}", glassInfo);
        EdgStorageCageDetails details = new EdgStorageCageDetails();
        BeanUtils.copyProperties(glassInfo, details);
        details.setState(100);
        Integer slot = list.get(0).getSlot();
        Integer deviceId = list.get(0).getDeviceId();
        log.info("5、获取空闲格子为:{},设备号为{}", slot, deviceId);
        details.setSlot(slot);
        details.setDeviceId(deviceId);
        edgStorageCageDetailsService.save(details);
 
        log.info("6、生成进片任务信息存入任务表");
        TaskCache taskCache = new TaskCache();
        taskCache.setGlassId(glassId);
        taskCache.setTaskStatus(0);
        taskCache.setStartCell(0);
        taskCache.setEndCell(slot);
        taskCache.setTaskType(1);
        taskCache.setCreateTime(new Date());
        taskCacheService.insertTaskCache(taskCache);
        log.info("6、发送确认字");
        S7object.getinstance().plccontrol.WriteWord(confirmationWrodAddress, (short) 1);
 
 
    }
 
    private void outTo(int line, String confirmationWrodAddress) {
        Assert.isTrue(line != 0, "A09、A10都有玻璃,无法出片");
        log.info("1、出片任务出【{}】号线,备注(09空闲:1;10空闲:2;都空闲:3)", line);
        int endcell = 0;
 
        List<TaskCache> oldTaskCacheList;
        if (line == 2) {
            endcell = Const.A10_OUT_TARGET_POSITION;
            oldTaskCacheList = taskCacheService.selectLastOutCacheInfos(Const.A10_OUT_TARGET_POSITION);
        } else {
            endcell = Const.A09_OUT_TARGET_POSITION;
            oldTaskCacheList = taskCacheService.selectLastOutCacheInfos(Const.A09_OUT_TARGET_POSITION);
        }
        log.info("2、判断出片线路{},获取最后该条线已出片的任务信息{}(备注:0待执行,1已执行).都空闲优先出A09线", endcell, oldTaskCacheList);
 
        String glassId = "";
        if (CollectionUtil.isNotEmpty(oldTaskCacheList)) {
            glassId = oldTaskCacheList.get(0).getGlassId();
        }
        EdgStorageCageDetails glassInfo = edgStorageCageDetailsService.selectOutGlass(glassId, threshold);
        log.info("3、按照历史已出玻璃id{}和阈值{},拿出理片笼内的出片信息{}", glassId, threshold, glassInfo);
 
        if (glassInfo != null) {
            log.info("4、添加出片任务,玻璃id:{},任务类型:{},起始位置:{},结束位置:{}", glassInfo.getGlassId(),
                    2, glassInfo.getSlot(), endcell);
            TaskCache taskCache = new TaskCache();
            taskCache.setGlassId(glassInfo.getGlassId());
            taskCache.setTaskStatus(0);
            taskCache.setStartCell(glassInfo.getSlot());
            taskCache.setEndCell(endcell);
            taskCache.setTaskType(2);
            taskCache.setCreateTime(new Date());
            taskCacheService.insertTaskCache(taskCache);
            S7object.getinstance().plccontrol.WriteWord(confirmationWrodAddress, (short) 1);
        }
 
 
    }
 
 
}