using AutoMapper; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Hosting; using ProductionLineMonitor.Core.Dtos; using ProductionLineMonitor.Core.IRepositories; using ProductionLineMonitor.Core.Models; using ProductionLineMonitor.Core.Services; using ProductionLineMonitor.Core.Utils; using ProductionLineMonitor.Web.HostedServices; using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography.X509Certificates; namespace ProductionLineMonitor.Web.Controllers { public class CimController : BaseController { private readonly IUnitOfWork _unitOfWork; private readonly IMapper _mapper; public CimController(IMapper mapper, IUnitOfWork unitOfWork) { _unitOfWork = unitOfWork; _mapper = mapper; } [HttpGet] public IActionResult Index() { return View(); } [Route("/Cim/ChangeShifts")] [HttpGet] public IActionResult GetChangeShifts() { var cims = _unitOfWork.CimRepository.GetList(); if (cims == null) { return Ok(ResultDto.Fail("没有相关数据!")); } var lst = new List(); DateTime time = DateTime.Now; DateTime startTime = Convert.ToDateTime($"{time:yyyy-MM-dd} 7:50:00"); DateTime endTime = Convert.ToDateTime($"{time:yyyy-MM-dd} 8:10:00"); foreach (var cim in cims) { ChangeShiftDto changeShiftDto = new ChangeShiftDto(); int index = cim.Topic.LastIndexOf('#'); if (index != -1) { changeShiftDto.Topic = cim.Topic.Substring(index + 1, cim.Topic.Length - index - 1); } if (index == -1) { continue; } var machine = _unitOfWork.MachineRepository.FirstOrDefault(x => x.Topic == cim.Topic); if (machine != null) { var outputs = _unitOfWork.MachineOutPutPerHourRepository.GetList( x => x.MachineId == machine.Id && x.UpdateTime >= startTime && x.UpdateTime <= endTime); if (outputs != null && outputs.Count() > 0) { changeShiftDto.IsChangeShift = true; lst.Add(changeShiftDto); } else { changeShiftDto.IsChangeShift = false; lst.Add(changeShiftDto); } } } return Ok(ResultDto>.Success( lst.Where(x => x.IsChangeShift == false).OrderBy(o => o.Topic))); } [Route("/Cim/GetCims")] [HttpGet] public IActionResult GetCims(bool isShowError = false) { var lst = new List(); IList cims; if (isShowError == false) { cims = _unitOfWork.CimRepository.GetList().ToList(); } else { cims = _unitOfWork.CimRepository.GetList( x => x.EAPIsConnected == false || x.PLCIsConnected == false || x.IsConnected == false).ToList(); } if (cims != null) { DateTime dateTime = DateTime.Now; foreach (var item in cims) { CimListDto dto = new CimListDto(); dto = _mapper.Map(item); int index = item.Topic.LastIndexOf('#'); if (index != -1) { dto.Topic = item.Topic.Substring(index + 1, item.Topic.Length - index - 1); //dto.LineNo = Convert.ToInt16(dto.Topic[..2]); int lineNo = 0; int.TryParse(dto.Topic[..2], out lineNo); dto.LineNo = lineNo; } if (item.IpAddress != null) { index = item.IpAddress.LastIndexOf(':'); if (index != -1) { dto.IpAddress = item.IpAddress[..index]; } } double t = (dateTime - item.Time).TotalMinutes; if (t > 5) dto.IsConnected = false; lst.Add(dto); } } return Ok(ResultDto>.Success(lst.OrderBy(o => o.LineNo))); } [HttpPost("/Cim/UpdateApp")] public IActionResult UpdateApp(string ids) { string[] strs = ids.Split(','); foreach (var id in strs) { var machine = _unitOfWork.MachineRepository.GetById(id); if (machine != null) { HostMqttClient.Pub($"/Machine/CheckAutoUpdate/{machine.Topic}", ""); } } return Ok(ResultDto.Success()); } [HttpPost("/Cim/SetEAP")] public IActionResult SetEAP(string ids, string ip, int port) { string[] strs = ids.Split(','); foreach (var id in strs) { var machine = _unitOfWork.MachineRepository.GetById(id); if (machine != null) { EapDto dto = new EapDto { Ip = ip, Port = port }; HostMqttClient.Pub($"/Machine/EAP/{machine.Topic}", dto.ToJson()); } } return Ok(ResultDto.Success()); } [HttpPost("/Cim/SetEapIp")] public IActionResult SetEapIp(string ids, string ip) { string[] strs = ids.Split(','); foreach (var id in strs) { var machine = _unitOfWork.MachineRepository.GetById(id); if (machine != null) { var cim = _unitOfWork.CimRepository.FirstOrDefault(x => x.Topic == machine.Topic); if (cim != null) { EapDto dto = new EapDto { Ip = ip, Port = cim.EAPPort }; HostMqttClient.Pub($"/Machine/EAP/{machine.Topic}", dto.ToJson()); } } } return Ok(ResultDto.Success()); } [HttpPost("/Cim/DeleteCimDevice")] public IActionResult DeleteCimDevice(string id) { try { _unitOfWork.CimRepository.Delete(id); _unitOfWork.SaveChanges(); return Ok(ResultDto.Success()); } catch (Exception ex) { return Ok(ResultDto.Fail(ex.Message)); } } } }