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;
|
}
|
|
}
|