123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- 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
- }
- }
|