自动化立体仓库 - WMS系统
zhangc
2025-02-14 9beae5f38c4cfe95560601fc4638923a34f393e9
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
package com.zy.asrs.task;
 
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.zy.asrs.entity.Agv;
import com.zy.asrs.entity.AgvWarn;
import com.zy.asrs.entity.AgvWarnLog;
import com.zy.asrs.mapper.AgvBasMapper;
import com.zy.asrs.service.AgvWarnLogService;
import com.zy.asrs.service.AgvWarnService;
import com.zy.asrs.task.handler.AgvWarnHandler;
import com.zy.common.utils.HttpHandler;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
 
/**
 * Created by vincent on 2020/7/7
 */
@Component
public class AgvWarnScheduler {
 
    private static final Logger log = LoggerFactory.getLogger(AgvWarnScheduler.class);
 
    @Autowired
    private AgvWarnHandler agvWarnHandler;
 
    @Autowired
    private AgvWarnService agvWarnService;
 
    @Autowired
    private AgvWarnLogService agvWarnLogService;
 
    @Autowired
    private ConfigService configService;
 
    @Autowired
    private AgvBasMapper agvBasMapper;
 
 
    /**
     *  删除告警日志
     */
    @Scheduled(cron = "0 0 1 * * ? ")
    private void delWarnLog() {
        String format = DateUtil.format(DateUtil.offsetMonth(new Date(), -1), "yyyy-MM-dd HH:mm:ss");
        List<AgvWarnLog> agvWarnList = agvWarnLogService.selectList(new EntityWrapper<AgvWarnLog>().le("modi_time", format));
        for (AgvWarnLog agvWarn : agvWarnList) {
            agvWarnLogService.deleteById(agvWarn);
        }
    }
 
    /**
     * 超过一分钟没更新的,删除
     */
    @Scheduled(fixedDelay = 10000)
    private void del() {
        String format = DateUtil.format(DateUtil.offsetMinute(new Date(), -1), "yyyy-MM-dd HH:mm:ss");
        List<AgvWarn> agvWarnList = agvWarnService.selectList(new EntityWrapper<AgvWarn>().le("modi_time", format));
        for (AgvWarn agvWarn : agvWarnList) {
            agvWarnHandler.start(agvWarn);
        }
    }
 
    @Scheduled(fixedDelay = 20000)
    private void report() {
        List<Agv> agvs = agvBasMapper.selectList(new EntityWrapper<Agv>());
        Config first = configService.selectOne(new EntityWrapper<Config>().eq("code", "AGV_WARN_RATE1"));
        Config second = configService.selectOne(new EntityWrapper<Config>().eq("code", "AGV_WARN_RATE2"));
        Config third = configService.selectOne(new EntityWrapper<Config>().eq("code", "AGV_WARN_RATE3"));
        Config url = configService.selectOne(new EntityWrapper<Config>().eq("code", "AGV_WARN_REPORT_URL"));
        List<AgvWarn> agvWarnList = agvWarnService.selectList(new EntityWrapper<AgvWarn>());
        StringBuffer buffer;
        HashMap<String, Object> sendData = new HashMap<>();
        for (AgvWarn agvWarn : agvWarnList) {
            Integer warnTime = getWarnTime(agvWarn.getTimes(), first.getValue(), second.getValue(), third.getValue());
            Date begin = DateUtil.parse(agvWarn.getBeginTime(), "yyyy-MM-dd HH:mm:ss");
            if (DateUtil.offsetMinute(agvWarn.getAppeTime(), -3).after(begin)) {
                begin = agvWarn.getAppeTime();
            }
            if (DateUtil.offsetMinute(new Date(), -warnTime).after(begin)) {
                buffer = new StringBuffer();
                String s = processRegion(agvs, agvWarn.getRobotCode());
                if (s == null) {
                    continue;
                }
                buffer.append(s + "[" + agvWarn.getRobotCode() + "]号AGV小车报警了\n");
                buffer.append("报警类型:" + agvWarn.getWarnContent() + "\n");
                if (!Cools.isEmpty(agvWarn.getSourceLocNo())) {
                    buffer.append("从:[" + agvWarn.getSourceLocNo() + "]到[" + agvWarn.getLocNo() + "]\n");
                }
                //buffer.append("报警开始时间:" + agvWarn.getBeginTime() + "\n");
                buffer.append("报警开始时间:" + DateUtil.formatDateTime(begin) + "\n");
                buffer.append("当前时间:" + DateUtil.formatDateTime(new Date()));
                //int i = agvWarn.getTimes() + 1;
                //buffer.append("第[" + i + "]次报警");
                HashMap<String, Object> param = new HashMap<>();
                HashMap<String, Object> data = new HashMap<>();
                data.put("content", buffer.toString());
                param.put("msgtype", "text");
                param.put("text", data);
                sendData.put(agvWarn.getRobotCode() + agvWarn.getWarnContent(), param);
            }
            for (HashMap.Entry<String, Object> entry : sendData.entrySet()) {
                log.info("发送报警信息:{}", JSON.toJSONString(entry.getValue()));
                String response = null;
                try {
                    response = new HttpHandler.Builder().setUri(url.getValue()).setJson(JSON.toJSONString(entry.getValue())).setHttps(true).build().doPost();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                JSONObject jsonObject = JSON.parseObject(response);
                if (jsonObject.get("errmsg").equals("ok")) {
                    agvWarn.setTimes(agvWarn.getTimes() + 1);
                    agvWarn.setModiTime(new Date());
                    agvWarnService.updateById(agvWarn);
                } else {
                    log.error("发送失败,错误信息:{}", jsonObject.get("errmsg"));
                }
            }
 
        }
    }
 
    /**
     * 若未配置数据,则5分钟一次
     * @param times
     * @return
     */
    private Integer getWarnTime(Integer times, String first, String second, String thrid) {
        switch (times) {
            case 0:
                return Integer.parseInt(first);
            case 1:
                return Integer.parseInt(first) + Integer.parseInt(second);
            case 2:
                return Integer.parseInt(first) + Integer.parseInt(second) + Integer.parseInt(thrid);
            default:
                return (times - 2) * Integer.parseInt(thrid) + Integer.parseInt(first) + Integer.parseInt(second) + Integer.parseInt(thrid);
        }
    }
 
    private String processRegion(List<Agv> agvs, String agvNo) {
        if (agvNo == null) {
            return "未知小车";
        }
        for (Agv agv : agvs) {
            if (agv.getAgvNo() != null && agv.getAgvNo().equals(agvNo)) {
                if (!Cools.isEmpty(agv.getFlag()) && agv.getFlag() == 1) {
                    return null;
                }
                return agv.getRegion();
            }
        }
        return "未知";
    }
 
}