Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(binding_mqtt): support MQTT URI scheme #60

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions lib/src/binding_mqtt/mqtt_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import "package:uuid/uuid.dart";

import "../../core.dart";
import "constants.dart";
import "mqtt_binding_exception.dart";

/// [PrefixMapping] for expanding MQTT Vocabulary terms from compact IRIs.
final mqttPrefixMapping = PrefixMapping(defaultPrefixValue: mqttContextUri);
Expand Down Expand Up @@ -59,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.
Expand Down Expand Up @@ -87,28 +96,30 @@ 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<String>("topic");

if (topic == null) {
throw MqttBindingException("MQTT topic was not defined on form.");
if (topic != null) {
return topic;
}

return topic;
return href._mqttTopic;
}

/// 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 path from the `href` field is being used as a fallback.
String get topicFilter {
final topic = _obtainVocabularyTerm<String>("filter");

if (topic == null) {
throw MqttBindingException("MQTT topic was not defined on form.");
if (topic != null) {
return topic;
}

return topic;
return href._mqttTopic;
}

/// Gets the MQTT `retain` value from this [Form] if present.
Expand Down
Loading