123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- 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<ChangeShiftDto>();
-
- 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<IEnumerable<ChangeShiftDto>>.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<CimListDto>();
-
- IList<Cim> 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<CimListDto>(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<IEnumerable<CimListDto>>.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));
- }
- }
- }
- }
|