Skip to content

Commit

Permalink
fixup! feat!: rework discoverer APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed May 29, 2024
1 parent 2d81eb4 commit 389c63b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/src/binding_mqtt/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion lib/src/binding_mqtt/mqtt_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -221,7 +222,7 @@ final class MqttClient extends ProtocolClient with MqttDiscoverer {
}

Timer(
_mqttConfig.discoveryTimeout,
discoveryTimeout,
() async {
client.disconnect();
},
Expand Down
10 changes: 2 additions & 8 deletions lib/src/binding_mqtt/mqtt_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
});

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,34 @@ 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
final Uri brokerUri;

/// 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
/// deserializable using the content type provided here will be ignored.
///
/// 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
Expand Down
4 changes: 4 additions & 0 deletions lib/src/core/implementation/thing_discovery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ class ThingDiscovery extends Stream<ThingDescription>
:final brokerUri,
:final discoveryTopic,
:final expectedContentType,
:final discoveryTimeout,
):
yield* _performMqttDiscovery(
brokerUri,
discoveryTopic,
expectedContentType,
discoveryTimeout,
);
}
}
Expand Down Expand Up @@ -253,6 +255,7 @@ class ThingDiscovery extends Stream<ThingDescription>
Uri brokerUri,
String discoveryTopic,
String expectedContentType,
Duration discoveryTimeout,
) async* {
final client = _clientForUriScheme(brokerUri.scheme);

Expand All @@ -261,6 +264,7 @@ class ThingDiscovery extends Stream<ThingDescription>
brokerUri,
discoveryTopic: discoveryTopic,
expectedContentType: expectedContentType,
discoveryTimeout: discoveryTimeout,
);

await for (final content in contentStream) {
Expand Down
1 change: 1 addition & 0 deletions lib/src/core/protocol_interfaces/protocol_discoverer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ base mixin MqttDiscoverer on ProtocolClient {
Uri brokerUri, {
required String discoveryTopic,
required String expectedContentType,
required Duration discoveryTimeout,
});
}

0 comments on commit 389c63b

Please sign in to comment.