CimService.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using ProductionLineMonitor.Core.Dtos;
  2. using ProductionLineMonitor.Core.IRepositories;
  3. using ProductionLineMonitor.Core.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. namespace ProductionLineMonitor.Application.Services.CimService
  10. {
  11. public class CimService : ICimService
  12. {
  13. private readonly IUnitOfWork _unitOfWork;
  14. public CimService(IUnitOfWork unitOfWork)
  15. {
  16. _unitOfWork = unitOfWork;
  17. }
  18. public void CreateOrUpdate(CimDto dto)
  19. {
  20. var cim = _unitOfWork.CimRepository.FirstOrDefault(x => x.Topic == dto.Topic);
  21. if (cim == null)
  22. {
  23. _unitOfWork.CimRepository.Create(new Cim()
  24. {
  25. Topic = dto.Topic,
  26. IpAddress = dto.IpAddress,
  27. User = dto.User,
  28. Time = dto.Time,
  29. PLCIsConnected = dto.PLCConnect,
  30. EAPIsConnected = dto.EAPConnect,
  31. IsConnected = true,
  32. Version = dto.Version,
  33. EAPIP = dto.EAPIP,
  34. EAPPort= dto.EAPPort,
  35. CreateTime = DateTime.Now
  36. });
  37. _unitOfWork.SaveChanges();
  38. }
  39. else
  40. {
  41. cim.IsConnected = true;
  42. cim.PLCIsConnected = dto.PLCConnect;
  43. cim.EAPIsConnected = dto.EAPConnect;
  44. if (cim.EAPIsConnected)
  45. {
  46. cim.IpAddress = dto.IpAddress;
  47. }
  48. cim.Topic = dto.Topic;
  49. cim.User = dto.User;
  50. cim.Time = dto.Time;
  51. cim.Version = dto.Version;
  52. cim.EAPIP= dto.EAPIP;
  53. cim.EAPPort= dto.EAPPort;
  54. cim.UpdateTime = DateTime.Now;
  55. _unitOfWork.CimRepository.Update(cim);
  56. _unitOfWork.SaveChanges();
  57. }
  58. }
  59. public ResultDto<IEnumerable<Cim>> GetCims(bool isShowError)
  60. {
  61. if (isShowError == false)
  62. {
  63. var cims = _unitOfWork.CimRepository.GetList();
  64. if (cims != null)
  65. foreach (var item in cims)
  66. {
  67. int index = item.Topic.LastIndexOf('#');
  68. if (index != -1)
  69. {
  70. item.Topic = item.Topic.Substring(index + 1, item.Topic.Length - index - 1);
  71. }
  72. if (item.IpAddress != null)
  73. {
  74. index = item.IpAddress.LastIndexOf(':');
  75. if (index != -1)
  76. {
  77. item.IpAddress = item.IpAddress[..index];
  78. }
  79. }
  80. }
  81. return ResultDto<IEnumerable<Cim>>.Success(cims.OrderBy(o => o.Topic.Substring(0, 2)));
  82. }
  83. else
  84. {
  85. var cims = _unitOfWork.CimRepository.GetList(
  86. x => x.EAPIsConnected == false
  87. || x.PLCIsConnected == false
  88. || x.IsConnected == false).OrderBy(o => o.Topic);
  89. if (cims != null)
  90. foreach (var item in cims)
  91. {
  92. int index = item.Topic.LastIndexOf('#');
  93. if (index != -1)
  94. {
  95. item.Topic = item.Topic.Substring(index + 1, item.Topic.Length - index - 1);
  96. }
  97. if (item.IpAddress != null)
  98. {
  99. index = item.IpAddress.LastIndexOf(':');
  100. if (index != -1)
  101. {
  102. item.IpAddress = item.IpAddress[..index];
  103. }
  104. }
  105. }
  106. return ResultDto<IEnumerable<Cim>>.Success(cims.OrderBy(o => o.Topic.Substring(0, 2)));
  107. }
  108. }
  109. public void UpdateMqttClientState(MqttClientStateDto dto)
  110. {
  111. var cim = _unitOfWork.CimRepository.FirstOrDefault(x => x.Topic == dto.ClientId);
  112. if (cim != null)
  113. {
  114. cim.IsConnected = dto.IsConnected;
  115. _unitOfWork.CimRepository.Update(cim);
  116. _unitOfWork.SaveChanges();
  117. }
  118. }
  119. }
  120. }