自动化立体仓库 - WCS系统
#
Junjie
2025-02-10 08b68c503f12a28399f0c70e460ee6af8b7185ea
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
package com.zy.asrs.task;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.asrs.domain.NotifyDto;
import com.zy.asrs.utils.NotifyUtils;
import com.zy.common.utils.HttpHandler;
import com.zy.common.utils.RedisUtil;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.ForkLiftSlave;
import com.zy.core.model.ShuttleSlave;
import com.zy.core.properties.SlaveProperties;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
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.List;
 
@Component
@Slf4j
public class NotifyScheduler {
 
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private NotifyUtils notifyUtils;
    @Autowired
    private SlaveProperties slaveProperties;
    @Autowired
    private ConfigService configService;
 
    @Scheduled(cron = "0/3 * * * * ? ")
    public synchronized void notifyShuttle(){
        for (ShuttleSlave slave : slaveProperties.getShuttle()) {
            notifyMsg(String.valueOf(SlaveType.Shuttle), slave.getId());
        }
    }
 
    @Scheduled(cron = "0/3 * * * * ? ")
    public synchronized void notifyForkLift(){
        for (ForkLiftSlave slave : slaveProperties.getForkLift()) {
            notifyMsg(String.valueOf(SlaveType.ForkLift), slave.getId());
        }
    }
 
    private synchronized void notifyMsg(String deviceType, Integer device) {
        Config notifyEnableConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyEnable"));
        if(notifyEnableConfig == null){
            return;
        }
        String notifyEnable = notifyEnableConfig.getValue();
        if (!notifyEnable.equals("Y")) {
            return;
        }
 
        Config notifyUriConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyUri"));
        if(notifyUriConfig == null){
            return;
        }
        String notifyUri = notifyUriConfig.getValue();
 
        Config notifyUriPathConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyUriPath"));
        if(notifyUriPathConfig == null){
            return;
        }
        String notifyUriPath = notifyUriPathConfig.getValue();
 
        List<String> keys = notifyUtils.takeKeys(deviceType, device);
        if(keys == null){
            return;
        }
 
        if (keys.isEmpty()) {
            return;
        }
 
        for (String key : keys) {
            Object object = redisUtil.get(key);
            if (object == null) {
                continue;
            }
            NotifyDto notifyDto = (NotifyDto) object;
 
            if (System.currentTimeMillis() - notifyDto.getLastRetryTime() < 1000 * notifyDto.getRetryTime()) {
                continue;
            }
 
            try {
                //触发通知
                String response = new HttpHandler.Builder()
                        .setUri(notifyUri)
                        .setPath(notifyUriPath)
                        .setJson(JSON.toJSONString(notifyDto))
                        .build()
                        .doPost();
                JSONObject jsonObject = JSON.parseObject(response);
                Integer code = jsonObject.getInteger("code");
                if(code == 200){
                    //通知成功
                    redisUtil.del(key);
                    return;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
 
            //通知失败
            int times = notifyDto.getRetryTimes() + 1;
            if (times >= notifyDto.getFailTimes()) {
                //超过次数
                redisUtil.del(key);
                return;
            }
 
            notifyDto.setLastRetryTime(System.currentTimeMillis());
            notifyDto.setRetryTimes(times);
            redisUtil.set(key, notifyDto);
            return;
        }
    }
 
}