RabbitMQController.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Caching.Memory;
  4. using Microsoft.Extensions.Options;
  5. using Ropin.Core.Common;
  6. using Ropin.Inspection.Api.Common.Options;
  7. using Ropin.Inspection.Api.Common.Token;
  8. using Ropin.Inspection.Service.Interface;
  9. using Ropin.Inspection.Service.SYS.Interface;
  10. using Ropin.Inspection.Service;
  11. using System.Net.Http;
  12. using Ropin.Inspection.Common.Helper;
  13. using Microsoft.AspNetCore.Authorization;
  14. using Newtonsoft.Json;
  15. using Ropin.Inspection.Api.Common;
  16. using Ropin.Inspection.Api.Wx;
  17. using Ropin.Inspection.Model.SearchModel;
  18. using Ropin.Inspection.Model.ViewModel.SYS;
  19. using Ropin.Inspection.Model.ViewModel;
  20. using Ropin.Inspection.Model;
  21. using Ropin.Inspection.Service.SYS;
  22. using System.Net.Http.Headers;
  23. using System.Threading.Tasks;
  24. using System;
  25. using UAParser;
  26. using ZXing.Aztec.Internal;
  27. using Ropin.Inspection.Model.SearchModel.DEV;
  28. using Ropin.Inspection.Service.LGS.Interface;
  29. using Ropin.Inspection.Model.ViewModel.LGS;
  30. using System.Net.Sockets;
  31. using Ropin.Inspection.Model.ViewModel.DEV;
  32. using Ropin.Inspection.Repository.DEV.Interface;
  33. using Ropin.Inspection.Service.DEV.Interface;
  34. using InfluxData.Net.InfluxDb.Models;
  35. using RabbitMQ.Client;
  36. using System.Collections.Generic;
  37. using Ropin.Inspection.Model.Entities;
  38. using NPOI.SS.Formula.Functions;
  39. using Newtonsoft.Json.Linq;
  40. using System.Linq;
  41. using System.Security.Cryptography.X509Certificates;
  42. namespace Ropin.Inspection.Api.Controllers.Base
  43. {
  44. public class RabbitMQController : BaseController
  45. {
  46. private readonly RabbitMQModel _options;
  47. private readonly ILargeScreenEventService _largeScreenEventRepository;
  48. private readonly ILargeScreenControlService _largeScreenControRepository;
  49. private readonly IDEVCmdService _ldevCmdRepository;
  50. public RabbitMQController(IOptionsMonitor<RabbitMQModel> options, ILargeScreenEventService largeScreenEventRepository, ILargeScreenControlService largeScreenControRepository, IDEVCmdService ldevCmdRepository)
  51. {
  52. _options = options.Get("RabbitMQModel");
  53. _largeScreenEventRepository = largeScreenEventRepository;
  54. _largeScreenControRepository = largeScreenControRepository;
  55. _ldevCmdRepository = ldevCmdRepository;
  56. }
  57. /// <summary>
  58. /// 发送指令
  59. /// </summary>
  60. /// <returns></returns>
  61. [HttpPost("SendCommand")]
  62. public async Task<ApiResult> SendCommand(SendCmdModel model)
  63. {
  64. if (model == null|| string.IsNullOrEmpty(model.C_ControlCode)|| string.IsNullOrEmpty(model.C_CmdId))
  65. {
  66. return new ApiResult(ReturnCode.ArgsError, "参数错误");
  67. }
  68. try
  69. {
  70. string cmdId = model.C_CmdId;
  71. //var cmdList = await _ldevCmdRepository.GetInstructionByCmdCode((new DevInstructionSearchModel() { CmdCode = cmdId }));
  72. var cmdList = await _ldevCmdRepository.GetInstructionConditionAsync((new DevInstructionSearchModel() { CmdCode = cmdId, IsPagination = false, C_Status = "1" }));
  73. if (cmdList==null|| cmdList.Count() == 0)
  74. {
  75. return new ApiResult(ReturnCode.DataError, "该命令下没有要执行的指令");
  76. }
  77. cmdList = cmdList.OrderBy(x => x.I_ExeOrder).ToList();
  78. List<TLGS_LargeScreenEvent> LargeScreenEvent=new List<TLGS_LargeScreenEvent> ();
  79. List<SendComRabbitMQParam> RabbitMQParamList=new List<SendComRabbitMQParam> ();
  80. foreach ( var item in cmdList)
  81. {
  82. string id = Guid.NewGuid().ToString();
  83. TLGS_LargeScreenEvent large = new TLGS_LargeScreenEvent();
  84. large.C_ID = id;
  85. large.C_ControlCode = model.C_ControlCode;
  86. large.C_DevStoreCode = model.C_DevStoreCode;
  87. large.C_BoxCode = item.C_BoxCode;
  88. large.C_Name = model.C_Name;
  89. large.C_Type = model.C_Type;
  90. large.C_Status = "2";
  91. LargeScreenEvent.Add(large);
  92. RabbitMQParamList.Add(new SendComRabbitMQParam
  93. {
  94. EveId = id,
  95. BoxId = item.C_BoxCode,
  96. cmdId = cmdId,
  97. pms = item.C_Params,
  98. protocol = item.C_Protocol,
  99. sort = item.I_ExeOrder
  100. });
  101. }
  102. if (RabbitMQParamList != null && RabbitMQParamList.Count > 0)
  103. {
  104. _options.msgStr = JsonConvert.SerializeObject(RabbitMQParamList);
  105. bool bol = await RabbitMQHelper.SnedRabbitMQ_ExchangeDirect(_options);
  106. if (!bol)
  107. {
  108. return new ApiResult(ReturnCode.DataError, "发送指令失败");
  109. }
  110. }
  111. if (LargeScreenEvent != null && LargeScreenEvent.Count > 0)
  112. {
  113. string status = "2";// bol ? "2" : "3";
  114. await _largeScreenEventRepository.CreateListAsync(LargeScreenEvent, status);
  115. }
  116. //return new ApiResult<RabbitMQModel>(_options, ReturnCode.Success);
  117. return new ApiResult(ReturnCode.Success);
  118. }
  119. catch (Exception ex)
  120. {
  121. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  122. }
  123. }
  124. /// <summary>
  125. /// 设备报警【手动报警】
  126. /// </summary>
  127. /// <returns></returns>
  128. [HttpPost("DevManualAddAlmMsg")]
  129. public async Task<ApiResult> DevManualAddAlmMsg(TpushMsgModel model)
  130. {
  131. if (model == null || string.IsNullOrEmpty(model.C_DevStoreCode) || string.IsNullOrEmpty(model.Msg))
  132. {
  133. return new ApiResult(ReturnCode.ArgsError, "参数错误");
  134. }
  135. try
  136. {
  137. model.CreateOn = DateTime.Now.ToString();
  138. _options.msgStr = JsonConvert.SerializeObject(model);
  139. _options.QueueName = "rabbit.alarmDevice";
  140. bool bol = await RabbitMQHelper.SnedRabbitMQ_ExchangeDirect(_options);
  141. if (!bol) {
  142. return new ApiResult(ReturnCode.DataError, "发送指令失败");
  143. }
  144. return new ApiResult(ReturnCode.Success);
  145. }
  146. catch (Exception ex)
  147. {
  148. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  149. }
  150. }
  151. }
  152. }