From 1cf40bd7cc2184fbf4036d05ac72571ae3ae66ad Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Fri, 21 Jul 2023 14:31:39 +0200 Subject: [PATCH 1/4] feat(binding_mqtt): support MQTT URI scheme --- lib/src/binding_mqtt/mqtt_extensions.dart | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/src/binding_mqtt/mqtt_extensions.dart b/lib/src/binding_mqtt/mqtt_extensions.dart index 6607cc0f..50cf0b4e 100644 --- a/lib/src/binding_mqtt/mqtt_extensions.dart +++ b/lib/src/binding_mqtt/mqtt_extensions.dart @@ -87,15 +87,22 @@ extension MqttFormExtension on AugmentedForm { /// Gets the MQTT topic for publishing from this [Form]. /// - /// Throws an [Exception] if no topic could be retrieved. + /// If present, this getter uses the dedicated vocabulary term `topic`. + /// Otherwise, the URI path from the `href` field is being used as a fallback. String get topicName { final topic = _obtainVocabularyTerm("topic"); - if (topic == null) { - throw MqttBindingException("MQTT topic was not defined on form."); + if (topic != null) { + return topic; } - return topic; + final path = Uri.decodeComponent(href.path); + + if (path.isEmpty) { + return path; + } + + return path.substring(1); } /// Gets the MQTT topic for subscribing from this [Form]. From 22422d71909486ca17a6a2f4fe87cb9287bc315e Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Mon, 24 Jul 2023 08:50:28 +0200 Subject: [PATCH 2/4] fixup! feat(binding_mqtt): support MQTT URI scheme --- lib/src/binding_mqtt/mqtt_extensions.dart | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/src/binding_mqtt/mqtt_extensions.dart b/lib/src/binding_mqtt/mqtt_extensions.dart index 50cf0b4e..deb99d0b 100644 --- a/lib/src/binding_mqtt/mqtt_extensions.dart +++ b/lib/src/binding_mqtt/mqtt_extensions.dart @@ -107,15 +107,17 @@ extension MqttFormExtension on AugmentedForm { /// Gets the MQTT topic for subscribing from this [Form]. /// - /// Throws an [Exception] if no topic could be retrieved. + /// If present, this getter uses the dedicated vocabulary term `filter`. + /// Otherwise, the URI query from the `href` field is being used as a + /// fallback. String get topicFilter { final topic = _obtainVocabularyTerm("filter"); - if (topic == null) { - throw MqttBindingException("MQTT topic was not defined on form."); + if (topic != null) { + return topic; } - return topic; + return Uri.decodeComponent(href.query.replaceAll("&", "/")); } /// Gets the MQTT `retain` value from this [Form] if present. From 0207f98108838506851d785e1e0a84f978255e95 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Mon, 24 Jul 2023 08:51:48 +0200 Subject: [PATCH 3/4] fixup! feat(binding_mqtt): support MQTT URI scheme --- lib/src/binding_mqtt/mqtt_extensions.dart | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/src/binding_mqtt/mqtt_extensions.dart b/lib/src/binding_mqtt/mqtt_extensions.dart index deb99d0b..2bd77db5 100644 --- a/lib/src/binding_mqtt/mqtt_extensions.dart +++ b/lib/src/binding_mqtt/mqtt_extensions.dart @@ -9,9 +9,12 @@ import "package:mqtt_client/mqtt_client.dart"; import "package:mqtt_client/mqtt_server_client.dart"; import "package:uuid/uuid.dart"; -import "../../core.dart"; -import "constants.dart"; -import "mqtt_binding_exception.dart"; +import '../../core.dart'; +import '../definitions/form.dart'; +import '../definitions/security/auto_security_scheme.dart'; +import '../definitions/security/basic_security_scheme.dart'; +import '../definitions/validation/validation_exception.dart'; +import 'constants.dart'; /// [PrefixMapping] for expanding MQTT Vocabulary terms from compact IRIs. final mqttPrefixMapping = PrefixMapping(defaultPrefixValue: mqttContextUri); From 47a23d3e5642aa8543da0cd02a8b0d7c999e6db1 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Wed, 26 Jul 2023 08:18:31 +0200 Subject: [PATCH 4/4] fixup! feat(binding_mqtt): support MQTT URI scheme --- lib/src/binding_mqtt/mqtt_extensions.dart | 31 +++++++++++------------ 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/src/binding_mqtt/mqtt_extensions.dart b/lib/src/binding_mqtt/mqtt_extensions.dart index 2bd77db5..1104289b 100644 --- a/lib/src/binding_mqtt/mqtt_extensions.dart +++ b/lib/src/binding_mqtt/mqtt_extensions.dart @@ -9,12 +9,8 @@ import "package:mqtt_client/mqtt_client.dart"; import "package:mqtt_client/mqtt_server_client.dart"; import "package:uuid/uuid.dart"; -import '../../core.dart'; -import '../definitions/form.dart'; -import '../definitions/security/auto_security_scheme.dart'; -import '../definitions/security/basic_security_scheme.dart'; -import '../definitions/validation/validation_exception.dart'; -import 'constants.dart'; +import "../../core.dart"; +import "constants.dart"; /// [PrefixMapping] for expanding MQTT Vocabulary terms from compact IRIs. final mqttPrefixMapping = PrefixMapping(defaultPrefixValue: mqttContextUri); @@ -62,6 +58,16 @@ extension MqttUriExtension on Uri { throw StateError("MQTT URI scheme $scheme is not supported."); } + + String get _mqttTopic { + final path = Uri.decodeComponent(this.path); + + if (path.isEmpty) { + return path; + } + + return path.substring(1); + } } /// Additional methods for making MQTT [Form]s easier to work with. @@ -99,20 +105,13 @@ extension MqttFormExtension on AugmentedForm { return topic; } - final path = Uri.decodeComponent(href.path); - - if (path.isEmpty) { - return path; - } - - return path.substring(1); + return href._mqttTopic; } /// Gets the MQTT topic for subscribing from this [Form]. /// /// If present, this getter uses the dedicated vocabulary term `filter`. - /// Otherwise, the URI query from the `href` field is being used as a - /// fallback. + /// Otherwise, the URI path from the `href` field is being used as a fallback. String get topicFilter { final topic = _obtainVocabularyTerm("filter"); @@ -120,7 +119,7 @@ extension MqttFormExtension on AugmentedForm { return topic; } - return Uri.decodeComponent(href.query.replaceAll("&", "/")); + return href._mqttTopic; } /// Gets the MQTT `retain` value from this [Form] if present.