DeviceService.RemoteMaintain.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using WingInterfaceLibrary.DTO.LiveRoom;
  5. using WingInterfaceLibrary.Enum;
  6. using WingInterfaceLibrary.Interface;
  7. using WingInterfaceLibrary.Internal.Request;
  8. using WingInterfaceLibrary.Notifications;
  9. using WingInterfaceLibrary.Notifications.Live;
  10. using WingInterfaceLibrary.Notifications.Remote;
  11. using WingInterfaceLibrary.Request;
  12. using WingInterfaceLibrary.Request.Remote;
  13. using WingServerCommon.Interfaces.Cache;
  14. using WingServerCommon.Service;
  15. namespace WingDeviceService.Service
  16. {
  17. /// <summary>
  18. /// 远程管理
  19. /// </summary>
  20. public partial class DeviceService : JsonRpcService, IDeviceService
  21. {
  22. #region Remote Maintain
  23. /// <summary>
  24. /// 申请连接/断开请求接口
  25. /// </summary>
  26. /// <param name="request"></param>
  27. /// <returns></returns>
  28. public async Task<bool> ApplyRemoteConnectionAsync(ControlDeviceConnectRequest request)
  29. {
  30. var req = new TokenRequest() { Token = request.Token };
  31. var result = await _authenticationService.GetTokenAsync(req) ?? new TokenDTO();
  32. if (string.IsNullOrEmpty(request.DeviceCode))
  33. {
  34. ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
  35. }
  36. var notification = new ConnectStatusToDeviceNotification()//设备通知
  37. {
  38. ControlUserCode = result.ClientId,
  39. ControlUserName = result.AccountName,
  40. ControlType = request.ControlType
  41. };
  42. BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
  43. notificationRequest.ClientIds = new List<string>() { request.DeviceCode };
  44. notificationRequest.Message = notification;
  45. notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
  46. notificationRequest.NotificationType = notification.NotificationType;
  47. notificationRequest.TransactionType = TransactionTypeEnum.RemoteControl;
  48. notificationRequest.RelevanceCode = request.DeviceCode;
  49. notificationRequest.ReceiverType = ApplicantTypeEnum.Device;
  50. var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
  51. return res;
  52. }
  53. /// <summary>
  54. /// 设备端接受连接/断开链接请求接口
  55. /// </summary>
  56. /// <param name="request"></param>
  57. /// <returns></returns>
  58. public async Task<bool> AcceptRemoteConnnectionAsync(ControlDeviceResponseRequest request)
  59. {
  60. string deviceCode = await GetClientIdByTokenAsync(request.Token);
  61. if (string.IsNullOrEmpty(request.UserCode))
  62. {
  63. ThrowCustomerException(CustomerRpcCode.UsercodeIsEmpty, "User code is empty");
  64. }
  65. var deviceInfo = CacheMaintenance.Instance.Get<IDeviceInfosManager>().Get(deviceCode);
  66. var req = new TokenRequest() { Token = request.UserCode };
  67. var result = await _authenticationService.GetTokenAsync(req) ?? new TokenDTO();
  68. //接受连接,创建房间
  69. if (request.ControlType == ControlDeviceParameterEnum.Start)
  70. {
  71. var deviceName = !string.IsNullOrWhiteSpace(deviceInfo.Name) ? deviceInfo.Name : deviceInfo.ShortCode;
  72. var deviceLiveInfo = new LiveMemberDTO
  73. {
  74. Code = deviceCode,
  75. Name = deviceName,
  76. MemberType = LiveMemberEnum.Device,
  77. HeadImageToken = deviceInfo.HeadPicUrl,
  78. Status = LiveMemberStatus.Accepted,
  79. };
  80. var room = new LiveRoomDTO
  81. {
  82. LiveRoomCode = request.UserCode + deviceCode,
  83. Name = result.AccountName,
  84. RelatedCode = request.UserCode,
  85. BusinessModule = BusinessModuleEnum.RemoteControl,
  86. Status = LiveRoomStatus.Default,
  87. UserInfos = new List<LiveMemberDTO>(),
  88. DeviceInfos = new List<LiveMemberDTO> { deviceLiveInfo }
  89. };
  90. await _rtcService.AddOrUpdateLiveRoomAsync(new AddOrUpdateLiveRoomRequest
  91. {
  92. LiveRoom = room,
  93. });
  94. await _rtcService.InitiateAsync(new InitiateRequest
  95. {
  96. LiveRoomCode = request.UserCode + deviceCode,
  97. InitiatorCode = request.UserCode,
  98. });
  99. }
  100. else if (request.ControlType == ControlDeviceParameterEnum.End)
  101. {
  102. //断开连接,关闭房间
  103. await _rtcService.CloseAsync(new CloseRequest
  104. {
  105. LiveRoomCode = request.UserCode + deviceCode,
  106. UserCode = request.UserCode,
  107. });
  108. }
  109. var notification = new ConnectStatusToClientNotification()//客户端通知
  110. {
  111. DeviceCode = result.ClientId,
  112. DeviceName = result.AccountName,
  113. ControlType = request.ControlType
  114. };
  115. BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
  116. notificationRequest.ClientIds = new List<string>() { request.UserCode };
  117. notificationRequest.Message = notification;
  118. notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
  119. notificationRequest.NotificationType = notification.NotificationType;
  120. notificationRequest.TransactionType = TransactionTypeEnum.RemoteControl;
  121. notificationRequest.RelevanceCode = request.UserCode;
  122. notificationRequest.ReceiverType = ApplicantTypeEnum.Client;
  123. var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
  124. return res;
  125. }
  126. /// <summary>
  127. /// 远程控制请求接口
  128. /// </summary>
  129. /// <param name="request"></param>
  130. /// <returns></returns>
  131. public async Task<bool> ApplyRemoteControlAsync(ControlDeviceParameterRequest request)
  132. {
  133. var result = false;
  134. if (string.IsNullOrEmpty(request.DeviceCode))
  135. {
  136. ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
  137. }
  138. var userInfo = await _authenticationService.GetTokenAsync(new TokenRequest() { Token = request.Token }) ?? new TokenDTO();
  139. string userCode = userInfo.ClientId;
  140. //获取房间缓存
  141. var roomId = userCode + request.DeviceCode;
  142. if (string.IsNullOrWhiteSpace(roomId))
  143. {
  144. ThrowCustomerException(CustomerRpcCode.LiveCodeIsEmpty, "Live code is empty");
  145. }
  146. var room = await _rtcService.GetLiveRoomAsync(new GetLiveRoomRequest { LiveRoomCode = roomId });
  147. if (room == null)
  148. {
  149. ThrowCustomerException(CustomerRpcCode.LiveRoomIsEmpty, "Live room is empty");
  150. }
  151. var existMember = room.UserInfos.FirstOrDefault(x => x.Code == userCode);
  152. if (existMember == null)
  153. {
  154. ThrowCustomerException(CustomerRpcCode.LivMemberNotFound, "Live member not found");
  155. }
  156. var deviceMember = room.DeviceInfos.FirstOrDefault(v => v.Code == request.DeviceCode);
  157. if (deviceMember == null)
  158. {
  159. ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
  160. }
  161. //检查是否用用户使用参数调节中
  162. var isControllingMember = room.UserInfos.FirstOrDefault(x => x.IsControllingParameter);
  163. if (isControllingMember != null && isControllingMember.Code != userCode)
  164. {
  165. ThrowCustomerException(CustomerRpcCode.ControllingParameterByOtherUser, "Controlling parameter by other");
  166. }
  167. var isControllingParameter = true;
  168. if (request.ControlType == ControlDeviceParameterEnum.End)
  169. {
  170. isControllingParameter = false;
  171. }
  172. if (existMember.IsControllingParameter != isControllingParameter)
  173. {
  174. await _rtcService.ChangeControllingStateAsync(new ChangeControllingStateRequest
  175. {
  176. LiveRoomCode = room.LiveRoomCode,
  177. UserCode = userCode,
  178. IsControllingParameter = isControllingParameter,
  179. });
  180. }
  181. var notification = new DeviceControlledParametersNotification()//设备通知
  182. {
  183. ControlUserCode = userCode,
  184. ControlUserName = userInfo.AccountName,
  185. ControlType = request.ControlType,
  186. Parameters = request.Parameters
  187. };
  188. BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
  189. notificationRequest.ClientIds = new List<string>() { request.DeviceCode };
  190. notificationRequest.Message = notification;
  191. notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
  192. notificationRequest.NotificationType = notification.NotificationType;
  193. notificationRequest.TransactionType = TransactionTypeEnum.RemoteControl;
  194. notificationRequest.RelevanceCode = roomId;
  195. notificationRequest.ReceiverType = ApplicantTypeEnum.Device;
  196. result = await _notificationService.BroadcastMessageAsync(notificationRequest);
  197. return result;
  198. }
  199. /// <summary>
  200. /// 获取日志请求接口
  201. /// </summary>
  202. /// <param name="request"></param>
  203. /// <returns></returns>
  204. public async Task<bool> GetRemoteLogAsync(GetRemoteLogRequest request)
  205. {
  206. string userCode = await GetClientIdByTokenAsync(request.Token);
  207. if (string.IsNullOrEmpty(request.DeviceCode))
  208. {
  209. ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
  210. }
  211. var notification = new GetRemoteLogToDeviceNotification()//设备通知
  212. {
  213. ControlUserCode = userCode,
  214. StartTime = request.StartTime,
  215. EndTime = request.EndTime
  216. };
  217. BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
  218. notificationRequest.ClientIds = new List<string>() { request.DeviceCode };
  219. notificationRequest.Message = notification;
  220. notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
  221. notificationRequest.NotificationType = notification.NotificationType;
  222. notificationRequest.TransactionType = TransactionTypeEnum.RemoteControl;
  223. notificationRequest.RelevanceCode = request.DeviceCode;
  224. notificationRequest.ReceiverType = ApplicantTypeEnum.Device;
  225. var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
  226. return res;
  227. }
  228. /// <summary>
  229. /// 设备端响应日志下载地址请求接口
  230. /// </summary>
  231. /// <param name="request"></param>
  232. /// <returns></returns>
  233. public async Task<bool> ResponseRemoteLogAsync(RemoteLogResponseRequest request)
  234. {
  235. string deviceCode = await GetClientIdByTokenAsync(request.Token);
  236. if (string.IsNullOrEmpty(request.UserCode))
  237. {
  238. ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
  239. }
  240. var notification = new GetRemoteLogToClientNotification()//设备通知
  241. {
  242. DeviceCode = deviceCode,
  243. LogFileToken = request.LogFileToken
  244. };
  245. BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
  246. notificationRequest.ClientIds = new List<string>() { request.UserCode };
  247. notificationRequest.Message = notification;
  248. notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
  249. notificationRequest.NotificationType = notification.NotificationType;
  250. notificationRequest.TransactionType = TransactionTypeEnum.RemoteControl;
  251. notificationRequest.RelevanceCode = request.UserCode;
  252. notificationRequest.ReceiverType = ApplicantTypeEnum.Client;
  253. var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
  254. return res;
  255. }
  256. #endregion
  257. }
  258. }