#
Junjie
8 小时以前 40101e1766a04cc82076556783ada6e1a7711b93
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
141
142
143
144
145
146
147
148
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("设备类型未定义");
        }
    }
 
 
}