#
Junjie
4 天以前 456273f11d75782e676d43763f0f0601ea5c37f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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 Object getDeviceCommandMsg(SlaveType deviceType, Integer deviceId) {
        TreeSet<String> listKey = getDeviceCommandMsgListKey(deviceType, deviceId);
        if (listKey.isEmpty()) {
            return null;
        }
 
        String firstKey = listKey.first();
        Object data = redisUtil.get(firstKey);
        if (destroyAfterReading) {
            redisUtil.del(firstKey);
        }
        return data;
    }
 
    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, Object command) {
        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) {
        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 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("设备类型未定义");
        }
    }
 
 
}