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 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 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 getDeviceMsgList(SlaveType deviceType, Integer deviceId) { TreeSet listKey = getDeviceMsgListKey(deviceType, deviceId); List 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(); command.setResultKey(key); redisUtil.set(key, command, 60 * 60 * 24); return key; } public void sendDeviceConfig(String allDevices) { redisUtil.set(RedisKeyType.DEVICE_CONFIG.key, allDevices); } public void sendFakeDeviceConfig(String fakeDevices) { redisUtil.set(RedisKeyType.FAKE_DEVICE_CONFIG.key, fakeDevices); } public TreeSet getDeviceMsgListKey(SlaveType deviceType, Integer deviceId) { String listKey = parseDeviceMsgKey(deviceType, deviceId); Set keys = redisUtil.searchKeys(listKey); TreeSet treeSet = new TreeSet<>(); for (String key : keys) { treeSet.add(key); } return treeSet; } public TreeSet getDeviceCommandMsgListKey(SlaveType deviceType, Integer deviceId) { String listKey = parseDeviceCommandMsgKey(deviceType, deviceId); Set keys = redisUtil.searchKeys(listKey); TreeSet 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("设备类型未定义"); } } }