denny hace 2 años
padre
commit
51693f1fa3
Se han modificado 1 ficheros con 215 adiciones y 183 borrados
  1. 215 183
      Service/NotificationService.cs

+ 215 - 183
Service/NotificationService.cs

@@ -91,7 +91,7 @@ namespace WingNotificationModule.Service
             }
             catch (Exception ex)
             {
-                Logger.WriteLineWarn($"NotificationService ApplyLock err, lock key:{lockKey}, ex:{ex}");
+                Logger.WriteLineError($"NotificationService ApplyLock err, lock key:{lockKey}, ex:{ex}");
                 return false;
             }
         }
@@ -108,7 +108,7 @@ namespace WingNotificationModule.Service
             }
             catch (Exception ex)
             {
-                Logger.WriteLineWarn($"NotificationService ReleaseLock err, release key:{lockUniqueCode}, ex:{ex}");
+                Logger.WriteLineError($"NotificationService ReleaseLock err, release key:{lockUniqueCode}, ex:{ex}");
                 return false;
             }
         }
@@ -121,40 +121,48 @@ namespace WingNotificationModule.Service
         /// <value>true</value>
         public async Task<bool> SendMessageAsync(SendNotificationRequest request)
         {
-            var res = await AddMessageInfoAsync(request);
-            if (res)
+            try
             {
-                IList<TokenDTO> tokens;
-                if (request.Tokens != null && request.Tokens.Count > 0)
+                var res = await AddMessageInfoAsync(request);
+                if (res)
                 {
-                    tokens = request.Tokens;
+                    IList<TokenDTO> tokens;
+                    if (request.Tokens != null && request.Tokens.Count > 0)
+                    {
+                        tokens = request.Tokens;
+                    }
+                    else
+                    {
+                        tokens = await _authenticationService.GetTokensWithClientIdAsync(new GetTokensWithClientIdRequest() { ClientId = request.ClientId });
+                    }
+                    var localTokens = tokens.Where(v => v.LoginServer == ConfigurationManager.Host).ToList();
+                    var result = await _notificationServer.SendMessageAsync(localTokens, request);
+                    var otherTokens = tokens.Where(v => v.LoginServer != ConfigurationManager.Host).ToList();
+                    if (otherTokens != null && otherTokens.Count > 0)
+                    {
+                        var notificationRequest = new SyncNotification()
+                        {
+                            Clients = otherTokens.Select(v => new NotificationClientInfo()
+                            {
+                                ClientId = v.ClientId,
+                                LoginServerUrl = v.LoginServer
+                            }).ToList(),
+                            Message = request.Message,
+                            WSConnectType = request.WSConnectType
+                        };
+                        await SyncToMasterAsync(SyncTypeEnum.Notification, notificationRequest);
+                    }
+                    return result;
                 }
                 else
                 {
-                    tokens = await _authenticationService.GetTokensWithClientIdAsync(new GetTokensWithClientIdRequest() { ClientId = request.ClientId });
-                }
-                var localTokens = tokens.Where(v => v.LoginServer == ConfigurationManager.Host).ToList();
-                var result = await _notificationServer.SendMessageAsync(localTokens, request);
-                var otherTokens = tokens.Where(v => v.LoginServer != ConfigurationManager.Host).ToList();
-                if (otherTokens != null && otherTokens.Count > 0)
-                {
-                    var notificationRequest = new SyncNotification()
-                    {
-                        Clients = otherTokens.Select(v => new NotificationClientInfo()
-                        {
-                            ClientId = v.ClientId,
-                            LoginServerUrl = v.LoginServer
-                        }).ToList(),
-                        Message = request.Message,
-                        WSConnectType = request.WSConnectType
-                    };
-                    await SyncToMasterAsync(SyncTypeEnum.Notification, notificationRequest);
+                    return res;
                 }
-                return result;
             }
-            else
+            catch (Exception ex)
             {
-                return res;
+                Logger.WriteLineError($"NotificationService SendMessageAsync RelevanceCode: {request.RelevanceCode}, err: {ex}");
+                return false;
             }
         }
 
