自动化立体仓库 - WCS系统
999
zhangc
6 天以前 19adf22200dfe8e2bba439a2c1c640332f436835
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
package com.zy.asrs.utils;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.SnowflakeIdWorker;
import com.zy.asrs.domain.NotifyDto;
import com.zy.asrs.domain.enums.NotifyMsgType;
import com.zy.common.utils.RedisUtil;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.SlaveType;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
 
@Component
public class NotifyUtils {
 
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private ConfigService configService;
 
    public synchronized boolean notify(String notifyType, Integer device, String taskNo, String superTaskNo, NotifyMsgType msgType) {
        return append(notifyType, device, taskNo, superTaskNo, msgType, null);
    }
 
    public synchronized boolean notify(String notifyType, Integer device, String taskNo, String superTaskNo, NotifyMsgType msgType, String data) {
        return append(notifyType, device, taskNo, superTaskNo, msgType, data);
    }
 
    public synchronized List<String> takeKeys(String notifyType, Integer device) {
        String key = getKey(notifyType, device);
        if(key == null){
            return null;
        }
 
        Set keys = redisUtil.keys(key + "*");
        if (keys == null) {
            return null;
        }
 
        List<String> list = new ArrayList<>();
        for (Object object : keys) {
            list.add(object.toString());
        }
        return list;
    }
 
    public String getKey(String notifyType, Integer device) {
        String key = null;
        if (notifyType.equals(String.valueOf(SlaveType.Shuttle))) {
            key = RedisKeyType.QUEUE_SHUTTLE.key + device;
        } else if (notifyType.equals(String.valueOf(SlaveType.ForkLift))) {
            key = RedisKeyType.QUEUE_FORK_LIFT.key + device;
        } else if (notifyType.equals("task")) {
            key = RedisKeyType.QUEUE_TASK.key + device;
        } else {
            return null;
        }
 
        return key;
    }
 
    private boolean append(String notifyType, Integer device, String taskNo, String superTaskNo, NotifyMsgType msgType, String data) {
        String key = getKey(notifyType, device);
        if (key == null) {
            return false;
        }
 
        NotifyDto dto = new NotifyDto();
        dto.setId(snowflakeIdWorker.nextId());
        dto.setNotifyType(notifyType);
        dto.setDevice(device);
        dto.setMsgType(msgType.flag);
        dto.setMsgDesc(msgType.desc);
        dto.setData(data);
        dto.setTaskNo(taskNo);
        dto.setSuperTaskNo(superTaskNo);
 
        //重试次数
        Config notifyFailTimesConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyFailTimes"));
        if (notifyFailTimesConfig != null) {
            dto.setFailTimes(Integer.parseInt(notifyFailTimesConfig.getValue()));
        }
 
        //重试间隔
        Config notifyRetryTimeConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyRetryTime"));
        if (notifyRetryTimeConfig != null) {
            dto.setRetryTime(Integer.parseInt(notifyRetryTimeConfig.getValue()));
        }
 
        redisUtil.set(key + "_" + dto.getId(), dto);
        return true;
    }
 
}