package com.zy.asrs.task;
|
|
import cn.hutool.core.date.DateTime;
|
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.AgvWarn;
|
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 ConfigService configService;
|
|
/**
|
* 超过
|
*/
|
@Scheduled(fixedDelay = 30000)
|
private void del() {
|
String format = DateUtil.format(DateUtil.offsetMinute(new Date(), -3), "yyyy-MM-dd HH:mm:ss");
|
List<AgvWarn> agvWarnList = agvWarnService.selectList(new EntityWrapper<AgvWarn>().ge("modi_time", format));
|
for (AgvWarn agvWarn : agvWarnList) {
|
agvWarnHandler.start(agvWarn);
|
}
|
}
|
|
@Scheduled(fixedDelay = 10000)
|
private void report() {
|
List<Config> config = configService.selectList(new EntityWrapper<Config>().eq("code", "AGV_WARN_REPORT_URL"));
|
List<AgvWarn> agvWarnList = agvWarnService.selectList(new EntityWrapper<AgvWarn>());
|
StringBuffer buffer;
|
for (AgvWarn agvWarn : agvWarnList) {
|
Integer warnTime = getWarnTime(agvWarn.getTimes());
|
DateTime begin = DateUtil.parse(agvWarn.getBeginTime(), "yyyy-MM-dd HH:mm:ss");
|
if (DateUtil.offsetMinute(begin, warnTime).after(new Date())) {
|
buffer = new StringBuffer();
|
buffer.append(agvWarn.getRobotCode() + "号AGV小车报警:");
|
buffer.append(agvWarn.getWarnContent());
|
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);
|
String response = null;
|
try {
|
response = new HttpHandler.Builder()
|
.setUri(config.get(0).getValue())
|
.setJson(JSON.toJSONString(param))
|
.setHttps(true)
|
.build()
|
.doPost();
|
} catch (IOException e) {
|
throw new RuntimeException(e);
|
}
|
JSONObject jsonObject = JSON.parseObject(response);
|
if (jsonObject.get("errmsg").equals("ok")) {
|
//发送成功
|
return;
|
} else {
|
log.error("发送失败,错误信息:{}", jsonObject.get("errmsg"));
|
}
|
}
|
}
|
}
|
|
/**
|
* 若未配置数据,则5分钟一次
|
* @param times
|
* @return
|
*/
|
private Integer getWarnTime(Integer times) {
|
try {
|
List<Config> config = configService.selectList(new EntityWrapper<Config>().eq("code", "AGV_WARN_RATE1"));
|
if (!Cools.isEmpty(config)) {
|
String value = config.get(0).getValue();
|
if (Cools.isEmpty(value)) {
|
return 5;
|
} else {
|
String[] split = value.split(",");
|
if (split.length < times + 1) {
|
Config config2 = configService.selectOne(new EntityWrapper<Config>().eq("code", "AGV_WARN_RATE2"));
|
int t = 0;
|
for (String s : split) {
|
t = t + Integer.parseInt(s);
|
}
|
return t + Integer.parseInt(config2.getValue()) * (times + 1 - split.length);
|
} else {
|
return Integer.parseInt(split[times]);
|
}
|
}
|
}
|
return 5;
|
} catch (Exception e) {
|
return 5;
|
}
|
}
|
|
|
}
|