120 lines
4.9 KiB
C#
120 lines
4.9 KiB
C#
using langguanApi.Common.Redis;
|
||
using langguanApi.Common;
|
||
using langguanApi.Extensions.AutoDI;
|
||
using langguanApi.Model;
|
||
using System.Linq.Expressions;
|
||
using langguanApi.Common.Proxy;
|
||
using langguanApi.Model.Dto;
|
||
|
||
namespace langguanApi.Service
|
||
{
|
||
/// <summary>
|
||
/// HomeService
|
||
/// </summary>
|
||
[ServiceInjection(InjectionType.Singleton)]
|
||
public class HomeService
|
||
{
|
||
private DeviceService _deviceService;
|
||
private Hj212Service _hj212Service;
|
||
private readonly IConfiguration _configuration;
|
||
private CacheManager _cacheManager;
|
||
private readonly WeatherService _weatherService;
|
||
private readonly AlertService _alertService;
|
||
private readonly DetectionService _detectionService;
|
||
private HttpProxy _httpProxy;
|
||
/// <summary>
|
||
/// HomeService
|
||
/// </summary>
|
||
/// <param name="device"></param>
|
||
/// <param name="hj212Service"></param>
|
||
public HomeService(DeviceService device, Hj212Service hj212Service,
|
||
IConfiguration configuration, CacheManager cacheManager,
|
||
WeatherService weatherService, AlertService alertService,
|
||
DetectionService detectionService, HttpProxy httpProxy)
|
||
{
|
||
_deviceService = device;
|
||
_hj212Service = hj212Service;
|
||
_configuration = configuration;
|
||
_cacheManager = cacheManager;
|
||
_weatherService = weatherService;
|
||
_alertService = alertService;
|
||
_detectionService = detectionService;
|
||
_httpProxy = httpProxy;
|
||
}
|
||
/// <summary>
|
||
/// view
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public async Task<ApiResult> View()
|
||
{
|
||
var devices = await _deviceService.GetAsync();
|
||
var cleanData = new
|
||
{
|
||
Yesterday = 0.8,
|
||
LastWeek = 0.6,
|
||
};
|
||
// 首页热力图
|
||
var hotmap = new { };
|
||
// 首页设备报警数据
|
||
var alerts = await _alertService.IndexData();
|
||
// 首页设备检测数据
|
||
var detections = await _detectionService.GetIndexData();
|
||
// 获取天气信息,缓存1小时,如果不存在,则调用WeatherService获取
|
||
Func<Task<object>> getWeatherFunc = async () => await _weatherService.GetWeather();
|
||
var weather = await _cacheManager.GetConvertVale(RedisKeylist.Weather, getWeatherFunc, 60 * 60*24*30);
|
||
// 获取空气质量,缓存2小时,如果不存在,则调用WeatherService获取
|
||
Func<Task<object>> getAriQualityFunc = async () => await _weatherService.GetAirQuality();
|
||
var ariQuality = await _cacheManager.GetConvertVale(RedisKeylist.AriQuality, getAriQualityFunc, 60 * 60 * 24 * 30);
|
||
Func<Task<object>> getTrendFunc = async () => await _hj212Service.GetIndexData();
|
||
var trend = await _cacheManager.GetConvertVale(RedisKeylist.Trend, getTrendFunc, 60 * 60 * 24 * 30);
|
||
// 获取远程接口污染物排放率
|
||
var rateResp = await _httpProxy.Get<RespModel<List<Rate>>>(null, _configuration.GetValue<string>("Apis:RateUrl"));
|
||
var rate = rateResp.data.ToList().Take(6);
|
||
var AlermResp = await _httpProxy.Get<RespModel<alarmList>>(null, _configuration.GetValue<string>("Apis:AlertUrl"));
|
||
if (AlermResp.code == 0 && AlermResp.data.List.Any())
|
||
{
|
||
alerts.AddRange(AlermResp.data.List);
|
||
}
|
||
// alerts += await _httpProxy.Get<string>(null, _configuration.GetValue<string>("Apis:AlertUrl"));
|
||
// alerts += await _httpProxy.Get<string>(null, _configuration.GetValue<string>("Apis:RateUrl"));
|
||
|
||
//首页清洁运输比例
|
||
var cleaData = new
|
||
{
|
||
yesterday = 0.8,
|
||
lastWeek = 0.6
|
||
};
|
||
Func<Task<object>> getTodayFunc = async () => await _hj212Service.GetTodayData();
|
||
var today = await _cacheManager.GetConvertVale(RedisKeylist.Today, getTodayFunc, 60 * 60 * 24 * 30);
|
||
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,
|
||
hotmap,
|
||
weather,
|
||
ariQuality,
|
||
rate,
|
||
trend,
|
||
alerts,
|
||
cleanData,
|
||
today,
|
||
getViewTop = "",
|
||
detections
|
||
}
|
||
};
|
||
}
|
||
}
|
||
}
|