using Microsoft.AspNetCore.Mvc; using ProductionLineMonitor.Application.Services.EnergyConsumptionService.Dtos; using ProductionLineMonitor.Core.Dtos; using ProductionLineMonitor.Core.IRepositories; using ProductionLineMonitor.Core.Models; using ProductionLineMonitor.Core.Utils; using ProductionLineMonitor.Web.HostedServices; using ProductionLineMonitor.Web.Services; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading; namespace ProductionLineMonitor.Web.Controllers { public class EnergyConsumptionController : BaseController { private readonly IUnitOfWork _unitOfWork; public EnergyConsumptionController(IUnitOfWork unitOfWork) { _unitOfWork = unitOfWork; } [HttpGet] public IActionResult Index() { return View(); } [HttpPost] public IActionResult GetEnergyConsumptionDtoByMes(string ids, DateTime startTime, DateTime endTime) { ECharts eCharts = new ECharts { X = DateTimeHelper.GetDateShiftV1(startTime, endTime) }; string[] strs = ids.Split(','); List threads = new List(); foreach (var id in strs) { var machine = _unitOfWork.MachineRepository.GetById(id); Thread thread = new Thread(() => { if (machine != null) { Serie serie = new Serie() { Id = machine.Id, Name = machine.Name }; var mps = MesApiService.GetDayEnergyConsumptions(machine.Topic, startTime.ToString("yyyy-MM-dd"), endTime.ToString("yyyy-MM-dd")); foreach (var item in mps) { object[] o = new object[2]; if (item.Shift == 0) { o[0] = item.Date + " 白班"; } if (item.Shift == 1) { o[0] = item.Date + " 夜班"; } o[1] = item.ElectricEnergy; serie.Data.Add(o); } eCharts.Series.Add(serie); if (!eCharts.Legend.Any(x => x.Contains(serie.Name))) { eCharts.Legend.Add(serie.Name); } } }); thread.Start(); threads.Add(thread); } foreach (var item in threads) { item.Join(); } return Ok(ResultDto.Success(eCharts)); } [HttpPost] public IActionResult GetHourEnergyConsumptionByMes(string id, string time) { ECharts eCharts = new ECharts(); int index = time.IndexOf(' '); if (index == -1) { return Ok(ResultDto.Fail("时间格式有误!")); } var machine = _unitOfWork.MachineRepository.GetById(id); if (machine == null) { return Ok(ResultDto.Fail("设备不存在!")); } string day = time[..index]; int shift; if (time.Substring(index + 1, 2) == "白班") shift = 0; else shift = 1; eCharts.ChartName = $"{machine.Name} {time}"; var mps = MesApiService.GetHourElectricEnergys(machine.Topic, day, shift); Serie serie1 = new Serie() { Id = machine.Id, Name = machine.Name + " 能耗" }; Serie serie2 = new Serie() { Id = machine.Id, Name = machine.Name + " 产能" }; Serie serie3 = new Serie() { Id = machine.Id, Name = machine.Name + " 能耗/产能" }; foreach (var item in mps) { object[] o1 = new object[2]; o1[0] = item.Time; o1[1] = item.ElectricEnergy; serie1.Data.Add(o1); object[] o2 = new object[2]; o2[0] = item.Time; o2[1] = item.Capacity; serie2.Data.Add(o2); object[] o3 = new object[2]; o3[0] = item.Time; if (item.Capacity == 0) { o3[1] = 0; serie3.Data.Add(o3); } else { o3[1] = Math.Round(item.ElectricEnergy / item.Capacity, 3); serie3.Data.Add(o3); } } eCharts.Series.Add(serie1); eCharts.Series.Add(serie2); eCharts.Series.Add(serie3); return Ok(ResultDto.Success(eCharts)); } [HttpPost] public IActionResult GetEnergyConsumptionsV1(string ids, DateTime startTime, DateTime endTime) { ECharts eCharts = new ECharts(); eCharts.X = DateTimeHelper.GetDateShiftV1(startTime, endTime); string[] strs = ids.Split(','); foreach (var id in strs) { var machine = _unitOfWork.MachineRepository.GetById(id); if (machine != null) { var mpcs = _unitOfWork.MachinePowerConsumptionRepository.GetList( x => x.MachineId == machine.Id && x.DataTime >= startTime && x.DataTime <= endTime.AddDays(+1).AddHours(+8) && (x.DataTime.Hour == 7 || x.DataTime.Hour == 19)).OrderBy(o => o.DataTime); Serie serie = new Serie() { Id = machine.Id, Name = machine.Name }; foreach (var item in mpcs) { object[] o = new object[2]; if (item.DataTime.Hour == 19) { o[0] = item.DataTime.AddHours(+1).ToString("yyyy-MM-dd 白班"); } if (item.DataTime.Hour == 7) { o[0] = item.DataTime.AddDays(-1).AddHours(+1).ToString("yyyy-MM-dd 夜班"); } if (item.PowerConsumption > 500) { o[1] = 0; } else { o[1] = Math.Round(item.PowerConsumption, 2); } serie.Data.Add(o); } eCharts.Series.Add(serie); if (!eCharts.Legend.Any(x => x.Contains(serie.Name))) { eCharts.Legend.Add(serie.Name); } } } return Ok(ResultDto.Success(eCharts)); } [HttpPost] public IActionResult GetEnergyConsumptions(string ids, DateTime startTime, DateTime endTime) { ECharts eCharts = new ECharts { X = DateTimeHelper.GetDateShift(startTime, endTime.AddDays(+1)) }; string[] strs = ids.Split(','); foreach (var id in strs) { var machine = _unitOfWork.MachineRepository.GetById(id); if (machine != null) { var mpcs = _unitOfWork.MachinePowerConsumptionRepository.GetList( x => x.MachineId == machine.Id && x.DataTime >= startTime && x.DataTime <= endTime.AddDays(+1) && (x.DataTime.Hour == 7 || x.DataTime.Hour == 19)).OrderBy(o => o.DataTime); Serie serie = new Serie() { Id = machine.Id, Name = machine.Name }; foreach (var item in mpcs) { object[] o = new object[2]; o[0] = item.DataTime.AddHours(+1).ToString("yyyy-MM-dd HH:mm:ss"); if (item.PowerConsumption > 500) { o[1] = 0; } else { o[1] = Math.Round(item.PowerConsumption, 2); } serie.Data.Add(o); } eCharts.Series.Add(serie); if (!eCharts.Legend.Any(x => x.Contains(serie.Name))) { eCharts.Legend.Add(serie.Name); } } } return Ok(ResultDto.Success(eCharts)); } [HttpPost] public IActionResult GetHourEnergyConsumptions(string id, DateTime time) { ECharts eCharts = new ECharts(); DateTime startTime, endTime; startTime = time.AddHours(-12); endTime = time; var machine = _unitOfWork.MachineRepository.GetById(id); if (machine != null) { eCharts.ChartName = $"{machine.Name} {startTime}~{endTime}"; var data = _unitOfWork.MachinePowerConsumptionRepository.GetList( x => x.MachineId == id && (x.DataTime >= startTime && x.DataTime < endTime)) .OrderBy(o => o.DataTime); Serie serie1 = new Serie() { Id = machine.Id, Name = machine.Name + " 能耗" }; Serie serie2 = new Serie() { Id = machine.Id, Name = machine.Name + " 产能" }; Serie serie3 = new Serie() { Id = machine.Id, Name = machine.Name + " 能耗/产能" }; foreach (var item in data) { object[] o1 = new object[2]; o1[0] = item.DataTime.ToString("yyyy-MM-dd HH:mm:ss"); o1[1] = Math.Round(item.PowerConsumption, 2); serie1.Data.Add(o1); object[] o2 = new object[2]; o2[0] = item.DataTime.ToString("yyyy-MM-dd HH:mm:ss"); o2[1] = item.Capacity; serie2.Data.Add(o2); object[] o3 = new object[2]; o3[0] = item.DataTime.ToString("yyyy-MM-dd HH:mm:ss"); if (item.Capacity == 0) { o3[1] = 0; serie3.Data.Add(o3); } else { o3[1] = Math.Round(item.PowerConsumption / item.Capacity, 3); serie3.Data.Add(o3); } } eCharts.Series.Add(serie1); eCharts.Series.Add(serie2); eCharts.Series.Add(serie3); } return Ok(ResultDto.Success(eCharts)); } [HttpPost] public IActionResult GetHourEnergyConsumptionsV1(string id, string time) { ECharts eCharts = new ECharts(); int index = time.IndexOf(' '); if (index == -1) { return Ok(ResultDto.Fail("时间格式有误!")); } DateTime day = Convert.ToDateTime(time[..index]); DateTime startTime = DateTime.Now, endTime = DateTime.Now; if (time.Substring(index + 1, 2) == "白班") { startTime = day.AddHours(+8); endTime = day.AddHours(+20); } if (time.Substring(index + 1, 2) == "夜班") { startTime = day.AddHours(+20); endTime = day.AddHours(+32); } var machine = _unitOfWork.MachineRepository.GetById(id); if (machine != null) { eCharts.ChartName = $"{machine.Name} {startTime:dd日HH时}~{endTime:dd日HH时}"; var data = _unitOfWork.MachinePowerConsumptionRepository.GetList( x => x.MachineId == id && (x.DataTime >= startTime && x.DataTime < endTime)) .OrderBy(o => o.DataTime); Serie serie1 = new Serie() { Id = machine.Id, Name = machine.Name + " 能耗" }; Serie serie2 = new Serie() { Id = machine.Id, Name = machine.Name + " 产能" }; Serie serie3 = new Serie() { Id = machine.Id, Name = machine.Name + " 能耗/产能" }; foreach (var item in data) { object[] o1 = new object[2]; o1[0] = item.DataTime.ToString("yyyy-MM-dd HH:mm:ss"); o1[1] = Math.Round(item.PowerConsumption, 2); serie1.Data.Add(o1); object[] o2 = new object[2]; o2[0] = item.DataTime.ToString("yyyy-MM-dd HH:mm:ss"); o2[1] = item.Capacity; serie2.Data.Add(o2); object[] o3 = new object[2]; o3[0] = item.DataTime.ToString("yyyy-MM-dd HH:mm:ss"); if (item.Capacity == 0) { o3[1] = 0; serie3.Data.Add(o3); } else { o3[1] = Math.Round(item.PowerConsumption / item.Capacity, 3); serie3.Data.Add(o3); } } eCharts.Series.Add(serie1); eCharts.Series.Add(serie2); eCharts.Series.Add(serie3); } return Ok(ResultDto.Success(eCharts)); } [HttpPost] public IActionResult ExportLineEnergyConsumption() { return Ok(); } [HttpGet("/EnergyConsumptionReport")] public IActionResult EnergyConsumptionReport() { return View(); } [HttpGet("/EnergyConsumptionReport/GetEnergyConsumptionReport")] public IActionResult GetEnergyConsumptionReport(string startDate, string endDate) { EnergyConsumptionReportDto report = new EnergyConsumptionReportDto(startDate, endDate, _unitOfWork); return Ok(report); } [HttpGet("/EnergyConsumptionReport/GetEnergyConsumptionReportV1")] public IActionResult GetEnergyConsumptionReportV1(DateTime startDate, DateTime endDate) { EnergyConsumptionReportDto report = new EnergyConsumptionReportDto(startDate, endDate, _unitOfWork); return Ok(report); } [HttpGet("/EnergyConsumptionMeterCheck")] public IActionResult EnergyConsumptionMeterCheck() { return View(); } [HttpGet("/EnergyConsumptionMeterCheck/GetMachineMeterStates")] public IActionResult GetMachineMeterStates() { return Ok(ElectricEnergyMeterCheckService.GetCheckMachines()); } [HttpPost("/EnergyConsumptionMeterCheck/ClearElectricEnergyCache")] public IActionResult ClearElectricEnergyCache() { ElectricEnergyMeterCheckService.ChearMachines(); return Ok(ResultDto.Success()); } [HttpPost("/EnergyConsumptionMeterCheck/InitMeters")] public IActionResult InitMeters() { var machines = _unitOfWork.MachineRepository.GetList().OrderBy(o => o.Type); foreach (var machine in machines) { var meters = _unitOfWork.ElectricEnergyMeterRepository .GetList(x => x.MachineId == machine.Id) .OrderBy(o => o.Order) .ToList(); if (meters == null || meters.Count() == 0) { var ees = MesApiService.GetElectricEnergyMeters(machine.Topic); foreach (var e in ees) { ElectricEnergyMeter m = new ElectricEnergyMeter { Id = Guid.NewGuid().ToString().ToUpper(), MachineId = machine.Id, SerialNumber = e.SerialNumber, }; _unitOfWork.ElectricEnergyMeterRepository.Create(m); } } } _unitOfWork.SaveChanges(); return Ok(ResultDto.Success()); } } }