using Abp.Authorization; using Abp.Domain.Repositories; using Abp.Domain.Uow; using iTextSharp.text; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Linq.Dynamic.Core; using System.Threading.Tasks; using ToolLibrary; using ToolLibrary.LogHelper; using YunDa.ISAS.Application.Core; using YunDa.ISAS.Application.Core.Session; using YunDa.ISAS.Application.Core.SwaggerHelper; using YunDa.ISAS.DataTransferObject; using YunDa.ISAS.DataTransferObject.Account; using YunDa.ISAS.DataTransferObject.EquipmentLiveData; using YunDa.ISAS.DataTransferObject.GeneralInformation.EquipmentInfoDto; using YunDa.ISAS.DataTransferObject.GeneralInformation.EquipmentInfoDto.SearchCondition; using YunDa.ISAS.Entities.ClientConfiguration.ThreeDimension; using YunDa.ISAS.Entities.DataMonitoring; using YunDa.ISAS.Entities.GeneralInformation; using YunDa.ISAS.Entities.MySQL.DataMonitoring; using YunDa.ISAS.Entities.VideoSurveillance; using YunDa.ISAS.Redis.Repositories; namespace YunDa.ISAS.Application.GeneralInformation { /// /// 设备信息 /// [Description("设备信息管理服务")] public class EquipmentLiveDataAppService : ISASAppServiceBase, IEquipmentLiveDataAppService { private LoginUserOutput _currentUser; private readonly IRepository _equipmentInfoRepository; private readonly IRepository _videoDevRepository; private readonly IRepository _linkageStrategyRepository; private readonly IRepository _measureTemperaturePointRepository; private readonly IRepository _equipmentTypeRepository; private readonly IRepository _presetPointRepository; private readonly IRepository _telemeteringConfigurationResitory; private readonly IRepository _telesignalisationConfigurationResitory; private readonly IRepository _telecommandConfigurationResitory; private readonly IRepository _telemeteringAlarmStrategyResitory; private readonly IRepository _telemeteringTemplateResitory; private readonly IRedisRepository _redisRepository; private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IRedisRepository _equipmentDataModelDicRedis; private readonly IRepository _threeDimensionResitory; public EquipmentLiveDataAppService( IRepository repositoryEquipmentInfo , IRepository repositoryEquipmentType , IRepository telemeteringConfigurationResitory , IRepository telesignalisationConfigurationResitory , IRepository telecommandConfigurationResitory , IRepository presetPointRepository , IRepository telemeteringAlarmStrategyResitory , IRepository telemeteringTemplateResitory , IRepository videoDevRepository , IRepository linkageStrategyRepository , IRepository measureTemperaturePointRepository , IRedisRepository redisRepository , IRedisRepository equipmentDataModelDicRedis , IRepository threeDimensionResitory , IUnitOfWorkManager unitOfWorkManager , ISessionAppService sessionAppService) : base(sessionAppService) { _equipmentInfoRepository = repositoryEquipmentInfo; _equipmentTypeRepository = repositoryEquipmentType; _telemeteringConfigurationResitory = telemeteringConfigurationResitory; _telesignalisationConfigurationResitory = telesignalisationConfigurationResitory; _telecommandConfigurationResitory = telecommandConfigurationResitory; _telemeteringAlarmStrategyResitory = telemeteringAlarmStrategyResitory; _presetPointRepository = presetPointRepository; _telemeteringTemplateResitory = telemeteringTemplateResitory; _currentUser = base.GetCurrentUser(); _unitOfWorkManager = unitOfWorkManager; _redisRepository = redisRepository; _equipmentDataModelDicRedis = equipmentDataModelDicRedis; _threeDimensionResitory = threeDimensionResitory; _videoDevRepository = videoDevRepository; _linkageStrategyRepository = linkageStrategyRepository; _measureTemperaturePointRepository = measureTemperaturePointRepository; } #region 基础接口 public RequestPageResult FindDatas(PageSearchCondition searchCondition) { throw new NotImplementedException(); } public Task> CreateOrUpdateAsync(EditEquipmentInfoInput input) { throw new NotImplementedException(); } public Task DeleteByIdsAsync(List ids) { throw new NotImplementedException(); } public Task DeleteByIdAsync(Guid id) { throw new NotImplementedException(); } #endregion /// /// 根据设备id获取设备实时运行数据 /// /// /// [HttpGet] [AbpAllowAnonymous] [ShowApi] public async Task> GetEquipmentLiveStateByEquipmentId(Guid id) { RequestResult rst = new RequestResult(); if (id == default) { rst.Message = "设备id为空"; return rst; } try { var dic = await _equipmentDataModelDicRedis.HashSetGetAllAsync(ConstantModel.EquipmentDataModelDicRedisKey); if (dic.Any(t=>t.EquipmentInfoId == id)) { var equipmentdata = dic.FirstOrDefault(t=> t.EquipmentInfoId == id); var cfgs = _threeDimensionResitory.GetAll().Where(t => t.EquipmentInfoId == id); equipmentdata.Telemeterings = equipmentdata.Telemeterings.Where(t => cfgs.Any(x => x.TelemeteringConfigurationId == t.Id)).ToList(); equipmentdata.Telesignalisations = equipmentdata.Telesignalisations.Where(t => cfgs.Any(x => x.TelesignalisationConfigurationId == t.Id)).ToList(); rst.Flag = true; rst.ResultData = equipmentdata; } } catch (Exception ex) { Log4Helper.Error(this.GetType(), "根据设备id获取设备实时运行数据出错", ex); } return rst; } /// /// 根据模型id获取设备实时运行数据 /// /// /// [HttpGet] [AbpAllowAnonymous] [ShowApi] public async Task> GetEquipmentLiveStateByModelId(int modelId) { RequestResult rst = new RequestResult(); if (modelId == 0) { rst.Message = "模型id为0"; return rst; } try { var ccDatas = _threeDimensionResitory.GetAll().ToList(); var dic = await _equipmentDataModelDicRedis.HashSetGetAllAsync(ConstantModel.EquipmentDataModelDicRedisKey); var cc = ccDatas.FirstOrDefault(t => t.ModelId == modelId); if (dic!=null&&cc != null&& cc.EquipmentInfoId.HasValue) { Guid id = cc.EquipmentInfoId.Value; if (dic.Any(t => t.EquipmentInfoId == id)) { var equipmentdata = dic.FirstOrDefault(t => t.EquipmentInfoId == id); var cfgs = ccDatas.Where(t => t.EquipmentInfoId == id); equipmentdata.Telemeterings = equipmentdata.Telemeterings.Where(t => cfgs.Any(x => x.TelemeteringConfigurationId == t.Id)).ToList(); equipmentdata.Telesignalisations = equipmentdata.Telesignalisations.Where(t => cfgs.Any(x => x.TelesignalisationConfigurationId == t.Id)).ToList(); rst.Flag = true; rst.ResultData = equipmentdata; } } } catch (Exception ex) { Log4Helper.Error(this.GetType(), "根据设备id获取设备实时运行数据出错", ex); } return rst; } /// /// 设备总览信息 /// /// /// [HttpGet] [AbpAllowAnonymous] [ShowApi] public async Task>> GetEquipmentOverviewAsync(Guid stationId) { RequestResult> rst = new(); try { if (stationId == default) { Log4Helper.Error(this.GetType(), "传入id为空"); } List datas = new List(); var equipmentTypesRepo = _equipmentTypeRepository.GetAllIncluding(t=>t.EquipmentInfos).OrderBy(t=>t.SeqNo).ToList(); var typesRepo = equipmentTypesRepo.Where(x => x.EquipmentTypeLevel == EquipmentTypeLevelEnum.Type); var equipmentTypeRepo = equipmentTypesRepo.Where(x => x.EquipmentTypeLevel == EquipmentTypeLevelEnum.Equipment); var dic = await _equipmentDataModelDicRedis.HashSetGetAllAsync(ConstantModel.EquipmentDataModelDicRedisKey); foreach (var item in typesRepo) { var equipments = equipmentTypeRepo.Where(t => t.EquipmentTypeId == item.Id).SelectMany(t => t.EquipmentInfos); var query = from equip in equipments join dataModel in dic on equip.Id equals dataModel.EquipmentInfoId where dataModel.Telemeterings!=null from telemetry in dataModel.Telemeterings where telemetry.ResultValue != -99999 select new { EquipmentId = equip.Id, // Include other properties as needed }; var query1 = from equip in equipments join dataModel in dic on equip.Id equals dataModel.EquipmentInfoId where dataModel.Telesignalisations != null from telemetry in dataModel.Telesignalisations where telemetry.ResultValue != -99999 select new { EquipmentId = equip.Id, // Include other properties as needed }; int lineCount = query.Concat(query1).Distinct().Count(); datas.Add(new EquimentOverviewOutput { Id = item.Id, SeqNo = datas.Count + 1, Count = equipments.Count(), LineCount = lineCount, IsVisable = true, TypeName = item.Name }); } var video = _videoDevRepository.GetAllIncluding(t=>t.PresetPoints).OrderBy(t=>t.SeqNo).ToList(); var nvr = video.Where(t => t.VideoDevId == null); int nvrOnlineCount = 0; //CameraOnlineInfo.GetDeviceConnected( nvr.Select(t=>t.IP).ToList()).Where(t=>t.Value).Count(); datas.Add(new EquimentOverviewOutput { Id = default, SeqNo = datas.Count + 1, Count = nvr.Count(), LineCount = nvrOnlineCount, IsVisable = true, TypeName = "NVR", Description = "" }); var camera = video.Where(t => t.VideoDevId != null); int cameraOnlineCount = 0;// //CameraOnlineInfo.GetDeviceConnected(camera.Where(x=> !string.IsNullOrWhiteSpace(x.IP)).Select(t => t.IP).ToList()).Where(t => t.Value).Count(); datas.Add(new EquimentOverviewOutput { Id = default, SeqNo = datas.Count + 1, Count = camera.Count(), LineCount = cameraOnlineCount, IsVisable = true, TypeName = "摄像头", Description = "" }); var presetsCount = _presetPointRepository.Count(); datas.Add(new EquimentOverviewOutput { Id = default, SeqNo = datas.Count + 1, Count = presetsCount, LineCount = -1, IsVisable = true, TypeName = "预置位" }); var linkageCount = _linkageStrategyRepository.Count(); datas.Add(new EquimentOverviewOutput { Id = default, SeqNo = datas.Count+1, Count = linkageCount, LineCount = -1, IsVisable = true, TypeName = "联动" }); var measureTempCount = _measureTemperaturePointRepository.Count(); datas.Add(new EquimentOverviewOutput { Id = default, SeqNo = datas.Count + 1, Count = measureTempCount, LineCount = -1, IsVisable = true, TypeName = "测温点" }); rst.ResultData = datas; rst.Flag = true; //_equipmentInfoRepository.GetAllIncluding(t => t.EquipmentType); } catch (Exception ex) { Log4Helper.Error(this.GetType(), "根据设备id获取设备实时运行数据出错", ex); } return rst; } } }