@@ -166,40 +174,48 @@ namespace WingNotificationModule.Service
         /// <value>true</value>
         public async Task<bool> PostMessageAsync(SendNotificationRequest request)
         {
-            var res = await AddMessageInfoAsync(request);
-            if (res)
+            try
             {
-                IList<TokenDTO> tokens;
-                if (request.Tokens != null && request.Tokens.Count > 0)
+                var res = await AddMessageInfoAsync(request);
+                if (res)
                 {
-                    tokens = request.Tokens;
+                    IList<TokenDTO> tokens;
+                    if (request.Tokens != null && request.Tokens.Count > 0)
+                    {
+                        tokens = request.Tokens;
+                    }
+                    else
+                    {
+                        tokens = await _authenticationService.GetTokensWithClientIdAsync(new GetTokensWithClientIdRequest() { ClientId = request.ClientId });
+                    }
+                    var localTokens = tokens.Where(v => v.LoginServer == ConfigurationManager.Host).ToList();
+                    var result = await _notificationServer.PostMessageAsync(localTokens, request.Message, request.WSConnectType);
+                    var otherTokens = tokens.Where(v => v.LoginServer != ConfigurationManager.Host).ToList();
+                    if (otherTokens != null && otherTokens.Count > 0)
+                    {
+                        var notificationRequest = new SyncNotification()
+                        {
+                            Clients = otherTokens.Select(v => new NotificationClientInfo()
+                            {
+                                ClientId = v.ClientId,
+                                LoginServerUrl = v.LoginServer
+                            }).ToList(),
+                            Message = request.Message,
+                            WSConnectType = request.WSConnectType
+                        };
+                        await SyncToMasterAsync(SyncTypeEnum.Notification, notificationRequest);
+                    }
+                    return result;
                 }
                 else
                 {
-                    tokens = await _authenticationService.GetTokensWithClientIdAsync(new GetTokensWithClientIdRequest() { ClientId = request.ClientId });
+                    return res;
                 }
-                var localTokens = tokens.Where(v => v.LoginServer == ConfigurationManager.Host).ToList();
-                var result = await _notificationServer.PostMessageAsync(localTokens, request.Message, request.WSConnectType);
-                var otherTokens = tokens.Where(v => v.LoginServer != ConfigurationManager.Host).ToList();
-                if (otherTokens != null && otherTokens.Count > 0)
-                {
-                    var notificationRequest = new SyncNotification()
-                    {
-                        Clients = otherTokens.Select(v => new NotificationClientInfo()
-                        {
-                            ClientId = v.ClientId,
-                            LoginServerUrl = v.LoginServer
-                        }).ToList(),
-                        Message = request.Message,
-                        WSConnectType = request.WSConnectType
-                    };
-                    await SyncToMasterAsync(SyncTypeEnum.Notification, notificationRequest);
-                }
-                return result;
             }
-            else
+            catch (Exception ex)
             {
-                return res;
+                Logger.WriteLineError($"NotificationService PostMessageAsync RelevanceCode: {request.RelevanceCode}, err: {ex}");
+                return false;
             }
         }
 
