342 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			342 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C#
		
	
	
	
using langguanApi.Extensions.AutoDI;
 | 
						|
using Newtonsoft.Json;
 | 
						|
using NPOI.SS.Formula.Functions;
 | 
						|
using System.Reflection;
 | 
						|
 | 
						|
namespace langguanApi.Service
 | 
						|
{
 | 
						|
    [ServiceInjection(InjectionType.Transient)]
 | 
						|
    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;
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 爬空气质量数据
 | 
						|
        /// </summary>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<object> GetAirQuality()
 | 
						|
        {
 | 
						|
            var client = _httpClientFactory.CreateClient();
 | 
						|
            var resp = await client.GetAsync(_configuration.GetSection("AirQuality").Value);
 | 
						|
            if (resp.IsSuccessStatusCode)
 | 
						|
            {
 | 
						|
                var data = await resp.Content.ReadAsStringAsync();
 | 
						|
                var air = JsonConvert.DeserializeObject<AirQuality>(data);
 | 
						|
                return Convert2QualityList(air);
 | 
						|
            }
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
        private List<Quality> Convert2QualityList(AirQuality airQuality)
 | 
						|
        {
 | 
						|
            Type modelType = typeof(AirQuality);
 | 
						|
            List<Quality> qualityList = new List<Quality>();
 | 
						|
            PropertyInfo[] PropertyList = modelType.GetProperties();
 | 
						|
            foreach (PropertyInfo item in PropertyList)
 | 
						|
            {
 | 
						|
                Quality quality = new Quality();
 | 
						|
                object value = item.GetValue(airQuality, null);
 | 
						|
                if (value != null)
 | 
						|
                {
 | 
						|
                    var tuple = GetStandard(item.Name, value.ToString());
 | 
						|
                    quality.key = tuple.Item1;
 | 
						|
                    quality.val = value.ToString();
 | 
						|
                    quality.standard = tuple.Item2;
 | 
						|
                    quality.level = tuple.Item3;
 | 
						|
                    qualityList.Add(quality);
 | 
						|
                }
 | 
						|
            }
 | 
						|
            return qualityList;
 | 
						|
        }
 | 
						|
        private Tuple<string, string, string> GetStandard(string key, string value)
 | 
						|
        {
 | 
						|
            string standard = "";
 | 
						|
            string keyword = "";
 | 
						|
            string level = "";
 | 
						|
            switch (key)
 | 
						|
            {
 | 
						|
                case "CO":
 | 
						|
                    keyword = "一氧化碳";
 | 
						|
                    standard = "0.5μg/m3";
 | 
						|
                    if (double.Parse(value) >= 0.5)
 | 
						|
                    {
 | 
						|
                        level = "优";
 | 
						|
                    }
 | 
						|
                    else if (double.Parse(value) >= 0.3)
 | 
						|
                    {
 | 
						|
                        level = "持平";
 | 
						|
                    }
 | 
						|
                    else
 | 
						|
                    {
 | 
						|
                        level = "差";
 | 
						|
                    }
 | 
						|
                    break;
 | 
						|
                case "NO2":
 | 
						|
                    keyword = "二氧化氮";
 | 
						|
                    standard = "50μg/m3";
 | 
						|
                    if (double.Parse(value) >= 50)
 | 
						|
                    {
 | 
						|
                        level = "优";
 | 
						|
                    }
 | 
						|
                    else if (double.Parse(value) >= 150)
 | 
						|
                    {
 | 
						|
                        level = "轻度污染";
 | 
						|
                    }
 | 
						|
                    else
 | 
						|
                    {
 | 
						|
                        level = "中度污染";
 | 
						|
                    }
 | 
						|
                    break;
 | 
						|
                case "O3":
 | 
						|
                    keyword = "臭氧";
 | 
						|
                    standard = "100μg/m3";
 | 
						|
                    if (double.Parse(value) >= 100)
 | 
						|
                    {
 | 
						|
                        level = "优";
 | 
						|
                    }
 | 
						|
                    else if (double.Parse(value) >= 50)
 | 
						|
                    {
 | 
						|
                        level = "持平";
 | 
						|
                    }
 | 
						|
                    else
 | 
						|
                    {
 | 
						|
                        level = "差";
 | 
						|
                    }
 | 
						|
                    break;
 | 
						|
                case "PM10":
 | 
						|
                    keyword = "PM10";
 | 
						|
                    standard = "50ug/m3";
 | 
						|
                    if (double.Parse(value) >= 50)
 | 
						|
                    {
 | 
						|
                        level = "优";
 | 
						|
                    }
 | 
						|
                    else if (double.Parse(value) >= 150)
 | 
						|
                    {
 | 
						|
                        level = "中";
 | 
						|
                    }
 | 
						|
                    else
 | 
						|
                    {
 | 
						|
                        level = "差";
 | 
						|
                    }
 | 
						|
                    break;
 | 
						|
                case "PM2_5":
 | 
						|
 | 
						|
                    keyword = "PM2.5";
 | 
						|
                    standard = "25ug/m3";
 | 
						|
                    if (double.Parse(value) >= 25)
 | 
						|
                    {
 | 
						|
                        level = "优";
 | 
						|
                    }
 | 
						|
                    else if (double.Parse(value) >= 100)
 | 
						|
                    {
 | 
						|
                        level = "中";
 | 
						|
                    }
 | 
						|
                    else
 | 
						|
                    {
 | 
						|
                        level = "差";
 | 
						|
                    }
 | 
						|
                    break;
 | 
						|
                case "SO2":
 | 
						|
 | 
						|
                    keyword = "二氧化硫";
 | 
						|
                    standard = "15μg/m3";
 | 
						|
                    if (double.Parse(value) >= 15)
 | 
						|
                    {
 | 
						|
                        level = "优";
 | 
						|
                    }
 | 
						|
                    else if (double.Parse(value) >= 35)
 | 
						|
                    {
 | 
						|
                        level = "中";
 | 
						|
                    }
 | 
						|
                    else
 | 
						|
                    {
 | 
						|
                        level = "差";
 | 
						|
                    }
 | 
						|
                    break;
 | 
						|
            }
 | 
						|
            return new Tuple<string, string, string>(keyword, standard, level);
 | 
						|
        }
 | 
						|
        public class Quality
 | 
						|
        {
 | 
						|
            /// <summary>
 | 
						|
            /// 空气质量key
 | 
						|
            /// </summary>
 | 
						|
            public string key { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// 空气质量指数数值
 | 
						|
            /// </summary>
 | 
						|
            public string val { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// 空气质量指数级别
 | 
						|
            /// </summary>
 | 
						|
            public string level { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// 空气质量指数标准
 | 
						|
            /// </summary>
 | 
						|
            public string standard { get; set; }
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 空气质量数据
 | 
						|
        /// </summary>
 | 
						|
        public class AirQuality
 | 
						|
        {
 | 
						|
            // public string Area { get; set; }
 | 
						|
            //   public int CityCode { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// 一氧化碳
 | 
						|
            /// </summary>
 | 
						|
            public string CO { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// 二氧化氮
 | 
						|
            /// </summary>
 | 
						|
            public string NO2 { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// 臭氧
 | 
						|
            /// </summary>
 | 
						|
            public string O3 { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// PM10
 | 
						|
            /// </summary>
 | 
						|
            public string PM10 { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// PM25
 | 
						|
            /// </summary>
 | 
						|
            public string PM2_5 { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// 二氧化硫
 | 
						|
            /// </summary>
 | 
						|
            public string SO2 { get; set; }
 | 
						|
        }
 | 
						|
        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; }
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |