liusuyi
2026-06-01 a2e7e8ff9cfaa69b001d483710bddbda50d55c91
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.ard.gb28181.service.impl;
 
import com.ard.gb28181.common.VideoManagerConstants;
import com.ard.gb28181.config.UserSetting;
import com.ard.gb28181.api.domain.Device;
import com.ard.gb28181.api.domain.DeviceChannel;
import com.ard.gb28181.service.IRedisCatchStorage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.List;
 
@SuppressWarnings("rawtypes")
@Slf4j
@Component
public class RedisCatchStorageImpl implements IRedisCatchStorage {
 
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
 
    @Autowired
    private UserSetting userSetting;
 
    /**
     * 查询设备信息
     *
     * @param deviceId 设备编号
     * @return 设备信息
     */
    @Override
    public Device getDevice(String deviceId) {
        String key = VideoManagerConstants.DEVICE_PREFIX;
        Object object = redisTemplate.opsForHash().get(key, deviceId);
 
        if (object == null) {
            return null;
        }
        return (Device) object;
    }
 
    /**
     * 将device信息写入redis
     *
     * @param device
     */
    @Override
    public void updateDevice(Device device) {
        String key = VideoManagerConstants.DEVICE_PREFIX;
        redisTemplate.opsForHash().put(key, device.getDeviceId(), device);
    }
 
    /**
     * 计数器。为cseq进行计数
     *
     * @return
     */
    @Override
    public Long getCSEQ() {
        String key = VideoManagerConstants.SIP_CSEQ_PREFIX + userSetting.getServerId();
 
        Long result = redisTemplate.opsForValue().increment(key, 1L);
        if (result != null && result > Integer.MAX_VALUE) {
            redisTemplate.opsForValue().set(key, 1);
            result = 1L;
        }
        return result;
    }
 
    /**
     * 根据设备id清除设备通道
     *
     * @param deviceId
     */
    @Override
    public void cleanChannelsForDevice(String deviceId) {
        String key = VideoManagerConstants.DEVICE_CHANNEL_PREFIX;
        redisTemplate.opsForHash().delete(key, deviceId);
    }
 
    /**
     * 查询全部通道刷新
     *
     * @param deviceId
     * @return
     */
    @Override
    public List<DeviceChannel> queryAllChannelsForRefresh(String deviceId) {
        String key = VideoManagerConstants.DEVICE_CHANNEL_PREFIX;
        Object object = redisTemplate.opsForHash().get(key, deviceId);
        if (object == null) {
            return new ArrayList<>();
        }
        return (List<DeviceChannel>) object;
    }
 
    /**
     * 批量添加设备通道
     *
     * @param deviceId
     * @param subList
     */
    @Override
    public void batchAdd(String deviceId, List<DeviceChannel> subList) {
        String key = VideoManagerConstants.DEVICE_CHANNEL_PREFIX;
        redisTemplate.opsForHash().put(key, deviceId, subList);
    }
 
    /**
     * 批量修改设备通道
     *
     * @param deviceId
     * @param subList
     */
    @Override
    public void batchUpdate(String deviceId, List<DeviceChannel> subList) {
        String key = VideoManagerConstants.DEVICE_CHANNEL_PREFIX;
        redisTemplate.opsForHash().put(key, deviceId, subList);
    }
 
    /**
     * 获取所有设备
     *
     * @return 设备列表
     */
    @Override
    public List<Device> getAllDevices() {
        String key = VideoManagerConstants.DEVICE_PREFIX;
        List<Object> values = redisTemplate.opsForHash().values(key);
        List<Device> devices = new ArrayList<>();
        for (Object value : values) {
            if (value instanceof Device) {
                devices.add((Device) value);
            }
        }
        return devices;
    }
}