#
Junjie
2025-07-15 63fc53379b059d4076a228c8b5b149ead84f20eb
src/main/java/com/zy/core/utils/DeviceMsgUtils.java
@@ -1,10 +1,13 @@
package com.zy.core.utils;
import com.core.exception.CoolException;
import com.alibaba.fastjson.JSON;
import com.zy.common.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 com.zy.core.properties.DeviceConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@@ -19,22 +22,40 @@
    @Value("${deviceMsgConfig.destroyAfterReading}")
    private boolean destroyAfterReading;
    @Value("${deviceMsgConfig.gatewayId}")
    private Integer gatewayId;
    @Autowired
    private RedisUtil redisUtil;
    public Object getDeviceCommandMsg(SlaveType deviceType, Integer deviceId) {
    public List<DeviceConfig> getDeviceConfig() {
        List<DeviceConfig> list = new ArrayList<>();
        Object obj = redisUtil.get(RedisKeyType.DEVICE_CONFIG.key);
        if(null == obj){
            return list;
        }
        List<DeviceConfig> deviceConfigs = JSON.parseArray(obj.toString(), DeviceConfig.class);
        for (DeviceConfig deviceConfig : deviceConfigs) {
            if(deviceConfig.getGatewayId().equals(gatewayId)){
                list.add(deviceConfig);
            }
        }
        return list;
    }
    public DeviceCommandMsgModel getDeviceCommandMsg(SlaveType deviceType, Integer deviceId) {
        TreeSet<String> listKey = getDeviceCommandMsgListKey(deviceType, deviceId);
        if (listKey.isEmpty()) {
            return null;
        }
        //deviceShuttleCommandMsgKey_1_1751674783851
        String firstKey = listKey.first();
        Object data = redisUtil.get(firstKey);
        DeviceCommandMsgModel commandMsgModel = (DeviceCommandMsgModel) redisUtil.get(firstKey);
        if (destroyAfterReading) {
            redisUtil.del(firstKey);
        }
        return data;
        return commandMsgModel;
    }
    public DeviceMsgModel getDeviceMsg(SlaveType deviceType, Integer deviceId) {
@@ -67,22 +88,22 @@
        redisUtil.set(key, msgModel, 60 * 60);
    }
    public String sendDeviceMsg(SlaveType deviceType, Integer deviceId, Object command) {
    public String sendDeviceMsg(SlaveType deviceType, Integer deviceId, DeviceMsgModel deviceMsgModel) {
        String key = parseDeviceMsgKey(deviceType, deviceId) + System.currentTimeMillis();
        DeviceMsgModel deviceMsgModel = new DeviceMsgModel();
        deviceMsgModel.setDeviceId(deviceId);
        deviceMsgModel.setDeviceMsg(command);
        redisUtil.set(key, deviceMsgModel, 60 * 60 * 24);
        return key;
    }
    public String sendDeviceCommand(SlaveType deviceType, Integer deviceId, Object command) {
    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);