#
Junjie
2024-05-05 2d2069e345283e8d3578e6238da319ea16f585b5
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package com.zy.asrs.wcs.asrs.execute;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wcs.core.domain.dto.MotionDto;
import com.zy.asrs.wcs.core.entity.Motion;
import com.zy.asrs.wcs.core.entity.Task;
import com.zy.asrs.wcs.core.kernel.KernelService;
import com.zy.asrs.wcs.core.service.TaskService;
import com.zy.asrs.wcs.core.utils.RedisUtil;
import com.zy.asrs.wcs.core.utils.ShuttleDispatcher;
import com.zy.asrs.wcs.core.utils.Utils;
import com.zy.asrs.wcs.rcs.cache.SlaveConnection;
import com.zy.asrs.wcs.rcs.entity.Device;
import com.zy.asrs.wcs.rcs.model.enums.SlaveType;
import com.zy.asrs.wcs.rcs.model.protocol.LiftProtocol;
import com.zy.asrs.wcs.rcs.model.protocol.ShuttleProtocol;
import com.zy.asrs.wcs.rcs.thread.LiftThread;
import com.zy.asrs.wcs.rcs.thread.ShuttleThread;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.List;
 
@Component
public class FlowExecute {
 
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ShuttleDispatcher shuttleDispatcher;
    @Autowired
    private KernelService kernelService;
 
    //执行流程图
    public boolean execute(List<JSONObject> list) {
        String currentId = list.get(0).getString("id");
 
        JSONObject devpType = list.get(0).getJSONObject("data").getJSONObject("devpType");
        String staNo = devpType.getString("staNo");
        //搜索工作档
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>()
                .eq(Task::getOriginSite, staNo)
                .eq(Task::getStatus, 1));
 
        for (Task task : tasks) {
            String redisKey = DigestUtils.md5Hex(JSON.toJSONString(list));
 
            redisSet(redisKey, "motionList", new ArrayList<Motion>());
            while (currentId != null) {
                //获取流程图
                JSONObject flow = findFLow(list, currentId);
                if (flow == null) {
                    break;
                }
 
                //执行
                boolean result = executeFlow(task, flow, redisKey);
 
                //执行后续流程
                if (Cools.isEmpty(flow.get("nextTrue")) && Cools.isEmpty(flow.get("nextFalse"))) {
                    break;//无后续流程
                }
 
                //更新id
                currentId = result ? flow.getString("nextTrue") : flow.getString("nextFalse");
            }
 
            System.out.println("执行完成");
            redisUtil.del(redisKey);//释放缓存
        }
        return true;
    }
 
    private boolean executeFlow(Task task, JSONObject flow, String redisKey) {
        System.out.println(flow.getString("id") + "被执行");
        JSONObject data = flow.getJSONObject("data");
        String type = data.getString("type");
        boolean result = false;
        if (type.equals("devp")) {
            result = executeDevpFlow(task, data, redisKey);
        } else if (type.equals("shuttle")) {
            result = executeShuttleFlow(task, data, redisKey);
        } else if (type.equals("lift")) {
            result = executeLiftFlow(task, data, redisKey);
        }
        return result;
    }
 
    private JSONObject findFLow(List<JSONObject> list, String id) {
        for (JSONObject flow : list) {
            if (flow.getString("id").equals(id)) {
                return flow;
            }
        }
        return null;
    }
 
    private boolean executeDevpFlow(Task task, JSONObject data, String redisKey) {
        JSONObject devpType = data.getJSONObject("devpType");
        String staNo = devpType.getString("staNo");
        //搜索工作
 
        return true;
    }
 
    private boolean executeShuttleFlow(Task task, JSONObject data, String redisKey) {
        JSONObject shuttleType = data.getJSONObject("shuttleType");
        String oper = shuttleType.getString("shuttleOper");
        if (oper.equals("searchIdleShuttle")) {
            //搜索空闲车
            ShuttleThread shuttleThread = shuttleDispatcher.searchIdleShuttle(task);
            if (shuttleThread == null) {
                throw new CoolException("穿梭车不存在");
            }
            //存入缓存
            redisSet(redisKey, "shuttleDevice", shuttleThread.getDevice());
            return true;
        } else if (oper.equals("judgeShuttleCurrentTaskLev")) {
            //判断小车是否在任务楼层
            //取出缓存小车
            Object shuttleDeviceObj = redisGet(redisKey, "shuttleDevice");
            if (shuttleDeviceObj == null) {
                throw new CoolException("穿梭车缓存为空");
            }
            Device device = JSON.parseObject(String.valueOf(shuttleDeviceObj), Device.class);
            if (device == null) {
                throw new CoolException("穿梭车设备不存在");
            }
 
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue());
            if (shuttleThread == null) {
                throw new CoolException("穿梭车不存在");
            }
 
            ShuttleProtocol shuttleProtocol = shuttleThread.getStatus();
            if (shuttleProtocol == null) {
                throw new CoolException("穿梭车不存在");
            }
 
            //小车坐标
            String shuttleLocNo = shuttleProtocol.getCurrentLocNo();
            String taskLocNo = null;
            if (task.getTaskSts() < 100) {
                taskLocNo = task.getDestLoc();
            } else if (task.getTaskSts() > 100 && task.getTaskSts() < 200) {
                taskLocNo = task.getOriginLoc();
            }
 
            // 小车和任务楼层一致
            if (Utils.getLev(shuttleLocNo) == Utils.getLev(taskLocNo)) {
                return true;
            }else {
                return false;
            }
        }
 
        return true;
    }
 
    private boolean executeLiftFlow(Task task, JSONObject data, String redisKey) {
        Object object = redisGet(redisKey, "motionList");
        List<Motion> motionList = JSON.parseArray(JSON.toJSONString(object), Motion.class);
 
        Object liftDeviceObj = redisGet(redisKey, "liftDevice");
        if (liftDeviceObj == null) {
            throw new CoolException("提升机缓存为空");
        }
 
        Device device = JSON.parseObject(String.valueOf(liftDeviceObj), Device.class);
        if (device == null) {
            throw new CoolException("提升机设备不存在");
        }
 
        LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, device.getId().intValue());
        if (liftThread == null) {
            throw new CoolException("提升机不存在");
        }
 
        LiftProtocol liftProtocol = liftThread.getStatus();
        if (liftProtocol == null) {
            throw new CoolException("提升机不存在");
        }
 
        JSONObject liftType = data.getJSONObject("liftType");
        String opera = liftType.getString("liftOper");
        if (opera.equals("move")) {
            //提升机升降
            String sourceLevStr = liftType.getString("sourceLev");
            String targetLevStr = liftType.getString("targetLev");
            Integer sourceLev = getLiftOperaLev(sourceLevStr, task, redisKey);
            Integer targetLev = getLiftOperaLev(targetLevStr, task, redisKey);
 
            motionList.addAll(kernelService.liftMove(
                    MotionDto.build((dto -> {
                        dto.setLiftNo(device.getId().intValue());
                        dto.setLev(sourceLev);
                    }))
                    , MotionDto.build((dto -> {
                        dto.setLiftNo(device.getId().intValue());
                        dto.setLev(targetLev);
                    }))
            ));
        }
 
        return true;
    }
 
    private Integer getLiftOperaLev(String opera, Task task, String redisKey) {
        if (opera.equals("sourceLev")) {
            return Utils.getLev(task.getOriginLoc());
        } else if (opera.equals("targetLev")) {
            return Utils.getLev(task.getDestLoc());
        } else if (opera.equals("shuttleLev")) {
            //取出缓存小车
            Object shuttleDeviceObj = redisGet(redisKey, "shuttleDevice");
            if (shuttleDeviceObj == null) {
                throw new CoolException("穿梭车缓存为空");
            }
            Device device = JSON.parseObject(String.valueOf(shuttleDeviceObj), Device.class);
            if (device == null) {
                throw new CoolException("穿梭车设备不存在");
            }
 
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue());
            if (shuttleThread == null) {
                throw new CoolException("穿梭车不存在");
            }
 
            ShuttleProtocol shuttleProtocol = shuttleThread.getStatus();
            if (shuttleProtocol == null) {
                throw new CoolException("穿梭车不存在");
            }
 
            return Utils.getLev(shuttleProtocol.getCurrentLocNo());
        }
 
        throw new CoolException("类型异常");
    }
 
    private boolean redisSet(String redisKey, String key, Object data) {
        if (redisUtil.hasKey(redisKey)) {
            Object obj = redisUtil.get(redisKey);
            JSONObject object = JSON.parseObject(String.valueOf(obj));
            object.put(key, data);
            redisUtil.set(redisKey, JSON.toJSONString(object));
        } else {
            JSONObject object = new JSONObject();
            object.put(key, data);
            redisUtil.set(redisKey, JSON.toJSONString(object));
        }
        return true;
    }
 
    private Object redisGet(String redisKey, String key) {
        if (redisUtil.hasKey(redisKey)) {
            Object obj = redisUtil.get(redisKey);
            JSONObject object = JSON.parseObject(String.valueOf(obj));
            if (!object.containsKey(key)) {
                return null;
            }
            return object.get(key);
        }
        return null;
    }
 
