| | |
| | | using MQTTnet; |
| | | using Microsoft.Extensions.Configuration; |
| | | using MQTTnet; |
| | | using MQTTnet.Client; |
| | | using MQTTnet.Protocol; |
| | | using MQTTnet.Server; |
| | |
| | | |
| | | namespace Tiger.IBusiness.Utility |
| | | { |
| | | /// <summary> |
| | | /// MQTTHelper |
| | | /// </summary> |
| | | public static class MQTTHelper |
| | | { |
| | | #region Variables |
| | | private static IConfiguration Setting = ApiConfig.Configuration; |
| | | #endregion |
| | | |
| | | #region Propertys |
| | | |
| | | #endregion |
| | | |
| | | #region Functions |
| | | public static void Start() |
| | | { |
| | | try |
| | | { |
| | | if (Setting["MQTT:Enable"].ToBoolean() == true) |
| | | { |
| | | |
| | | ConsoleExt.WriteLine("Start MQTT Service..........", ConsoleColor.Yellow); |
| | | Logger.Default.Info("Start MQTT Service"); |
| | | } |
| | | } |
| | | catch (System.Exception ex) |
| | | { |
| | | Logger.Default.Fatal(ex, "Start MQTT Service Exception"); |
| | | } |
| | | } |
| | | |
| | | public static void Stop() |
| | | { |
| | | try |
| | | { |
| | | |
| | | Logger.Console.Info("Stop MQTT Service"); |
| | | } |
| | | catch (System.Exception ex) |
| | | { |
| | | Logger.Console.Fatal(ex, "Stop MQTT Service Exception"); |
| | | } |
| | | } |
| | | |
| | | |
| | | #endregion |
| | | } |
| | | /// <summary> |
| | | /// EMQX 帮助类 |
| | | /// </summary> |
| | |
| | | Password = password; |
| | | } |
| | | |
| | | #region Variables |
| | | private List<string> TopicList = new(); |
| | | #endregion |
| | | |
| | | #region Propertys |
| | | public string Id { get; set; } = Guid.NewGuid().ToString("N"); |
| | | public string Tag { get; set; } = "ClientMonitor"; |
| | | public string Name { get; set; } = "ClientMonitor"; |
| | | public bool IsRunning { get; set; } |
| | | public IMqttClient Client { get; set; } = new MqttFactory().CreateMqttClient();//MQTT客户端 |
| | | public string Address { get; set; } |
| | | public int Port { get; set; } = 1883; |
| | | public string ClientId { get; set; } = $"{Environment.MachineName}-Client-{Guid.NewGuid():N}"; |
| | | public string ClientId { get; set; } = $"{Environment.MachineName}-ApiClient-{Guid.NewGuid():N}"; |
| | | public string Username { get; set; } |
| | | public string Password { get; set; } |
| | | public int ApiPort { get; set; } = 18083; |
| | | public string ApiKey { get; set; } |
| | | public string ApiSecretKey { get; set; } |
| | | public string ApiCredentials => Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ApiKey}:{ApiSecretKey}")); |
| | | /// <summary> |
| | | /// MQTT客户端 |
| | | /// </summary> |
| | | public IMqttClient Client = new MqttFactory().CreateMqttClient(); |
| | | private List<string> TopicList = new(); |
| | | //遗嘱消息 |
| | | public string WillMsgTopic { get; set; } |
| | | public MqttQualityOfServiceLevel WillMsgQos { get; set; } |
| | | public bool WillMsgRetain { get; set; } |
| | | public string WillMsgPayload { get; set; } |
| | | public int? WillMsgDelay { get; set; } |
| | | public int? WillMsgExpiry { get; set; } |
| | | #endregion |
| | | |
| | | |
| | | public Result<IMqttClient> Connect(string clientId = null) |
| | | { |