public override async Task GetWeather(IAsyncStreamReader <Empty> requests, IServerStreamWriter <WeatherReply> responseStream, ServerCallContext context) { Console.WriteLine("start"); if (_responseStream == null) { _responseStream = responseStream; _requests = requests; } while (await _requests.MoveNext()) { Trace("Receive data request"); var reply = new WeatherReply(); Trace("Creating data "); var rng = new Random(); reply.Forecasts.Add(Enumerable.Range(1, 5).Select(index => new WeatherForecast { DateTimeStamp = Timestamp.FromDateTime(DateTime.UtcNow.AddDays(index)), TemperatureC = rng.Next(-20, 55), Summary = Summaries[rng.Next(Summaries.Length)] })); await _responseStream.WriteAsync(reply); Trace("Data sent"); } }
public override Task <WeatherReply> GetWeather(WeatherForecast request, ServerCallContext context) { var reply = new WeatherReply(); var rng = new Random(); reply.Forecasts.Add(Enumerable.Range(1, 10).Select(index => new WeatherForecast { DateTimeStamp = Timestamp.FromDateTime(DateTime.Now.AddDays(index).ToUniversalTime()), TemperatureC = rng.Next(20, 55), Summary = Summaries[rng.Next(Summaries.Length)] })); return(Task.FromResult(reply)); }
public async Task <string> Get(string zip, string replyTo) { using HttpClient http = new HttpClient(); string query = $"http://api.weatherapi.com/v1/current.json?key={Settings.WeatherKey}&q={zip}"; HttpResponseMessage response = await http.GetAsync(query); if (response.IsSuccessStatusCode) { string resp = await response.Content.ReadAsStringAsync(); WeatherReply wr = JsonConvert.DeserializeObject <WeatherReply>(resp); string r = "Weather in " + wr.location.name + ", " + wr.location.region + " is " + wr.current.condition.text + " and " + wr.current.temp_f + ", it feels like " + wr.current.feelslike_f + "."; string reply = $"PRIVMSG {replyTo} {r}"; return(reply); } return(""); }
// TODO переделать на общий алгоритм public override WeatherReply Get(PlaceEnum place) { var query = new WeatherQuery(WeatherServiceEnum.Wunderground, place); var uri = String.Format(UriPattern, Config.Wunderground_ApiKey, Config.Wunderground_Places_Codes[place] ); var responseStr = HttpHelper.Get(new Uri(uri)); var weather = parser.Parse(responseStr); var reply = new WeatherReply(query, weather, DateTime.Now); return reply; }
public async Task <string> Get(string zip, string replyTo) { using HttpClient http = new HttpClient(); string query = $"http://api.weatherapi.com/v1/forecast.json?key={Settings.WeatherKey}&q={zip}&days=1"; HttpResponseMessage response = await http.GetAsync(query); if (response.IsSuccessStatusCode) { string resp = await response.Content.ReadAsStringAsync(); WeatherReply wr = JsonConvert.DeserializeObject <WeatherReply>(resp); Day forecast = wr.forecast.ForecastDay[0].day; string r = "Forecast for " + wr.location.name + ", " + wr.location.region + " - " + wr.forecast.ForecastDay[0].date + ": " + forecast.condition.text + ". High temp: " + forecast.maxtemp_f + "F. Low Temp: " + forecast.mintemp_f + "F. Humiditiy: " + forecast.avghumidity + "%. Rain Chance: " + forecast.daily_chance_of_rain + "%."; string reply = $"PRIVMSG {replyTo} {r}"; return(reply); } return(""); }
public override Task <WeatherReply> GetWeather(Empty request, ServerCallContext context) { var reply = new WeatherReply(); var rng = new Random(); reply.Forecasts.Add(Enumerable.Range(1, 5).Select(index => { var temperatureC = rng.Next(-20, 55); return(new WeatherForecast { DateTimeStamp = Timestamp.FromDateTime(DateTime.UtcNow.AddDays(index)), TemperatureC = temperatureC, TemperatureF = 32 + (int)(temperatureC / 0.5556), Summary = Summaries[rng.Next(Summaries.Length)] }); })); return(Task.FromResult(reply)); }