No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

108 líneas
3.9 KiB

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