AudioBroadcastManager.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package com.genersoft.iot.vmp.gb28181.session;
  2. import com.genersoft.iot.vmp.conf.SipConfig;
  3. import com.genersoft.iot.vmp.gb28181.bean.AudioBroadcastCatch;
  4. import com.genersoft.iot.vmp.gb28181.utils.SipUtils;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Component;
  7. import java.util.*;
  8. import java.util.concurrent.ConcurrentHashMap;
  9. import java.util.stream.Collectors;
  10. import java.util.stream.Stream;
  11. /**
  12. * 语音广播消息管理类
  13. * @author lin
  14. */
  15. @Component
  16. public class AudioBroadcastManager {
  17. @Autowired
  18. private SipConfig config;
  19. public static Map<String, AudioBroadcastCatch> data = new ConcurrentHashMap<>();
  20. public void update(AudioBroadcastCatch audioBroadcastCatch) {
  21. if (SipUtils.isFrontEnd(audioBroadcastCatch.getDeviceId())) {
  22. data.put(audioBroadcastCatch.getDeviceId(), audioBroadcastCatch);
  23. }else {
  24. data.put(audioBroadcastCatch.getDeviceId() + audioBroadcastCatch.getChannelId(), audioBroadcastCatch);
  25. }
  26. }
  27. public void del(String deviceId, String channelId) {
  28. if (SipUtils.isFrontEnd(deviceId)) {
  29. data.remove(deviceId);
  30. }else {
  31. data.remove(deviceId + channelId);
  32. }
  33. }
  34. public void delByDeviceId(String deviceId) {
  35. for (String key : data.keySet()) {
  36. if (key.startsWith(deviceId)) {
  37. data.remove(key);
  38. }
  39. }
  40. }
  41. public List<AudioBroadcastCatch> getAll(){
  42. Collection<AudioBroadcastCatch> values = data.values();
  43. return new ArrayList<>(values);
  44. }
  45. public boolean exit(String deviceId, String channelId) {
  46. for (String key : data.keySet()) {
  47. if (SipUtils.isFrontEnd(deviceId)) {
  48. return key.equals(deviceId);
  49. }else {
  50. return key.equals(deviceId + channelId);
  51. }
  52. }
  53. return false;
  54. }
  55. public AudioBroadcastCatch get(String deviceId, String channelId) {
  56. AudioBroadcastCatch audioBroadcastCatch;
  57. if (SipUtils.isFrontEnd(deviceId)) {
  58. audioBroadcastCatch = data.get(deviceId);
  59. }else {
  60. audioBroadcastCatch = data.get(deviceId + channelId);
  61. }
  62. if (audioBroadcastCatch == null) {
  63. Stream<AudioBroadcastCatch> allAudioBroadcastCatchStreamForDevice = data.values().stream().filter(
  64. audioBroadcastCatchItem -> Objects.equals(audioBroadcastCatchItem.getDeviceId(), deviceId));
  65. List<AudioBroadcastCatch> audioBroadcastCatchList = allAudioBroadcastCatchStreamForDevice.collect(Collectors.toList());
  66. if (audioBroadcastCatchList.size() == 1 && Objects.equals(config.getId(), channelId)) {
  67. audioBroadcastCatch = audioBroadcastCatchList.get(0);
  68. }
  69. }
  70. return audioBroadcastCatch;
  71. }
  72. public List<AudioBroadcastCatch> get(String deviceId) {
  73. List<AudioBroadcastCatch> audioBroadcastCatchList= new ArrayList<>();
  74. if (SipUtils.isFrontEnd(deviceId)) {
  75. audioBroadcastCatchList.add(data.get(deviceId));
  76. }else {
  77. for (String key : data.keySet()) {
  78. if (key.startsWith(deviceId)) {
  79. audioBroadcastCatchList.add(data.get(key));
  80. }
  81. }
  82. }
  83. return audioBroadcastCatchList;
  84. }
  85. }