123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 |
- 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<Thread> threads = new List<Thread>();
- 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<ECharts>.Success(eCharts));
- }
- [HttpPost]
- public IActionResult GetHourEnergyConsumptionByMes(string id, string time)
- {
- ECharts eCharts = new ECharts();
- int index = time.IndexOf(' ');
- if (index == -1)
- {
- return Ok(ResultDto<ECharts>.Fail("时间格式有误!"));
- }
- var machine = _unitOfWork.MachineRepository.GetById(id);
- if (machine == null)
- {
- return Ok(ResultDto<ECharts>.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<ECharts>.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<ECharts>.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<ECharts>.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<ECharts>.Success(eCharts));
- }
- [HttpPost]
- public IActionResult GetHourEnergyConsumptionsV1(string id, string time)
- {
- ECharts eCharts = new ECharts();
- int index = time.IndexOf(' ');
- if (index == -1)
- {
- return Ok(ResultDto<ECharts>.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<ECharts>.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());
- }
- }
- }
|