#
Junjie
2024-03-27 eb73160dca035ca64aa5fc777b437682c0fe6dc8
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package com.zy.asrs.wcs.core.timer;
 
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wcs.core.entity.Task;
import com.zy.asrs.wcs.core.kernel.command.AgvCommandService;
import com.zy.asrs.wcs.core.kernel.command.CraneCommandService;
import com.zy.asrs.wcs.core.kernel.command.ShuttleCommandService;
import com.zy.asrs.wcs.core.model.enums.DeviceCtgType;
import com.zy.asrs.wcs.core.model.enums.MotionCtgType;
import com.zy.asrs.wcs.core.model.enums.MotionStsType;
import com.zy.asrs.wcs.core.model.enums.TaskStsType;
import com.zy.asrs.wcs.core.service.TaskService;
import com.zy.asrs.wcs.core.entity.Motion;
import com.zy.asrs.wcs.core.service.MotionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * Created by vincent on 2023/10/10
 */
@Slf4j
@Component
public class MotionTimer {
 
    @Autowired
    private TaskService taskService;
    @Autowired
    private MotionService motionService;
    @Autowired
    private AgvCommandService agvCommandService;
//    @Autowired
//    private ConveyorCommandService conveyorCommandService;
    @Autowired
    private CraneCommandService craneCommandService;
//    @Autowired
//    private LiftCommandService liftCommandService;
    @Autowired
    private ShuttleCommandService shuttleCommandService;
 
//    @Scheduled(cron = "0/1 * * * * ? ")
    public synchronized void executeWrkMast() {
        Date now = new Date();
        // ANALYZE_INBOUND
        for (Task task : taskService.selectByAnalyzeSts()) {
            Motion executingMotion = motionService.selectOfTop1(task.getUuid(), MotionStsType.EXECUTING.val(), task.getHostId());
            if (executingMotion != null) {//存在正在执行的motion
                continue;
            }
 
            Motion motion = motionService.selectOfTop1(task.getUuid(), MotionStsType.INIT.val(), task.getHostId());
            if (null != motion) {
                boolean result = this.executeMotion(motion);
                if (!result) {
                    continue;
                }
 
                // 更新wrkMast
                switch (TaskStsType.query(task.getTaskSts())) {
                    case ANALYZE_INBOUND:
                        task.setTaskSts(TaskStsType.EXECUTE_INBOUND.sts);
                        break;
                    case ANALYZE_OUTBOUND:
                        task.setTaskSts(TaskStsType.EXECUTE_OUTBOUND.sts);
                        break;
                }
                task.setUpdateTime(now);
                if (!taskService.updateById(task)) {
                    log.error("{}工作档更新状态失败!", task.getTaskNo());
                }
            }
        }
        // EXECUTE_INBOUND
        for (Task task : taskService.selectByExecuteSts()) {
            if (!motionService.hasRunningMotion(task.getUuid(), task.getHostId())) {
                Motion motion = motionService.selectOfTop1(task.getUuid(), MotionStsType.WAITING.val(), task.getHostId());
                if (null != motion) {
                    boolean result = this.executeMotion(motion);
                    if (!result) {
                        continue;
                    }
                } else {
                    if (motionService.selectOfTop1(task.getUuid(), MotionStsType.EXECUTING.val(), task.getHostId()) != null) {
                        continue;
                    }
 
                    // 更新wrkMast
                    switch (TaskStsType.query(task.getTaskSts())) {
                        case EXECUTE_INBOUND:
                            task.setTaskSts(TaskStsType.COMPLETE_INBOUND.sts);
                            break;
                        case EXECUTE_OUTBOUND:
                            task.setTaskSts(TaskStsType.COMPLETE_OUTBOUND.sts);
                            break;
                    }
                    task.setUpdateTime(now);
                    if (!taskService.updateById(task)) {
                        log.error("{}工作档更新状态失败!", task.getTaskNo());
                    }
                }
            }
        }
    }
 
//    @Scheduled(cron = "0/1 * * * * ? ")
//    public synchronized void executeWrkCharge() {
//        Date now = new Date();
//        // ANALYZE_INBOUND
//        for (WrkCharge wrkCharge : wrkChargeMapper.selectByAnalyzeSts()) {
//            Motion executingMotion = motionService.selectOfTop1(wrkCharge.getUuid(), MotionStsType.EXECUTING.val());
//            if (executingMotion != null) {//存在正在执行的motion
//                continue;
//            }
//
//            Motion motion = motionService.selectOfTop1(wrkCharge.getUuid(), MotionStsType.INIT.val());
//            if (null != motion) {
//                boolean result = this.executeMotion(motion);
//                if (!result) {
//                    continue;
//                }
//                // 更新wrkMast
//                switch (WrkMastStsType.query(wrkCharge.getWrkSts())) {
//                    case ANALYZE_CHARGE:
//                        wrkCharge.setWrkSts(WrkMastStsType.EXECUTE_CHARGE.sts);
//                        break;
//                    case ANALYZE_MOVE:
//                        wrkCharge.setWrkSts(WrkMastStsType.EXECUTE_MOVE.sts);
//                        break;
//                }
//                wrkCharge.setModiTime(now);
//                if (wrkChargeMapper.updateById(wrkCharge) == 0) {
//                    log.error("{}其他工作档更新状态失败!", wrkCharge.getWrkNo());
//                }
//            }
//        }
//        // EXECUTE_INBOUND
//        for (WrkCharge wrkCharge : wrkChargeMapper.selectByExecuteSts()) {
//            if (!motionService.hasRunningMotion(wrkCharge.getUuid())) {
//                Motion motion = motionService.selectOfTop1(wrkCharge.getUuid(), MotionStsType.WAITING.val());
//                if (null != motion) {
//                    boolean result = this.executeMotion(motion);
//                    if (!result) {
//                        continue;
//                    }
//                } else {
//                    if (motionService.selectOfTop1(wrkCharge.getUuid(), MotionStsType.EXECUTING.val()) != null) {
//                        continue;
//                    }
//
//                    // 更新wrkMast
//                    switch (WrkMastStsType.query(wrkCharge.getWrkSts())) {
//                        case EXECUTE_CHARGE:
//                            wrkCharge.setWrkSts(WrkMastStsType.CHARGE_WORKING.sts);
//                            break;
//                        case EXECUTE_MOVE:
//                            wrkCharge.setWrkSts(WrkMastStsType.COMPLETE_MOVE.sts);
//                            break;
//                    }
//                    wrkCharge.setModiTime(now);
//                    if (wrkChargeMapper.updateById(wrkCharge) == 0) {
//                        log.error("{}他工作档更新状态失败!", wrkCharge.getWrkNo());
//                    }
//                }
//            }
//        }
//    }
 
