diff --git a/langguanApi.xml b/langguanApi.xml index 05606d3..a694e41 100644 --- a/langguanApi.xml +++ b/langguanApi.xml @@ -34,6 +34,14 @@ + + + post数据 + + + + + sub @@ -201,6 +209,34 @@ + + + 环境治理页面接口 + + + + + 环境治理服务 + + + + + 构造函数 + + + + + + 获取设备状态数据 + + + + + + 环境治理首页 + + + 获取指定设备的历史数据 @@ -794,6 +830,46 @@ 原始数据 是否通过 + + + RespModel + + + + + 状态码 + + + + + 在线 + + + + + 报警信息 + + + + + 设备列表 + + + + + 设备类型 1雾炮 2雾桩 3干雾 + + + + + 日志 + + + + + 设备状态 + + 用于用户管理的Dto @@ -1886,6 +1962,24 @@ + + + 环境治理页面Service + + + + + 首页数据 + + + + + + 获取设备状态 + + + + 首页数据 @@ -2079,6 +2173,12 @@ + + + 获取菜单树 + + + 递归获取子菜单列表 diff --git a/langguanApi/Common/Proxy/HttpProxy.cs b/langguanApi/Common/Proxy/HttpProxy.cs index a79f303..3a323ff 100644 --- a/langguanApi/Common/Proxy/HttpProxy.cs +++ b/langguanApi/Common/Proxy/HttpProxy.cs @@ -44,5 +44,33 @@ namespace langguanApi.Common.Proxy return default(T); } } + /// + /// post数据 + /// + /// + /// + /// + public async Task PostData(object data, string requestUri) + { + StringContent content = null; + var client = _httpClientFactory.CreateClient(); + client.BaseAddress = new Uri(requestUri); + if (data != null) + { + string json = Newtonsoft.Json.JsonConvert.SerializeObject(data); + content = new StringContent(json, System.Text.Encoding.UTF8, "application/json"); + } + var response = await client.PostAsync(requestUri, content); + if (response.IsSuccessStatusCode) + { + var result = await response.Content.ReadAsStringAsync(); + return Newtonsoft.Json.JsonConvert.DeserializeObject(result); + } + else + { + Console.WriteLine($"请求失败,状态码:{response.StatusCode},请求地址 {requestUri}"); + return default(T); + } + } } } diff --git a/langguanApi/Controllers/AlertController.cs b/langguanApi/Controllers/AlertController.cs index d97b87f..e91eb47 100644 --- a/langguanApi/Controllers/AlertController.cs +++ b/langguanApi/Controllers/AlertController.cs @@ -1,4 +1,6 @@ -using langguanApi.Model; +using langguanApi.Common.Proxy; +using langguanApi.Model; +using langguanApi.Model.Dto; using langguanApi.Service; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -10,7 +12,8 @@ namespace langguanApi.Controllers public class AlertController : ControllerBase { private readonly AlertService _alertService; - public AlertController(AlertService alertService) + + public AlertController(AlertService alertService, HttpProxy httpProxy, IConfiguration configuration) { _alertService = alertService; } @@ -25,6 +28,7 @@ namespace langguanApi.Controllers { start = start.HasValue ? start.Value.Date : DateTime.Now.AddDays(-7); end = end.HasValue ? end.Value.Date.AddDays(1) : DateTime.Now.Date.AddDays(1); + var alerts = await _alertService.ExportData(start, end); return File(alerts, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", $"{start.Value.ToString("yyyy-MM-dd")}-{end.Value.ToString("yyyy-MM-dd")}_设备报警.xlsx"); diff --git a/langguanApi/Controllers/EnvGovController.cs b/langguanApi/Controllers/EnvGovController.cs new file mode 100644 index 0000000..09836f0 --- /dev/null +++ b/langguanApi/Controllers/EnvGovController.cs @@ -0,0 +1,47 @@ +using langguanApi.Service; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace langguanApi.Controllers +{ + /// + /// 环境治理页面接口 + /// + [Route("api/[controller]")] + [ApiController] + public class EnvGovController : ControllerBase + { + /// + /// 环境治理服务 + /// + private readonly EnvGovService _envGovService; + /// + /// 构造函数 + /// + /// + public EnvGovController(EnvGovService envGovService) + { + _envGovService = envGovService; + } + /// + /// 获取设备状态数据 + /// + /// + [HttpGet("DeviceStatu")] + public async Task GetEnvGovData(string deviceId) + { + var data = await _envGovService.DeviceStatu(deviceId); + return Ok(data); + } + /// + /// 环境治理首页 + /// + /// + [HttpGet("Index")] + public async Task IndexView() + { + var result = await _envGovService.IndexView(); + return Ok(result); + } + } +} diff --git a/langguanApi/Controllers/UserController.cs b/langguanApi/Controllers/UserController.cs index ad69e3a..0d1e8e2 100644 --- a/langguanApi/Controllers/UserController.cs +++ b/langguanApi/Controllers/UserController.cs @@ -24,14 +24,14 @@ namespace langguanApi.Controllers [HttpPost("login")] public async Task Login([FromBody] UserLogin user) { - var result = _userService.login(user.Username, user.Password); + var result = await _userService.login(user.Username, user.Password); if (result != null) { - return Ok(result); + return Ok(new ApiResult() { data = result }); } else { - return BadRequest(result); + return Ok(new ApiResult() { code = -1, msg = "用户名或密码错误" }); } } /// diff --git a/langguanApi/Model/Dto/RespModel.cs b/langguanApi/Model/Dto/RespModel.cs new file mode 100644 index 0000000..2f993de --- /dev/null +++ b/langguanApi/Model/Dto/RespModel.cs @@ -0,0 +1,78 @@ +namespace langguanApi.Model.Dto +{ + /// + /// RespModel + /// + public class RespModel + { + /// + /// 状态码 + /// + public int code { get; set; } + public T data { get; set; } + public string msg { get; set; } + } + /// + /// 在线 + /// + public class Rate + { + public string key { get; set; } + public string val { get; set; } + } + public class alarmList + { + public List List { get; set; } + } + /// + /// 报警信息 + /// + public class alarm + { + public string DeviceName { get; set; } + public string AlarmName { get; set; } + public string Time { get; set; } + } + public class devceList + { + public List List { get; set; } + } + /// + /// 设备列表 + /// + public class Devce + { + public string DeviceName { get; set; } + public string DeviceID { get; set; } + public string GroupID { get; set; } + /// + /// 设备类型 1雾炮 2雾桩 3干雾 + /// + public int DeviceType { get; set; } + public double Lon { get; set; } + public double Lat { get; set; } + public List RunLog { get; set; } + public double CurrentAngel { get; set; } + + } + /// + /// 日志 + /// + public class Devlogs + { + public string Time { get; set; } + public string Text { get; set; } + } + /// + /// 设备状态 + /// + public class DeviceStatu + { + public string DeviceID { get; set; } + public string DeviceName { get; set; } + public double ElecInfo { get; set; } + public double WaterFlow { get; set; } + public double Pressure { get; set; } + public long RunTime { get; set; } + } +} diff --git a/langguanApi/Model/Entity/UserEntity.cs b/langguanApi/Model/Entity/UserEntity.cs index c37d11c..71dd65b 100644 --- a/langguanApi/Model/Entity/UserEntity.cs +++ b/langguanApi/Model/Entity/UserEntity.cs @@ -70,5 +70,11 @@ namespace langguanApi.Model.Entity { public string RoleName { get; set; } } + public class UserAnMenusViewModel + { + public UserEntity UserInfo { get; set; } + public string RoleName { get; set; } + public List Menus { get; set; } + } } diff --git a/langguanApi/Model/FactorCode.cs b/langguanApi/Model/FactorCode.cs index 5678686..b8975e8 100644 --- a/langguanApi/Model/FactorCode.cs +++ b/langguanApi/Model/FactorCode.cs @@ -416,6 +416,7 @@ a34040, a99010, a99049, + a99054 //a99051, #endregion } diff --git a/langguanApi/Service/AlertService.cs b/langguanApi/Service/AlertService.cs index eeac91e..2d11ab8 100644 --- a/langguanApi/Service/AlertService.cs +++ b/langguanApi/Service/AlertService.cs @@ -1,4 +1,5 @@ -using langguanApi.Extensions.AutoDI; +using langguanApi.Common.Proxy; +using langguanApi.Extensions.AutoDI; using langguanApi.Model; using langguanApi.Model.Dto; using langguanApi.Model.Entity; @@ -11,8 +12,12 @@ namespace langguanApi.Service [ServiceInjection(InjectionType.Transient)] public class AlertService : BaseService { - public AlertService(IConfiguration config) : base(config, nameof(Alert)) + private readonly HttpProxy _httpProxy; + private readonly IConfiguration _configuration; + public AlertService(IConfiguration config, HttpProxy httpProxy) : base(config, nameof(Alert)) { + _httpProxy = httpProxy; + _configuration = config; } /// /// 新加 @@ -54,13 +59,35 @@ namespace langguanApi.Service /// public async Task ExportData(DateTime? start, DateTime? end) { - Expression> exp = filter => filter.CreateDateTime >= start - && filter.CreateDateTime <= end - && filter.IsDelete == false; + && filter.CreateDateTime <= end && filter.IsDelete == false; + List result = new List(); + //获取接口报警数据 + Dictionary dic = new Dictionary() { }; + dic.Add("BeginTime", start.Value.ToString("yyyy-MM-dd HH:mm:ss")); + dic.Add("EndTime", end.Value.ToString("yyyy-MM-dd HH:mm:ss")); + var AlermResp = await _httpProxy.Get>(dic,_configuration.GetValue("Apis:AlertUrl")); + if (AlermResp.code == 0) + { + var romte = AlermResp.data.List.Select(s => new Alert + { + AlertContent = s.AlarmName, + deviceName = s.DeviceName, + CreateDateTime = DateTime.Parse(s.Time) + }).ToList(); + if (romte.Any()) + { + result.AddRange(romte); + } + } var list = (await base.GetListWithExp(exp)).OrderByDescending(s => s.CreateDateTime).ToList(); if (list.Any()) { + result.AddRange(list); + } + if (result.Any()) + { + result = result.OrderByDescending(s => s.CreateDateTime).ToList(); var mapper = new Mapper(); mapper.Map("报警开始时间", s => s.CreateDateTime) .Map("设备名称", s => s.deviceName) @@ -68,7 +95,7 @@ namespace langguanApi.Service .Map("内容", s => s.AlertContent) .Format("yyyy-MM-dd HH:mm:ss", s => s.CreateDateTime); MemoryStream stream = new MemoryStream(); - mapper.Save(stream, list, sheetName: "sheet1", leaveOpen: true); + mapper.Save(stream, result, sheetName: "sheet1", leaveOpen: true); return stream.ToArray(); } return null; diff --git a/langguanApi/Service/EnvGovService.cs b/langguanApi/Service/EnvGovService.cs new file mode 100644 index 0000000..997a548 --- /dev/null +++ b/langguanApi/Service/EnvGovService.cs @@ -0,0 +1,97 @@ +using IceCoffee.Common.Templates; +using langguanApi.Common.Proxy; +using langguanApi.Extensions.AutoDI; +using langguanApi.Model; +using langguanApi.Model.Dto; +using Microsoft.AspNetCore.Mvc.ApiExplorer; +using Microsoft.OpenApi.Writers; + +namespace langguanApi.Service +{ + /// + /// 环境治理页面Service + /// + [ServiceInjection(InjectionType.Transient)] + public class EnvGovService + { + private HttpProxy _httpProxy; + private readonly IConfiguration _configuration; + + public EnvGovService(HttpProxy httpProxy, IConfiguration configuration) + { + _httpProxy = httpProxy; + _configuration = configuration; + } + /// + /// 首页数据 + /// + /// + public async Task IndexView() + { + var url = _configuration.GetValue("Apis:DeviceList"); + var deviceResp = await _httpProxy.Get>(null, url); + var deviceList = deviceResp.data.List.GroupBy(g => g.GroupID) + .Select(s => new + { + group = s.Key, + items = new + { + devices = s.ToList().GroupBy(m => m.DeviceType) + .Select(o => new + { + devicetype = ConvertDeviceType(o.Key), + items = o.ToList() + .Select(d => new + { + d.DeviceID, + d.DeviceName, + d.Lon, + d.Lat, + d.CurrentAngel, + d.RunLog + }) + }) + } + }); + return new ApiResult + { + code = deviceResp.code, + msg = deviceResp.msg, + data = deviceList + }; + } + + private string ConvertDeviceType(int deviceType) + { + switch (deviceType) + { + case 1: + return "雾炮"; + case 2: + return "雾桩"; + case 3: + return "干雾"; + } + return null; + } + /// + /// 获取设备状态 + /// + /// + /// + public async Task DeviceStatu(string deviceId) + { + var url = _configuration.GetValue("Apis:DeviceStatu"); + var deviceStatuResp = await _httpProxy.Get>>(null, url); + var result = deviceStatuResp.data.Where(s => s.DeviceID == deviceId).FirstOrDefault(); + return new ApiResult + { + //接口返回错误,暂时注释 + // code = deviceStatuResp.code, + code = 0, + msg = deviceStatuResp.msg, + data = result + }; + } + } +} diff --git a/langguanApi/Service/HJ212SocketServer.cs b/langguanApi/Service/HJ212SocketServer.cs index 049d6fa..60cbd55 100644 --- a/langguanApi/Service/HJ212SocketServer.cs +++ b/langguanApi/Service/HJ212SocketServer.cs @@ -96,13 +96,14 @@ namespace langguanApi.Service if (hj.DecodeData(rawText)) { var body = JsonConvert.SerializeObject(hj.CP); + Console.WriteLine("解析成功: " + body); var entity = JsonConvert.DeserializeObject(body); entity.deviceMN = hj.DATA_HEAD["MN"]; await _deviceService.Add(new DeviceAddDto { deviceMN = hj.DATA_HEAD["MN"], DeviceType = SetDeviceType(hj.DATA_HEAD["ST"]), - Ip= session.RemoteIPEndPoint.ToString(), + Ip = session.RemoteIPEndPoint.ToString(), }); //校验通过,开始入库 await _hj212Service.Add(entity, session.RemoteIPEndPoint.ToString()); diff --git a/langguanApi/Service/HomeService.cs b/langguanApi/Service/HomeService.cs index 0089e7b..2aef934 100644 --- a/langguanApi/Service/HomeService.cs +++ b/langguanApi/Service/HomeService.cs @@ -4,6 +4,7 @@ using langguanApi.Extensions.AutoDI; using langguanApi.Model; using System.Linq.Expressions; using langguanApi.Common.Proxy; +using langguanApi.Model.Dto; namespace langguanApi.Service { @@ -66,8 +67,12 @@ namespace langguanApi.Service var ariQuality = await _cacheManager.GetConvertVale(RedisKeylist.AriQuality, getAriQualityFunc, 60 * 120); Func> getTrendFunc = async () => await _hj212Service.GetIndexData(); var trend = await _cacheManager.GetConvertVale(RedisKeylist.Trend, getTrendFunc, 60 * new Random().Next(70)); - // alerts += await _httpProxy.Get(null, _configuration.GetValue("Apis:AlertUrl")); - // alerts += await _httpProxy.Get(null, _configuration.GetValue("Apis:RateUrl")); + // 获取远程接口污染物排放率 + var rateResp = await _httpProxy.Get>>(null, _configuration.GetValue("Apis:RateUrl")); + var rate = rateResp.data.ToList(); + var AlermResp = await _httpProxy.Get>(null, _configuration.GetValue("Apis:AlertUrl")); + // alerts += await _httpProxy.Get(null, _configuration.GetValue("Apis:AlertUrl")); + // alerts += await _httpProxy.Get(null, _configuration.GetValue("Apis:RateUrl")); //首页清洁运输比例 var cleaData = new @@ -97,10 +102,7 @@ namespace langguanApi.Service hotmap, weather, ariQuality, - rate = new[] { - new { key = "cems", val = 0.7 } , - new { key = "VOC", val = 0.6 } - }, + rate, trend, alerts, cleanData, diff --git a/langguanApi/Service/MenuService.cs b/langguanApi/Service/MenuService.cs index e22dfb7..6855264 100644 --- a/langguanApi/Service/MenuService.cs +++ b/langguanApi/Service/MenuService.cs @@ -48,6 +48,15 @@ namespace langguanApi.Service /// /// public async Task GetMenuTree() + { + var result = await GetMenuTreeList(); + return new ApiResult() { data = result }; + } + /// + /// 获取菜单树 + /// + /// + public async Task> GetMenuTreeList() { List dto = new List(); var MenuList = await GetChildList("0"); @@ -63,7 +72,7 @@ namespace langguanApi.Service Children = await GetChildList(item.Id) }); } - return new ApiResult() { data = dto }; + return dto; } /// /// 递归获取子菜单列表 diff --git a/langguanApi/Service/UserService.cs b/langguanApi/Service/UserService.cs index fda7d9c..288fd1a 100644 --- a/langguanApi/Service/UserService.cs +++ b/langguanApi/Service/UserService.cs @@ -19,10 +19,15 @@ namespace langguanApi.Service { private ILogger _logger; private RoleService _roleService; - public UserService(IConfiguration config, ILogger logger, RoleService roleService) : base(config, nameof(UserEntity).Replace("Entity", "")) + private RoleMenuServie _roleMenuServie; + private MenuService _menuService; + public UserService(IConfiguration config, ILogger logger, + RoleService roleService, RoleMenuServie roleMenuServie, MenuService menuService) : base(config, nameof(UserEntity).Replace("Entity", "")) { _logger = logger; _roleService = roleService; + _roleMenuServie = roleMenuServie; + _menuService = menuService; } /// /// 登录 @@ -30,12 +35,23 @@ namespace langguanApi.Service /// /// /// - public async Task login(string username, string password) + public async Task login(string username, string password) { Expression> exp = filter => filter.Username == username && filter.Password == StringHelper.MD5Encrypt32(password); - var list = await base.GetListWithExp(exp); - return list.FirstOrDefault(); + var userEntity = (await base.GetListWithExp(exp)).FirstOrDefault(); + if (userEntity != null) + { + var menuIds = await _roleMenuServie.GetByRoleId(userEntity.RoleId); + var menus = await _menuService.GetMenuTreeList(); + return new UserAnMenusViewModel() + { + UserInfo = userEntity, + RoleName = (await _roleService.GetAsync(userEntity.RoleId))?.RoleName, + Menus = menus + }; + } + return null; } /// /// 根据用户名获取用户信息 diff --git a/langguanApi/appsettings.json b/langguanApi/appsettings.json index b1f015a..d9e74dd 100644 --- a/langguanApi/appsettings.json +++ b/langguanApi/appsettings.json @@ -18,10 +18,10 @@ "Weather": "https://weather.cma.cn/api/now/54511", //天气预报地址,中国气象 "AirQuality": "https://air.cnemc.cn:18007/CityData/GetAQIDataPublishLiveInfo?cityCode=110000", //空气质量API地址 "Apis": { - "RateUrl": "https://air.cnemc.cn:18007/CityData/GetAQIDataPublishLiveInfo?cityCode=110000", //首页设备在线API地址 - "AlertUrl": "https://air.cnemc.cn:18007/CityData/GetAQIDataPublishLiveInfo?cityCode=110000", //首页设报警API地址 - "DeviceList": "https://air.cnemc.cn:18007/CityData/GetAQIDataPublishLiveInfo?cityCode=110000", //环境治理 API地址 - "DeviceInfo": "https://air.cnemc.cn:18007/CityData/GetAQIDataPublishLiveInfo?cityCode=110000" //设备明细 API地址 + "RateUrl": "https://mock.apipark.cn/m1/4687001-0-default/DustApi/DeviceOnlineRate", //首页设备在线率API地址 + "AlertUrl": "https://mock.apipark.cn/m1/4687001-0-default/DustApi/DeviceHistoryAlarm", //首页设报警API地址 + "DeviceList": "https://mock.apipark.cn/m1/4687001-0-default/DustApi/GetDeviceList", //环境治理 API地址 + "DeviceStatu": "https://mock.apipark.cn/m1/4687001-0-default/DustApi/GetDeviceStatu" //设备明细 API地址 }, "Home": {