EnergyConsumptionController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. using Microsoft.AspNetCore.Mvc;
  2. using ProductionLineMonitor.Application.Services.EnergyConsumptionService.Dtos;
  3. using ProductionLineMonitor.Core.Dtos;
  4. using ProductionLineMonitor.Core.IRepositories;
  5. using ProductionLineMonitor.Core.Models;
  6. using ProductionLineMonitor.Core.Utils;
  7. using ProductionLineMonitor.Web.HostedServices;
  8. using ProductionLineMonitor.Web.Services;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data;
  12. using System.Linq;
  13. using System.Threading;
  14. namespace ProductionLineMonitor.Web.Controllers
  15. {
  16. public class EnergyConsumptionController : BaseController
  17. {
  18. private readonly IUnitOfWork _unitOfWork;
  19. public EnergyConsumptionController(IUnitOfWork unitOfWork)
  20. {
  21. _unitOfWork = unitOfWork;
  22. }
  23. [HttpGet]
  24. public IActionResult Index()
  25. {
  26. return View();
  27. }
  28. [HttpPost]
  29. public IActionResult GetEnergyConsumptionDtoByMes(string ids, DateTime startTime, DateTime endTime)
  30. {
  31. ECharts eCharts = new ECharts
  32. {
  33. X = DateTimeHelper.GetDateShiftV1(startTime, endTime)
  34. };
  35. string[] strs = ids.Split(',');
  36. List<Thread> threads = new List<Thread>();
  37. foreach (var id in strs)
  38. {
  39. var machine = _unitOfWork.MachineRepository.GetById(id);
  40. Thread thread = new Thread(() =>
  41. {
  42. if (machine != null)
  43. {
  44. Serie serie = new Serie()
  45. {
  46. Id = machine.Id,
  47. Name = machine.Name
  48. };
  49. var mps = MesApiService.GetDayEnergyConsumptions(machine.Topic,
  50. startTime.ToString("yyyy-MM-dd"), endTime.ToString("yyyy-MM-dd"));
  51. foreach (var item in mps)
  52. {
  53. object[] o = new object[2];
  54. if (item.Shift == 0)
  55. {
  56. o[0] = item.Date + " 白班";
  57. }
  58. if (item.Shift == 1)
  59. {
  60. o[0] = item.Date + " 夜班";
  61. }
  62. o[1] = item.ElectricEnergy;
  63. serie.Data.Add(o);
  64. }
  65. eCharts.Series.Add(serie);
  66. if (!eCharts.Legend.Any(x => x.Contains(serie.Name)))
  67. {
  68. eCharts.Legend.Add(serie.Name);
  69. }
  70. }
  71. });
  72. thread.Start();
  73. threads.Add(thread);
  74. }
  75. foreach (var item in threads)
  76. {
  77. item.Join();
  78. }
  79. return Ok(ResultDto<ECharts>.Success(eCharts));
  80. }
  81. [HttpPost]
  82. public IActionResult GetHourEnergyConsumptionByMes(string id, string time)
  83. {
  84. ECharts eCharts = new ECharts();
  85. int index = time.IndexOf(' ');
  86. if (index == -1)
  87. {
  88. return Ok(ResultDto<ECharts>.Fail("时间格式有误!"));
  89. }
  90. var machine = _unitOfWork.MachineRepository.GetById(id);
  91. if (machine == null)
  92. {
  93. return Ok(ResultDto<ECharts>.Fail("设备不存在!"));
  94. }
  95. string day = time[..index];
  96. int shift;
  97. if (time.Substring(index + 1, 2) == "白班")
  98. shift = 0;
  99. else
  100. shift = 1;
  101. eCharts.ChartName = $"{machine.Name} {time}";
  102. var mps = MesApiService.GetHourElectricEnergys(machine.Topic, day, shift);
  103. Serie serie1 = new Serie()
  104. {
  105. Id = machine.Id,
  106. Name = machine.Name + " 能耗"
  107. };
  108. Serie serie2 = new Serie()
  109. {
  110. Id = machine.Id,
  111. Name = machine.Name + " 产能"
  112. };
  113. Serie serie3 = new Serie()
  114. {
  115. Id = machine.Id,
  116. Name = machine.Name + " 能耗/产能"
  117. };
  118. foreach (var item in mps)
  119. {
  120. object[] o1 = new object[2];
  121. o1[0] = item.Time;
  122. o1[1] = item.ElectricEnergy;
  123. serie1.Data.Add(o1);
  124. object[] o2 = new object[2];
  125. o2[0] = item.Time;
  126. o2[1] = item.Capacity;
  127. serie2.Data.Add(o2);
  128. object[] o3 = new object[2];
  129. o3[0] = item.Time;
  130. if (item.Capacity == 0)
  131. {
  132. o3[1] = 0;
  133. serie3.Data.Add(o3);
  134. }
  135. else
  136. {
  137. o3[1] = Math.Round(item.ElectricEnergy / item.Capacity, 3);
  138. serie3.Data.Add(o3);
  139. }
  140. }
  141. eCharts.Series.Add(serie1);
  142. eCharts.Series.Add(serie2);
  143. eCharts.Series.Add(serie3);
  144. return Ok(ResultDto<ECharts>.Success(eCharts));
  145. }
  146. [HttpPost]
  147. public IActionResult GetEnergyConsumptionsV1(string ids, DateTime startTime, DateTime endTime)
  148. {
  149. ECharts eCharts = new ECharts();
  150. eCharts.X = DateTimeHelper.GetDateShiftV1(startTime, endTime);
  151. string[] strs = ids.Split(',');
  152. foreach (var id in strs)
  153. {
  154. var machine = _unitOfWork.MachineRepository.GetById(id);
  155. if (machine != null)
  156. {
  157. var mpcs = _unitOfWork.MachinePowerConsumptionRepository.GetList(
  158. x => x.MachineId == machine.Id
  159. && x.DataTime >= startTime && x.DataTime <= endTime.AddDays(+1).AddHours(+8)
  160. && (x.DataTime.Hour == 7 || x.DataTime.Hour == 19)).OrderBy(o => o.DataTime);
  161. Serie serie = new Serie()
  162. {
  163. Id = machine.Id,
  164. Name = machine.Name
  165. };
  166. foreach (var item in mpcs)
  167. {
  168. object[] o = new object[2];
  169. if (item.DataTime.Hour == 19)
  170. {
  171. o[0] = item.DataTime.AddHours(+1).ToString("yyyy-MM-dd 白班");
  172. }
  173. if (item.DataTime.Hour == 7)
  174. {
  175. o[0] = item.DataTime.AddDays(-1).AddHours(+1).ToString("yyyy-MM-dd 夜班");
  176. }
  177. if (item.PowerConsumption > 500)
  178. {
  179. o[1] = 0;
  180. }
  181. else
  182. {
  183. o[1] = Math.Round(item.PowerConsumption, 2);
  184. }
  185. serie.Data.Add(o);
  186. }
  187. eCharts.Series.Add(serie);
  188. if (!eCharts.Legend.Any(x => x.Contains(serie.Name)))
  189. {
  190. eCharts.Legend.Add(serie.Name);
  191. }
  192. }
  193. }
  194. return Ok(ResultDto<ECharts>.Success(eCharts));
  195. }
  196. [HttpPost]
  197. public IActionResult GetEnergyConsumptions(string ids, DateTime startTime, DateTime endTime)
  198. {
  199. ECharts eCharts = new ECharts
  200. {
  201. X = DateTimeHelper.GetDateShift(startTime, endTime.AddDays(+1))
  202. };
  203. string[] strs = ids.Split(',');
  204. foreach (var id in strs)
  205. {
  206. var machine = _unitOfWork.MachineRepository.GetById(id);
  207. if (machine != null)
  208. {
  209. var mpcs = _unitOfWork.MachinePowerConsumptionRepository.GetList(
  210. x => x.MachineId == machine.Id
  211. && x.DataTime >= startTime && x.DataTime <= endTime.AddDays(+1)
  212. && (x.DataTime.Hour == 7 || x.DataTime.Hour == 19)).OrderBy(o => o.DataTime);
  213. Serie serie = new Serie()
  214. {
  215. Id = machine.Id,
  216. Name = machine.Name
  217. };
  218. foreach (var item in mpcs)
  219. {
  220. object[] o = new object[2];
  221. o[0] = item.DataTime.AddHours(+1).ToString("yyyy-MM-dd HH:mm:ss");
  222. if (item.PowerConsumption > 500)
  223. {
  224. o[1] = 0;
  225. }
  226. else
  227. {
  228. o[1] = Math.Round(item.PowerConsumption, 2);
  229. }
  230. serie.Data.Add(o);
  231. }
  232. eCharts.Series.Add(serie);
  233. if (!eCharts.Legend.Any(x => x.Contains(serie.Name)))
  234. {
  235. eCharts.Legend.Add(serie.Name);
  236. }
  237. }
  238. }
  239. return Ok(ResultDto<ECharts>.Success(eCharts));
  240. }
  241. [HttpPost]
  242. public IActionResult GetHourEnergyConsumptions(string id, DateTime time)
  243. {
  244. ECharts eCharts = new ECharts();
  245. DateTime startTime, endTime;
  246. startTime = time.AddHours(-12);
  247. endTime = time;
  248. var machine = _unitOfWork.MachineRepository.GetById(id);
  249. if (machine != null)
  250. {
  251. eCharts.ChartName = $"{machine.Name} {startTime}~{endTime}";
  252. var data = _unitOfWork.MachinePowerConsumptionRepository.GetList(
  253. x => x.MachineId == id
  254. && (x.DataTime >= startTime && x.DataTime < endTime))
  255. .OrderBy(o => o.DataTime);
  256. Serie serie1 = new Serie()
  257. {
  258. Id = machine.Id,
  259. Name = machine.Name + " 能耗"
  260. };
  261. Serie serie2 = new Serie()
  262. {
  263. Id = machine.Id,
  264. Name = machine.Name + " 产能"
  265. };
  266. Serie serie3 = new Serie()
  267. {
  268. Id = machine.Id,
  269. Name = machine.Name + " 能耗/产能"
  270. };
  271. foreach (var item in data)
  272. {
  273. object[] o1 = new object[2];
  274. o1[0] = item.DataTime.ToString("yyyy-MM-dd HH:mm:ss");
  275. o1[1] = Math.Round(item.PowerConsumption, 2);
  276. serie1.Data.Add(o1);
  277. object[] o2 = new object[2];
  278. o2[0] = item.DataTime.ToString("yyyy-MM-dd HH:mm:ss");
  279. o2[1] = item.Capacity;
  280. serie2.Data.Add(o2);
  281. object[] o3 = new object[2];
  282. o3[0] = item.DataTime.ToString("yyyy-MM-dd HH:mm:ss");
  283. if (item.Capacity == 0)
  284. {
  285. o3[1] = 0;
  286. serie3.Data.Add(o3);
  287. }
  288. else
  289. {
  290. o3[1] = Math.Round(item.PowerConsumption / item.Capacity, 3);
  291. serie3.Data.Add(o3);
  292. }
  293. }
  294. eCharts.Series.Add(serie1);
  295. eCharts.Series.Add(serie2);
  296. eCharts.Series.Add(serie3);
  297. }
  298. return Ok(ResultDto<ECharts>.Success(eCharts));
  299. }
  300. [HttpPost]
  301. public IActionResult GetHourEnergyConsumptionsV1(string id, string time)
  302. {
  303. ECharts eCharts = new ECharts();
  304. int index = time.IndexOf(' ');
  305. if (index == -1)
  306. {
  307. return Ok(ResultDto<ECharts>.Fail("时间格式有误!"));
  308. }
  309. DateTime day = Convert.ToDateTime(time[..index]);
  310. DateTime startTime = DateTime.Now, endTime = DateTime.Now;
  311. if (time.Substring(index + 1, 2) == "白班")
  312. {
  313. startTime = day.AddHours(+8);
  314. endTime = day.AddHours(+20);
  315. }
  316. if (time.Substring(index + 1, 2) == "夜班")
  317. {
  318. startTime = day.AddHours(+20);
  319. endTime = day.AddHours(+32);
  320. }
  321. var machine = _unitOfWork.MachineRepository.GetById(id);
  322. if (machine != null)
  323. {
  324. eCharts.ChartName = $"{machine.Name} {startTime:dd日HH时}~{endTime:dd日HH时}";
  325. var data = _unitOfWork.MachinePowerConsumptionRepository.GetList(
  326. x => x.MachineId == id
  327. && (x.DataTime >= startTime && x.DataTime < endTime))
  328. .OrderBy(o => o.DataTime);
  329. Serie serie1 = new Serie()
  330. {
  331. Id = machine.Id,
  332. Name = machine.Name + " 能耗"
  333. };
  334. Serie serie2 = new Serie()
  335. {
  336. Id = machine.Id,
  337. Name = machine.Name + " 产能"
  338. };
  339. Serie serie3 = new Serie()
  340. {
  341. Id = machine.Id,
  342. Name = machine.Name + " 能耗/产能"
  343. };
  344. foreach (var item in data)
  345. {
  346. object[] o1 = new object[2];
  347. o1[0] = item.DataTime.ToString("yyyy-MM-dd HH:mm:ss");
  348. o1[1] = Math.Round(item.PowerConsumption, 2);
  349. serie1.Data.Add(o1);
  350. object[] o2 = new object[2];
  351. o2[0] = item.DataTime.ToString("yyyy-MM-dd HH:mm:ss");
  352. o2[1] = item.Capacity;
  353. serie2.Data.Add(o2);
  354. object[] o3 = new object[2];
  355. o3[0] = item.DataTime.ToString("yyyy-MM-dd HH:mm:ss");
  356. if (item.Capacity == 0)
  357. {
  358. o3[1] = 0;
  359. serie3.Data.Add(o3);
  360. }
  361. else
  362. {
  363. o3[1] = Math.Round(item.PowerConsumption / item.Capacity, 3);
  364. serie3.Data.Add(o3);
  365. }
  366. }
  367. eCharts.Series.Add(serie1);
  368. eCharts.Series.Add(serie2);
  369. eCharts.Series.Add(serie3);
  370. }
  371. return Ok(ResultDto<ECharts>.Success(eCharts));
  372. }
  373. [HttpPost]
  374. public IActionResult ExportLineEnergyConsumption()
  375. {
  376. return Ok();
  377. }
  378. [HttpGet("/EnergyConsumptionReport")]
  379. public IActionResult EnergyConsumptionReport()
  380. {
  381. return View();
  382. }
  383. [HttpGet("/EnergyConsumptionReport/GetEnergyConsumptionReport")]
  384. public IActionResult GetEnergyConsumptionReport(string startDate, string endDate)
  385. {
  386. EnergyConsumptionReportDto report = new EnergyConsumptionReportDto(startDate, endDate, _unitOfWork);
  387. return Ok(report);
  388. }
  389. [HttpGet("/EnergyConsumptionReport/GetEnergyConsumptionReportV1")]
  390. public IActionResult GetEnergyConsumptionReportV1(DateTime startDate, DateTime endDate)
  391. {
  392. EnergyConsumptionReportDto report = new EnergyConsumptionReportDto(startDate, endDate, _unitOfWork);
  393. return Ok(report);
  394. }
  395. [HttpGet("/EnergyConsumptionMeterCheck")]
  396. public IActionResult EnergyConsumptionMeterCheck()
  397. {
  398. return View();
  399. }
  400. [HttpGet("/EnergyConsumptionMeterCheck/GetMachineMeterStates")]
  401. public IActionResult GetMachineMeterStates()
  402. {
  403. return Ok(ElectricEnergyMeterCheckService.GetCheckMachines());
  404. }
  405. [HttpPost("/EnergyConsumptionMeterCheck/ClearElectricEnergyCache")]
  406. public IActionResult ClearElectricEnergyCache()
  407. {
  408. ElectricEnergyMeterCheckService.ChearMachines();
  409. return Ok(ResultDto.Success());
  410. }
  411. [HttpPost("/EnergyConsumptionMeterCheck/InitMeters")]
  412. public IActionResult InitMeters()
  413. {
  414. var machines = _unitOfWork.MachineRepository.GetList().OrderBy(o => o.Type);
  415. foreach (var machine in machines)
  416. {
  417. var meters = _unitOfWork.ElectricEnergyMeterRepository
  418. .GetList(x => x.MachineId == machine.Id)
  419. .OrderBy(o => o.Order)
  420. .ToList();
  421. if (meters == null || meters.Count() == 0)
  422. {
  423. var ees = MesApiService.GetElectricEnergyMeters(machine.Topic);
  424. foreach (var e in ees)
  425. {
  426. ElectricEnergyMeter m = new ElectricEnergyMeter
  427. {
  428. Id = Guid.NewGuid().ToString().ToUpper(),
  429. MachineId = machine.Id,
  430. SerialNumber = e.SerialNumber,
  431. };
  432. _unitOfWork.ElectricEnergyMeterRepository.Create(m);
  433. }
  434. }
  435. }
  436. _unitOfWork.SaveChanges();
  437. return Ok(ResultDto.Success());
  438. }
  439. }
  440. }