//    private boolean executeFlow(List<Map<String, Object>> list) {
//        for (Map<String, Object> map : list) {
//            JSONObject data = (JSONObject) map.get("data");
//            if (data.getString("type").equals("devp")) {
//                JSONObject devp = data.getJSONObject("devpType");
//                String devpNo = devp.getString("devpNo");//输送线PLC
//                String staNo = devp.getString("staNo");//站号
//                Boolean enableStaStatus = devp.getBoolean("enableStaStatus");//判断站点状态
//                JSONArray staStatus = devp.getJSONArray("staStatus");//站点状态列表
//                String staJudgementFailExecute = devp.getString("staJudgementFailExecute");//判断失败后是否继续执行流程
//                Boolean writeWorkNoStatus = devp.getBoolean("writeWorkNoStatus");//是否写入工作号
//                Boolean writeStaNoStatus = devp.getBoolean("writeStaNoStatus");//是否写入目标站
//                String writeWorkNo = devp.getString("writeWorkNo");//写入工作号数据
//                String writeStaNo = devp.getString("writeStaNo");//写入目标站数据
//                if (enableStaStatus) {
//                    //判断站点状态
//                    boolean statusFlag = true;//默认判断通过
//                    for (Object status : staStatus) {
//                        System.out.println(status);
//                    }
//
//                    if (!statusFlag) {
//                        //判断不通过
//                        if (staJudgementFailExecute.equals("stop")) {
//                            //判断失败后不继续执行
//                            return false;
//                        }
//                    }
//
//                }
//
//                if (writeWorkNoStatus) {
//                    //写入工作号
//                }
//
//                if (writeStaNoStatus) {
//                    //写入目标站
//                }
//                System.out.println(devp);
//            }
//            System.out.println(data);
//        }
//
//        System.out.println(list);
//        return false;
//    }
 
}