123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using System.Collections.Concurrent;
- using WingInterfaceLibrary.Enum;
- using WingInterfaceLibrary.LiveConsultation;
- using WingServerCommon.Interfaces.Cache;
- using WingServerCommon.Log;
- namespace WingLiveConsultationService
- {
- public class LiveConsultationRoomManager
- {
- protected ConcurrentDictionary<string, LiveConsultationRoom> _rooms = new ConcurrentDictionary<string, LiveConsultationRoom>();
- private Func<string, LiveConsultationRoom> OnCreateRoomFromDB;
- public LiveConsultationRoomManager(Func<string, LiveConsultationRoom> createRoomFromDB)
- {
- OnCreateRoomFromDB = createRoomFromDB;
- }
- public LiveConsultationRoom Get(string roomId)
- {
- return _rooms.GetValueOrDefault(roomId);
- }
- public bool Remove(string roomId)
- {
- return _rooms.TryRemove(roomId, out _);
- }
- public LiveConsultationRoom GetOrAdd(string roomId)
- {
- return _rooms.GetOrAdd(roomId, k =>
- {
- var room = OnCreateRoomFromDB.Invoke(roomId);
- Logger.WriteLineInfo($"LiveConsultationService Add room in cache, roomId:{roomId}, patientName:{room.PatientName}");
- return room;
- });
- }
- public void RefreshMemberInfosByToken(string roomId, string liveMemberId = "")
- {
- var room = Get(roomId);
- if (room != null)
- {
- var tokenManager = CacheMaintenance.Instance.Get<ITokensManager>();
- foreach (var liveMember in room.Members)
- {
- var id = liveMember.Id;
- if (!string.IsNullOrWhiteSpace(liveMemberId) && id != liveMemberId)
- {
- continue;
- }
- var userToken = tokenManager.Where(t => t.ClientId == id)?.OrderByDescending(x => x.IsOnline)?.FirstOrDefault();
- if (userToken != null)
- {
- var isBusy = IsBusy(id, roomId);
- var loginSource = (LoginSource)userToken.LoginSource;
- liveMember.LoginServerUrl = userToken.LoginServer;
- liveMember.LoginSource = (LoginSource)userToken.LoginSource;
- liveMember.IsOnline = userToken.IsOnline;
- liveMember.IsBusy = isBusy;
- }
- }
- }
- }
- public bool IsBusy(string clientId, string excludeRoomId)
- {
- var liveRoom = _rooms.Values.FirstOrDefault(r =>
- {
- if (excludeRoomId == r.RoomId)
- {
- return false;
- }
- if (r.Status == LiveConsultationRoomStatus.Connected || r.Status == LiveConsultationRoomStatus.Initiating)
- {
- if (r.Members.Any(d => d.Id == clientId && d.Status == LiveConsultationMemberStatus.Joined))
- {
- return true;
- }
- }
- return false;
- });
- return liveRoom != null;
- }
- /// <summary>
- /// 从缓存获取房间信息,通过设备code或者用户code
- /// </summary>
- /// <param name="deviceCode"></param>
- /// <param name="userCode"></param>
- public LiveConsultationRoom GetRoomInfoByConditionCode(string deviceCode, string userCode)
- {
- try
- {
- LiveConsultationRoom defaultRoom = null;
- if (_rooms?.Count > 0)
- {
- foreach (var tempRoom in _rooms.Values)
- {
- if (tempRoom != null && tempRoom != null && !string.IsNullOrEmpty(tempRoom.RoomId))
- {
- if (tempRoom.Status == LiveConsultationRoomStatus.Connected || tempRoom.Status == LiveConsultationRoomStatus.Initiating)
- {
- if (!string.IsNullOrEmpty(deviceCode) && !string.IsNullOrEmpty(userCode) &&
- tempRoom?.DeviceInfos?.Count(c => c.Id == deviceCode && c.Status == LiveConsultationMemberStatus.Joined) > 0 &&
- tempRoom?.UserInfos?.Count(c => c.Id == userCode && c.Status == LiveConsultationMemberStatus.Joined) > 0)
- {
- defaultRoom = tempRoom;
- break;
- }
- else if (!string.IsNullOrEmpty(deviceCode) && tempRoom?.DeviceInfos?.Count(c => c.Id == deviceCode && c.Status == LiveConsultationMemberStatus.Joined) > 0)
- {
- defaultRoom = tempRoom;
- break;
- }
- else if (!string.IsNullOrEmpty(userCode) && tempRoom?.UserInfos?.Count(c => c.Id == userCode && c.Status == LiveConsultationMemberStatus.Joined) > 0)
- {
- defaultRoom = tempRoom;
- break;
- }
- }
- }
- }
- }
- return defaultRoom;
- }
- catch (Exception ex)
- {
- return null;
- }
- }
- }
- }
|