示例#1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation("create channel");
            using (var channel = GrpcChannel.ForAddress("https://localhost:5001"))
            {
                _logger.LogInformation("channel created");

                _logger.LogInformation("create client");
                var client = new Weather.WeatherClient(channel);
                _logger.LogInformation("client created");

                var d = new DateTime(2019, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                var i = 0;
                while (!stoppingToken.IsCancellationRequested)
                {
                    try
                    {
                        _logger.LogInformation("load weather data");
                        var request = new WeatherRequest
                        {
                            Date = Timestamp.FromDateTime(d)
                        };
                        var weather = await client.GetWeatherAsync(
                            request, null, null, stoppingToken);

                        _logger.LogInformation(
                            $"Temp: {weather.AvgTemperature}; " +
                            $"Precipitaion: {weather.Precipitaion}");

                        await _weatherService.Create(new WeatherData
                        {
                            Id             = i,
                            WeatherStation = "US1WAKG0045",
                            AvgTemperature = weather.AvgTemperature,
                            AvgWindSpeed   = weather.AvgWindSpeed,
                            MaxTemperature = weather.MaxTemperature,
                            MinTemperature = weather.MinTemperature,
                            Precipitaion   = weather.Precipitaion,
                            Date           = weather.Date.ToDateTime()
                        });
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, ex.Message);
                    }
                    d = d.AddDays(1);
                    i++;
                    await Task.Delay(1000, stoppingToken);
                }
            }
        }
        public ActionResult <IEnumerable <WeatherForecast> > Create()
        {
            var rng = new Random();
            var weatherForecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date         = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary      = Summaries[rng.Next(Summaries.Length)]
            })
                                   .ToArray();

            _weatherService.Create(weatherForecasts);

            return(Ok());
        }
 public void Create(WeatherForecast model)
 {
     _weatherService.Create(model);
 }