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.
 
 
 
 

123 line
4.3 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 sessionId, MqttClientSessionsManager mqttClientSessionsManager)
  80. {
  81. return new MqttClientSession(sessionId, mqttClientSessionsManager, _serviceProvider.GetRequiredService<ILogger<MqttClientSession>>(), _serviceProvider.GetRequiredService<ILogger<MqttClientPendingMessagesQueue>>());
  82. }
  83. public IMqttClient CreateMqttClient()
  84. {
  85. return _serviceProvider.GetRequiredService<IMqttClient>();
  86. }
  87. public IManagedMqttClient CreateManagedMqttClient()
  88. {
  89. return _serviceProvider.GetRequiredService<IManagedMqttClient>();
  90. }
  91. public IMqttServer CreateMqttServer()
  92. {
  93. return _serviceProvider.GetRequiredService<IMqttServer>();
  94. }
  95. public IMqttServer CreateMqttServer(Action<MqttServerOptions> configure)
  96. {
  97. var options = _serviceProvider.GetRequiredService<IOptions<MqttServerOptions>>();
  98. configure(options.Value);
  99. return _serviceProvider.GetRequiredService<IMqttServer>();
  100. }
  101. }
  102. }