Skip to content

Commit

Permalink
feat: implement requestThingDescription method
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Nov 20, 2023
1 parent b686b57 commit bc54366
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 0 deletions.
4 changes: 4 additions & 0 deletions example/coap_discovery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Future<void> main(List<String> args) async {
final wot = await servient.start();
final uri = Uri.parse('coap://plugfest.thingweb.io:5683/testthing');

final thingDescription = await wot.requestThingDescription(uri);

await handleThingDescription(wot, thingDescription);

// Example using for-await-loop
try {
await for (final thingDescription in wot.discover(uri)) {
Expand Down
10 changes: 10 additions & 0 deletions lib/src/binding_coap/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,14 @@ final class CoapClient implements ProtocolClient {
yield content;
}
}

@override
Future<Content> requestThingDescription(Uri url) async =>
// TODO(JKRhb): Implement a different method here.
_sendDiscoveryRequest(
url,
coap.RequestMethod.get,
form: null,
format: coap.CoapMediaType.applicationTdJson,
);
}
6 changes: 6 additions & 0 deletions lib/src/binding_http/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,10 @@ final class HttpClient implements ProtocolClient {

yield encodedLinks;
}

@override
Future<Content> requestThingDescription(Uri url) {
// TODO: implement requestThingDescription
throw UnimplementedError();
}
}
6 changes: 6 additions & 0 deletions lib/src/binding_mqtt/mqtt_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,10 @@ final class MqttClient implements ProtocolClient {
// TODO: implement discoverWithCoreLinkFormat
throw UnimplementedError();
}

@override
Future<Content> requestThingDescription(Uri url) {
// TODO: implement requestThingDescription
throw UnimplementedError();
}
}
3 changes: 3 additions & 0 deletions lib/src/core/protocol_interfaces/protocol_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ abstract interface class ProtocolClient {
void Function(Exception error)? error,
required void Function() complete,
});

/// Requests a Thing Description as [Content] from a [url].
Future<Content> requestThingDescription(Uri url);
}
17 changes: 17 additions & 0 deletions lib/src/core/servient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'protocol_interfaces/protocol_client.dart';
import 'protocol_interfaces/protocol_client_factory.dart';
import 'protocol_interfaces/protocol_server.dart';
import 'security_provider.dart';
import 'thing_discovery.dart';
import 'wot.dart';

/// Exception that is thrown by a [Servient].
Expand Down Expand Up @@ -208,4 +209,20 @@ class Servient {
);
}
}

/// Requests a [ThingDescription] from a [url].
Future<ThingDescription> requestThingDescription(Uri url) async {
final client = clientFor(url.scheme);
final content = await client.requestThingDescription(url);

final value = await contentSerdes.contentToValue(content, null);

if (value is! Map<String, dynamic>) {
throw DiscoveryException(
'Could not parse Thing Description obtained from $url',
);
}

return ThingDescription.fromJson(value);
}
}
5 changes: 5 additions & 0 deletions lib/src/core/wot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,9 @@ class WoT implements scripting_api.WoT {
}) {
return ThingDiscovery(url, thingFilter, _servient, method: method);
}

@override
Future<ThingDescription> requestThingDescription(Uri url) {
return _servient.requestThingDescription(url);
}
}
3 changes: 3 additions & 0 deletions lib/src/scripting_api/wot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ abstract interface class WoT {
/// based on the underlying impementation.
Future<ExposedThing> produce(ExposedThingInit exposedThingInit);

/// Requests a [ThingDescription] from the given [url].
Future<ThingDescription> requestThingDescription(Uri url);

/// Discovers [ThingDescription]s from a given [url] using the specified
/// [method].
///
Expand Down

0 comments on commit bc54366

Please sign in to comment.