141 lines
4.3 KiB
C#
141 lines
4.3 KiB
C#
|
|
using Newtonsoft.Json;
|
|||
|
|
|
|||
|
|
namespace langguanApi.Service
|
|||
|
|
{
|
|||
|
|
public class WeatherService
|
|||
|
|
{
|
|||
|
|
private IHttpClientFactory _httpClientFactory;
|
|||
|
|
private IConfiguration _configuration;
|
|||
|
|
private ILogger<WeatherService> _logger;
|
|||
|
|
public WeatherService(IHttpClientFactory httpClientFactory,
|
|||
|
|
IConfiguration configuration, ILogger<WeatherService> logger)
|
|||
|
|
{
|
|||
|
|
_httpClientFactory = httpClientFactory;
|
|||
|
|
_configuration = configuration;
|
|||
|
|
_logger=logger;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 爬气象局的天气数据%
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task<object> GetWeather()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var client = _httpClientFactory.CreateClient();
|
|||
|
|
var resp = await client.GetAsync(_configuration.GetSection("Weather").Value);
|
|||
|
|
if (resp.IsSuccessStatusCode)
|
|||
|
|
{
|
|||
|
|
var data = await resp.Content.ReadAsStringAsync();
|
|||
|
|
var result = JsonConvert.DeserializeObject<Root>(data);
|
|||
|
|
return new
|
|||
|
|
{
|
|||
|
|
location = result?.data.location.name,
|
|||
|
|
result?.data.now.precipitation,
|
|||
|
|
result?.data.now.temperature,
|
|||
|
|
result?.data.now.pressure,
|
|||
|
|
result?.data.now.humidity,
|
|||
|
|
result?.data.now.windDirection,
|
|||
|
|
result?.data.now.windDirectionDegree,
|
|||
|
|
result?.data.now.windSpeed,
|
|||
|
|
result?.data.now.windScale,
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogError(ex.Message);
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
public class Location
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 54511
|
|||
|
|
/// </summary>
|
|||
|
|
public string id { get; set; }
|
|||
|
|
/// <summary>
|
|||
|
|
/// 北京
|
|||
|
|
/// </summary>
|
|||
|
|
public string name { get; set; }
|
|||
|
|
/// <summary>
|
|||
|
|
/// 中国, 北京, 北京
|
|||
|
|
/// </summary>
|
|||
|
|
public string path { get; set; }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public class Now
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// Precipitation
|
|||
|
|
/// </summary>
|
|||
|
|
public double precipitation { get; set; }
|
|||
|
|
/// <summary>
|
|||
|
|
/// Temperature
|
|||
|
|
/// </summary>
|
|||
|
|
public double temperature { get; set; }
|
|||
|
|
/// <summary>
|
|||
|
|
/// Pressure
|
|||
|
|
/// </summary>
|
|||
|
|
public double pressure { get; set; }
|
|||
|
|
/// <summary>
|
|||
|
|
/// Humidity
|
|||
|
|
/// </summary>
|
|||
|
|
public double humidity { get; set; }
|
|||
|
|
/// <summary>
|
|||
|
|
/// 东北风
|
|||
|
|
/// </summary>
|
|||
|
|
public string windDirection { get; set; }
|
|||
|
|
/// <summary>
|
|||
|
|
/// WindDirectionDegree
|
|||
|
|
/// </summary>
|
|||
|
|
public double windDirectionDegree { get; set; }
|
|||
|
|
/// <summary>
|
|||
|
|
/// WindSpeed
|
|||
|
|
/// </summary>
|
|||
|
|
public double windSpeed { get; set; }
|
|||
|
|
/// <summary>
|
|||
|
|
/// 微风
|
|||
|
|
/// </summary>
|
|||
|
|
public string windScale { get; set; }
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
///
|
|||
|
|
/// </summary>
|
|||
|
|
public class Data
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// Location
|
|||
|
|
/// </summary>
|
|||
|
|
public Location location { get; set; }
|
|||
|
|
/// <summary>
|
|||
|
|
/// Now
|
|||
|
|
/// </summary>
|
|||
|
|
public Now now { get; set; }
|
|||
|
|
/// <summary>
|
|||
|
|
/// Alarm
|
|||
|
|
/// </summary>
|
|||
|
|
public List<object> alarm { get; set; }
|
|||
|
|
/// <summary>
|
|||
|
|
/// 2024/01/15 10:05
|
|||
|
|
/// </summary>
|
|||
|
|
public DateTime lastUpdate { get; set; }
|
|||
|
|
}
|
|||
|
|
public class Root
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// success
|
|||
|
|
/// </summary>
|
|||
|
|
public string msg { get; set; }
|
|||
|
|
/// <summary>
|
|||
|
|
/// Code
|
|||
|
|
/// </summary>
|
|||
|
|
public int code { get; set; }
|
|||
|
|
/// <summary>
|
|||
|
|
/// Data
|
|||
|
|
/// </summary>
|
|||
|
|
public Data data { get; set; }
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|