Пример #1
0
        /// <summary>
        /// Executes a service request, and returns the response.
        /// </summary>
        /// <param name="request">Encapsulates the request to send.</param>
        /// <param name="withCaching">Specifies whether or not to allow service-level caching for this call</param>
        /// <param name="onReconnect">What to do if the connection is broken, then resumed</param>
        /// <param name="silentMode">If true, will not display any alerts or popups</param>
        /// <returns>The server response</returns>
        protected async Task <TResponse> DoRequest <TRequest, TResponse>(TRequest request, bool withCaching = false, Action onReconnect = null, bool silentMode = false)
            where TRequest : IApiRequest
            where TResponse : IApiResponse, new()
        {
            bool fromCache = false;

            var response = withCaching ? this.GetFromCache(request) : null;

            if (response == null || !(response is TResponse))
            {
                response = await WebSocketsClient.DoRequestAsync <TRequest, TResponse>(request, onReconnect, silentMode : silentMode);
            }
            else
            {
                fromCache = true;
            }

            if (response?.Header?.ServerVersion != null)
            {
                _lastServerVersion = ServerVersion.Parse(response.Header.ServerVersion);
            }

            if (fromCache)
            {
                LogUtility.LogMessage("Response for service request " + request?.Header?.Type + " returned from cache [Channel : " + request?.Header?.Channel + "].");
            }

            if (!fromCache && withCaching)
            {
                LogUtility.LogMessage("Caching response for service request " + request?.Header?.Type + " [Channel : " + request?.Header?.Channel + "].");
                this.CacheResponse(request, response);
            }

            return((TResponse)response);
        }
Пример #2
0
        public async Task <ConnectionResponse> RequestConnection(string username, string password, bool silentMode = false)
        {
            try
            {
                string timestamp = null;
                if (DataCache.ApplicationMetadata?.TimeStamp != null)
                {
                    timestamp = DataCache.ApplicationMetadata.TimeStamp.ToString();
                }

                //TODO: how to determine the value of the app version
                var request = new ConnectionRequest(username, password, "1", timestamp: timestamp, sessionId: User.Current?.SessionId);

                var output = await WebSocketsClient.DoRequestAsync <ConnectionRequest, ConnectionResponse>(request, silentMode : silentMode);

                ServiceContainer.InvalidateCache();
                if (output != null && output.Body != null)
                {
                    if (User.Current != null)
                    {
                        User.Current.SessionId = output.Body.SessionId;

                        if (output.Body.Devices != null)
                        {
                            if (output.Body.MetaData != null)
                            {
                                DataCache.ApplicationMetadata = output.Body.MetaData;
                            }

                            if (output.Body.User?.Name != null)
                            {
                                User.Current.Name = output.Body.User?.Name;
                            }

                            if (output.Body.User?.DevicesAccess != null)
                            {
                                User.Current.DevicesAccess = output.Body.User?.DevicesAccess;
                            }

                            DataCache.SetDeviceOrder(output.Body?.Devices?.Keys?.ToList());
                            DataCache.CacheDevicesResponse(output.Body?.Devices?.Items);
                            DataCache.SetActiveAlertsCount(output.Body?.AlertsCount);
                            DataCache.SyncCommandProgresses(output.Body?.CommandProgresses);
                        }

                        User.Current.Save();
                    }
                }

                return(output);
            }
            catch (Exception e)
            {
                LogUtility.LogException(e);
            }

            return(null);
        }