Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
- using System.Net.Http;
- using Newtonsoft.Json;
- namespace testWebAplication
- {
- [ApiController]
- [Route("[controller]")]
- public class WeatherForecastController : ControllerBase
- {
- [HttpGet(Name = "GetWeatherForecast")]
- // Asyncrhonous function thats return ASP.NET api data from outside API
- public async Task<IEnumerable<WeatherForecast>> Get()
- {
- var apiUrl = "10.10.0.45/getSensorsData";
- var weatherForecasts = await GetData(apiUrl);
- return weatherForecasts;
- }
- // Asynchronous function that fetch all data from outside api and returns it
- private async Task<IEnumerable<WeatherForecast>> GetData(string apiUrl)
- {
- // Create http client that fetches the data from outside api and awaites reponse
- using (HttpClient client = new HttpClient())
- {
- HttpResponseMessage response = await client.GetAsync(apiUrl);
- // If response retiturn succes code, desearize and assing fetched data from the outside api
- if (response.IsSuccessStatusCode)
- {
- // Get json from response and deseralize it
- string json = await response.Content.ReadAsStringAsync();
- var data = JsonConvert.DeserializeObject<Data>(json);
- // Assing all extracted varriables to the api varriables
- var weatherForecasts = data.extractedData.Select(item => new WeatherForecast
- {
- localDate = DateOnly.FromDateTime(DateTime.Now),
- localTime = TimeOnly.FromDateTime(DateTime.Now),
- lat = Convert.ToInt32(item.Name == "lat" ? item.Varriable : null),
- lon = Convert.ToInt32(item.Name == "lon" ? item.Varriable : null),
- country = item.Name == "country" ? Convert.ToString(item.Varriable) : null,
- region = item.Name == "region" ? Convert.ToString(item.Varriable) : null,
- city = item.Name == "city" ? Convert.ToString(item.Varriable) : null,
- celsiusTemperature = item.Name == "celsiusTemperature" ? Convert.ToInt32(item.Varriable) : 0,
- windDegree = item.Name == "windDegree" ? Convert.ToInt32(item.Varriable) : 0,
- windDir = item.Name == "windDir" ? Convert.ToString(item.Varriable) : null,
- co = item.Name == "co" ? Convert.ToInt32(item.Varriable) : 0,
- o3 = item.Name == "o3" ? Convert.ToInt32(item.Varriable) : 0,
- windKm = item.Name == "windKm" ? Convert.ToInt32(item.Varriable) : 0,
- clouds = item.Name == "clouds" ? Convert.ToInt32(item.Varriable) : 0,
- pm10 = item.Name == "pm10" ? Convert.ToInt32(item.Varriable) : 0,
- uvSensor = item.Name == "uvSensor" ? Convert.ToInt32(item.Varriable) : 0,
- condition = item.Name == "condition" ? Convert.ToString(item.Varriable) : null
- });
- return weatherForecasts;
- }
- else
- {
- return null;
- }
- }
- }
- }
- // Define classes for json deserialization
- public class Data
- {
- [JsonProperty("weatherData")]
- public apiData[] extractedData { get; set; }
- }
- public class apiData
- {
- [JsonProperty("name")]
- public string Name { get; set; }
- [JsonProperty("varriable")]
- public object Varriable { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement