终端一体化运控平台
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

83 líneas
2.9 KiB

  1. using BPA.Communication;
  2. using BPA.Communication.Base;
  3. using BPA.Helper;
  4. using BPA.Message;
  5. using BPASmartClient.JXJFoodBigStation.Model.MQTT.Message;
  6. using BPASmartClient.JXJFoodBigStation.Model.Siemens;
  7. using Microsoft.EntityFrameworkCore.ChangeTracking;
  8. using System;
  9. using System.Collections.Concurrent;
  10. using System.Collections.Generic;
  11. using System.Configuration;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. namespace BPASmartClient.JXJFoodBigStation.Model.MQTT
  17. {
  18. public class MqttServer
  19. {
  20. private volatile static MqttServer _Instance;
  21. public static void Dis() => _Instance = null;
  22. public static MqttServer GetInstance => _Instance ?? (_Instance = new MqttServer());
  23. private MqttServer() { }
  24. MqttHelper mqttHelper = new MqttHelper();
  25. /// <summary>
  26. /// MQTT 消息
  27. /// </summary>
  28. ConcurrentQueue<string> msg = new ConcurrentQueue<string>();
  29. public Task Init(params object[] pars)
  30. {
  31. return Task.Factory.StartNew(() =>
  32. {
  33. //mqttHelper.IsVerifyNetwork = false;
  34. mqttHelper.MessageRecive = new Action<string>(s => { msg.Enqueue(s); });
  35. mqttHelper.Connected = new Action(() =>
  36. {
  37. if (pars.Length > 0)
  38. {
  39. pars.ToList().ForEach(item => { mqttHelper.Subscrib(item?.ToString()); });
  40. }
  41. else
  42. {
  43. //订阅消息
  44. }
  45. TaskManage.GetInstance.StartLong(new Action(() =>
  46. {
  47. while (msg.Count > 0)
  48. {
  49. try
  50. {
  51. if (msg.TryDequeue(out string str))
  52. {
  53. //解析消息
  54. }
  55. }
  56. catch (Exception ex)
  57. {
  58. MessageLog.GetInstance.ShowEx(ex.ToString());
  59. }
  60. }
  61. Thread.Sleep(10);
  62. }), "MCS工单信息接收", true);
  63. });
  64. mqttHelper.Connect(new ConfigurationOptions()
  65. {
  66. UserName= ConfigurationManager.AppSettings["MQTT_USERNAME"],
  67. Password= ConfigurationManager.AppSettings["MQTT_PASSWORD"],
  68. IpAddress = ConfigurationManager.AppSettings["MQTT_IPADDRESS"],
  69. Port =Convert.ToInt32( ConfigurationManager.AppSettings["MQTT_PORT"]),
  70. ClientId = $"MES系统业务消息,{Guid.NewGuid()}"
  71. });
  72. });
  73. }
  74. }
  75. }