package com.zy.core.Utils;
|
|
import com.core.exception.CoolException;
|
import com.zy.common.utils.RedisUtil;
|
import com.zy.core.enums.RedisKeyType;
|
import com.zy.core.enums.SlaveType;
|
import com.zy.core.model.DeviceMsgModel;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Component;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Set;
|
import java.util.TreeSet;
|
|
@Component
|
public class DeviceMsgUtils {
|
|
@Value("${deviceMsgConfig.destroyAfterReading}")
|
private boolean destroyAfterReading;
|
|
@Autowired
|
private RedisUtil redisUtil;
|
|
public DeviceMsgModel getDeviceMsg(SlaveType deviceType, Integer deviceId) {
|
TreeSet<String> listKey = getDeviceMsgListKey(deviceType, deviceId);
|
if (listKey.isEmpty()) {
|
return null;
|
}
|
|
String firstKey = listKey.first();
|
DeviceMsgModel deviceMsgModel = (DeviceMsgModel) redisUtil.get(firstKey);
|
if (destroyAfterReading) {
|
redisUtil.del(firstKey);
|
}
|
return deviceMsgModel;
|
}
|
|
public List<DeviceMsgModel> getDeviceMsgList(SlaveType deviceType, Integer deviceId) {
|
TreeSet<String> listKey = getDeviceMsgListKey(deviceType, deviceId);
|
List<DeviceMsgModel> deviceMsgModels = new ArrayList<>();
|
for (String key : listKey) {
|
DeviceMsgModel deviceMsgModel = (DeviceMsgModel) redisUtil.get(key);
|
deviceMsgModels.add(deviceMsgModel);
|
}
|
return deviceMsgModels;
|
}
|
|
public void pushDeviceMsg(SlaveType deviceType, Integer deviceId, DeviceMsgModel msgModel) {
|
String deviceMsgKey = parseDeviceMsgKey(deviceType, deviceId);
|
String key = deviceMsgKey + System.currentTimeMillis();
|
redisUtil.set(key, msgModel, 60 * 60);
|
}
|
|
public String sendCommand(SlaveType deviceType, Integer deviceId, Object command) {
|
String key = parseDeviceCommandMsgKey(deviceType, deviceId) + System.currentTimeMillis();
|
redisUtil.set(key, command, 60 * 60 * 24);
|
return key;
|
}
|
|
public TreeSet<String> getDeviceMsgListKey(SlaveType deviceType, Integer deviceId) {
|
String listKey = parseDeviceMsgKey(deviceType, deviceId);
|
Set<String> keys = redisUtil.searchKeys(listKey);
|
|
TreeSet<String> treeSet = new TreeSet<>();
|
for (String key : keys) {
|
treeSet.add(key);
|
}
|
|
return treeSet;
|
}
|
|
public String parseDeviceMsgKey(SlaveType deviceType, Integer deviceId) {
|
if (deviceType == null) {
|
throw new CoolException("设备类型为空");
|
}
|
|
if (deviceType.equals(SlaveType.Shuttle)) {
|
return RedisKeyType.DEVICE_SHUTTLE_MSG_KEY_.key + deviceId + "_";
|
} else if (deviceType.equals(SlaveType.ForkLift)) {
|
return RedisKeyType.DEVICE_FORK_LIFT_MSG_KEY_.key + deviceId + "_";
|
}else {
|
throw new CoolException("设备类型未定义");
|
}
|
}
|
|
public String parseDeviceCommandMsgKey(SlaveType deviceType, Integer deviceId) {
|
if (deviceType == null) {
|
throw new CoolException("设备类型为空");
|
}
|
|
if (deviceType.equals(SlaveType.Shuttle)) {
|
return RedisKeyType.DEVICE_SHUTTLE_COMMAND_MSG_KEY.key + deviceId + "_";
|
} else if (deviceType.equals(SlaveType.ForkLift)) {
|
return RedisKeyType.DEVICE_FORK_LIFT_COMMAND_MSG_KEY.key + deviceId + "_";
|
}else {
|
throw new CoolException("设备类型未定义");
|
}
|
}
|
|
}
|