Example #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);
                }
            }
        }
Example #2
0
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            string name;

            do
            {
                using var channel = GrpcChannel.ForAddress("https://localhost:5001");
                var client = new Weather.WeatherClient(channel);
                Console.Write("Введите город или stop для выхода: ");
                name = Console.ReadLine();
                var reply = await client.GetWeatherAsync(new WeatherRequest { Name = name });

                Console.WriteLine("Ответ сервера: " + reply.Message);
            } while (name != "stop");
        }
Example #3
0
        static async Task Main(string[] args)
        {
            using var channel = GrpcChannel.ForAddress("https://localhost:5001");
            var client = new Greeter.GreeterClient(channel);
            var reply  = await client.SayHelloAsync(
                new HelloRequest { Name = "GreeterClient", Languages = "Java, C#" });

            Console.WriteLine("Greeting: " + reply.Message);

            var clientWaeather  = new Weather.WeatherClient(channel);
            var weatherResponse = await clientWaeather.GetWeatherAsync(new GetWeatherRequest { Name = "london" });


            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();

            Console.WriteLine("Hello World!");
        }
Example #4
0
        public async Task <WeatherResponse> GetWeather()
        {
            try
            {
                var httpClient = new HttpClient();
                var channel    = GrpcChannel.ForAddress(App.GRPCBackendUrl, new GrpcChannelOptions {
                    HttpClient = httpClient
                });
                var weatherClient = new Weather.WeatherClient(channel);
                var result        = await weatherClient.GetWeatherAsync(new Empty());

                return(result);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }