Пример #1
0
        public async Task Run(
            [TimerTrigger("0 */5 * * * *", RunOnStartup = true)] TimerInfo myTimer,
            [CosmosDB(
                 databaseName: "%DatabaseName%",
                 collectionName: "%CollectionName%",
                 CreateIfNotExists = true,
                 PartitionKey = "/location/id",
                 CollectionThroughput = 400,
                 ConnectionStringSetting = "CosmosDBConnection")] IAsyncCollector <WeatherPersistJson> weatherOut,
            ILogger log)
        {
            log.LogInformation("WeatherTimerTrigger fired.");

            try {
                WeatherHttpJson currentWeather = await this.getWeatherJson();

                WeatherPersistJson convertedWeather = new WeatherPersistJson(currentWeather);

                await weatherOut.AddAsync(convertedWeather);
            } catch (HttpRequestException ex) {
                log.LogError(ex, $"Failed HTTP request: {ex.ToString()}");
            }
        }
        public WeatherPersistJson(WeatherHttpJson wj)
        {
            this.CloudsPercentage = wj.Clouds.Percentage;

            this.DateTime = new WeatherDateTime()
            {
                DataCalculationUtc   = wj.DateTimeUtc,
                TimezoneShiftSeconds = wj.TimezoneShiftSeconds,
                SunriseUtc           = wj.Sys.SunriseUtc,
                SunsetUtc            = wj.Sys.SunsetUtc
            };

            this.Descriptions = wj.Weather.Select(weather => new Description
            {
                Id = weather.Id,
                MainDescription = weather.Main,
                LongDescription = weather.Description,
                Icon            = weather.Icon
            }).ToArray();

            this.HumidityPercentage = wj.Main.HumidityPerc;

            this.Internal = new Internal()
            {
                Base   = wj.Base,
                Cod    = wj.Cod,
                System = new WeatherPersistSystem()
                {
                    Type    = wj.Sys.Type,
                    Id      = wj.Sys.Id,
                    Message = wj.Sys.Message
                }
            };

            this.Location = new Location()
            {
                Id          = wj.Id,
                Name        = wj.Name,
                Country     = wj.Sys.Country,
                Coordinates = new Coordinates
                {
                    LatitudeDeg  = wj.Coordinates.LatitudeDeg,
                    LongitudeDeg = wj.Coordinates.LongitudeDeg
                }
            };

            this.Precipitation = new Precipitation();

            if (wj.Rain != null)
            {
                this.Precipitation.Rain = new PrecipitationValues
                {
                    OneHourMm = wj.Rain.OneHourMm,

                    ThreeHourMm = wj.Rain.ThreeHourMm
                };
            }

            if (wj.Snow != null)
            {
                this.Precipitation.Snow = new PrecipitationValues
                {
                    OneHourMm = wj.Snow.OneHourMm,

                    ThreeHourMm = wj.Snow.ThreeHourMm
                };
            }

            this.PressurehPa = wj.Main.PressurehPa;

            this.Temperature = new Temperature()
            {
                CurrentC = wj.Main.TempC,
                MinimumC = wj.Main.TempMinC,
                MaximumC = wj.Main.TempMaxC
            };

            this.Visibility = wj.Visibility;

            this.Wind = new Wind {
                SpeedKmph = wj.Wind.SpeedKmph, DirectionDeg = wj.Wind.DirectionDeg
            };
        }