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-06-13 16:39:07 +00:00
|
|
|
|
private readonly DetectionService _detectionService;
|
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,
|
|
|
|
|
|
DetectionService detectionService)
|
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-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-05-20 14:56:49 +00:00
|
|
|
|
Expression<Func<Model.HJ212, bool>> filter = exp => true;
|
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);
|
2024-06-14 14:33:39 +00:00
|
|
|
|
Func<Task<object>> getTrendFunc = async () => await _hj212Service.GetIndexData();
|
|
|
|
|
|
var trend = await _cacheManager.GetConvertVale(RedisKeylist.Trend, getTrendFunc, 60 * new Random().Next(70));
|
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-15 19:17:41 +00:00
|
|
|
|
weather,
|
2024-05-20 14:56:49 +00:00
|
|
|
|
ariQuality,
|
2024-06-15 19:17:41 +00:00
|
|
|
|
rate = new[] {
|
|
|
|
|
|
new { key = "cems", val = 0.7 } ,
|
|
|
|
|
|
new { key = "VOC", val = 0.6 }
|
|
|
|
|
|
},
|
2024-06-14 14:33:39 +00:00
|
|
|
|
trend,
|
2024-06-13 16:39:07 +00:00
|
|
|
|
alerts,
|
2024-06-15 19:17:41 +00:00
|
|
|
|
cleanData,
|
|
|
|
|
|
today,
|
2024-06-14 14:33:39 +00:00
|
|
|
|
getViewTop = "",
|
2024-06-13 16:39:07 +00:00
|
|
|
|
detections
|
2024-05-20 14:56:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|