lg_backend/langguanApi/Service/HomeService.cs

83 lines
2.9 KiB
C#
Raw Permalink Normal View History

2024-05-20 14:56:49 +00:00
using langguanApi.Common.Redis;
using langguanApi.Common;
using langguanApi.Extensions.AutoDI;
using langguanApi.Model;
using System.Linq.Expressions;
namespace langguanApi.Service
{
/// <summary>
/// HomeService
/// </summary>
2024-05-20 15:10:31 +00:00
[ServiceInjection(InjectionType.Singleton)]
2024-05-20 14:56:49 +00:00
public class HomeService
{
private DeviceService _deviceService;
private Hj212Service _hj212Service;
private readonly IConfiguration _configuration;
private CacheManager _cacheManager;
private readonly WeatherService _weatherService;
2024-05-21 15:53:40 +00:00
private readonly AlertService _alertService;
2024-05-20 14:56:49 +00:00
/// <summary>
/// HomeService
/// </summary>
/// <param name="device"></param>
/// <param name="hj212Service"></param>
public HomeService(DeviceService device, Hj212Service hj212Service,
2024-05-27 15:42:08 +00:00
IConfiguration configuration, CacheManager cacheManager,
2024-05-21 15:53:40 +00:00
WeatherService weatherService, AlertService alertService)
2024-05-20 14:56:49 +00:00
{
_deviceService = device;
_hj212Service = hj212Service;
_configuration = configuration;
_cacheManager = cacheManager;
_weatherService = weatherService;
2024-05-21 15:53:40 +00:00
_alertService = alertService;
2024-05-20 14:56:49 +00:00
}
/// <summary>
/// view
/// </summary>
/// <returns></returns>
public async Task<ApiResult> View()
{
var devices = await _deviceService.GetAsync();
var ariQuality = "";
2024-05-27 15:42:08 +00:00
var cleanData = new
{
Yesterday = 0.8,
LastWeek = 0.6,
};
2024-05-20 14:56:49 +00:00
Expression<Func<Model.HJ212, bool>> filter = exp => true;
var Realtime = await _hj212Service.Realtime();
2024-05-21 15:53:40 +00:00
var getViewTop = await _hj212Service.GetViewTop();
var alerts = await _alertService.IndexData();
2024-05-20 14:56:49 +00:00
// 获取天气信息缓存1小时如果不存在则调用WeatherService获取
Func<Task<object>> getWeatherFunc = async () => await _weatherService.GetWeather();
var weather = await _cacheManager.GetConvertVale(RedisKeylist.Weather, getWeatherFunc, 60 * 60);
return new ApiResult
{
code = 0,
data = new
{
home = new
{
center = new
{
lon = _configuration.GetValue<double>("Home:Center:Lon"),
lat = _configuration.GetValue<double>("Home:Center:Lat"),
},
title = _configuration.GetValue<string>("Home:Title"),
},
devices,
ariQuality,
Realtime,
2024-05-21 15:53:40 +00:00
getViewTop,
weather,
alerts
2024-05-20 14:56:49 +00:00
}
};
}
}
}