Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

130 рядки
4.6 KiB

  1. using System;
  2. using MQTTnet.Core.Adapter;
  3. using MQTTnet.Core.Client;
  4. using MQTTnet.Core.Serializer;
  5. using Microsoft.Extensions.Logging;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Options;
  8. using MQTTnet.Implementations;
  9. using MQTTnet.Core.ManagedClient;
  10. using MQTTnet.Core.Server;
  11. using MQTTnet.Core.Channel;
  12. namespace MQTTnet
  13. {
  14. public class MqttFactory : IMqttCommunicationAdapterFactory, IMqttClientSesssionFactory, IMqttClientFactory, IMqttServerFactory
  15. {
  16. private readonly IServiceProvider _serviceProvider;
  17. private static IServiceProvider BuildServiceProvider()
  18. {
  19. var serviceProvider = new ServiceCollection()
  20. .AddMqttClient()
  21. .AddMqttServer()
  22. .AddLogging()
  23. .BuildServiceProvider();
  24. serviceProvider.GetRequiredService<ILoggerFactory>()
  25. .AddMqttTrace();
  26. return serviceProvider;
  27. }
  28. public MqttFactory()
  29. : this(BuildServiceProvider())
  30. {
  31. }
  32. public MqttFactory(IServiceProvider serviceProvider)
  33. {
  34. _serviceProvider = serviceProvider;
  35. }
  36. public ILoggerFactory GetLoggerFactory()
  37. {
  38. return _serviceProvider.GetRequiredService<ILoggerFactory>();
  39. }
  40. public IMqttCommunicationAdapter CreateClientMqttCommunicationAdapter(IMqttClientOptions options)
  41. {
  42. var logger = _serviceProvider.GetRequiredService<ILogger<MqttChannelCommunicationAdapter>>();
  43. return new MqttChannelCommunicationAdapter(CreateMqttCommunicationChannel(options.ChannelOptions), CreateSerializer(options.ProtocolVersion), logger);
  44. }
  45. public IMqttCommunicationAdapter CreateServerMqttCommunicationAdapter(IMqttCommunicationChannel channel)
  46. {
  47. var serializer = _serviceProvider.GetRequiredService<IMqttPacketSerializer>();
  48. var logger = _serviceProvider.GetRequiredService<ILogger<MqttChannelCommunicationAdapter>>();
  49. return new MqttChannelCommunicationAdapter(channel, serializer, logger);
  50. }
  51. public IMqttCommunicationChannel CreateMqttCommunicationChannel(IMqttClientChannelOptions options)
  52. {
  53. if (options == null) throw new ArgumentNullException(nameof(options));
  54. switch (options)
  55. {
  56. case MqttClientTcpOptions tcpOptions:
  57. return CreateTcpChannel(tcpOptions);
  58. case MqttClientWebSocketOptions webSocketOptions:
  59. return CreateWebSocketChannel(webSocketOptions);
  60. default:
  61. throw new NotSupportedException();
  62. }
  63. }
  64. public MqttTcpChannel CreateTcpChannel(MqttClientTcpOptions tcpOptions)
  65. {
  66. return new MqttTcpChannel(tcpOptions);
  67. }
  68. public MqttWebSocketChannel CreateWebSocketChannel(MqttClientWebSocketOptions webSocketOptions)
  69. {
  70. return new MqttWebSocketChannel(webSocketOptions);
  71. }
  72. public MqttPacketSerializer CreateSerializer(MqttProtocolVersion protocolVersion)
  73. {
  74. return new MqttPacketSerializer
  75. {
  76. ProtocolVersion = protocolVersion
  77. };
  78. }
  79. public MqttClientSession CreateClientSession(string clientId, MqttClientSessionsManager clientSessionsManager)
  80. {
  81. return new MqttClientSession(
  82. clientId,
  83. _serviceProvider.GetRequiredService<IOptions<MqttServerOptions>>(),
  84. clientSessionsManager,
  85. _serviceProvider.GetRequiredService<MqttClientSubscriptionsManager>(),
  86. _serviceProvider.GetRequiredService<ILogger<MqttClientSession>>(),
  87. _serviceProvider.GetRequiredService<ILogger<MqttClientPendingMessagesQueue>>());
  88. }
  89. public IMqttClient CreateMqttClient()
  90. {
  91. return _serviceProvider.GetRequiredService<IMqttClient>();
  92. }
  93. public IManagedMqttClient CreateManagedMqttClient()
  94. {
  95. return _serviceProvider.GetRequiredService<IManagedMqttClient>();
  96. }
  97. public IMqttServer CreateMqttServer()
  98. {
  99. return _serviceProvider.GetRequiredService<IMqttServer>();
  100. }
  101. public IMqttServer CreateMqttServer(Action<MqttServerOptions> configure)
  102. {
  103. if (configure == null) throw new ArgumentNullException(nameof(configure));
  104. var options = _serviceProvider.GetRequiredService<IOptions<MqttServerOptions>>();
  105. configure(options.Value);
  106. return _serviceProvider.GetRequiredService<IMqttServer>();
  107. }
  108. }
  109. }