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.DeviceCommandMsgModel;
|
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 String getDeviceConfig() {
|
Object obj = redisUtil.get(RedisKeyType.DEVICE_CONFIG.key);
|
if(null == obj){
|
return null;
|
}
|
return obj.toString();
|
}
|
|
public DeviceCommandMsgModel getDeviceCommandMsg(SlaveType deviceType, Integer deviceId) {
|
TreeSet<String> listKey = getDeviceCommandMsgListKey(deviceType, deviceId);
|
if (listKey.isEmpty()) {
|
return null;
|
}
|
|
String firstKey = listKey.first();
|
DeviceCommandMsgModel commandMsgModel = (DeviceCommandMsgModel) redisUtil.get(firstKey);
|
if (destroyAfterReading) {
|
redisUtil.del(firstKey);
|
}
|
return commandMsgModel;
|
}
|
|
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 sendDeviceMsg(SlaveType deviceType, Integer deviceId, DeviceMsgModel deviceMsgModel) {
|
String key = parseDeviceMsgKey(deviceType, deviceId) + System.currentTimeMillis();
|
redisUtil.set(key, deviceMsgModel, 60 * 60 * 24);
|
return key;
|
}
|
|
public String sendDeviceCommand(SlaveType deviceType, Integer deviceId, DeviceCommandMsgModel command) {
|
String key = parseDeviceCommandMsgKey(deviceType, deviceId) + System.currentTimeMillis();
|
redisUtil.set(key, command, 60 * 60 * 24);
|
return key;
|
}
|
|
public void sendDeviceConfig(String allDevices) {
|
redisUtil.set(RedisKeyType.DEVICE_CONFIG.key, allDevices);
|
}
|
|
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 TreeSet<String> getDeviceCommandMsgListKey(SlaveType deviceType, Integer deviceId) {
|
String listKey = parseDeviceCommandMsgKey(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("设备类型未定义");
|
}
|
}
|
|
|
}
|