diff --git a/example/coap_discovery.dart b/example/coap_discovery.dart index 10e63882..b9b06023 100644 --- a/example/coap_discovery.dart +++ b/example/coap_discovery.dart @@ -35,6 +35,10 @@ Future main(List 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)) { diff --git a/lib/src/binding_coap/coap_client.dart b/lib/src/binding_coap/coap_client.dart index a8b38630..ed57912c 100644 --- a/lib/src/binding_coap/coap_client.dart +++ b/lib/src/binding_coap/coap_client.dart @@ -538,4 +538,14 @@ final class CoapClient implements ProtocolClient { yield content; } } + + @override + Future requestThingDescription(Uri url) async => + // TODO(JKRhb): Implement a different method here. + _sendDiscoveryRequest( + url, + coap.RequestMethod.get, + form: null, + format: coap.CoapMediaType.applicationTdJson, + ); } diff --git a/lib/src/binding_http/http_client.dart b/lib/src/binding_http/http_client.dart index 67cea18b..5d83289a 100644 --- a/lib/src/binding_http/http_client.dart +++ b/lib/src/binding_http/http_client.dart @@ -332,4 +332,10 @@ final class HttpClient implements ProtocolClient { yield encodedLinks; } + + @override + Future requestThingDescription(Uri url) { + // TODO: implement requestThingDescription + throw UnimplementedError(); + } } diff --git a/lib/src/binding_mqtt/mqtt_client.dart b/lib/src/binding_mqtt/mqtt_client.dart index 756634e5..e0ce0f26 100644 --- a/lib/src/binding_mqtt/mqtt_client.dart +++ b/lib/src/binding_mqtt/mqtt_client.dart @@ -251,4 +251,10 @@ final class MqttClient implements ProtocolClient { // TODO: implement discoverWithCoreLinkFormat throw UnimplementedError(); } + + @override + Future requestThingDescription(Uri url) { + // TODO: implement requestThingDescription + throw UnimplementedError(); + } } diff --git a/lib/src/core/protocol_interfaces/protocol_client.dart b/lib/src/core/protocol_interfaces/protocol_client.dart index 06298c26..38d91b5f 100644 --- a/lib/src/core/protocol_interfaces/protocol_client.dart +++ b/lib/src/core/protocol_interfaces/protocol_client.dart @@ -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 requestThingDescription(Uri url); } diff --git a/lib/src/core/servient.dart b/lib/src/core/servient.dart index 40544724..fc9580bc 100644 --- a/lib/src/core/servient.dart +++ b/lib/src/core/servient.dart @@ -15,6 +15,7 @@ import 'exposed_thing.dart'; import 'protocol_interfaces/protocol_client.dart'; import 'protocol_interfaces/protocol_client_factory.dart'; import 'protocol_interfaces/protocol_server.dart'; +import 'thing_discovery.dart'; import 'wot.dart'; /// Exception that is thrown by a [Servient]. @@ -225,4 +226,20 @@ class Servient { return clientFactory.createClient(); } + + /// Requests a [ThingDescription] from a [url]. + Future 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) { + throw DiscoveryException( + 'Could not parse Thing Description obtained from $url', + ); + } + + return ThingDescription.fromJson(value); + } } diff --git a/lib/src/core/wot.dart b/lib/src/core/wot.dart index 7408e17e..1bdf8fc0 100644 --- a/lib/src/core/wot.dart +++ b/lib/src/core/wot.dart @@ -90,4 +90,9 @@ class WoT implements scripting_api.WoT { }) { return ThingDiscovery(url, thingFilter, _servient, method: method); } + + @override + Future requestThingDescription(Uri url) { + return _servient.requestThingDescription(url); + } } diff --git a/lib/src/scripting_api/wot.dart b/lib/src/scripting_api/wot.dart index 5efc82eb..207e0723 100644 --- a/lib/src/scripting_api/wot.dart +++ b/lib/src/scripting_api/wot.dart @@ -31,6 +31,9 @@ abstract interface class WoT { /// based on the underlying impementation. Future produce(ExposedThingInit exposedThingInit); + /// Requests a [ThingDescription] from the given [url]. + Future requestThingDescription(Uri url); + /// Discovers [ThingDescription]s from a given [url] using the specified /// [method]. ///