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.
 
 

139 lines
5.7 KiB

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