    @Scheduled(cron = "0/1 * * * * ? ")
    public void scanMotionByExecuting() {
        List<Motion> motionList = motionService.selectBySts(MotionStsType.EXECUTING.val());
        for (Motion motion : motionList) {
            this.finishMotion(motion);
        }
    }
 
    private synchronized boolean executeMotion(Motion motion) {
        Boolean executeRes = Boolean.FALSE;
        switch (Objects.requireNonNull(MotionCtgType.get(motion.getMotionCtgEl())).deviceCtg) {
            case CONVEYOR:
//                executeRes = conveyorCommandService.accept(motion);
                break;
            case CRANE:
                executeRes = craneCommandService.accept(motion);
                break;
            case SHUTTLE:
                executeRes = shuttleCommandService.accept(motion);
                break;
            case LIFT:
//                executeRes = liftCommandService.accept(motion);
                break;
            case AGV:
                executeRes = agvCommandService.accept(motion);
                break;
            default:
                break;
        }
 
        if (executeRes) {
            Date now = new Date();
            motion.setMotionSts(MotionStsType.EXECUTING.val());
            motion.setUpdateTime(now);
            motion.setIoTime(now);
            if (!motionService.updateById(motion)) {
                throw new CoolException(motion.generateFlag() + "动作更新失败状态");
            }
            motionService.theNextBeWaiting(motion.getUuid(), motion);
            return true;
        }
 
        return false;
    }
 
    private synchronized void finishMotion(Motion motion) {
        Boolean executeRes = Boolean.FALSE;
        switch (Objects.requireNonNull(DeviceCtgType.get(motion.getDeviceCtgEl()))){
            case AGV:
                break;
            case CRANE:
                break;
            case LIFT:
//                executeRes = liftCommandService.finish(motion);
                break;
            case SHUTTLE:
                executeRes = shuttleCommandService.finish(motion);
                break;
            case CONVEYOR:
//                executeRes = conveyorCommandService.finish(motion);
                break;
            default:
                break;
        }
        if (executeRes) {
            Date now = new Date();
            motion.setMotionSts(MotionStsType.COMPLETE.val());
            motion.setUpdateTime(now);
            if (!motionService.updateById(motion)) {
                throw new CoolException(motion.generateFlag() + "动作更新失败状态");
            }
        }
    }
 
 
}