Alan is always up to date on the weather forecast
private async Task GetWeatherInfos()
{
try
{
var position = await PositionManager.GetPosition();
WeatherForecastObject weather_infos = await WeatherAPIManager.GetWeather(position.Coordinate.Latitude, position.Coordinate.Longitude);
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
main_page.SwitchToWeatherGrid();
main_page.PrintWeatherInfos(weather_infos);
});
await GUIOutput("The weather forecast for " + weather_infos.name + " are: " + weather_infos.weather[0].description +
" with temperature between " + ((int)weather_infos.main.temp_min).ToString() + " °C and " + ((int)weather_infos.main.temp_max).ToString() + " °C." +
" The humidity rate is equal to " + ((int)weather_infos.main.humidity).ToString() + " % and the wind speed is " + ((int)weather_infos.wind.speed).ToString() + " meters per second.", true);
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
main_page.SwitchToDefaultGrid();
});
}
catch
{
await GUIOutput("I'm sorry, I couldn't retrieve the informations from the web.", true);
}
state = AssistantState.BACKGROUND_DEFAULT;
}
public class WeatherAPIManager
{
public async static Task<WeatherForecastObject> GetWeather(double lat, double lon)
{
var http = new HttpClient();
var response = await http.GetAsync("http://api.openweathermap.org/data/2.5/weather?lat=" + lat.ToString() + "&lon=" + lon.ToString() + "&units=metric&APPID=0bf33bf113a3c37dea2ddbb52272ea90");
string response_str = await response.Content.ReadAsStringAsync();
var serializer = new DataContractJsonSerializer(typeof(WeatherForecastObject));
var stream = new MemoryStream(Encoding.UTF8.GetBytes(response_str));
return (WeatherForecastObject)serializer.ReadObject(stream);
}
}
This functionality uses the same code layout as the "Inspire me" one: there are two
functions (GetWeatherInfos
and GetWeather
) that are respectively
in charge of "error handling + output" and the actual weather info retrieval. It is worth
noting that in the GetWeatherInfos
function the use of GUIOutput
allowed us to display the weather data in a very structured way while still having the
vocal assistant voice accurately describing the forecast thanks to the UI's layered
structure (click here for more details).