public async Task <double> GetTemperature(string city) { var url = "http://api.openweathermap.org/data/2.5/weather?q=" + $"{city}" + "&units=metric&APPID=" + $"{Settings.OpenWeatherKey}" + "&units=celsius"; var rootWeather = new RootWeather(); using (var httpClient = new HttpClient()) { using (var response = await httpClient.GetAsync(url)) { string apiResponse = await response.Content.ReadAsStringAsync(); rootWeather = JsonConvert.DeserializeObject <RootWeather>(apiResponse); if (rootWeather.cod == 404) { return(0); } else { return(rootWeather.main.temp); } } } }
public async Task <RootWeather> GetWeather(string city, string country) { string openWeatherUrl = $"{url}/{version}/{type}?q={city},{country}&appid={openWeatherApiKey}&units={units}"; HttpClient Client = new HttpClient(); HttpResponseMessage Response = await Client.GetAsync(openWeatherUrl); string content = await Response.Content.ReadAsStringAsync(); RootWeather weather = (RootWeather)JsonConvert.DeserializeObject(content, typeof(RootWeather)); return(weather); }
private static async Task <Stream> AddTextToImage(RootWeather weatherRootObject, HttpResponseMessage httpResponseMessage) { int temperature = Convert.ToInt32(weatherRootObject.Main.Temp); double windSpeed = weatherRootObject.Wind.Speed; Stream responseContent = await httpResponseMessage.Content.ReadAsStreamAsync(); string text1 = GetBeerMessage(temperature); string text2 = string.Format($"Temperature: {temperature}"); string text3 = string.Format($"Wind: {windSpeed}km/u"); var renderedImage = ImageHelper.AddTextToImage(responseContent, (text1, (20, 20)), (text2, (20, 50)), (text3, (20, 80))); return(renderedImage); }
public static async Task RunAsync([QueueTrigger("openweather", Connection = "AzureWebJobsStorage")] string myQueueItem, ILogger log) { try { TriggerMessage triggerMessage = (TriggerMessage)JsonConvert.DeserializeObject(myQueueItem, typeof(TriggerMessage)); RootWeather weather = await WeatherDAO.Instance.GetWeather(triggerMessage.CityName, triggerMessage.CountryCode); CloudQueueMessage cloudQueueMessage = CreateCloudQueueMessage(triggerMessage, weather); var storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage")); await PostMessageToQueue(cloudQueueMessage, storageAccount); } catch (Exception e) { log.LogError(e.Data.ToString()); } }
public static async Task RunAsync([QueueTrigger("openweather-out", Connection = "AzureWebJobsStorage")] string myQueueItem, ILogger log) { try { BlobMessage message = (BlobMessage)JsonConvert.DeserializeObject(myQueueItem, typeof(BlobMessage)); RootWeather weatherRootObject = message.Weather; Coord coordinates = weatherRootObject.Coord; var storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage")); CloudBlockBlob cloudBlockBlob = await GetCloudBlockBlob(message, storageAccount); HttpResponseMessage httpResponseMessage = await MapsDAO.Instance.GetMaps(coordinates); Stream renderedImage = await AddTextToImage(weatherRootObject, httpResponseMessage); await cloudBlockBlob.UploadFromStreamAsync(renderedImage); } catch (Exception e) { log.LogError(e.Data.ToString()); } }
public async Task <double> GetTemperature(double lat, double lon) { var url = "http://api.openweathermap.org/data/2.5/weather?" + $"lat={lat.ToString().Replace(",", ".")}" + $"&lon={lon.ToString().Replace(",", ".")}" + "&units=metric&APPID=" + $"{Settings.OpenWeatherKey}" + "&units=celsius"; var rootWeather = new RootWeather(); using (var httpClient = new HttpClient()) { using (var response = await httpClient.GetAsync(url)) { string apiResponse = await response.Content.ReadAsStringAsync(); rootWeather = JsonConvert.DeserializeObject <RootWeather>(apiResponse); return(rootWeather.main.temp); } } }
private void GetDeserializedObject() { Weather = Deserializer.DeserializeJSON(); }
private static BlobMessage CreateBlobTriggerMessage(TriggerMessage triggerMessage, RootWeather weather) { BlobMessage message = new BlobMessage(weather) { CityName = triggerMessage.CityName, CountryCode = triggerMessage.CountryCode, Blob = triggerMessage.Blob, Guid = triggerMessage.Guid, }; return(message); }
private static CloudQueueMessage CreateCloudQueueMessage(TriggerMessage triggerMessage, RootWeather weather) { BlobMessage blobMessage = CreateBlobTriggerMessage(triggerMessage, weather); var message = JsonConvert.SerializeObject(blobMessage); var cloudQueueMessage = new CloudQueueMessage(message); return(cloudQueueMessage); }