LiveConsultationService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using System.Collections.Generic;
  2. using System;
  3. using System.Threading.Tasks;
  4. using System.Linq;
  5. using System.Net.Http.Headers;
  6. using WingInterfaceLibrary.Interface;
  7. using WingInterfaceLibrary.Request;
  8. using WingInterfaceLibrary.Request.Authentication;
  9. using WingInterfaceLibrary.Enum;
  10. using WingInterfaceLibrary.Request.Notification;
  11. using WingInterfaceLibrary.Enum.NotificationEnum;
  12. using WingServerCommon.Service;
  13. using WingInterfaceLibrary.LiveConsultation;
  14. using WingInterfaceLibrary.DTO.Organization;
  15. using WingInterfaceLibrary.Request.Consultation;
  16. using WingInterfaceLibrary.DTO;
  17. using WingInterfaceLibrary.Result;
  18. using WingInterfaceLibrary.DTO.Consultation;
  19. using WingInterfaceLibrary.Interface.DBInterface;
  20. using WingServerCommon.Interfaces.Cache;
  21. using WingInterfaceLibrary.DB.Request;
  22. namespace WingLiveConsultationService
  23. {
  24. public class LiveConsultationService : JsonRpcService, ILiveConsultationService
  25. {
  26. private readonly VideoProtocol _videoProtocol; //Todo from config
  27. private ILiveConsultationRoomManager _realtimeDiagosisRoomManager;
  28. private IAuthenticationService _authenticationService;
  29. private INotificationService _notificationService;
  30. private IUserDBService _userDBService;
  31. private IOrganizationDBService _organizationDBService;
  32. /// <summary>
  33. /// Init service
  34. /// </summary>
  35. public override void Load(JsonRpcClientPool jsonRpcClientPool)
  36. {
  37. base.Load(jsonRpcClientPool);
  38. _authenticationService = GetProxy<IAuthenticationService>();
  39. _notificationService = GetProxy<INotificationService>();
  40. _userDBService = GetProxy<IUserDBService>();
  41. _organizationDBService = GetProxy<IOrganizationDBService>();
  42. }
  43. /// <summary>
  44. /// Dispatch message function
  45. /// </summary>
  46. public static Action<IList<RemoteNotifyTarget>, NotificationTypeEnum> DispatchMessage;
  47. /// <summary>
  48. /// Push message function
  49. /// </summary>
  50. public static Action<IList<string>, NotificationTypeEnum> PushMessage;
  51. /// <summary>
  52. /// Initiate a diagnosis meeting
  53. /// </summary>
  54. /// <param name="request">The request</param>
  55. /// <returns></returns>
  56. public async Task<InitiateLiveConsultationResult> InitiateLiveConsultationAsync(InitiateLiveConsultationRequest request)
  57. {
  58. var token = request.Token;
  59. var roomId = string.Empty;//TODO request.ConsultationCode;
  60. var otherIds = request.OtherIds;
  61. var tokenRequest = new TokenRequest() { Token = token };
  62. var tokensRequest = new GetTokenWithClientIdsRequest()
  63. {
  64. ClientIds = otherIds as List<string>
  65. };
  66. var initiateToken = await _authenticationService.GetTokenAsync(tokenRequest);
  67. var othersTokens = await _authenticationService.GetTokenWithClientIdsAsync(tokensRequest);
  68. LiveConsultationMemeber initiator = null;
  69. if (_videoProtocol == VideoProtocol.Rtc)
  70. {
  71. initiator = new LiveConsultationMemeber() { Id = initiateToken.Code, Name = initiateToken.AccountName };
  72. }
  73. else
  74. {
  75. //var channel = TODO get channel from wing rtmp service
  76. var liveData = new LiveData()
  77. {
  78. //RtmpPushUrl = TODO
  79. };
  80. initiator = new LiveConsultationMemeber()
  81. {
  82. Id = initiateToken.Code,
  83. Name = initiateToken.AccountName,
  84. LiveData = liveData
  85. };
  86. }
  87. var others = new List<LiveConsultationMemeber>();
  88. var devices = new List<LiveConsultationMemeber>();
  89. foreach (var tokenInfo in othersTokens)
  90. {
  91. var type = tokenInfo.AccountType;
  92. var liveData = new LiveData()
  93. {
  94. //RtmpPushUrl = TODO
  95. };
  96. var member = new LiveConsultationMemeber()
  97. {
  98. Id = tokenInfo.Code,
  99. Name = tokenInfo.AccountName,
  100. LiveData = liveData
  101. };
  102. if (type == AccountType.User)
  103. {
  104. others.Add(member);
  105. }
  106. else if (type == AccountType.US || type == AccountType.USBox)
  107. {
  108. devices.Add(member);
  109. }
  110. }
  111. var room = _realtimeDiagosisRoomManager.CreateRoom(roomId, initiator, others, devices);
  112. BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
  113. var ids = others.Select(x => x.Id).ToList();
  114. notificationRequest.ClientIds = ids; //TODO set message content
  115. //notificationRequest.Message = new NotifyMessage
  116. var notification = new InviteLiveConsultationNotification();
  117. //notify other guys
  118. await _notificationService.BroadcastMessageAsync(notificationRequest);
  119. var result = new InitiateLiveConsultationResult();
  120. //Start a timer for timeout process
  121. room.StartCheckConnectionTimeout(60000);//TOOD get the timeout from config
  122. return result;
  123. }
  124. /// <summary>
  125. /// Accept a diagnosis request
  126. /// </summary>
  127. /// <param name="request">The request</param>
  128. /// <returns></returns>
  129. public async Task<AcceptLiveConsultationResult> AcceptLiveConsultationAsync(AcceptLiveConsultationRequest request)
  130. {
  131. var token = request.Token;
  132. //todo roomCode change to roomId ??
  133. var roomId = request.RoomCode;
  134. var tokenRequest = new TokenRequest() { Token = token };
  135. var accepterToken = await _authenticationService.GetTokenAsync(tokenRequest);
  136. var result = new AcceptLiveConsultationResult();
  137. var room = _realtimeDiagosisRoomManager.GetRoom(roomId);
  138. if (room == null)
  139. {
  140. //TODO
  141. //ThrowRpcException()
  142. }
  143. else
  144. {
  145. //Notify other guys
  146. var others = room.Members.Where(x => x.Id != accepterToken.Code);
  147. BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
  148. var otherIds = others.Select(x => x.Id).ToList();
  149. notificationRequest.ClientIds = otherIds; //TODO set message content
  150. //notificationRequest.Message = new NotifyMessage
  151. //notify other guys
  152. await _notificationService.BroadcastMessageAsync(notificationRequest);
  153. }
  154. return result;
  155. }
  156. /// <summary>
  157. /// Reject a diagnosis request
  158. /// </summary>
  159. /// <param name="request">The request</param>
  160. /// <returns></returns>
  161. public Task<RejectLiveConsultationResult> RejectLiveConsultationAsync(RejectLiveConsultationRequest request)
  162. {
  163. throw new NotImplementedException();
  164. }
  165. /// <summary>
  166. /// Join a diagnosis request
  167. /// </summary>
  168. /// <param name="request">The request</param>
  169. /// <returns></returns>
  170. public Task<JoinLiveConsultationResult> JoinLiveConsultationAsync(JoinLiveConsultationRequest request)
  171. {
  172. throw new NotImplementedException();
  173. }
  174. /// <summary>
  175. /// Leave a diagnosis request
  176. /// </summary>
  177. /// <param name="request">The request</param>
  178. /// <returns></returns>
  179. public Task<LeaveLiveConsultationResult> LeaveLiveConsultationAsync(LeaveLiveConsultationRequest request)
  180. {
  181. throw new NotImplementedException();
  182. }
  183. /// <summary>
  184. /// Send HeartRate request in a diagnosis metting
  185. /// </summary>
  186. /// <param name="request">The request</param>
  187. /// <returns></returns>
  188. public Task<LiveConsultationHeartRateResult> HeartRateAsync(LiveConsultationHeartRateRequest request)
  189. {
  190. throw new NotImplementedException();
  191. }
  192. private void ThrowRpcException(CustomerRpcCode code, string message, string internalMessage = null)
  193. {
  194. base.ThrowRpcException((int)code, message, internalMessage ?? message);
  195. }
  196. public async Task<List<OrganizationBaseDTO>> FindParentOrganizationsAsync(FindHigherOrganizationsRequest request)
  197. {
  198. var userCode = await GetClientIdByTokenAsync(request.Token);
  199. var userInfoDO = await _userDBService.FindUserByCodeAsync(userCode);
  200. var result = new List<OrganizationBaseDTO>
  201. {
  202. };
  203. if (userInfoDO != null)
  204. {
  205. var organization = CacheMaintenance.Instance.Get<IOrganizationsManager>().Get(userInfoDO.RootOrganizationCode);
  206. var organizationList = await _organizationDBService.GetOrganizationListAsync(new GetOrganizationListDBRequest
  207. {
  208. OrganizationType = OrganizationTypeEnum.Corporation,
  209. Isinvented = false,
  210. });
  211. organizationList = organizationList.Where(x => x.OrganizationCode != userInfoDO.RootOrganizationCode)?.ToList() ?? new List<OrganizationDTO>();
  212. result = organizationList.Select(c => new OrganizationBaseDTO()
  213. {
  214. OrganizationCode = c.OrganizationCode,
  215. OrganizationName = c.OrganizationName,
  216. }).ToList();
  217. }
  218. return result;
  219. }
  220. public Task<List<OrganizationBaseDTO>> FindGrassRootsOrganizationsAsync(FindGrassRootsOrganizationsRequest request)
  221. {
  222. throw new NotImplementedException();
  223. }
  224. public Task<List<BaseDTO>> FindOrganizationExpertsAsync(FindOrganizationExpertsRequest request)
  225. {
  226. throw new NotImplementedException();
  227. }
  228. public Task<string> ApplyConsultationAsync(ApplyConsultationRequest request)
  229. {
  230. throw new NotImplementedException();
  231. }
  232. public Task<PageResult<ConsultationDetailDTO>> FindConsultationByPageAsync(FindConsultationByPageRequest request)
  233. {
  234. throw new NotImplementedException();
  235. }
  236. public Task<ConsultationDetailDTO> FindConsultationDetailAsync(FindConsultationDetailRequest request)
  237. {
  238. throw new NotImplementedException();
  239. }
  240. public Task<bool> ApprovalConsultationAsync(ApprovalConsultationRequest request)
  241. {
  242. throw new NotImplementedException();
  243. }
  244. public Task<bool> RevokeConsultationAsync(RevokeConsultationRequest request)
  245. {
  246. throw new NotImplementedException();
  247. }
  248. public Task<bool> DeleteConsultationAsync(DeleteConsultationRequest request)
  249. {
  250. throw new NotImplementedException();
  251. }
  252. public Task<bool> UploadConsultationFilesAsync(UploadConsultationFilesRequest request)
  253. {
  254. throw new NotImplementedException();
  255. }
  256. public Task<List<ConsultationFileDTO>> FindConsultationFilesAsync(FindConsultationFilesRequest request)
  257. {
  258. throw new NotImplementedException();
  259. }
  260. public Task<CancelLiveConsultationResult> InitiateLiveConsultationAsync(CancelLiveConsultationRequest request)
  261. {
  262. throw new NotImplementedException();
  263. }
  264. private async Task<string> GetClientIdByTokenAsync(string token)
  265. {
  266. var request = new TokenRequest() { Token = token };
  267. var result = await _authenticationService.GetTokenAsync(request);
  268. return result.ClientId; //return User code
  269. }
  270. }
  271. }