#
Junjie
18 小时以前 86e79681da9c98bd08bd1f2be0c6dbd3f3dd3159
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
package com.zy.asrs.utils;
 
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.Arrays;
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 List<String> getSupportedNotifyTypes() {
        return Arrays.asList(
                String.valueOf(SlaveType.Crn),
                String.valueOf(SlaveType.Devp),
                String.valueOf(SlaveType.DualCrn),
                String.valueOf(SlaveType.Rgv),
                "task"
        );
    }
 
    public String getKeyPrefix(String notifyType) {
        if (notifyType.equals(String.valueOf(SlaveType.Crn))) {
            return RedisKeyType.QUEUE_CRN.key;
        } else if (notifyType.equals(String.valueOf(SlaveType.Devp))) {
            return RedisKeyType.QUEUE_DEVP.key;
        } else if (notifyType.equals(String.valueOf(SlaveType.DualCrn))) {
            return RedisKeyType.QUEUE_DUAL_CRN.key;
        } else if (notifyType.equals(String.valueOf(SlaveType.Rgv))) {
            return RedisKeyType.QUEUE_RGV.key;
        } else if (notifyType.equals("task")) {
            return RedisKeyType.QUEUE_TASK.key;
        }
        return null;
    }
 
    public String getNotifyTypeDesc(String notifyType) {
        if (notifyType.equals(String.valueOf(SlaveType.Crn))) {
            return "堆垛机";
        } else if (notifyType.equals(String.valueOf(SlaveType.Devp))) {
            return "输送线";
        } else if (notifyType.equals(String.valueOf(SlaveType.DualCrn))) {
            return "双伸位堆垛机";
        } else if (notifyType.equals(String.valueOf(SlaveType.Rgv))) {
            return "RGV";
        } else if (notifyType.equals("task")) {
            return "任务";
        }
        return notifyType;
    }
 
    public String getKey(String notifyType, Integer device) {
        String prefix = getKeyPrefix(notifyType);
        if (prefix == null) {
            return null;
        }
        return prefix + device;
    }
 
    private boolean append(String notifyType, Integer device, String taskNo, String superTaskNo, NotifyMsgType msgType, String data) {
        boolean notifyEnable = true;
        Config notifyEnableConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyEnable"));
        if (notifyEnableConfig != null) {
            notifyEnable = notifyEnableConfig.getValue().equals("Y");
        }
 
        if (!notifyEnable) {
            return false;
        }
 
        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;
    }
 
}