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

Make directory discovery a bit more robust #204

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions example/tdd_discovery.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2024 Contributors to the Eclipse Foundation. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// SPDX-License-Identifier: BSD-3-Clause

// ignore_for_file: avoid_print

import "package:dart_wot/binding_http.dart";
import "package:dart_wot/core.dart";

Future<void> main() async {
final servient = Servient.create(
clientFactories: [
HttpClientFactory(),
],
);
final wot = await servient.start();

final url = Uri.parse("http://plugfest.thingweb.io:8081");
print("Requesting TD from $url ...");
final thingDiscoveryProcess = await wot.exploreDirectory(url);

thingDiscoveryProcess.listen(
(thingDescription) => print(thingDescription.title),
onError: print,
);

await servient.shutdown();
}
4 changes: 3 additions & 1 deletion lib/src/core/definitions/extensions/json_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ extension ParseField on Map<String, dynamic> {
return <String, dynamic>{} as T;
}

throw FormatException("Expected $T, got ${fieldValue.runtimeType}");
throw FormatException(
"Expected $T, got ${fieldValue.runtimeType} for field $name",
);
}

/// Parses a single field with a given [name] as a [Uri].
Expand Down
22 changes: 19 additions & 3 deletions lib/src/core/implementation/servient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// SPDX-License-Identifier: BSD-3-Clause

import "dart:developer";

import "package:meta/meta.dart";
import "package:uuid/uuid.dart";

Expand Down Expand Up @@ -379,6 +381,21 @@ class InternalServient implements Servient {
return ThingDescription.fromJson(dataSchemaValue.value);
}

Stream<ThingDescription> _processThingDescriptions(
List<Object?> rawThingDescriptions,
) async* {
for (final rawThingDescription in rawThingDescriptions) {
if (rawThingDescription is Map<String, Object?>) {
try {
yield rawThingDescription.toThingDescription();
} on Exception catch (e) {
log(e.toString());
yield* Stream.error(e);
}
}
}
}

/// Retrieves [ThingDescription] from a Thing Description Directory (TDD).
///
/// This method expects the TDD's Thing Description to be located under the
Expand Down Expand Up @@ -435,9 +452,8 @@ class InternalServient implements Servient {
);
}

final thingDescriptionStream = Stream.fromIterable(
rawThingDescriptions.whereType<Map<String, Object?>>(),
).map((rawThingDescription) => rawThingDescription.toThingDescription());
final thingDescriptionStream =
_processThingDescriptions(rawThingDescriptions);

return ThingDiscoveryProcess(thingDescriptionStream, thingFilter);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/core/implementation/thing_discovery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -485,11 +485,11 @@ class ThingDiscoveryProcess extends Stream<ThingDescription>
}) {
final streamSubscription = _thingDescriptionStream.listen(
onData,
onError: (error, stackTrace) {
onError: (error) {
if (error is Exception) {
_error = error;
// ignore: avoid_dynamic_calls
onError?.call(error, stackTrace);
onError?.call(error);
}
},
onDone: () {
Expand Down
2 changes: 1 addition & 1 deletion test/core/discovery_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ void main() {
(event) {
counter++;
},
onError: (error, stackTrace) async {},
onError: (error) async {},
onDone: () {
expect(counter, 0);
expect(thingDiscoveryProcess.done, true);
Expand Down
Loading