123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- using System.Collections.Generic;
- using System;
- using System.Threading.Tasks;
- using System.Linq;
- using System.Net.Http.Headers;
- using WingInterfaceLibrary.Interface;
- using WingInterfaceLibrary.Request;
- using WingInterfaceLibrary.Request.Authentication;
- using WingInterfaceLibrary.Enum;
- using WingInterfaceLibrary.Request.Notification;
- using WingInterfaceLibrary.Enum.NotificationEnum;
- using WingServerCommon.Service;
- using WingInterfaceLibrary.LiveConsultation;
- using WingInterfaceLibrary.DTO.Organization;
- using WingInterfaceLibrary.Request.Consultation;
- using WingInterfaceLibrary.DTO;
- using WingInterfaceLibrary.Result;
- using WingInterfaceLibrary.DTO.Consultation;
- using WingInterfaceLibrary.Interface.DBInterface;
- using WingServerCommon.Interfaces.Cache;
- using WingInterfaceLibrary.DB.Request;
- namespace WingLiveConsultationService
- {
- public class LiveConsultationService : JsonRpcService, ILiveConsultationService
- {
- private readonly VideoProtocol _videoProtocol; //Todo from config
- private ILiveConsultationRoomManager _realtimeDiagosisRoomManager;
- private IAuthenticationService _authenticationService;
- private INotificationService _notificationService;
- private IUserDBService _userDBService;
- private IOrganizationDBService _organizationDBService;
- /// <summary>
- /// Init service
- /// </summary>
- public override void Load(JsonRpcClientPool jsonRpcClientPool)
- {
- base.Load(jsonRpcClientPool);
- _authenticationService = GetProxy<IAuthenticationService>();
- _notificationService = GetProxy<INotificationService>();
- _userDBService = GetProxy<IUserDBService>();
- _organizationDBService = GetProxy<IOrganizationDBService>();
- }
- /// <summary>
- /// Dispatch message function
- /// </summary>
- public static Action<IList<RemoteNotifyTarget>, NotificationTypeEnum> DispatchMessage;
- /// <summary>
- /// Push message function
- /// </summary>
- public static Action<IList<string>, NotificationTypeEnum> PushMessage;
- /// <summary>
- /// Initiate a diagnosis meeting
- /// </summary>
- /// <param name="request">The request</param>
- /// <returns></returns>
- public async Task<InitiateLiveConsultationResult> InitiateLiveConsultationAsync(InitiateLiveConsultationRequest request)
- {
- var token = request.Token;
- var roomId = string.Empty;//TODO request.ConsultationCode;
- var otherIds = request.OtherIds;
- var tokenRequest = new TokenRequest() { Token = token };
- var tokensRequest = new GetTokenWithClientIdsRequest()
- {
- ClientIds = otherIds as List<string>
- };
- var initiateToken = await _authenticationService.GetTokenAsync(tokenRequest);
- var othersTokens = await _authenticationService.GetTokenWithClientIdsAsync(tokensRequest);
- LiveConsultationMemeber initiator = null;
- if (_videoProtocol == VideoProtocol.Rtc)
- {
- initiator = new LiveConsultationMemeber() { Id = initiateToken.Code, Name = initiateToken.AccountName };
- }
- else
- {
- //var channel = TODO get channel from wing rtmp service
- var liveData = new LiveData()
- {
- //RtmpPushUrl = TODO
- };
- initiator = new LiveConsultationMemeber()
- {
- Id = initiateToken.Code,
- Name = initiateToken.AccountName,
- LiveData = liveData
- };
- }
- var others = new List<LiveConsultationMemeber>();
- var devices = new List<LiveConsultationMemeber>();
- foreach (var tokenInfo in othersTokens)
- {
- var type = tokenInfo.AccountType;
- var liveData = new LiveData()
- {
- //RtmpPushUrl = TODO
- };
- var member = new LiveConsultationMemeber()
- {
- Id = tokenInfo.Code,
- Name = tokenInfo.AccountName,
- LiveData = liveData
- };
- if (type == AccountType.User)
- {
- others.Add(member);
- }
- else if (type == AccountType.US || type == AccountType.USBox)
- {
- devices.Add(member);
- }
- }
- var room = _realtimeDiagosisRoomManager.CreateRoom(roomId, initiator, others, devices);
- BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
- var ids = others.Select(x => x.Id).ToList();
- notificationRequest.ClientIds = ids; //TODO set message content
- //notificationRequest.Message = new NotifyMessage
- var notification = new InviteLiveConsultationNotification();
- //notify other guys
- await _notificationService.BroadcastMessageAsync(notificationRequest);
- var result = new InitiateLiveConsultationResult();
- //Start a timer for timeout process
- room.StartCheckConnectionTimeout(60000);//TOOD get the timeout from config
- return result;
- }
- /// <summary>
- /// Accept a diagnosis request
- /// </summary>
- /// <param name="request">The request</param>
- /// <returns></returns>
- public async Task<AcceptLiveConsultationResult> AcceptLiveConsultationAsync(AcceptLiveConsultationRequest request)
- {
- var token = request.Token;
- //todo roomCode change to roomId ??
- var roomId = request.RoomCode;
- var tokenRequest = new TokenRequest() { Token = token };
- var accepterToken = await _authenticationService.GetTokenAsync(tokenRequest);
- var result = new AcceptLiveConsultationResult();
- var room = _realtimeDiagosisRoomManager.GetRoom(roomId);
- if (room == null)
- {
- //TODO
- //ThrowRpcException()
- }
- else
- {
- //Notify other guys
- var others = room.Members.Where(x => x.Id != accepterToken.Code);
- BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
- var otherIds = others.Select(x => x.Id).ToList();
- notificationRequest.ClientIds = otherIds; //TODO set message content
- //notificationRequest.Message = new NotifyMessage
- //notify other guys
- await _notificationService.BroadcastMessageAsync(notificationRequest);
- }
- return result;
- }
- /// <summary>
- /// Reject a diagnosis request
- /// </summary>
- /// <param name="request">The request</param>
- /// <returns></returns>
- public Task<RejectLiveConsultationResult> RejectLiveConsultationAsync(RejectLiveConsultationRequest request)
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// Join a diagnosis request
- /// </summary>
- /// <param name="request">The request</param>
- /// <returns></returns>
- public Task<JoinLiveConsultationResult> JoinLiveConsultationAsync(JoinLiveConsultationRequest request)
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// Leave a diagnosis request
- /// </summary>
- /// <param name="request">The request</param>
- /// <returns></returns>
- public Task<LeaveLiveConsultationResult> LeaveLiveConsultationAsync(LeaveLiveConsultationRequest request)
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// Send HeartRate request in a diagnosis metting
- /// </summary>
- /// <param name="request">The request</param>
- /// <returns></returns>
- public Task<LiveConsultationHeartRateResult> HeartRateAsync(LiveConsultationHeartRateRequest request)
- {
- throw new NotImplementedException();
- }
- private void ThrowRpcException(CustomerRpcCode code, string message, string internalMessage = null)
- {
- base.ThrowRpcException((int)code, message, internalMessage ?? message);
- }
- public async Task<List<OrganizationBaseDTO>> FindParentOrganizationsAsync(FindHigherOrganizationsRequest request)
- {
- var userCode = await GetClientIdByTokenAsync(request.Token);
- var userInfoDO = await _userDBService.FindUserByCodeAsync(userCode);
- var result = new List<OrganizationBaseDTO>
- {
- };
- if (userInfoDO != null)
- {
- var organization = CacheMaintenance.Instance.Get<IOrganizationsManager>().Get(userInfoDO.RootOrganizationCode);
- var organizationList = await _organizationDBService.GetOrganizationListAsync(new GetOrganizationListDBRequest
- {
- OrganizationType = OrganizationTypeEnum.Corporation,
- Isinvented = false,
- });
- organizationList = organizationList.Where(x => x.OrganizationCode != userInfoDO.RootOrganizationCode)?.ToList() ?? new List<OrganizationDTO>();
- result = organizationList.Select(c => new OrganizationBaseDTO()
- {
- OrganizationCode = c.OrganizationCode,
- OrganizationName = c.OrganizationName,
- }).ToList();
- }
- return result;
- }
- public Task<List<OrganizationBaseDTO>> FindGrassRootsOrganizationsAsync(FindGrassRootsOrganizationsRequest request)
- {
- throw new NotImplementedException();
- }
- public Task<List<BaseDTO>> FindOrganizationExpertsAsync(FindOrganizationExpertsRequest request)
- {
- throw new NotImplementedException();
- }
- public Task<string> ApplyConsultationAsync(ApplyConsultationRequest request)
- {
- throw new NotImplementedException();
- }
- public Task<PageResult<ConsultationDetailDTO>> FindConsultationByPageAsync(FindConsultationByPageRequest request)
- {
- throw new NotImplementedException();
- }
- public Task<ConsultationDetailDTO> FindConsultationDetailAsync(FindConsultationDetailRequest request)
- {
- throw new NotImplementedException();
- }
- public Task<bool> ApprovalConsultationAsync(ApprovalConsultationRequest request)
- {
- throw new NotImplementedException();
- }
- public Task<bool> RevokeConsultationAsync(RevokeConsultationRequest request)
- {
- throw new NotImplementedException();
- }
- public Task<bool> DeleteConsultationAsync(DeleteConsultationRequest request)
- {
- throw new NotImplementedException();
- }
- public Task<bool> UploadConsultationFilesAsync(UploadConsultationFilesRequest request)
- {
- throw new NotImplementedException();
- }
- public Task<List<ConsultationFileDTO>> FindConsultationFilesAsync(FindConsultationFilesRequest request)
- {
- throw new NotImplementedException();
- }
- public Task<CancelLiveConsultationResult> InitiateLiveConsultationAsync(CancelLiveConsultationRequest request)
- {
- throw new NotImplementedException();
- }
- private async Task<string> GetClientIdByTokenAsync(string token)
- {
- var request = new TokenRequest() { Token = token };
- var result = await _authenticationService.GetTokenAsync(request);
- return result.ClientId; //return User code
- }
- }
- }
|