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.
 
 
 
 

75 rivejä
2.3 KiB

  1. using System;
  2. using MQTTnet.Client;
  3. namespace MQTTnet.ManagedClient
  4. {
  5. public class ManagedMqttClientOptionsBuilder
  6. {
  7. private readonly ManagedMqttClientOptions _options = new ManagedMqttClientOptions();
  8. private MqttClientOptionsBuilder _clientOptionsBuilder;
  9. public ManagedMqttClientOptionsBuilder WithAutoReconnectDelay(TimeSpan value)
  10. {
  11. _options.AutoReconnectDelay = value;
  12. return this;
  13. }
  14. public ManagedMqttClientOptionsBuilder WithStorage(IManagedMqttClientStorage value)
  15. {
  16. _options.Storage = value;
  17. return this;
  18. }
  19. public ManagedMqttClientOptionsBuilder WithClientOptions(IMqttClientOptions value)
  20. {
  21. if (_clientOptionsBuilder != null)
  22. {
  23. throw new InvalidOperationException("Cannot use client options builder and client options at the same time.");
  24. }
  25. _options.ClientOptions = value ?? throw new ArgumentNullException(nameof(value));
  26. return this;
  27. }
  28. public ManagedMqttClientOptionsBuilder WithClientOptions(MqttClientOptionsBuilder builder)
  29. {
  30. if (_options.ClientOptions != null)
  31. {
  32. throw new InvalidOperationException("Cannot use client options builder and client options at the same time.");
  33. }
  34. _clientOptionsBuilder = builder;
  35. return this;
  36. }
  37. public ManagedMqttClientOptionsBuilder WithClientOptions(Action<MqttClientOptionsBuilder> options)
  38. {
  39. if (options == null) throw new ArgumentNullException(nameof(options));
  40. if (_clientOptionsBuilder == null)
  41. {
  42. _clientOptionsBuilder = new MqttClientOptionsBuilder();
  43. }
  44. options(_clientOptionsBuilder);
  45. return this;
  46. }
  47. public ManagedMqttClientOptions Build()
  48. {
  49. if (_clientOptionsBuilder != null)
  50. {
  51. _options.ClientOptions = _clientOptionsBuilder.Build();
  52. }
  53. if (_options.ClientOptions == null)
  54. {
  55. throw new InvalidOperationException("The ClientOptions cannot be null.");
  56. }
  57. return _options;
  58. }
  59. }
  60. }