using ProductionLineMonitor.Core.Models; using ProductionLineMonitor.Web.Services; using System; using System.Collections.Generic; using System.Linq; namespace ProductionLineMonitor.Application.Services.EnergyConsumptionService.Dtos { public class MachineElectricEnergyDto : Machine { public int Floor { get; set; } public int Line { get; set; } public List Meters { get; set; } = new List(); } public class MachineElectricEnergyMeterCheckDto : MachineElectricEnergyDto { public enum MachineMeterState { Normal, TimeOut, ValueDoesNotChange, MesApiGetNull, OValue } public int MachineState { get; set; } public MachineMeterState State { get; private set; } = MachineMeterState.Normal; public string Message { get; private set; } = string.Empty; public int TimeOutValue { get; private set; } = 10; public int Index { get; private set; } = 0; public int LastIndex { get; private set; } = 0; public List[] ElectricEnergyMeterDtos { get; private set; } = new List[10]; public void GetElectricEnergyMeterDtos() { if (Index >= 10) Index = 0; var dtos = MesApiService.GetElectricEnergyMeters(Topic).ToList(); ElectricEnergyMeterDtos[Index] = dtos; Index++; } public void Check() { DateTime time = DateTime.Now; if (Meters == null || Meters.Count() == 0) { State = MachineMeterState.MesApiGetNull; Message = $"待导入!"; return; } if (ElectricEnergyMeterDtos[Index - 1] == null || ElectricEnergyMeterDtos[Index - 1].Count() == 0) { State = MachineMeterState.MesApiGetNull; Message = $"MesApi 未获取到数据!"; return; } foreach (var electricEnergyMeterDto in ElectricEnergyMeterDtos[Index - 1]) { var meter = Meters.FirstOrDefault(x => x.SerialNumber == electricEnergyMeterDto.SerialNumber); if (meter == null) continue; if (!meter.Enable) continue; double min = (time - electricEnergyMeterDto.DataTime).TotalMinutes; if (min > TimeOutValue) { State = MachineMeterState.TimeOut; Message = $"编号 {electricEnergyMeterDto.SerialNumber} 超过 {(int)min} 分钟未推送!"; return; } if (electricEnergyMeterDto.ElectricEnergy == 0) { State = MachineMeterState.OValue; Message = $"编号 {electricEnergyMeterDto.SerialNumber} 能耗为 0!"; return; } if (Index - 2 < 0) LastIndex = 9; else LastIndex = Index - 2; var last = ElectricEnergyMeterDtos[LastIndex]; if (last == null) continue; var lastMeter = last.FirstOrDefault(x => x.SerialNumber == meter.SerialNumber); if (lastMeter == null) continue; if (electricEnergyMeterDto.ElectricEnergy < lastMeter.ElectricEnergy) { State = MachineMeterState.ValueDoesNotChange; Message = $"编号 {electricEnergyMeterDto.SerialNumber} 能耗在负增长!"; return; } if (Index == 10) LastIndex = 0; else LastIndex = Index; last = ElectricEnergyMeterDtos[LastIndex]; if (last == null) continue; lastMeter = last.FirstOrDefault(x => x.SerialNumber == meter.SerialNumber); if (lastMeter == null) continue; if (electricEnergyMeterDto.ElectricEnergy == lastMeter.ElectricEnergy && electricEnergyMeterDto.Uab == lastMeter.Uab && electricEnergyMeterDto.Ubc == lastMeter.Ubc && electricEnergyMeterDto.Uca == lastMeter.Uca && electricEnergyMeterDto.Ua == lastMeter.Ua && electricEnergyMeterDto.Ub == lastMeter.Ub && electricEnergyMeterDto.Uc == lastMeter.Uc && electricEnergyMeterDto.Ia == lastMeter.Ia && electricEnergyMeterDto.Ib == lastMeter.Ib && electricEnergyMeterDto.Ic == lastMeter.Ic) { double min1 = (electricEnergyMeterDto.DataTime - lastMeter.DataTime).TotalMinutes; State = MachineMeterState.ValueDoesNotChange; Message = $"编号 {electricEnergyMeterDto.SerialNumber} {(int)min1}分钟 值无变化!"; return; } } State = MachineMeterState.Normal; Message = "正常"; } } }