@@ -211,35 +227,43 @@ namespace WingNotificationModule.Service
         /// <value>true</value>
         public async Task<bool> BroadcastMessageAsync(BroadcastNotificationRequest request)
         {
-            var res = await AddMessageInfoAsync(null, request);
-            if (res)
+            try
             {
-                var tokens = await _authenticationService.GetTokenWithClientIdsAsync(new GetTokenWithClientIdsRequest() { ClientIds = request.ClientIds.ToList() });
-                var localTokens = tokens.Where(v => v.LoginServer == ConfigurationManager.Host).ToList();
-                var result = await _notificationServer.BroadcastMessageAsync(localTokens, request);
-                if (request.IsNeedSyn)
+                var res = await AddMessageInfoAsync(null, request);
+                if (res)
                 {
-                    var otherTokens = tokens.Where(v => v.LoginServer != ConfigurationManager.Host).ToList();
-                    if (otherTokens != null && otherTokens.Count > 0)
+                    var tokens = await _authenticationService.GetTokenWithClientIdsAsync(new GetTokenWithClientIdsRequest() { ClientIds = request.ClientIds.ToList() });
+                    var localTokens = tokens.Where(v => v.LoginServer == ConfigurationManager.Host).ToList();
+                    var result = await _notificationServer.BroadcastMessageAsync(localTokens, request);
+                    if (request.IsNeedSyn)
                     {
-                        var notificationRequest = new SyncNotification()
+                        var otherTokens = tokens.Where(v => v.LoginServer != ConfigurationManager.Host).ToList();
+                        if (otherTokens != null && otherTokens.Count > 0)
                         {
-                            Clients = otherTokens.Select(v => new NotificationClientInfo()
+                            var notificationRequest = new SyncNotification()
                             {
-                                ClientId = v.ClientId,
-                                LoginServerUrl = v.LoginServer
-                            }).ToList(),
-                            Message = request.Message,
-                            WSConnectType = request.WSConnectType
-                        };
-                        await SyncToMasterAsync(SyncTypeEnum.Notification, notificationRequest);
+                                Clients = otherTokens.Select(v => new NotificationClientInfo()
+                                {
+                                    ClientId = v.ClientId,
+                                    LoginServerUrl = v.LoginServer
+                                }).ToList(),
+                                Message = request.Message,
+                                WSConnectType = request.WSConnectType
+                            };
+                            await SyncToMasterAsync(SyncTypeEnum.Notification, notificationRequest);
+                        }
                     }
+                    return result;
+                }
+                else
+                {
+                    return res;
                 }
-                return result;
             }
-            else
+            catch (Exception ex)
             {
-                return res;
+                Logger.WriteLineError($"NotificationService BroadcastMessageAsync RelevanceCode: {request.RelevanceCode}, err: {ex}");
+                return false;
             }
         }
 
@@ -273,7 +297,7 @@ namespace WingNotificationModule.Service
             }
             catch (Exception ex)
             {
-                Logger.WriteLineWarn($"NotificationService SyncToMasterAsync err, syncType:{syncType.ToString()}, err: {ex}");
+                Logger.WriteLineError($"NotificationService SyncToMasterAsync err, syncType:{syncType.ToString()}, err: {ex}");
             }
         }
 
