You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MqttM2.cs 5.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using uPLibrary.Networking.M2Mqtt;
  11. using uPLibrary.Networking.M2Mqtt.Messages;
  12. using Newtonsoft.Json;
  13. using System.Collections.Concurrent;
  14. using System.Drawing;
  15. using HBLConsole.Model;
  16. using HBLConsole.Service;
  17. namespace HBLConsole.Communication
  18. {
  19. public class MqttM2
  20. {
  21. private volatile static MqttM2 _Instance;
  22. public static MqttM2 GetInstance => _Instance ?? (_Instance = new MqttM2());
  23. private MqttM2() { }
  24. string productKey = "grgp0rFA2uu";//产品密钥
  25. string deviceName = "Machine01";//设备名称
  26. string deviceSecret = "ee810ac992033ffc3047f4e80cb49521";//设备秘钥
  27. MqttClient mqttClient;
  28. //IotMessage iotMessage = new IotMessage();
  29. //LowerComputerStateInfo lowerComputerStateInfoClass = new LowerComputerStateInfo();
  30. //string PublishContent;
  31. /// <summary>
  32. /// 连接成功
  33. /// </summary>
  34. public Action ConnectOk { get; set; }
  35. public void Init()
  36. {
  37. // 计算MQTT连接参数。
  38. MqttSign sign = new MqttSign();
  39. sign.calculate(productKey, deviceName, deviceSecret);
  40. // 使用Paho连接阿里云物联网平台。
  41. int port = 443;
  42. string broker = productKey + ".iot-as-mqtt.cn-shanghai.aliyuncs.com";
  43. mqttClient = new MqttClient(broker, port, true, MqttSslProtocols.TLSv1_2, null, null);
  44. mqttClient.Connect(sign.getClientid(), sign.getUsername(), sign.getPassword(), true, 60);
  45. if (mqttClient.IsConnected)
  46. {
  47. if (ConnectOk != null) ConnectOk();
  48. MessageLog.GetInstance.Show("阿里云连接成功");
  49. //订阅消息
  50. //string[] topic = new string[] { "/sys/grgp0rFA2uu/Machine01/thing/event/property/post" };
  51. //byte[] qosLevels = new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }; // qos=2
  52. //mqttClient.Subscribe(topic, qosLevels);
  53. //mqttClient.MqttMsgPublishReceived += MqttClient_MqttMsgPublishReceived;//接收消息事件
  54. //PublishInfo();
  55. }
  56. else
  57. MessageLog.GetInstance.Show("阿里云连接失败");
  58. }
  59. private void MqttClient_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
  60. {
  61. MessageLog.GetInstance.Show($"接收到阿里云消息:{Encoding.UTF8.GetString(e.Message)}");
  62. }
  63. /// <summary>
  64. /// 推送消息
  65. /// </summary>
  66. public void PublishInfo(string Info)
  67. {
  68. mqttClient.Publish($"/sys/grgp0rFA2uu/{deviceName}/thing/event/property/post", Encoding.UTF8.GetBytes(Info), 0, false);
  69. //ThreadManagerment.GetInstance.StartLong(new Action(() =>
  70. //{
  71. // AlarmHelper.GetInstance.EdgeAlarm(GVL_Fault.GetInstance.MCUDisconnected, "单片机掉线", AlarmLevel.严重报警);
  72. // AlarmHelper.GetInstance.EdgeAlarm(GVL_Fault.GetInstance.IceCreamAbnormal, "冰淇淋机异常");
  73. // AlarmHelper.GetInstance.EdgeAlarm(GVL_Fault.GetInstance.TakeBowlAbnormal, "取碗异常");
  74. // AlarmHelper.GetInstance.EdgeAlarm(GVL_Fault.GetInstance.RobotAbnormal, "机器人异常", AlarmLevel.严重报警);
  75. // #region 报警信息设置
  76. // lowerComputerStateInfoClass.AlarmInfo.Clear();
  77. // foreach (var item in AlarmHelper.GetInstance.GetAlarmInfo)
  78. // {
  79. // lowerComputerStateInfoClass.AlarmInfo.Add(new IotAlarmInfo()
  80. // {
  81. // time = item.date,
  82. // AlarmLevel = item.AlarmLevel,
  83. // info = item.AlarmInfomation,
  84. // color = new ColorSet()
  85. // {
  86. // a = AlarmHelper.GetInstance.colorValue[item.AlarmLevel].A,
  87. // b = AlarmHelper.GetInstance.colorValue[item.AlarmLevel].B,
  88. // g = AlarmHelper.GetInstance.colorValue[item.AlarmLevel].G,
  89. // r = AlarmHelper.GetInstance.colorValue[item.AlarmLevel].R,
  90. // },
  91. // infoSrc = ""
  92. // });
  93. // }
  94. // #endregion
  95. // #region 报警频率显示
  96. // lowerComputerStateInfoClass.AlarmHz.Clear();
  97. // foreach (var item in Json<AlarmHzParameter>.GetInstance.Base.AlarmHz)
  98. // {
  99. // lowerComputerStateInfoClass.AlarmHz.Add(new AlarmHzObj() { Count = item.Value, Name = item.Key });
  100. // }
  101. // #endregion
  102. // #region 忙碌状态显示
  103. // lowerComputerStateInfoClass.Busy = GVL_VAR.GetInstance.Busy ? "空闲" : "忙碌";
  104. // #endregion
  105. // iotMessage.@params.LowerComputerStateInfo = JsonConvert.SerializeObject(lowerComputerStateInfoClass);
  106. // PublishContent = JsonConvert.SerializeObject(iotMessage);
  107. // mqttClient.Publish($"/sys/grgp0rFA2uu/{deviceName}/thing/event/property/post", Encoding.UTF8.GetBytes(PublishContent), 0, false);
  108. // Thread.Sleep(1000);
  109. //}), "阿里云数据上报", new Action(() => { if (mqttClient != null) mqttClient.Disconnect(); }));
  110. }
  111. /// <summary>
  112. /// 断开连接
  113. /// </summary>
  114. public void DisConnect()
  115. {
  116. if (mqttClient != null) mqttClient.Disconnect();
  117. }
  118. }
  119. }