From e5c9360b6af0982ce114f26bd5763905b66abfd2 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Fri, 21 Jul 2023 14:31:39 +0200 Subject: [PATCH] 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 332a29dc..682468a1 100644 --- a/lib/src/binding_mqtt/mqtt_extensions.dart +++ b/lib/src/binding_mqtt/mqtt_extensions.dart @@ -91,15 +91,22 @@ extension MqttFormExtension on Form { /// 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].