Skip to content

Commit

Permalink
fixup! feat(binding_mqtt): support MQTT URI scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 27, 2024
1 parent 9158b91 commit 40eaea2
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions lib/src/binding_mqtt/mqtt_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -99,28 +105,21 @@ 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<String>("filter");

if (topic != null) {
return topic;
}

return Uri.decodeComponent(href.query.replaceAll("&", "/"));
return href._mqttTopic;
}

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

0 comments on commit 40eaea2

Please sign in to comment.