648540858
2022-11-18 ecf84bb0f1952c4a044ff6c8aa18226b31593f3f
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
package com.genersoft.iot.vmp.gb28181.session;
 
import com.genersoft.iot.vmp.conf.SipConfig;
import com.genersoft.iot.vmp.gb28181.bean.AudioBroadcastCatch;
import com.genersoft.iot.vmp.gb28181.utils.SipUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * 语音广播消息管理类
 * @author lin
 */
@Component
public class AudioBroadcastManager {
 
    @Autowired
    private SipConfig config;
 
    public static Map<String, AudioBroadcastCatch> data = new ConcurrentHashMap<>();
 
    public void add(AudioBroadcastCatch audioBroadcastCatch) {
        this.update(audioBroadcastCatch);
    }
 
    public void update(AudioBroadcastCatch audioBroadcastCatch) {
        if (SipUtils.isFrontEnd(audioBroadcastCatch.getDeviceId())) {
            data.put(audioBroadcastCatch.getDeviceId(), audioBroadcastCatch);
        }else {
            data.put(audioBroadcastCatch.getDeviceId() + audioBroadcastCatch.getChannelId(), audioBroadcastCatch);
        }
    }
 
    public void del(String deviceId, String channelId) {
        if (SipUtils.isFrontEnd(deviceId)) {
            data.remove(deviceId);
        }else {
            data.remove(deviceId + channelId);
        }
 
    }
 
    public void delByDeviceId(String deviceId) {
        for (String key : data.keySet()) {
            if (key.startsWith(deviceId)) {
                data.remove(key);
            }
        }
    }
 
    public List<AudioBroadcastCatch> getAll(){
        Collection<AudioBroadcastCatch> values = data.values();
        return new ArrayList<>(values);
    }
 
 
    public boolean exit(String deviceId, String channelId) {
        for (String key : data.keySet()) {
            if (SipUtils.isFrontEnd(deviceId)) {
                return key.equals(deviceId);
            }else {
                return key.equals(deviceId + channelId);
            }
        }
        return false;
    }
 
    public AudioBroadcastCatch get(String deviceId, String channelId) {
        AudioBroadcastCatch audioBroadcastCatch;
        if (SipUtils.isFrontEnd(deviceId)) {
            audioBroadcastCatch = data.get(deviceId);
        }else {
            audioBroadcastCatch = data.get(deviceId + channelId);
        }
        if (audioBroadcastCatch == null) {
            Stream<AudioBroadcastCatch> allAudioBroadcastCatchStreamForDevice = data.values().stream().filter(
                    audioBroadcastCatchItem -> Objects.equals(audioBroadcastCatchItem.getDeviceId(), deviceId));
            List<AudioBroadcastCatch> audioBroadcastCatchList = allAudioBroadcastCatchStreamForDevice.collect(Collectors.toList());
            if (audioBroadcastCatchList.size() == 1 && Objects.equals(config.getId(), channelId)) {
                audioBroadcastCatch = audioBroadcastCatchList.get(0);
            }
        }
 
        return audioBroadcastCatch;
    }
}