diff --git a/lib/src/binding_mqtt/constants.dart b/lib/src/binding_mqtt/constants.dart index 90f4cc91..5b60baf0 100644 --- a/lib/src/binding_mqtt/constants.dart +++ b/lib/src/binding_mqtt/constants.dart @@ -32,8 +32,8 @@ const mqttContextUri = "http://www.example.org/mqtt-binding#"; /// The default prefix used in MQTT-related compact URIs (CURIEs) in TDs. const defaultMqttPrefix = "mqv"; -/// Default timeout length used for reading properties and discovering TDs. -const defaultTimeout = Duration(seconds: 10); +/// Default timeout length used for reading properties. +const defaultReadTimeout = Duration(seconds: 10); /// Default duration MQTT connections are kept alive in seconds. const defaultKeepAlivePeriod = 20; diff --git a/lib/src/binding_mqtt/mqtt_client.dart b/lib/src/binding_mqtt/mqtt_client.dart index e5442921..09c54ed4 100644 --- a/lib/src/binding_mqtt/mqtt_client.dart +++ b/lib/src/binding_mqtt/mqtt_client.dart @@ -203,6 +203,7 @@ final class MqttClient extends ProtocolClient with MqttDiscoverer { Uri brokerUri, { required String discoveryTopic, required String expectedContentType, + required Duration discoveryTimeout, }) async* { final client = await _connect(brokerUri, null); @@ -221,7 +222,7 @@ final class MqttClient extends ProtocolClient with MqttDiscoverer { } Timer( - _mqttConfig.discoveryTimeout, + discoveryTimeout, () async { client.disconnect(); }, diff --git a/lib/src/binding_mqtt/mqtt_config.dart b/lib/src/binding_mqtt/mqtt_config.dart index 4af38cde..fc16675d 100644 --- a/lib/src/binding_mqtt/mqtt_config.dart +++ b/lib/src/binding_mqtt/mqtt_config.dart @@ -14,7 +14,7 @@ import "constants.dart"; /// The default [QoS] values for the different operation types will be used if /// no Quality of Service is defined in the respective form. /// -/// If no [readTimeout] or [discoveryTimeout] is defined, a [defaultTimeout] of +/// If no [readTimeout] is defined, a default timeout of /// 10 seconds will be used. Furthermore, the [keepAlivePeriod] defaults to 20 /// seconds. class MqttConfig { @@ -24,8 +24,7 @@ class MqttConfig { this.defaultWriteQoS = QoS.atMostOnce, this.defaultActionQoS = QoS.atMostOnce, this.defaultSubscribeQoS = QoS.atLeastOnce, - this.readTimeout = defaultTimeout, - this.discoveryTimeout = defaultTimeout, + this.readTimeout = defaultReadTimeout, this.keepAlivePeriod = defaultKeepAlivePeriod, }); @@ -50,11 +49,6 @@ class MqttConfig { /// If no value has been read until the timeout has expired, the operation /// will be canceled. final Duration readTimeout; - - /// Timeout value used for discovery using MQTT. - /// - /// The discovery process will be aborted once the timeout has expired. - final Duration discoveryTimeout; } /// Enum for indicating the default Quality of Service (QoS) that should be used diff --git a/lib/src/core/implementation/discovery/discovery_configuration.dart b/lib/src/core/implementation/discovery/discovery_configuration.dart index e6fbdbfa..e706326d 100644 --- a/lib/src/core/implementation/discovery/discovery_configuration.dart +++ b/lib/src/core/implementation/discovery/discovery_configuration.dart @@ -147,6 +147,7 @@ final class MqttDiscoveryConfiguration extends DiscoveryConfiguration { this.brokerUri, { this.discoveryTopic = "wot/td/#", this.expectedContentType = "application/td+json", + this.discoveryTimeout = const Duration(seconds: 5), }); /// [Uri] of the broker the @@ -154,10 +155,13 @@ final class MqttDiscoveryConfiguration extends DiscoveryConfiguration { /// The topic that will be used for performing the discovery process. /// + /// If a wildcard topic is used, then the discovery process may return more + /// than one TD. + /// /// Defaults to `wot/td/#`. final String discoveryTopic; - /// The content type that is expected by to be returned during the discovery + /// The Thing Description content type that is expected during the discovery /// process. /// /// Data that is received during the discovery process that is not @@ -165,6 +169,12 @@ final class MqttDiscoveryConfiguration extends DiscoveryConfiguration { /// /// Defaults to `application/td+json`. final String expectedContentType; + + /// Time period after which the MQTT discovery process is going to be + /// cancelled. + /// + /// Defaults to five seconds. + final Duration discoveryTimeout; } /// Base class for configuring discovery mechanisms that involve a two-step diff --git a/lib/src/core/implementation/thing_discovery.dart b/lib/src/core/implementation/thing_discovery.dart index 3f4c8832..76ca079a 100644 --- a/lib/src/core/implementation/thing_discovery.dart +++ b/lib/src/core/implementation/thing_discovery.dart @@ -74,11 +74,13 @@ class ThingDiscovery extends Stream :final brokerUri, :final discoveryTopic, :final expectedContentType, + :final discoveryTimeout, ): yield* _performMqttDiscovery( brokerUri, discoveryTopic, expectedContentType, + discoveryTimeout, ); } } @@ -253,6 +255,7 @@ class ThingDiscovery extends Stream Uri brokerUri, String discoveryTopic, String expectedContentType, + Duration discoveryTimeout, ) async* { final client = _clientForUriScheme(brokerUri.scheme); @@ -261,6 +264,7 @@ class ThingDiscovery extends Stream brokerUri, discoveryTopic: discoveryTopic, expectedContentType: expectedContentType, + discoveryTimeout: discoveryTimeout, ); await for (final content in contentStream) { diff --git a/lib/src/core/protocol_interfaces/protocol_discoverer.dart b/lib/src/core/protocol_interfaces/protocol_discoverer.dart index 889a5e18..cc4dc787 100644 --- a/lib/src/core/protocol_interfaces/protocol_discoverer.dart +++ b/lib/src/core/protocol_interfaces/protocol_discoverer.dart @@ -61,5 +61,6 @@ base mixin MqttDiscoverer on ProtocolClient { Uri brokerUri, { required String discoveryTopic, required String expectedContentType, + required Duration discoveryTimeout, }); }