CheryClient.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using MQTTnet;
  6. using MQTTnet.Client;
  7. using MQTTnet.Extensions.ManagedClient;
  8. using NLog.Fluent;
  9. namespace Dfs.WayneChina.CheryFuelController
  10. {
  11. public class CheryClient
  12. {
  13. private readonly string _host;
  14. private readonly int _port;
  15. private readonly string _clientId;
  16. private readonly int _interval;
  17. private readonly string _jobDownUrl;
  18. private readonly string _jobResUpUrl;
  19. private IManagedMqttClient _mqttClient;
  20. #region Logger
  21. NLog.Logger logger = NLog.LogManager.LoadConfiguration("NLog.config").GetLogger("Application");
  22. #endregion
  23. public CheryClient(string host, int port, string clientId, string username, string password,
  24. int interval, string jobDownUrl, string jobResultUpUrl)
  25. {
  26. _host = host;
  27. _port = port;
  28. _clientId = clientId;
  29. _interval = interval;
  30. _jobDownUrl = jobDownUrl;
  31. _jobResUpUrl = jobResultUpUrl;
  32. _mqttClient = new MqttFactory().CreateManagedMqttClient();
  33. _mqttClient.ConnectedAsync += this.OnConnected;
  34. _mqttClient.DisconnectedAsync += this.OnDisconnected;
  35. _mqttClient.ConnectingFailedAsync += this.OnConnectingFailed;
  36. _mqttClient.ApplicationMessageReceivedAsync += this.HandleApplicationMessageReceivedAsync;
  37. }
  38. public Task HandleApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs eventArgs)
  39. {
  40. string topic = $"Topic: {eventArgs.ApplicationMessage.Topic}";
  41. string payload = $"- Payload: {System.Text.Encoding.UTF8.GetString(eventArgs.ApplicationMessage.Payload)}";
  42. logger.Info($"Received: {topic + payload}");
  43. return Task.CompletedTask;
  44. }
  45. private ManagedMqttClientOptions CreateOptions(string host, int port, string clientId)
  46. {
  47. MqttClientOptionsBuilder builder = new MqttClientOptionsBuilder()
  48. //.WithClientId(clientId)
  49. .WithTcpServer(host, port);
  50. ManagedMqttClientOptions options = new ManagedMqttClientOptionsBuilder()
  51. .WithAutoReconnectDelay(TimeSpan.FromSeconds(_interval))
  52. .WithClientOptions(builder.Build())
  53. .Build();
  54. return options;
  55. }
  56. public async Task StartAsync()
  57. {
  58. var options = CreateOptions(_host, _port, _clientId);
  59. await _mqttClient.StartAsync(options);
  60. string topic = "test/downFillSet";
  61. await _mqttClient.SubscribeAsync(new[] { new MqttTopicFilterBuilder().WithTopic(topic).Build() });
  62. }
  63. public Task StopAsync()
  64. {
  65. return _mqttClient.StopAsync();
  66. }
  67. public Task OnConnected(MqttClientConnectedEventArgs obj)
  68. {
  69. logger.Info($"Successfully connected to Broker, assigned id: {obj.ConnectResult.AssignedClientIdentifier}");
  70. return Task.CompletedTask;
  71. }
  72. public Task OnConnectingFailed(ConnectingFailedEventArgs obj)
  73. {
  74. logger.Error($"Couldn't connect to broker, {obj.Exception}");
  75. return Task.CompletedTask;
  76. }
  77. public Task OnDisconnected(MqttClientDisconnectedEventArgs obj)
  78. {
  79. logger.Error("Successfully disconnected.");
  80. return Task.CompletedTask;
  81. }
  82. }
  83. }