RabbitMQReceiveService.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. using FBoxClientDriver.Contract;
  2. using InfluxData.Net.InfluxDb;
  3. using log4net;
  4. using Microsoft.AspNet.SignalR.Client;
  5. using Microsoft.AspNetCore.Connections;
  6. using Microsoft.EntityFrameworkCore.Metadata;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Microsoft.Extensions.Hosting;
  9. using Newtonsoft.Json;
  10. using RabbitMQ.Client;
  11. using RabbitMQ.Client.Events;
  12. using Ropin.Environmentally.DcsService.AppSetingModel;
  13. using Ropin.Inspection.Model.Entities;
  14. using Ropin.Inspection.Model.SearchModel.DEV;
  15. using Ropin.Inspection.Repository;
  16. using Ropin.Inspection.Repository.DEV.Interface;
  17. using System;
  18. using System.Runtime.Intrinsics.Arm;
  19. using System.Text;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using MQTTnet;
  23. using MQTTnet.Client;
  24. using MQTTnet.Protocol;
  25. using static System.Net.WebRequestMethods;
  26. using Ropin.Inspection.Common.Helper;
  27. using Ropin.Inspection.Model.ViewModel.DEV;
  28. using System.Collections.Generic;
  29. using Ropin.Inspection.Repository.LGS.Interface;
  30. using System.Net.Http;
  31. using System.Net.Sockets;
  32. using System.Net;
  33. using FBoxClientDriver.Contract.Entity;
  34. using System.IO;
  35. using System.Linq;
  36. using System.IO.Pipelines;
  37. using Ubiety.Dns.Core;
  38. using Microsoft.AspNet.SignalR.Client.Http;
  39. using Renci.SshNet;
  40. using static Zstandard.Net.ZstandardInterop;
  41. using System.Net.Http.Headers;
  42. using LinqKit;
  43. using Ropin.Environmentally.DcsService.Service.Model;
  44. namespace Ropin.Environmentally.DcsService.Service
  45. {
  46. public class RabbitMQReceiveService : IHostedService, IDisposable
  47. {
  48. private readonly IServiceProvider _provider;
  49. private InfluxDbClient clientDb;
  50. private static readonly ILog log = LogManager.GetLogger(typeof(RabbitMQReceiveService));
  51. private readonly RabbitMQModel _rabbitMQModel;
  52. private readonly IniInfluxDB _IniInfluxData;
  53. private readonly CmdModel _cmdModel;
  54. public RabbitMQReceiveService(IServiceProvider provider, RabbitMQModel rabbitMQModel, IniInfluxDB IniInfluxData, CmdModel cmdModel)
  55. {
  56. this._provider = provider;
  57. _rabbitMQModel = rabbitMQModel;
  58. _IniInfluxData = IniInfluxData;
  59. _cmdModel = cmdModel;
  60. }
  61. private RabbitMQ.Client.IConnection con;
  62. private RabbitMQ.Client.IModel channel;
  63. public Task StartAsync(CancellationToken cancellationToken)
  64. {
  65. Task.Run(async () =>
  66. {
  67. //await MQTTService("10708f9b-79fe-4fb2-9c5f-22b78993d233", "{\"DO1\":1}");
  68. await AddRabbitMQ();
  69. //while (true)
  70. //{
  71. // await Task.Delay(5000);//10000:10s
  72. //}
  73. });
  74. return Task.CompletedTask;
  75. }
  76. public async Task AddRabbitMQ()
  77. {
  78. try
  79. {
  80. var factory = new ConnectionFactory()
  81. {
  82. HostName = _rabbitMQModel.HostName,//"60.204.212.71",//IP地址
  83. Port = _rabbitMQModel.Port,//5672,//端口号
  84. UserName = _rabbitMQModel.UserName,//"guest",//用户账号
  85. VirtualHost = _rabbitMQModel.VirtualHost,//"/",
  86. Password = _rabbitMQModel.Password,// "guest"//用户密码
  87. };
  88. if (con == null || con.IsOpen == false)
  89. {
  90. con = factory.CreateConnection();//创建连接对象
  91. }
  92. if (channel == null || channel.IsOpen == false)
  93. {
  94. channel = con.CreateModel();//创建连接会话对象
  95. }
  96. channel.ExchangeDeclare(_rabbitMQModel.ExchangeName, type: ExchangeType.Direct); // Direct 交换机示例
  97. //声明队列
  98. var queueName = channel.QueueDeclare(
  99. queue: _rabbitMQModel.QueueName, //消息队列名称
  100. durable: false, //是否缓存
  101. exclusive: false,
  102. autoDelete: false,
  103. arguments: null
  104. ).QueueName;
  105. channel.QueueBind(queueName, _rabbitMQModel.ExchangeName, _rabbitMQModel.RoutingKey);
  106. //告诉Rabbit每次只能向消费者发送一条信息,再消费者未确认之前,不再向他发送信息
  107. channel.BasicQos(0, 1, false);
  108. channel.ConfirmSelect(); // 开启消息确认模式
  109. //创建消费者对象
  110. var consumer = new EventingBasicConsumer(channel);
  111. consumer.Received += async (model, ea) =>
  112. {
  113. try
  114. {
  115. var body = ea.Body.ToArray();
  116. var message = Encoding.UTF8.GetString(body);
  117. log.Info("【RabbitMQ】" + message);
  118. var ReceiveData = JsonConvert.DeserializeObject<List<SendComRabbitMQParam>>(message);
  119. ReceiveData = ReceiveData.OrderBy(x => x.sort).ToList();
  120. bool result= await CmdExecute(ReceiveData);
  121. if (result)
  122. {
  123. // 确认消息已处理
  124. channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
  125. }
  126. else
  127. {
  128. channel.BasicNack(ea.DeliveryTag, false, true); // 重新入队或发送到死信队列的逻辑需要自定义实现
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. channel.BasicNack(ea.DeliveryTag, false, true); // 重新入队或发送到死信队列的逻辑需要自定义实现
  134. log.Info("【RabbitMQ-接收消息处理异常了】" + ex.Message);
  135. }
  136. };
  137. //消费者开启监听 将autoAck设置false 手动确认关闭;true:自动关闭;
  138. channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer);
  139. }
  140. catch (Exception ex)
  141. {
  142. log.Info("【异常-RabbitMQ】" + ex.Message);
  143. }
  144. }
  145. public async Task<bool > CmdExecute(List<SendComRabbitMQParam> list)
  146. {
  147. bool cmdResult= false;
  148. try
  149. {
  150. using (var scope = _provider.GetRequiredService<IServiceScopeFactory>().CreateScope())
  151. {
  152. var largeScreenEventRepository = scope.ServiceProvider.GetService<ILargeScreenEventRepository>();
  153. var ItdevBoxRepository = scope.ServiceProvider.GetService<ITdevBoxRepository>();
  154. foreach (var item in list)
  155. {
  156. if (item == null) continue;
  157. var items = await ItdevBoxRepository.GetByConditionAsync(C => C.C_ID == item.BoxId&&C.C_Status=="1");
  158. var content = items.FirstOrDefault();
  159. if (content == null)
  160. {
  161. log.Info($"数据错误-CmdExecut:盒子{item.BoxId},没有找到数据");
  162. }
  163. //0 = 禁用; 1 = 启用; 2 = 发送成功; 3 = 发送失败; 4 = 执行成功; 5 = 执行失败
  164. string satus = "2"; bool bol=false;
  165. switch (item.protocol)
  166. {
  167. case "MQTT":
  168. case "COME_TYPE_001":
  169. bol = await MQTTService(item.BoxId, item.pms, content.C_BoxNo);
  170. satus = bol ? "4" : "5";
  171. break;
  172. case "HTTP":
  173. case "COME_TYPE_002":
  174. bol= await HTTPService(item.BoxId, item.pms, content.C_BoxNo);
  175. break;
  176. case "TCP":
  177. case "COME_TYPE_003": break;
  178. case "UDP":
  179. case "COME_TYPE_004": break;
  180. //case "LoRa":
  181. //case "COME_TYPE_005": break;
  182. default: break;
  183. }
  184. if (satus == "4"|| satus == "5")
  185. {
  186. if (!string.IsNullOrEmpty(item.EveId))
  187. {
  188. cmdResult = await largeScreenEventRepository.UpdateLargeScreenEventStatusBYId(item.EveId, satus);
  189. }
  190. }
  191. else
  192. {
  193. cmdResult = true;
  194. }
  195. }
  196. }
  197. }
  198. catch (Exception ex)
  199. {
  200. log.Info("【异常-CmdExecute】" + ex.Message);
  201. }
  202. return cmdResult;
  203. }
  204. #region MQTT
  205. public async Task<bool> MQTTService(string boxCode,string param, string boxNo)
  206. {
  207. bool bol = true;
  208. try
  209. {
  210. var factory = new MqttFactory();
  211. var mqttClient = factory.CreateMqttClient();
  212. try
  213. {
  214. var optionsBuilder = new MqttClientOptionsBuilder()
  215. .WithTcpServer(_cmdModel.Mqtt.Ip, _cmdModel.Mqtt.Port) // 要访问的mqtt服务端的 ip 和 端口号
  216. .WithCredentials(_cmdModel.Mqtt.UserName, _cmdModel.Mqtt.Password) // 要访问的mqtt服务端的用户名和密码
  217. .WithClientId(Guid.NewGuid().ToString()) // 设置客户端id
  218. .WithCleanSession()
  219. .WithTls(new MqttClientOptionsBuilderTlsParameters
  220. {
  221. UseTls = false // 是否使用 tls加密
  222. }).Build();
  223. await mqttClient.ConnectAsync(optionsBuilder);
  224. string topic = _cmdModel.Mqtt.Theme + "/" + boxNo;// 027655321321654653
  225. // 发布消息
  226. var message = new MqttApplicationMessageBuilder()
  227. .WithTopic(topic) // 主题
  228. .WithPayload(param)
  229. .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
  230. .Build();
  231. var publishResult= await mqttClient.PublishAsync(message);
  232. log.Info($"【MQTT】【PublishAsync】{JsonConvert.SerializeObject(publishResult)}");
  233. }
  234. catch (Exception e)
  235. {
  236. log.Info("【异常-MqttFactory】" + e.Message);
  237. bol = false;
  238. }
  239. // 断开连接
  240. await mqttClient.DisconnectAsync();
  241. }
  242. catch (Exception ex)
  243. {
  244. log.Info("【异常-MQTTService】" + ex.Message);
  245. bol= false;
  246. }
  247. return bol;
  248. }
  249. #endregion
  250. #region HTTP【90%】
  251. public static TokenResult tokenResult;
  252. public static DateTime tokenTime;
  253. //webAPI
  254. public async Task<bool> HTTPService(string boxCode, string param,string boxNo)
  255. {
  256. bool result = false;
  257. try
  258. {
  259. var ReceiveData = JsonConvert.DeserializeObject<HttpMQModel>(param);
  260. if (ReceiveData== null)
  261. {
  262. log.Info($"参数错误-HTTPService【boxCode:{boxCode}】【param:{param}】");
  263. }
  264. if (tokenResult == null|| DateTime.Now >= tokenTime)
  265. {
  266. tokenResult = FanyiHelper.GetToken(new AccessUser { client_id = _cmdModel.Http.clientId, client_secret = _cmdModel.Http.clientSecret });
  267. tokenTime = DateTime.Now.AddSeconds(7200);
  268. }
  269. using (var scope = _provider.GetRequiredService<IServiceScopeFactory>().CreateScope())
  270. {
  271. var TdevBoxDevSpotRepository = scope.ServiceProvider.GetService<ITdevBoxDevSpotRepository>();
  272. var predicate = PredicateBuilder.New<TDEV_BoxDevSpot>(true);//查询条件,推荐后台使用这种方式灵活筛选
  273. predicate = predicate.And(i => i.C_Status.Equals("1"));
  274. predicate = predicate.And(i => i.C_BoxCode.Equals(boxCode));
  275. var items = await TdevBoxDevSpotRepository.GetByConditionAsync(predicate);
  276. if (!items.Any())
  277. {
  278. log.Info($"没有数据-HTTPService【boxCode:{boxCode}】里面没有设备点数据");
  279. }
  280. foreach (var item in items)
  281. {
  282. if (item!=null)
  283. {
  284. using (var httpClient = new HttpClient())
  285. {
  286. Dictionary<string, string> dict = new Dictionary<string, string>();
  287. httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenResult.access_token);
  288. dict["id"] = item.C_ID;//监控点uid,若使用id属性,则无需使用name
  289. dict["name"] = item.C_ChineseName;//监控点名称,使用name则无需使用id属性
  290. dict["groupname"] = item.C_GroupName;//监控分组名称,与name联合使用,用于区分不同分组下监控点名称重名
  291. //type-{0:value值为监控点中指定的类型},{1:value值整数为10进制值,浮点数和其它直接上传}
  292. dict["type"] = ReceiveData.type;
  293. dict["value"] = ReceiveData.value;// 值
  294. using (var content = new FormUrlEncodedContent(dict))
  295. {
  296. var msg = httpClient.PostAsync($"{_cmdModel.Http.Api}?boxNo={boxNo}", content);
  297. if (msg.IsFaulted)
  298. {
  299. log.Info($"接口数据返回-HTTPService:IsFaulted=true【boxCode:{boxCode}】");
  300. }
  301. else
  302. {
  303. var postResult = msg.Result.Content.ReadAsStringAsync().Result;
  304. log.Info($"接口数据返回-HTTPService【{JsonConvert.SerializeObject(postResult)}】");
  305. }
  306. }
  307. }
  308. }
  309. }
  310. }
  311. }
  312. catch (Exception ex)
  313. {
  314. log.Info("【异常-HTTPService】" + ex.Message);
  315. result = false;
  316. }
  317. return result;
  318. }
  319. //ip+端口
  320. public async Task<bool> HTTPService1(string boxCode, string param)
  321. {
  322. bool result = false;
  323. try
  324. {
  325. #region ip+端口
  326. var gatewayUri = $"http://60.204.212.71:1883/"; // 确保URI是正确的
  327. using (var httpClient = new HttpClient())
  328. {
  329. // 设置请求的URI
  330. httpClient.BaseAddress = new Uri(gatewayUri);
  331. // 准备要发送的数据
  332. var data = new StringContent(param, Encoding.UTF8, "application/json");
  333. //// 发送GET请求
  334. //var getResponse = await httpClient.GetAsync("http://example.com/api/data");
  335. // 发送POST请求
  336. var response = await httpClient.PostAsync($"{_cmdModel.Http.Api}?boxNo={boxCode}", data);
  337. // 确保响应成功
  338. response.EnsureSuccessStatusCode();
  339. log.Info($"数据发送成功-HTTPService【{JsonConvert.SerializeObject(response)}】");
  340. if (response.IsSuccessStatusCode)
  341. {
  342. // 读取响应内容(如果需要)
  343. string responseBody = await response.Content.ReadAsStringAsync();
  344. Console.WriteLine(responseBody);
  345. }
  346. }
  347. #endregion
  348. }
  349. catch (Exception ex)
  350. {
  351. log.Info("【异常-HTTPService】" + ex.Message);
  352. result = false;
  353. }
  354. return result;
  355. }
  356. #endregion
  357. #region TCP【50%】
  358. public bool TCPService(string boxCode, string param)
  359. {
  360. bool result = false;
  361. try
  362. {
  363. // 网关地址和端口
  364. string host = "60.204.212.71";
  365. int port = 1883;
  366. // 创建TcpClient实例
  367. using (TcpClient client = new TcpClient())
  368. {
  369. // 连接到网关
  370. client.Connect(host, port);
  371. // 获取网络流
  372. using (NetworkStream stream = client.GetStream())
  373. {
  374. // 接收数据
  375. byte[] bytes = new byte[1024];
  376. int bytesRead = stream.Read(bytes, 0, bytes.Length);
  377. string data = Encoding.UTF8.GetString(bytes, 0, bytesRead);
  378. Console.WriteLine("Received: " + data);
  379. log.Info($"数据接收-TCPService-Read【{data}】");
  380. if (!string.IsNullOrEmpty(data))
  381. {
  382. // 发送数据
  383. byte[] responseBytes = Encoding.UTF8.GetBytes(param);
  384. stream.Write(responseBytes, 0, responseBytes.Length);
  385. }
  386. // 关闭连接
  387. client.Close();
  388. }
  389. }
  390. }
  391. catch (Exception ex)
  392. {
  393. log.Info("【异常-TCPService】" + ex.Message);
  394. result = false;
  395. }
  396. return result;
  397. }
  398. public bool TCPService1(string boxCode, string param)
  399. {
  400. bool result = false;
  401. try
  402. {
  403. // 网关地址和端口
  404. string host = "60.204.212.71";
  405. int port = 1883;
  406. // 创建TcpClient实例
  407. using (TcpClient client = new TcpClient())
  408. {
  409. // 连接到网关
  410. client.Connect(host, port);
  411. // 获取网络流
  412. using (NetworkStream stream = client.GetStream())
  413. {
  414. //// 如果需要从服务器获取数据
  415. //byte[] bytes = new byte[1024];
  416. //int bytesRead0 = stream.Read(bytes, 0, bytes.Length);
  417. //string data0 = Encoding.ASCII.GetString(bytes, 0, bytesRead0);
  418. // 要发送的数据
  419. //string data = "Hello, TCP Server!";
  420. byte[] buffer = Encoding.UTF8.GetBytes(param);
  421. // 发送数据
  422. stream.Write(buffer, 0, buffer.Length);
  423. // 接收响应(如果有的话)
  424. buffer = new byte[client.ReceiveBufferSize];
  425. int bytesRead = stream.Read(buffer, 0, client.ReceiveBufferSize);
  426. if (bytesRead > 0)
  427. {
  428. string response = Encoding.UTF8.GetString(buffer, 0, bytesRead);
  429. log.Info($"数据发送成功-TCPService【{JsonConvert.SerializeObject(response)}】");
  430. }
  431. }
  432. }
  433. }
  434. catch (Exception ex)
  435. {
  436. log.Info("【异常-TCPService】" + ex.Message);
  437. result = false;
  438. }
  439. return result;
  440. }
  441. #endregion
  442. #region UDP【50%】
  443. public bool UDPService(string boxCode, string param)
  444. {
  445. bool result = false;
  446. try
  447. {
  448. // 网关IP地址和端口
  449. string gatewayIp = "60.204.212.71";
  450. int gatewayPort = 1883;
  451. // UDP数据
  452. byte[] data = Encoding.UTF8.GetBytes(param);
  453. // 创建UdpClient实例
  454. using (UdpClient client = new UdpClient())
  455. {
  456. // 目标IP地址和端口
  457. IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(gatewayIp), gatewayPort);
  458. try
  459. {
  460. byte[] data1 = client.Receive(ref endPoint);
  461. string message = Encoding.ASCII.GetString(data1);
  462. // 发送数据
  463. int sendResult= client.Send(data, data.Length, endPoint);
  464. log.Info($"数据发送成功-UDPService【{sendResult}】");
  465. }
  466. catch (Exception ex)
  467. {
  468. log.Info("【数据发送失败-异常-UDPService】" + ex.Message);
  469. result = false;
  470. }
  471. }
  472. }
  473. catch (Exception ex)
  474. {
  475. log.Info("【异常-UDPService】" + ex.Message);
  476. result = false;
  477. }
  478. return result;
  479. }
  480. public bool UDPService2(string boxCode, string param)
  481. {
  482. try
  483. {
  484. string gatewayIp = "60.204.212.71";
  485. // 固定的IP地址和端口
  486. IPAddress ip = IPAddress.Parse("127.0.0.1");//"127.0.0.1"
  487. int port = 1883;
  488. // 目标IP地址和端口
  489. IPEndPoint endPoint = new IPEndPoint(ip, port);
  490. Socket socket;
  491. socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  492. socket.Bind(endPoint);
  493. EndPoint endPoint1 = new IPEndPoint(IPAddress.Any, 0);
  494. // 接收数据
  495. byte[] bytes = new byte[1024];
  496. int bytesRead = socket.ReceiveFrom(bytes, ref endPoint1);
  497. string data = Encoding.UTF8.GetString(bytes, 0, bytesRead);
  498. // 创建UdpClient实例并绑定到本地的任意可用端口
  499. using (UdpClient udpClient = new UdpClient(port))
  500. {
  501. // 绑定到指定的远程端口、【
  502. udpClient.Client.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.ReuseAddress, true);
  503. udpClient.Client.Bind(new IPEndPoint(ip, port));
  504. Console.WriteLine($"Listening for UDP data on {udpClient.Client.LocalEndPoint}");
  505. // 无限循环来接收数据
  506. while (true)
  507. {
  508. try
  509. {
  510. // 等待接收数据
  511. IPEndPoint remoteEndPoint = null;
  512. byte[] buffer = udpClient.Receive(ref remoteEndPoint);
  513. // 如果不是预期的IP地址,则忽略
  514. if (remoteEndPoint.Address != ip)
  515. continue;
  516. // 将字节转换为字符串
  517. string message = Encoding.UTF8.GetString(buffer);
  518. Console.WriteLine($"Received: {message} from {remoteEndPoint}");
  519. }
  520. catch (Exception e)
  521. {
  522. Console.WriteLine(e.ToString());
  523. }
  524. }
  525. }
  526. }
  527. catch (Exception ex)
  528. {
  529. throw;
  530. }
  531. }
  532. #endregion
  533. #region LoRa【50%】
  534. public bool LoRaService(string boxCode, string param)
  535. {
  536. bool result = false;
  537. try
  538. {
  539. string _gatewayAddress= "60.204.212.71";
  540. int _gatewayPort= 1883;
  541. using (TcpClient client = new TcpClient(_gatewayAddress, _gatewayPort))
  542. using (NetworkStream stream = client.GetStream())
  543. using (StreamWriter writer = new StreamWriter(stream))
  544. {
  545. // 这里的param应该是符合LoRa协议的数据包格式
  546. writer.WriteLine(param);
  547. writer.Flush();
  548. // 可以添加代码来读取网关的响应
  549. }
  550. }
  551. catch (Exception ex)
  552. {
  553. log.Info("【异常-LoRaService】" + ex.Message);
  554. result = false;
  555. }
  556. return result;
  557. }
  558. #endregion
  559. public Task StopAsync(CancellationToken cancellationToken)
  560. {
  561. Dispose();
  562. return Task.CompletedTask;
  563. }
  564. public void Dispose()
  565. {
  566. if (channel!=null)
  567. {
  568. channel.Close();
  569. }
  570. if (con!=null)
  571. {
  572. con.Close();
  573. }
  574. }
  575. }
  576. }