zhaowenyu
2026-01-06 4e8a1c02b45f72f6377603778e8c8746ed82f374
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package com.ard.vtdu.service.impl;
 
import com.ard.common.utils.DateUtils;
import com.ard.vtdu.domain.ArdVtdu;
import com.ard.vtdu.domain.Conf;
import com.ard.vtdu.mapper.ArdVtduMapper;
import com.ard.vtdu.service.IArdVtduService;
import com.ard.vtdu.service.IArdMediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
 
/**
 * 流媒体管理Service业务层处理
 *
 * @author ard
 * @date 2023-08-29
 */
@Service
@Slf4j
public class ArdVtduServiceImpl implements IArdVtduService {
    @Resource
    private ArdVtduMapper ardVtduMapper;
    @Resource
    private IArdMediaService mediaService;
 
 
 
    @PostConstruct
    public void initMediaMTX() {
        //清空mediamtx流媒体数据
        mediaService.clearAll();
    }
 
    /**
     * 查询流媒体管理
     *
     * @param name 流媒体管理主键
     * @return 流媒体管理
     */
    @Override
    public ArdVtdu selectArdVtduByName(String name) {
        return ardVtduMapper.selectArdVtduByName(name);
    }
 
    /**
     * 查询流媒体管理列表
     *
     * @param ardVtdu 流媒体管理
     * @return 流媒体管理
     */
    @Override
    public List<ArdVtdu> selectArdVtduList(ArdVtdu ardVtdu) {
        return ardVtduMapper.selectArdVtduList(ardVtdu);
    }
 
    /**
     * 新增流媒体管理
     *
     * @param ardVtdu 流媒体管理
     * @return 结果
     */
    @Override
    public int insertArdVtdu(ArdVtdu ardVtdu) {
        ardVtdu.setCreateTime(DateUtils.getNowDate());
        ardVtdu = mediaService.addPath(ardVtdu);
        return ardVtduMapper.insertArdVtdu(ardVtdu);
    }
 
    /**
     * 修改流媒体管理
     *
     * @param ardVtdu 流媒体管理
     * @return 结果
     */
    @Override
    public int updateArdVtdu(ArdVtdu ardVtdu) {
        ardVtdu.setUpdateTime(DateUtils.getNowDate());
        ardVtdu = mediaService.editPath(ardVtdu);
        ardVtdu.setUpdateTime(DateUtils.getNowDate());
        return ardVtduMapper.updateArdVtdu(ardVtdu);
    }
 
    /**
     * 批量删除流媒体管理
     *
     * @param names 需要删除的数据主键集合
     * @return 结果
     */
    @Override
    public int deleteArdVtduByNames(String[] names) {
        for (String name : names) {
            log.debug("流媒体【" + name + "】通道删除");
        }
        mediaService.removePath(names);
        return ardVtduMapper.deleteArdVtduByNames(names);
    }
 
    /**
     * 删除流媒体管理
     *
     * @param name 流媒体通道名称
     * @return 结果
     */
    @Override
    public int deleteArdVtduByName(String name) {
        mediaService.removePath(name);
        log.debug("流媒体【" + name + "】通道删除");
        return ardVtduMapper.deleteArdVtduByName(name);
    }
 
    /**
     * 删除流媒体管理信息
     *
     * @param cameraId 相机ID
     * @return 结果
     */
    @Override
    public int deleteArdVtduByCameraId(String cameraId) {
        return ardVtduMapper.deleteArdVtduByCameraId(cameraId);
    }
 
    /**
     * 清除所有流媒体数据
     *
     * @author 刘苏义
     * @date 2024/7/10 13:31
     */
    @Override
    public void clearAllArdVtdu() {
        ardVtduMapper.clearAllArdVtdu();
    }
 
    //需要更新的(流媒体和vtdu相同)
    @Override
    public List<ArdVtdu> sameList(List<ArdVtdu> vtduses, List<String> names) {
        return vtduses.stream().filter(vtdu -> names.contains(vtdu.getName())).collect(Collectors.toList());
    }
 
    //需要删除的(流媒体中多的)
    @Override
    public List<String> diffListToDel(List<ArdVtdu> vtduses, List<String> names) {
        if (vtduses.size() >= names.size()) {
            List<String> nameList = vtduses.stream().map(ArdVtdu::getName)
                    .filter(item -> !names.contains(item))
                    .collect(Collectors.toList());
            return nameList;
        } else {
            List<String> nameList =
                    names.stream().filter(item -> !vtduses.stream().map(ArdVtdu::getName).collect(Collectors.toList()).contains(item))
                    .collect(Collectors.toList());
            return nameList;
        }
    }
 
    //需要新增的(流媒体中少的)
    @Override
    public List<ArdVtdu> diffListToAdd(List<ArdVtdu> vtduses, List<String> names) {
        return vtduses.stream().filter(vtdu -> !names.contains(vtdu.getName())).collect(Collectors.toList());
    }
 
    /**
     * @return
     * @Author 刘苏义
     * @Description 同步本地vtdu库和流媒体中的数据
     * @Date 2024/7/10 15:26
     * @Param vtdus vtdu库的集合
     * @Param names 流媒体的name集合
     */
    @Override
    public void asyncMediaMTX(List<ArdVtdu> vtdus, List<String> names) {
        //需要更新的数据,参数顺序注意
        sameList(vtdus, names).forEach(vtdu -> {
            Conf conf = mediaService.getPathConfig(vtdu.getName());
            if (conf == null || conf.getSource() == null) {
                return;
            }
            String currentRtspSource = vtdu.getRtspSource();  // 提前获取需要比对的RTSP源
            String confRtspSource = conf.getSource();
 
            // 比较 RTSP 源是否一致
            boolean isRtspSourceChanged = !Objects.equals(confRtspSource, currentRtspSource);
            // 如果 RTSP 源发生变化,则更新路径
            if (isRtspSourceChanged) {
                mediaService.editPath(vtdu);
            }
        });
        //需要删除的数据
        diffListToDel(vtdus, names).forEach(name -> {
            mediaService.removePath(name);
        });
        //需要新增的数据
        diffListToAdd(vtdus, names).forEach(vtdu -> {
            mediaService.addPath(vtdu);
        });
    }
}