@@ -320,145 +344,153 @@ namespace WingNotificationModule.Service
         /// <returns>true</returns>
         private async Task<bool> AddMessageInfoAsync(SendNotificationRequest request, BroadcastNotificationRequest broadcastRequest = null)
         {
-            if (request == null && broadcastRequest == null)
-            {
-                ThrowCustomerException(CustomerRpcCode.NotificationParamError, "Notification param error");
-            }
-            AddMessagesDBRequest req = null;
-            if (request != null)
+            try
             {
-                IList<TokenDTO> tokens;
-                if (request.Tokens != null && request.Tokens.Count > 0)
-                {
-                    tokens = request.Tokens;
-                }
-                else
+                if (request == null && broadcastRequest == null)
                 {
-                    tokens = await _authenticationService.GetTokensWithClientIdAsync(new GetTokensWithClientIdRequest() { ClientId = request.ClientId });
+                    ThrowCustomerException(CustomerRpcCode.NotificationParamError, "Notification param error");
                 }
-                var tokenInfo = tokens.FirstOrDefault() ?? new TokenDTO();
-                string accountName = "";
-                if (string.IsNullOrEmpty(tokenInfo.ClientId))
+                AddMessagesDBRequest req = null;
+                if (request != null)
                 {
-                    if (broadcastRequest.ReceiverType == ApplicantTypeEnum.Client)
+                    IList<TokenDTO> tokens;
+                    if (request.Tokens != null && request.Tokens.Count > 0)
+                    {
+                        tokens = request.Tokens;
+                    }
+                    else
+                    {
+                        tokens = await _authenticationService.GetTokensWithClientIdAsync(new GetTokensWithClientIdRequest() { ClientId = request.ClientId });
+                    }
+                    var tokenInfo = tokens.FirstOrDefault() ?? new TokenDTO();
+                    string accountName = "";
+                    if (string.IsNullOrEmpty(tokenInfo.ClientId))
                     {
-                        var userList = CacheMaintenance.Instance.Get<IUserInfoManager>().Where(x => x.Code == request.ClientId)?.ToList() ?? new List<CacheUserDTO>();
-                        if (userList.Count > 0)
+                        if (broadcastRequest.ReceiverType == ApplicantTypeEnum.Client)
                         {
-                            var userInfoDTO = userList.FirstOrDefault() ?? new CacheUserDTO();
-                            if (!string.IsNullOrEmpty(userInfoDTO.FullName))
+                            var userList = CacheMaintenance.Instance.Get<IUserInfoManager>().Where(x => x.Code == request.ClientId)?.ToList() ?? new List<CacheUserDTO>();
+                            if (userList.Count > 0)
                             {
-                                accountName = userInfoDTO.FullName;
+                                var userInfoDTO = userList.FirstOrDefault() ?? new CacheUserDTO();
+                                if (!string.IsNullOrEmpty(userInfoDTO.FullName))
+                                {
+                                    accountName = userInfoDTO.FullName;
+                                }
+                                else
+                                {
+                                    accountName = userInfoDTO.UserName;
+                                }
                             }
-                            else
+                        }
+                        else if (broadcastRequest.ReceiverType == ApplicantTypeEnum.Device)
+                        {
+                            var deviceList = CacheMaintenance.Instance.Get<IDeviceInfosManager>().Where(x => x.Code == request.ClientId)?.ToList() ?? new List<CacheDeviceDTO>();
+                            if (deviceList.Count > 0)
                             {
-                                accountName = userInfoDTO.UserName;
+                                var deviceInfoDTO = deviceList.FirstOrDefault() ?? new CacheDeviceDTO();
+                                accountName = deviceInfoDTO.Name;
                             }
                         }
                     }
-                    else if (broadcastRequest.ReceiverType == ApplicantTypeEnum.Device)
+                    else
                     {
-                        var deviceList = CacheMaintenance.Instance.Get<IDeviceInfosManager>().Where(x => x.Code == request.ClientId)?.ToList() ?? new List<CacheDeviceDTO>();
-                        if (deviceList.Count > 0)
-                        {
-                            var deviceInfoDTO = deviceList.FirstOrDefault() ?? new CacheDeviceDTO();
-                            accountName = deviceInfoDTO.Name;
-                        }
+                        accountName = tokenInfo.AccountName;
                     }
-                }
-                else
-                {
-                    accountName = tokenInfo.AccountName;
-                }
-                var clientInfos = new List<ClientInfoDTO>()
-               {
-                   new ClientInfoDTO() {
-                       ClientId = request.ClientId,
-                       Name = accountName,
-                       IsReaded = false
-                   }
-               };
-                JObject jsonObj = JObject.Parse(request.JsonMessage);
-                req = new AddMessagesDBRequest()
+                    var clientInfos = new List<ClientInfoDTO>()
                 {
-                    Code = jsonObj["Code"].ToString(),
-                    NotificationType = request.NotificationType,
-                    Content = request.JsonMessage,
-                    ServerHost = ConfigurationManager.Host,
-                    NotifyTime = DateTime.UtcNow,
-                    ReceiverType = request.ReceiverType,
-                    TransactionType = request.TransactionType,
-                    RelevanceCode = request.RelevanceCode,
-                    ClientInfos = clientInfos
+                    new ClientInfoDTO() {
+                        ClientId = request.ClientId,
+                        Name = accountName,
+                        IsReaded = false
+                    }
                 };
-            }
-            else
-            {
-                var clientInfos = new List<ClientInfoDTO>();
-                if (broadcastRequest.ClientIds?.Count > 0)
+                    JObject jsonObj = JObject.Parse(request.JsonMessage);
+                    req = new AddMessagesDBRequest()
+                    {
+                        Code = jsonObj["Code"].ToString(),
+                        NotificationType = request.NotificationType,
+                        Content = request.JsonMessage,
+                        ServerHost = ConfigurationManager.Host,
+                        NotifyTime = DateTime.UtcNow,
+                        ReceiverType = request.ReceiverType,
+                        TransactionType = request.TransactionType,
+                        RelevanceCode = request.RelevanceCode,
+                        ClientInfos = clientInfos
+                    };
+                }
+                else
                 {
-                    foreach (var clientId in broadcastRequest.ClientIds)
+                    var clientInfos = new List<ClientInfoDTO>();
+                    if (broadcastRequest.ClientIds?.Count > 0)
                     {
-                        var curTokens = await _authenticationService.GetTokensWithClientIdAsync(new GetTokensWithClientIdRequest() { ClientId = clientId });
-                        var curTokenInfo = curTokens.FirstOrDefault() ?? new TokenDTO();
-                        string accountName = "";
-                        if (string.IsNullOrEmpty(curTokenInfo.ClientId))
+                        foreach (var clientId in broadcastRequest.ClientIds)
                         {
-                            if (broadcastRequest.ReceiverType == ApplicantTypeEnum.Client)
+                            var curTokens = await _authenticationService.GetTokensWithClientIdAsync(new GetTokensWithClientIdRequest() { ClientId = clientId });
+                            var curTokenInfo = curTokens.FirstOrDefault() ?? new TokenDTO();
+                            string accountName = "";
+                            if (string.IsNullOrEmpty(curTokenInfo.ClientId))
                             {
-                                var userList = CacheMaintenance.Instance.Get<IUserInfoManager>().Where(x => x.Code == clientId)?.ToList() ?? new List<CacheUserDTO>();
-                                if (userList.Count > 0)
+                                if (broadcastRequest.ReceiverType == ApplicantTypeEnum.Client)
                                 {
-                                    var userInfoDTO = userList.FirstOrDefault() ?? new CacheUserDTO();
-                                    if (!string.IsNullOrEmpty(userInfoDTO.FullName))
+                                    var userList = CacheMaintenance.Instance.Get<IUserInfoManager>().Where(x => x.Code == clientId)?.ToList() ?? new List<CacheUserDTO>();
+                                    if (userList.Count > 0)
                                     {
-                                        accountName = userInfoDTO.FullName;
+                                        var userInfoDTO = userList.FirstOrDefault() ?? new CacheUserDTO();
+                                        if (!string.IsNullOrEmpty(userInfoDTO.FullName))
+                                        {
+                                            accountName = userInfoDTO.FullName;
+                                        }
+                                        else
+                                        {
+                                            accountName = userInfoDTO.UserName;
+                                        }
                                     }
-                                    else
+                                }
+                                else if (broadcastRequest.ReceiverType == ApplicantTypeEnum.Device)
+                                {
+                                    var deviceList = CacheMaintenance.Instance.Get<IDeviceInfosManager>().Where(x => x.Code == clientId)?.ToList() ?? new List<CacheDeviceDTO>();
+                                    if (deviceList.Count > 0)
                                     {
-                                        accountName = userInfoDTO.UserName;
+                                        var deviceInfoDTO = deviceList.FirstOrDefault() ?? new CacheDeviceDTO();
+                                        accountName = deviceInfoDTO.Name;
                                     }
                                 }
                             }
-                            else if (broadcastRequest.ReceiverType == ApplicantTypeEnum.Device)
+                            else
                             {
-                                var deviceList = CacheMaintenance.Instance.Get<IDeviceInfosManager>().Where(x => x.Code == clientId)?.ToList() ?? new List<CacheDeviceDTO>();
-                                if (deviceList.Count > 0)
-                                {
-                                    var deviceInfoDTO = deviceList.FirstOrDefault() ?? new CacheDeviceDTO();
-                                    accountName = deviceInfoDTO.Name;
-                                }
+                                accountName = curTokenInfo.AccountName;
                             }
+                            var entity = new ClientInfoDTO()
+                            {
+                                ClientId = clientId,
+                                Name = accountName,
+                                IsReaded = false
+                            };
+                            clientInfos.Add(entity);
                         }
-                        else
-                        {
-                            accountName = curTokenInfo.AccountName;
-                        }
-                        var entity = new ClientInfoDTO()
-                        {
-                            ClientId = clientId,
-                            Name = accountName,
-                            IsReaded = false
-                        };
-                        clientInfos.Add(entity);
                     }
+                    JObject jsonObj = JObject.Parse(broadcastRequest.JsonMessage);
+                    req = new AddMessagesDBRequest()
+                    {
+                        Code = jsonObj["Code"].ToString(),
+                        NotificationType = broadcastRequest.NotificationType,
+                        Content = broadcastRequest.JsonMessage,
+                        ServerHost = ConfigurationManager.Host,
+                        NotifyTime = DateTime.UtcNow,
+                        ReceiverType = broadcastRequest.ReceiverType,
+                        TransactionType = broadcastRequest.TransactionType,
+                        RelevanceCode = broadcastRequest.RelevanceCode,
+                        ClientInfos = clientInfos
+                    };
                 }
-                JObject jsonObj = JObject.Parse(broadcastRequest.JsonMessage);
-                req = new AddMessagesDBRequest()
-                {
-                    Code = jsonObj["Code"].ToString(),
-                    NotificationType = broadcastRequest.NotificationType,
-                    Content = broadcastRequest.JsonMessage,
-                    ServerHost = ConfigurationManager.Host,
-                    NotifyTime = DateTime.UtcNow,
-                    ReceiverType = broadcastRequest.ReceiverType,
-                    TransactionType = broadcastRequest.TransactionType,
-                    RelevanceCode = broadcastRequest.RelevanceCode,
-                    ClientInfos = clientInfos
-                };
+                var result = await _messageDBService.AddMessageInfoDBAsync(req);
+                return result;
+            }
+            catch (Exception ex)
+            {
+                Logger.WriteLineError($"NotificationService AddMessageInfoAsync err, {ex}");
+                return false;
             }
-            var result = await _messageDBService.AddMessageInfoDBAsync(req);
-            return result;
         }
 
         /// <summary>
@@ -471,7 +503,7 @@ namespace WingNotificationModule.Service
         {
             try
             {
-                Logger.WriteLineInfo($"Notification SyncServerMessageAsync, source url:{request.SourceUrl}, syncType:{request.SyncType.ToString()},wsConnectType{request.Notification.WSConnectType}");
+                Logger.WriteLineInfo($"Notification ReceiveSyncServerMessageAsync, source url:{request.SourceUrl}, syncType:{request.SyncType.ToString()},wsConnectType{request.Notification.WSConnectType}");
                 SyncNotification message = request.Notification;
                 var localClients = message.Clients.Where(v => v.LoginServerUrl == ConfigurationManager.Host).ToList();
                 var localClientIds = localClients.Select(v => v.ClientId).ToList();
@@ -482,7 +514,7 @@ namespace WingNotificationModule.Service
             }
             catch (Exception ex)
             {
-                Logger.WriteLineWarn($"NotificationService SyncServerMessageAsync err, {ex}");
+                Logger.WriteLineError($"NotificationService ReceiveSyncServerMessageAsync err, {ex}");
                 return false;
             }
         }