diff --git a/Common/Redis/RedisKeyList.cs b/Common/Redis/RedisKeyList.cs index 6c03f11..7fcc18a 100644 --- a/Common/Redis/RedisKeyList.cs +++ b/Common/Redis/RedisKeyList.cs @@ -31,6 +31,10 @@ return $"device_status_{sn}"; } /// + /// 天气预报 + /// + public static string Index_Weather = "index_weather"; + /// /// 缓存设备token /// /// diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index de97d07..3b6136a 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -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; /// /// /// /// /// /// - public HomeController(DeviceService deviceService, PositionService positionService, RedisService redisService) + /// + /// + public HomeController(DeviceService deviceService, PositionService positionService, + RedisService redisService, AlarmService alarmService, WeatherService weatherService) { _deviceService = deviceService; _positionService = positionService; _redisService = redisService; + _alarmService = alarmService; + _weatherService = weatherService; } /// /// 首页 @@ -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); } diff --git a/Model/DeviceEntity.cs b/Model/DeviceEntity.cs index 6f04901..f933daf 100644 --- a/Model/DeviceEntity.cs +++ b/Model/DeviceEntity.cs @@ -65,6 +65,10 @@ namespace LY.App.Model /// 图片 /// public string Img { get; set; } + /// + /// 图片icon + /// + public string icon { get; set; } } @@ -105,6 +109,7 @@ namespace LY.App.Model /// 图片 /// public string Img { get; set; } + public string icon { get; set; } } public class UpdateDevice : AddDevice { diff --git a/Model/PositionDeviceDto.cs b/Model/PositionDeviceDto.cs index 6841382..569225b 100644 --- a/Model/PositionDeviceDto.cs +++ b/Model/PositionDeviceDto.cs @@ -3,9 +3,8 @@ using SqlSugar; namespace LY.App.Model { - public class PositionDeviceDto + public class PositionDeviceDto: PositionInfo { - public PositionInfo position { get; set; } public List 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; } + /// + /// 图片icon + /// + public string icon { get; set; } + public string Img { get; set; } } } diff --git a/Program.cs b/Program.cs index 830b68d..d25099a 100644 --- a/Program.cs +++ b/Program.cs @@ -45,6 +45,7 @@ string redisConnection = builder.Configuration.GetValue("Redis:Connectio builder.Services.AddSingleton(new RedisService(redisConnection)); ////עSignalR builder.Services.AddSignalR(); +builder.Services.AddHttpClient(); //עע builder.Services.ServicesAutoInjectionExtension(); //ݿ diff --git a/Service/AlarmService.cs b/Service/AlarmService.cs index b57f737..5d3ab29 100644 --- a/Service/AlarmService.cs +++ b/Service/AlarmService.cs @@ -163,6 +163,52 @@ namespace LY.App.Service }).ToListAsync(); return new ApiResult() { data = items }; } + + /// + /// 首页统计 + /// + /// + public async Task> IndexCount() + { + Dictionary result = new Dictionary(); + // 获取当前日期和时间 + DateTime currentDate = DateTime.Now; + // 获取当天的开始时间 + DateTime startDate = currentDate.Date; + // 获取当天的结束时间(23:59:59) + DateTime endDate = currentDate.Date.AddDays(1).AddTicks(-1); + //计算当天 + var todaywaring = await _db.Queryable().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() + // .Where(s => s.BatchId > 0) + // .Where(s => s.CreateTime >= startDate && s.CreateTime <= endDate); + //计算总数 + var totalcount = _db.Queryable().SplitTable() + .Where(s => s.alarmLevel > 0) + .GroupBy(s => s.BatchId) + .Select(s => s.BatchId); + //计算处理总数 + //var totalhandle = _db.Queryable() + // .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; + } /// /// 分页 /// diff --git a/Service/PositionService.cs b/Service/PositionService.cs index ff6458c..19337c7 100644 --- a/Service/PositionService.cs +++ b/Service/PositionService.cs @@ -141,14 +141,10 @@ namespace LY.App.Service .Where(s => positionIds.Contains(s.PositionId)) .Where(s => s.IsDeleted == false) .ToListAsync(); - List result = new List(); - foreach (var item in query) + List result = query.Adapt>(); + foreach (var item in result) { - result.Add(new PositionDeviceDto() - { - position = item, - Devices = deviceList.Where(s => s.PositionId == item.Id).ToList().Adapt>() - }); + item.Devices = deviceList.Where(s => s.PositionId == item.Id).ToList()?.Adapt>() ?? new List(); } return result; } diff --git a/Service/WeatherService.cs b/Service/WeatherService.cs new file mode 100644 index 0000000..d755086 --- /dev/null +++ b/Service/WeatherService.cs @@ -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; + } + /// + /// 爬气象局的天气数据% + /// + /// + public async Task 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(data); + return result?.data; + } + } + + } + catch (Exception ex) + { + + Console.WriteLine(ex.Message); + } + return null; + } + public class Location + { + /// + /// 54511 + /// + public string id { get; set; } + /// + /// 北京 + /// + public string name { get; set; } + /// + /// 中国, 北京, 北京 + /// + public string path { get; set; } + } + + public class Now + { + /// + /// Precipitation + /// + public double precipitation { get; set; } + /// + /// Temperature + /// + public double temperature { get; set; } + /// + /// Pressure + /// + public double pressure { get; set; } + /// + /// Humidity + /// + public double humidity { get; set; } + /// + /// 东北风 + /// + public string windDirection { get; set; } + /// + /// WindDirectionDegree + /// + public double windDirectionDegree { get; set; } + /// + /// WindSpeed + /// + public double windSpeed { get; set; } + /// + /// 微风 + /// + public string windScale { get; set; } + } + /// + /// + /// + public class Data + { + /// + /// Location + /// + public Location location { get; set; } + /// + /// Now + /// + public Now now { get; set; } + /// + /// Alarm + /// + public List alarm { get; set; } + /// + /// 2024/01/15 10:05 + /// + public DateTime lastUpdate { get; set; } + } + public class Root + { + /// + /// success + /// + public string msg { get; set; } + /// + /// Code + /// + public int code { get; set; } + /// + /// Data + /// + public Data data { get; set; } + } + } +} diff --git a/appsettings.json b/appsettings.json index 5ebb9fb..16eb2fd 100644 --- a/appsettings.json +++ b/appsettings.json @@ -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 //雪花算法的wordId,多台服务器时,需要配置不同的wordId,