Exemple #1
0
        private async Task NextSync(TimeSpan timeout, CancellationToken cancellationToken)
        {
            Trace.TraceInformation("NextSync started.");
            try
            {
                Trace.TraceInformation($"Before provider.");
                using (var provider = new TemperatureProvider())
                {
                    var lastDateTime = await provider.GetLastTemperatureDate();

                    DateTime requestDateTime;
                    if (lastDateTime.HasValue)
                    {
                        requestDateTime = lastDateTime.Value;
                        Trace.TraceInformation($"Last date in database is {lastDateTime}. ");
                    }
                    else
                    {
                        requestDateTime = _defaultDateTime;
                        Trace.TraceInformation($"Last date in database is absent. Use default datetime {_defaultDateTime}");
                    }

                    requestDateTime = requestDateTime.AddMinutes(1);
                    var httpDownload = new HttpDownload(_httpUrl);
                    Trace.TraceInformation($"Send request with date {requestDateTime}. ");
                    httpDownload.SendRequest(requestDateTime);
                    httpDownload.GetResponse();

                    if (httpDownload.IsPagePresent())
                    {
                        var device = httpDownload.DownloadTemperature();
                        Trace.TraceInformation($"Device name {device.Name}. Found {device.Temperatures.Count()} temperatures information. ");
                        if (device.Temperatures.Any())
                        {
                            Trace.TraceInformation($"Start add {device.Temperatures.Count()} items temperatures information to database. ");
                            var deviceId = await provider.AddDeviceIfNotExist(device);

                            Task task = provider.AddTemperatures(deviceId, device.Temperatures);
                        }
                    }
                    else
                    {
                        Trace.TraceWarning($"Response without StatusCode - OK");
                    }

                    await Task.Delay(timeout, cancellationToken);
                }
            }
            catch (OperationCanceledException)
            {
                return;
            }
            catch (HttpRequestException ex)
            {
                if ((timeout += timeout) > _maxTimeout)
                {
                    Trace.TraceError($"Communication error, but maximum timeout was reached. Sync will be terminated. {ex}");
                    Environment.Exit(1);
                }
                else
                {
                    Trace.TraceWarning($"Communication error. Timeout increased to {timeout}. {ex}");
                    Trace.TraceInformation("Timeout " + timeout);
                    await Task.Delay(timeout, cancellationToken);
                }
            }
            finally
            {
                Trace.TraceWarning("Before NextSync");
                await NextSync(timeout, cancellationToken);
            }
        }