CimController.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using AutoMapper;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Hosting;
  4. using ProductionLineMonitor.Core.Dtos;
  5. using ProductionLineMonitor.Core.IRepositories;
  6. using ProductionLineMonitor.Core.Models;
  7. using ProductionLineMonitor.Core.Services;
  8. using ProductionLineMonitor.Core.Utils;
  9. using ProductionLineMonitor.Web.HostedServices;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Security.Cryptography.X509Certificates;
  14. namespace ProductionLineMonitor.Web.Controllers
  15. {
  16. public class CimController : BaseController
  17. {
  18. private readonly IUnitOfWork _unitOfWork;
  19. private readonly IMapper _mapper;
  20. public CimController(IMapper mapper, IUnitOfWork unitOfWork)
  21. {
  22. _unitOfWork = unitOfWork;
  23. _mapper = mapper;
  24. }
  25. [HttpGet]
  26. public IActionResult Index()
  27. {
  28. return View();
  29. }
  30. [Route("/Cim/ChangeShifts")]
  31. [HttpGet]
  32. public IActionResult GetChangeShifts()
  33. {
  34. var cims = _unitOfWork.CimRepository.GetList();
  35. if (cims == null)
  36. {
  37. return Ok(ResultDto.Fail("没有相关数据!"));
  38. }
  39. var lst = new List<ChangeShiftDto>();
  40. DateTime time = DateTime.Now;
  41. DateTime startTime = Convert.ToDateTime($"{time:yyyy-MM-dd} 7:50:00");
  42. DateTime endTime = Convert.ToDateTime($"{time:yyyy-MM-dd} 8:10:00");
  43. foreach (var cim in cims)
  44. {
  45. ChangeShiftDto changeShiftDto = new ChangeShiftDto();
  46. int index = cim.Topic.LastIndexOf('#');
  47. if (index != -1)
  48. {
  49. changeShiftDto.Topic = cim.Topic.Substring(index + 1, cim.Topic.Length - index - 1);
  50. }
  51. if (index == -1)
  52. {
  53. continue;
  54. }
  55. var machine = _unitOfWork.MachineRepository.FirstOrDefault(x => x.Topic == cim.Topic);
  56. if (machine != null)
  57. {
  58. var outputs = _unitOfWork.MachineOutPutPerHourRepository.GetList(
  59. x => x.MachineId == machine.Id && x.UpdateTime >= startTime && x.UpdateTime <= endTime);
  60. if (outputs != null && outputs.Count() > 0)
  61. {
  62. changeShiftDto.IsChangeShift = true;
  63. lst.Add(changeShiftDto);
  64. }
  65. else
  66. {
  67. changeShiftDto.IsChangeShift = false;
  68. lst.Add(changeShiftDto);
  69. }
  70. }
  71. }
  72. return Ok(ResultDto<IEnumerable<ChangeShiftDto>>.Success(
  73. lst.Where(x => x.IsChangeShift == false).OrderBy(o => o.Topic)));
  74. }
  75. [Route("/Cim/GetCims")]
  76. [HttpGet]
  77. public IActionResult GetCims(bool isShowError = false)
  78. {
  79. var lst = new List<CimListDto>();
  80. IList<Cim> cims;
  81. if (isShowError == false)
  82. {
  83. cims = _unitOfWork.CimRepository.GetList().ToList();
  84. }
  85. else
  86. {
  87. cims = _unitOfWork.CimRepository.GetList(
  88. x => x.EAPIsConnected == false
  89. || x.PLCIsConnected == false
  90. || x.IsConnected == false).ToList();
  91. }
  92. if (cims != null)
  93. {
  94. DateTime dateTime = DateTime.Now;
  95. foreach (var item in cims)
  96. {
  97. CimListDto dto = new CimListDto();
  98. dto = _mapper.Map<CimListDto>(item);
  99. int index = item.Topic.LastIndexOf('#');
  100. if (index != -1)
  101. {
  102. dto.Topic = item.Topic.Substring(index + 1, item.Topic.Length - index - 1);
  103. //dto.LineNo = Convert.ToInt16(dto.Topic[..2]);
  104. int lineNo = 0;
  105. int.TryParse(dto.Topic[..2], out lineNo);
  106. dto.LineNo = lineNo;
  107. }
  108. if (item.IpAddress != null)
  109. {
  110. index = item.IpAddress.LastIndexOf(':');
  111. if (index != -1)
  112. {
  113. dto.IpAddress = item.IpAddress[..index];
  114. }
  115. }
  116. double t = (dateTime - item.Time).TotalMinutes;
  117. if (t > 5)
  118. dto.IsConnected = false;
  119. lst.Add(dto);
  120. }
  121. }
  122. return Ok(ResultDto<IEnumerable<CimListDto>>.Success(lst.OrderBy(o => o.LineNo)));
  123. }
  124. [HttpPost("/Cim/UpdateApp")]
  125. public IActionResult UpdateApp(string ids)
  126. {
  127. string[] strs = ids.Split(',');
  128. foreach (var id in strs)
  129. {
  130. var machine = _unitOfWork.MachineRepository.GetById(id);
  131. if (machine != null)
  132. {
  133. HostMqttClient.Pub($"/Machine/CheckAutoUpdate/{machine.Topic}", "");
  134. }
  135. }
  136. return Ok(ResultDto.Success());
  137. }
  138. [HttpPost("/Cim/SetEAP")]
  139. public IActionResult SetEAP(string ids, string ip, int port)
  140. {
  141. string[] strs = ids.Split(',');
  142. foreach (var id in strs)
  143. {
  144. var machine = _unitOfWork.MachineRepository.GetById(id);
  145. if (machine != null)
  146. {
  147. EapDto dto = new EapDto
  148. {
  149. Ip = ip,
  150. Port = port
  151. };
  152. HostMqttClient.Pub($"/Machine/EAP/{machine.Topic}", dto.ToJson());
  153. }
  154. }
  155. return Ok(ResultDto.Success());
  156. }
  157. [HttpPost("/Cim/SetEapIp")]
  158. public IActionResult SetEapIp(string ids, string ip)
  159. {
  160. string[] strs = ids.Split(',');
  161. foreach (var id in strs)
  162. {
  163. var machine = _unitOfWork.MachineRepository.GetById(id);
  164. if (machine != null)
  165. {
  166. var cim = _unitOfWork.CimRepository.FirstOrDefault(x => x.Topic == machine.Topic);
  167. if (cim != null)
  168. {
  169. EapDto dto = new EapDto
  170. {
  171. Ip = ip,
  172. Port = cim.EAPPort
  173. };
  174. HostMqttClient.Pub($"/Machine/EAP/{machine.Topic}", dto.ToJson());
  175. }
  176. }
  177. }
  178. return Ok(ResultDto.Success());
  179. }
  180. [HttpPost("/Cim/DeleteCimDevice")]
  181. public IActionResult DeleteCimDevice(string id)
  182. {
  183. try
  184. {
  185. _unitOfWork.CimRepository.Delete(id);
  186. _unitOfWork.SaveChanges();
  187. return Ok(ResultDto.Success());
  188. }
  189. catch (Exception ex)
  190. {
  191. return Ok(ResultDto.Fail(ex.Message));
  192. }
  193. }
  194. }
  195. }