lg_backend/langguanApi/Service/HomeService.cs

117 lines
4.9 KiB
C#
Raw 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;
2024-06-18 16:23:45 +00:00
using langguanApi.Common.Proxy;
2024-06-24 15:13:14 +00:00
using langguanApi.Model.Dto;
2024-05-20 14:56:49 +00:00
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-06-13 16:39:07 +00:00
private readonly DetectionService _detectionService;
2024-06-18 16:23:45 +00:00
private HttpProxy _httpProxy;
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-06-13 16:39:07 +00:00
WeatherService weatherService, AlertService alertService,
2024-06-18 16:23:45 +00:00
DetectionService detectionService, HttpProxy httpProxy)
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-06-13 16:39:07 +00:00
_detectionService = detectionService;
2024-06-18 16:23:45 +00:00
_httpProxy = httpProxy;
2024-05-20 14:56:49 +00:00
}
/// <summary>
/// view
/// </summary>
/// <returns></returns>
public async Task<ApiResult> View()
{
var devices = await _deviceService.GetAsync();
2024-05-27 15:42:08 +00:00
var cleanData = new
{
Yesterday = 0.8,
LastWeek = 0.6,
};
2024-06-18 16:23:45 +00:00
// 首页热力图
var hotmap = new { };
2024-06-13 16:39:07 +00:00
// 首页设备报警数据
2024-05-21 15:53:40 +00:00
var alerts = await _alertService.IndexData();
2024-06-13 16:39:07 +00:00
// 首页设备检测数据
var detections = await _detectionService.GetIndexData();
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);
2024-06-13 16:39:07 +00:00
// 获取空气质量缓存2小时如果不存在则调用WeatherService获取
Func<Task<object>> getAriQualityFunc = async () => await _weatherService.GetAirQuality();
2024-06-12 16:47:19 +00:00
var ariQuality = await _cacheManager.GetConvertVale(RedisKeylist.AriQuality, getAriQualityFunc, 60 * 120);
Func<Task<object>> getTrendFunc = async () => await _hj212Service.GetIndexData();
var trend = await _cacheManager.GetConvertVale(RedisKeylist.Trend, getTrendFunc, 60 * new Random().Next(70));
2024-06-24 15:13:14 +00:00
// 获取远程接口污染物排放率
var rateResp = await _httpProxy.Get<RespModel<List<Rate>>>(null, _configuration.GetValue<string>("Apis:RateUrl"));
var rate = rateResp.data.ToList().Take(6);
2024-06-24 15:13:14 +00:00
var AlermResp = await _httpProxy.Get<RespModel<alarmList>>(null, _configuration.GetValue<string>("Apis:AlertUrl"));
// alerts += await _httpProxy.Get<string>(null, _configuration.GetValue<string>("Apis:AlertUrl"));
// alerts += await _httpProxy.Get<string>(null, _configuration.GetValue<string>("Apis:RateUrl"));
2024-06-18 16:23:45 +00:00
2024-06-13 16:39:07 +00:00
//首页清洁运输比例
var cleaData = new
{
yesterday = 0.8,
lastWeek = 0.6
};
2024-06-15 19:17:41 +00:00
var d1 = await _hj212Service.GetTodayData();
Func<Task<object>> getTodayFunc = async () => await _hj212Service.GetTodayData();
var today = await _cacheManager.GetConvertVale(RedisKeylist.Today, getTodayFunc, 60 * 30);
2024-05-20 14:56:49 +00:00
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"),
},
2024-06-15 19:17:41 +00:00
2024-05-20 14:56:49 +00:00
devices,
2024-06-18 16:23:45 +00:00
hotmap,
2024-06-15 19:17:41 +00:00
weather,
2024-05-20 14:56:49 +00:00
ariQuality,
2024-06-24 15:13:14 +00:00
rate,
trend,
alerts= AlermResp.data.List,
2024-06-15 19:17:41 +00:00
cleanData,
today,
getViewTop = "",
2024-06-13 16:39:07 +00:00
detections
2024-05-20 14:56:49 +00:00
}
};
}
}
}