1
zhang
2025-08-13 1498da813428be4e35423b6cf26fc45a203e73ab
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package com.zy.asrs.service.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.zy.asrs.entity.LocMast;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.mapper.WrkMastMapper;
import com.zy.asrs.service.LocMastService;
import com.zy.asrs.service.WrkMastService;
import com.zy.common.utils.News;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.DevpSlave;
import com.zy.core.model.Task;
import com.zy.core.model.protocol.StaProtocol;
import com.zy.core.properties.SlaveProperties;
import com.zy.core.thread.SiemensDevpThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 立体仓库WCS系统主流程业务
 * Created by vincent on 2020/8/6
 */
@Slf4j
@Service("ctuMainService")
@Transactional
public class CtuMainServiceImpl {
 
    public static final long COMMAND_TIMEOUT = 5 * 1000;
 
 
    @Autowired
    private SlaveProperties slaveProperties;
 
    @Autowired
    private WrkMastMapper wrkMastMapper;
 
    @Autowired
    private LocMastService locMastService;
 
    @Autowired
    private WrkMastService wrkMastService;
 
 
    public Integer wrkNo = 10000;
 
    /**
     * 入库,从拣料站到入库站(CTU取货站)
     */
    public synchronized void generateStoreWrkFile(Integer mark) {
        // 根据输送线plc遍历
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历入库口
            for (DevpSlave.Sta inSta : devp.getInSta()) {
                // 获取入库站信息
                SiemensDevpThread devpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo());
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                // 判断是否满足条件
//                if (!staProtocol.isLoading()) {
//                    continue;
//                }
                // && staProtocol.isInEnable()
                if (staProtocol.isAutoing() && !staProtocol.isEmptyMk() && staProtocol.isPakMk()) {
                    WrkMast wrkMast = wrkMastMapper.selectByWrkNo(staProtocol.getWorkNo());
                    if (wrkMast != null) {
                        //下发移动任务,并生成入库工作档
                        WrkMast in = new WrkMast();
                        wrkMast.setWrkSts(223L);
                        wrkMast.setModiTime(new Date());
                        wrkMastService.updateById(wrkMast);
                        in.setSourceLocNo("1007");
                        in.setLocNo(wrkMast.getSourceLocNo());
                        in.setIoType(10);
                        in.setIoTime(new Date());
                        in.setWrkSts(1L); // 工作状态:11.生成出库ID
                        in.setIoPri(13D); // 优先级:13
                        in.setFullPlt("Y"); // 满板:Y
                        in.setPicking("N"); // 拣料
                        in.setExitMk("N"); // 退出
                        in.setEmptyMk("Y"); // 空板
                        in.setLinkMis("N");
                        in.setAppeTime(new Date());
                        in.setModiTime(new Date());
                        in.setBarcode(wrkMast.getBarcode());
                        in.setPlcWrkNo(wrkMast.getPlcWrkNo());
                        wrkMastService.insert(in);
                        LocMast locMast = locMastService.selectById(wrkMast.getSourceLocNo());
                        locMast.setLocSts("S");
                        locMast.setModiTime(new Date());
                        locMastService.updateById(locMast);
                    }
                }
            }
        }
    }
 
 
    /**
     * 出库
     */
    public synchronized void out(Integer mark) {
        // 根据输送线plc遍历
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历入库口
            for (DevpSlave.Sta inSta : devp.getOutSta()) {
                // 获取入库站信息
                SiemensDevpThread devpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo());
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                // 判断是否满足条件
                if (!staProtocol.isLoading()) {
                    continue;
                }
                //&& staProtocol.isOutEnable()
                if (staProtocol.isAutoing() && !staProtocol.isEmptyMk() && (staProtocol.getWorkNo() == 0 || staProtocol.getWorkNo() == 9999) && staProtocol.isPakMk()) {
                    News.warnNoLog("" + mark + " - 0" + " - 开始执行");
                    // 判断重复工作档
                    // 106也算上,以后106-》107用于更新库存
                    WrkMast wrkMast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("loc_no", "1001").in("wrk_sts", 106, 107));
                    if (wrkMast == null) {
                        continue;
                    }
 
                    // 命令下发区 --------------------------------------------------------------------------
 
                    // 更新站点信息 且 下发plc命令
                    staProtocol.setWorkNo(wrkMast.getPlcWrkNo());
                    staProtocol.setStaNo((short) 1004);
                    devpThread.setPakMk(staProtocol.getSiteId(), false);
                    boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                    log.info("输送线下发3:{},{}", wrkMast.getWrkNo(), 1004);
                    if (result) {
                        // 更新工作主档
                        wrkMast.setWrkSts(108L);
                        wrkMast.setModiTime(new Date());
                        if (wrkMastMapper.updateById(wrkMast) == 0) {
                            News.error("" + mark + " - 1" + " - 更新工作档失败!!! [工作号:{}]", wrkMast.getWrkNo());
                        }
                    } else {
                        News.error("" + mark + " - 2" + " - 发布命令至输送线队列失败!!! [plc编号:{}]", devp.getId());
                    }
 
                } else {
                    News.errorNoLog("" + mark + " - 6" + " - 站点信息不符合入库条件!!!" + " 自动信号:" + staProtocol.isLoading() + "、可入信号:" + staProtocol.isInEnable() + "、空板信号:" + staProtocol.isEmptyMk() + "、工作号:" + staProtocol.getWorkNo() + "、锁定标记" + staProtocol.isPakMk() + "、入库印记:" + staProtocol.getStamp());
                }
            }
        }
    }
 
    public static final ArrayList<Integer> staNos2 = new ArrayList<Integer>() {{
 
        add(101);
        add(102);
        add(103);
        add(104);
        add(105);
        add(106);
        add(107);
        add(108);
    }};
 
    public synchronized void in(Integer mark) {
        // 遍历入库口
        for (Integer inSta : staNos2) {
            // 获取入库站信息
            SiemensDevpThread devpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
            StaProtocol staProtocol = devpThread.getStation().get(inSta);
            if (staProtocol == null) {
                continue;
            } else {
                staProtocol = staProtocol.clone();
            }
            // 判断是否满足条件
            if (!staProtocol.isAutoing() && !staProtocol.isLoading()) {
                continue;
            }
            // && staProtocol.isInEnable()
            if (staProtocol.isInEnable()) {
                getWrk(staProtocol, "-2");
            }
            if (staProtocol.isOutEnable()) {
                getWrk(staProtocol, "-3");
            }
        }
    }
 
    private void getWrk(StaProtocol staProtocol, String lev) {
        List<WrkMast> sourceLocNo = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("source_loc_no", staProtocol.getSiteId() + lev));
        if (!Cools.isEmpty(sourceLocNo) && !sourceLocNo.isEmpty()) {
            return;
        }
        WrkMast in = new WrkMast();
        in.setSourceLocNo(staProtocol.getSiteId() + lev);
        in.setLocNo("");
        in.setIoType(10);
        in.setIoTime(new Date());
        in.setWrkSts(1L); // 工作状态:11.生成出库ID
        in.setIoPri(13D); // 优先级:13
        in.setFullPlt("Y"); // 满板:Y
        in.setPicking("N"); // 拣料
        in.setExitMk("N"); // 退出
        in.setEmptyMk("Y"); // 空板
        in.setLinkMis("N");
        in.setAppeTime(new Date());
        in.setModiTime(new Date());
        wrkMastService.insert(in);
    }
 
}