首页数据

This commit is contained in:
yanghongwei 2025-03-28 00:34:20 +08:00
parent 2d9492d4df
commit d445c36372
9 changed files with 218 additions and 11 deletions

View File

@ -31,6 +31,10 @@
return $"device_status_{sn}";
}
/// <summary>
/// 天气预报
/// </summary>
public static string Index_Weather = "index_weather";
/// <summary>
/// 缓存设备token
/// </summary>
/// <param name="id"></param>

View File

@ -18,17 +18,24 @@ namespace LY.App.Controllers
private readonly DeviceManager deviceManager = DeviceManager.Instance;
private readonly PositionService _positionService;
private readonly RedisService _redisService;
private readonly AlarmService _alarmService;
private readonly WeatherService _weatherService;
/// <summary>
///
/// </summary>
/// <param name="deviceService"></param>
/// <param name="positionService"></param>
/// <param name="redisService"></param>
public HomeController(DeviceService deviceService, PositionService positionService, RedisService redisService)
/// <param name="alarmService"></param>
/// <param name="weatherService"></param>
public HomeController(DeviceService deviceService, PositionService positionService,
RedisService redisService, AlarmService alarmService, WeatherService weatherService)
{
_deviceService = deviceService;
_positionService = positionService;
_redisService = redisService;
_alarmService = alarmService;
_weatherService = weatherService;
}
/// <summary>
/// 首页
@ -46,9 +53,17 @@ namespace LY.App.Controllers
item.IsOnline = await _redisService.ExistsAsync(RedisKeyList.DeviceStatus(item.DeviceSN));
}
});
var alarmCount = await _redisService.GetOrSetAsync(RedisKeyList.index_data(),
async () => await _alarmService.IndexCount(),
TimeSpan.FromDays(1));
var weather = await _redisService.GetOrSetAsync(RedisKeyList.Index_Weather,
async () => await _weatherService.GetWeather(),
TimeSpan.FromHours(3));
result.data = new
{
positions
positions,
alarmCount,
weather
};
return Ok(result);
}

View File

@ -65,6 +65,10 @@ namespace LY.App.Model
/// 图片
/// </summary>
public string Img { get; set; }
/// <summary>
/// 图片icon
/// </summary>
public string icon { get; set; }
}
@ -105,6 +109,7 @@ namespace LY.App.Model
/// 图片
/// </summary>
public string Img { get; set; }
public string icon { get; set; }
}
public class UpdateDevice : AddDevice
{

View File

@ -3,9 +3,8 @@ using SqlSugar;
namespace LY.App.Model
{
public class PositionDeviceDto
public class PositionDeviceDto: PositionInfo
{
public PositionInfo position { get; set; }
public List<DeviceItem> Devices { get; set; }
}
public class DeviceItem
@ -18,5 +17,10 @@ namespace LY.App.Model
public double Lon { get; set; }
public string Model { get; set; }
public bool IsOnline { get; set; }
/// <summary>
/// 图片icon
/// </summary>
public string icon { get; set; }
public string Img { get; set; }
}
}

View File

@ -45,6 +45,7 @@ string redisConnection = builder.Configuration.GetValue<string>("Redis:Connectio
builder.Services.AddSingleton(new RedisService(redisConnection));
////注册SignalR
builder.Services.AddSignalR();
builder.Services.AddHttpClient();
//注册依赖注入
builder.Services.ServicesAutoInjectionExtension();
//数据库

View File

@ -163,6 +163,52 @@ namespace LY.App.Service
}).ToListAsync();
return new ApiResult() { data = items };
}
/// <summary>
/// 首页统计
/// </summary>
/// <returns></returns>
public async Task<Dictionary<string, int>> IndexCount()
{
Dictionary<string, int> result = new Dictionary<string, int>();
// 获取当前日期和时间
DateTime currentDate = DateTime.Now;
// 获取当天的开始时间
DateTime startDate = currentDate.Date;
// 获取当天的结束时间23:59:59
DateTime endDate = currentDate.Date.AddDays(1).AddTicks(-1);
//计算当天
var todaywaring = await _db.Queryable<Alarm>().SplitTable()
.Where(s => s.alarmLevel > 0)
.Where(s => s.CreateTime >= startDate && s.CreateTime <= endDate)
.GroupBy(s => s.BatchId)
.Select(s => s.BatchId).CountAsync();
//计算当天处理次数
//var todayhandle = _db.Queryable<AttackEntity>()
// .Where(s => s.BatchId > 0)
// .Where(s => s.CreateTime >= startDate && s.CreateTime <= endDate);
//计算总数
var totalcount = _db.Queryable<Alarm>().SplitTable()
.Where(s => s.alarmLevel > 0)
.GroupBy(s => s.BatchId)
.Select(s => s.BatchId);
//计算处理总数
//var totalhandle = _db.Queryable<AttackEntity>()
// .Where(x => x.BatchId > 0);
//组合查询结果
//return new
//{
// todaywaring = await todaywaring.CountAsync(),
// todayhandle =0,
// totalcount = await totalcount.CountAsync(),
// totalhandle = 0
//};
result.Add("todaywaring", todaywaring);
result.Add("todayhandle", 0);
result.Add("totalcount", await totalcount.CountAsync());
result.Add("totalhandle", 0);
return result;
}
/// <summary>
/// 分页
/// </summary>

View File

@ -141,14 +141,10 @@ namespace LY.App.Service
.Where(s => positionIds.Contains(s.PositionId))
.Where(s => s.IsDeleted == false)
.ToListAsync();
List<PositionDeviceDto> result = new List<PositionDeviceDto>();
foreach (var item in query)
List<PositionDeviceDto> result = query.Adapt<List<PositionDeviceDto>>();
foreach (var item in result)
{
result.Add(new PositionDeviceDto()
{
position = item,
Devices = deviceList.Where(s => s.PositionId == item.Id).ToList().Adapt<List<DeviceItem>>()
});
item.Devices = deviceList.Where(s => s.PositionId == item.Id).ToList()?.Adapt<List<DeviceItem>>() ?? new List<DeviceItem>();
}
return result;
}

135
Service/WeatherService.cs Normal file
View File

@ -0,0 +1,135 @@
using LY.App.Extensions.DI;
using Newtonsoft.Json;
namespace LY.App.Service
{
[ServiceInjection(InjectionType.Scoped)]
public class WeatherService
{
private IHttpClientFactory _httpClientFactory;
private IConfiguration _configuration;
public WeatherService(IHttpClientFactory httpClientFactory,
IConfiguration configuration)
{
_httpClientFactory = httpClientFactory;
_configuration = configuration;
}
/// <summary>
/// 爬气象局的天气数据%
/// </summary>
/// <returns></returns>
public async Task<Data> GetWeather()
{
try
{
var client = _httpClientFactory.CreateClient();
var url = _configuration.GetSection("Weather").Value;
if (!string.IsNullOrEmpty(url))
{
var resp = await client.GetAsync(url);
if (resp.IsSuccessStatusCode)
{
var data = await resp.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<Root>(data);
return result?.data;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
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; }
}
}
}

View File

@ -26,6 +26,7 @@
"Redis": {
"ConnectionString": "101.43.201.20:6379,password=Aa123,abortConnect =false"
},
"Weather": "https://weather.cma.cn/api/now/54511", //,
"Vertify": 5, //
"BatchId": 60, //,
"SnowFlakeWordId": 1 //wordIdwordId