fly 2 years ago
parent
commit
ee09392a87
1 changed files with 264 additions and 0 deletions
  1. 264 0
      Service/DeviceService.RemoteMaintain.cs

+ 264 - 0
Service/DeviceService.RemoteMaintain.cs

@@ -0,0 +1,264 @@
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using WingInterfaceLibrary.DTO.LiveRoom;
+using WingInterfaceLibrary.Enum;
+using WingInterfaceLibrary.Interface;
+using WingInterfaceLibrary.Internal.Request;
+using WingInterfaceLibrary.Notifications;
+using WingInterfaceLibrary.Notifications.Live;
+using WingInterfaceLibrary.Notifications.Remote;
+using WingInterfaceLibrary.Request;
+using WingInterfaceLibrary.Request.Remote;
+using WingServerCommon.Interfaces.Cache;
+using WingServerCommon.Service;
+
+namespace WingDeviceService.Service
+{
+    /// <summary>
+    /// 远程管理
+    /// </summary>
+    public partial class DeviceService : JsonRpcService, IDeviceService
+    {
+        #region Remote Maintain
+        /// <summary>
+        /// 申请连接/断开请求接口
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        public async Task<bool> ApplyRemoteConnectionAsync(ControlDeviceConnectRequest request)
+        {
+            var req = new TokenRequest() { Token = request.Token };
+            var result = await _authenticationService.GetTokenAsync(req) ?? new TokenDTO();
+            if (string.IsNullOrEmpty(request.DeviceCode))
+            {
+                ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
+            }
+            var notification = new ConnectStatusToDeviceNotification()//设备通知
+            {
+                ControlUserCode = result.ClientId,
+                ControlUserName = result.AccountName,
+                ControlType = request.ControlType
+            };
+            BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
+            notificationRequest.ClientIds = new List<string>() { request.DeviceCode };
+            notificationRequest.Message = notification;
+            notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
+            notificationRequest.NotificationType = notification.NotificationType;
+            notificationRequest.TransactionType = TransactionTypeEnum.RemoteControl;
+            notificationRequest.RelevanceCode = request.DeviceCode;
+            notificationRequest.ReceiverType = ApplicantTypeEnum.Device;
+            var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
+            return res;
+        }
+
+        /// <summary>
+        /// 设备端接受连接/断开链接请求接口
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        public async Task<bool> AcceptRemoteConnnectionAsync(ControlDeviceResponseRequest request)
+        {
+            string deviceCode = await GetClientIdByTokenAsync(request.Token);
+            if (string.IsNullOrEmpty(request.UserCode))
+            {
+                ThrowCustomerException(CustomerRpcCode.UsercodeIsEmpty, "User code is empty");
+            }
+            var deviceInfo = CacheMaintenance.Instance.Get<IDeviceInfosManager>().Get(deviceCode);
+            var req = new TokenRequest() { Token = request.UserCode };
+            var result = await _authenticationService.GetTokenAsync(req) ?? new TokenDTO();
+            //接受连接,创建房间
+            if (request.ControlType == ControlDeviceParameterEnum.Start)
+            {
+                var deviceName = !string.IsNullOrWhiteSpace(deviceInfo.Name) ? deviceInfo.Name : deviceInfo.ShortCode;
+                var deviceLiveInfo = new LiveMemberDTO
+                {
+                    Code = deviceCode,
+                    Name = deviceName,
+                    MemberType = LiveMemberEnum.Device,
+                    HeadImageToken = deviceInfo.HeadPicUrl,
+                    Status = LiveMemberStatus.Accepted,
+                };
+                var room = new LiveRoomDTO
+                {
+                    LiveRoomCode = request.UserCode + deviceCode,
+                    Name = result.AccountName,
+                    RelatedCode = request.UserCode,
+                    BusinessModule = BusinessModuleEnum.RemoteControl,
+                    Status = LiveRoomStatus.Default,
+                    UserInfos = new List<LiveMemberDTO>(),
+                    DeviceInfos = new List<LiveMemberDTO> { deviceLiveInfo }
+                };
+                await _rtcService.AddOrUpdateLiveRoomAsync(new AddOrUpdateLiveRoomRequest
+                {
+                    LiveRoom = room,
+                });
+                await _rtcService.InitiateAsync(new InitiateRequest
+                {
+                    LiveRoomCode = request.UserCode + deviceCode,
+                    InitiatorCode = request.UserCode,
+                });
+            }
+            else if (request.ControlType == ControlDeviceParameterEnum.End)
+            {
+                //断开连接,关闭房间
+                await _rtcService.CloseAsync(new CloseRequest
+                {
+                    LiveRoomCode = request.UserCode + deviceCode,
+                    UserCode = request.UserCode,
+                });
+            }
+            var notification = new ConnectStatusToClientNotification()//客户端通知
+            {
+                DeviceCode = result.ClientId,
+                DeviceName = result.AccountName,
+                ControlType = request.ControlType
+            };
+            BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
+            notificationRequest.ClientIds = new List<string>() { request.UserCode };
+            notificationRequest.Message = notification;
+            notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
+            notificationRequest.NotificationType = notification.NotificationType;
+            notificationRequest.TransactionType = TransactionTypeEnum.RemoteControl;
+            notificationRequest.RelevanceCode = request.UserCode;
+            notificationRequest.ReceiverType = ApplicantTypeEnum.Client;
+            var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
+            return res;
+        }
+
+        /// <summary>
+        /// 远程控制请求接口
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        public async Task<bool> ApplyRemoteControlAsync(ControlDeviceParameterRequest request)
+        {
+            var result = false;
+            if (string.IsNullOrEmpty(request.DeviceCode))
+            {
+                ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
+            }
+            var userInfo = await _authenticationService.GetTokenAsync(new TokenRequest() { Token = request.Token }) ?? new TokenDTO();
+            string userCode = userInfo.ClientId;
+            //获取房间缓存
+            var roomId = userCode + request.DeviceCode;
+            if (string.IsNullOrWhiteSpace(roomId))
+            {
+                ThrowCustomerException(CustomerRpcCode.LiveCodeIsEmpty, "Live code is empty");
+            }
+            var room = await _rtcService.GetLiveRoomAsync(new GetLiveRoomRequest { LiveRoomCode = roomId });
+            if (room == null)
+            {
+                ThrowCustomerException(CustomerRpcCode.LiveRoomIsEmpty, "Live room is empty");
+            }
+            var existMember = room.UserInfos.FirstOrDefault(x => x.Code == userCode);
+            if (existMember == null)
+            {
+                ThrowCustomerException(CustomerRpcCode.LivMemberNotFound, "Live member not found");
+            }
+            
+            var deviceMember = room.DeviceInfos.FirstOrDefault(v => v.Code == request.DeviceCode);
+            if (deviceMember == null)
+            {
+                ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
+            }
+            //检查是否用用户使用参数调节中
+            var isControllingMember = room.UserInfos.FirstOrDefault(x => x.IsControllingParameter);
+            if (isControllingMember != null && isControllingMember.Code != userCode)
+            {
+                ThrowCustomerException(CustomerRpcCode.ControllingParameterByOtherUser, "Controlling parameter by other");
+            }
+            var isControllingParameter = true;
+            if (request.ControlType == ControlDeviceParameterEnum.End)
+            {
+                isControllingParameter = false;
+            }
+            if (existMember.IsControllingParameter != isControllingParameter)
+            {
+                await _rtcService.ChangeControllingStateAsync(new ChangeControllingStateRequest
+                {
+                    LiveRoomCode = room.LiveRoomCode,
+                    UserCode = userCode,
+                    IsControllingParameter = isControllingParameter,
+                });
+            }
+            var notification = new DeviceControlledParametersNotification()//设备通知
+            {
+                ControlUserCode = userCode,
+                ControlUserName = userInfo.AccountName,
+                ControlType = request.ControlType,
+                Parameters = request.Parameters
+            };
+            BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
+            notificationRequest.ClientIds = new List<string>() { request.DeviceCode };
+            notificationRequest.Message = notification;
+            notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
+            notificationRequest.NotificationType = notification.NotificationType;
+            notificationRequest.TransactionType = TransactionTypeEnum.RemoteControl;
+            notificationRequest.RelevanceCode = roomId;
+            notificationRequest.ReceiverType = ApplicantTypeEnum.Device;
+            result = await _notificationService.BroadcastMessageAsync(notificationRequest);
+            return result;
+        }
+
+        /// <summary>
+        /// 获取日志请求接口
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        public async Task<bool> GetRemoteLogAsync(GetRemoteLogRequest request)
+        {
+            string userCode = await GetClientIdByTokenAsync(request.Token);
+            if (string.IsNullOrEmpty(request.DeviceCode))
+            {
+                ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
+            }
+            var notification = new GetRemoteLogToDeviceNotification()//设备通知
+            {
+                ControlUserCode = userCode,
+                StartTime = request.StartTime,
+                EndTime = request.EndTime
+            };
+            BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
+            notificationRequest.ClientIds = new List<string>() { request.DeviceCode };
+            notificationRequest.Message = notification;
+            notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
+            notificationRequest.NotificationType = notification.NotificationType;
+            notificationRequest.TransactionType = TransactionTypeEnum.RemoteControl;
+            notificationRequest.RelevanceCode = request.DeviceCode;
+            notificationRequest.ReceiverType = ApplicantTypeEnum.Device;
+            var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
+            return res;
+        }
+
+        /// <summary>
+        /// 设备端响应日志下载地址请求接口
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        public async Task<bool> ResponseRemoteLogAsync(RemoteLogResponseRequest request)
+        {
+            string deviceCode = await GetClientIdByTokenAsync(request.Token);
+            if (string.IsNullOrEmpty(request.UserCode))
+            {
+                ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
+            }
+            var notification = new GetRemoteLogToClientNotification()//设备通知
+            {
+                DeviceCode = deviceCode,
+                LogFileToken = request.LogFileToken
+            };
+            BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
+            notificationRequest.ClientIds = new List<string>() { request.UserCode };
+            notificationRequest.Message = notification;
+            notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
+            notificationRequest.NotificationType = notification.NotificationType;
+            notificationRequest.TransactionType = TransactionTypeEnum.RemoteControl;
+            notificationRequest.RelevanceCode = request.UserCode;
+            notificationRequest.ReceiverType = ApplicantTypeEnum.Client;
+            var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
+            return res;
+        }
+        #endregion 
+    }
+}