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

Implement ExposedThing functionality #182

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ You can consume Thing Descriptions and interact with a Thing based on its
exposed Properties, Actions, and Events.
Discovery support is currently limited to the "direct" method (i.e. fetching a
TD using a single URL).
Exposing Things is not yet supported but will be added in future versions.
Exposing Things is currently only (partially) supported for the HTTP binding.

Using the Protocol Interfaces in the `core` package, you can add support for
additional protocols in your own application or library. The main requirement
Expand Down
117 changes: 117 additions & 0 deletions example/exposed_thing/http_server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// 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";

String testPayload = "Hello World";

void main() async {
final servient = Servient.create(
clientFactories: [HttpClientFactory()],
servers: [
HttpServer(
HttpConfig(
port: 3000,
),
),
],
);

final wot = await servient.start();

final exposedThing = await wot.produce({
"@context": "https://www.w3.org/2022/wot/td/v1.1",
"title": "My Lamp Thing",
"id": "test",
"properties": {
"status": {
"type": "string",
"forms": [
{
"href": "/status",
}
],
},
},
"actions": {
"toggle": {
"input": {
"type": "boolean",
},
"output": {
"type": "null",
},
"forms": [
{
"href": "/toggle",
}
],
},
},
});

exposedThing
..setPropertyReadHandler("status", ({
data,
formIndex,
uriVariables,
}) async {
return InteractionInput.fromString(testPayload);
})
..setPropertyWriteHandler("status", (
interactionOutput, {
data,
formIndex,
uriVariables,
}) async {
final value = await interactionOutput.value();

if (value is String) {
testPayload = value;
return;
}

throw const FormatException();
})
..setActionHandler("toggle", (
actionInput, {
data,
formIndex,
uriVariables,
}) async {
print(await actionInput.value());

return InteractionInput.fromNull();
});

final thingDescription = await wot
.requestThingDescription(Uri.parse("http://localhost:3000/test"));
print(thingDescription.toJson());
final consumedThing = await wot.consume(thingDescription);

var value = await (await consumedThing.readProperty("status")).value();
print(value);

await consumedThing.writeProperty(
"status",
DataSchemaValueInput(DataSchemaValue.fromString("bye")),
);

value = await (await consumedThing.readProperty("status")).value();
print(value);

final actionOutput = await consumedThing.invokeAction(
"toggle",
input: InteractionInput.fromBoolean(true),
);

print(await actionOutput.value());

await servient.shutdown();
}
10 changes: 8 additions & 2 deletions lib/src/binding_coap/coap_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ final class CoapServer implements ProtocolServer {
final int? preferredBlockSize;

@override
Future<void> expose(ExposedThing thing) {
Future<void> expose(ExposableThing thing) {
// TODO(JKRhb): implement expose
throw UnimplementedError();
}

@override
Future<void> start([ServerSecurityCallback? serverSecurityCallback]) {
Future<void> start(Servient servient) {
// TODO(JKRhb): implement start
throw UnimplementedError();
}
Expand All @@ -42,4 +42,10 @@ final class CoapServer implements ProtocolServer {
// TODO(JKRhb): implement stop
throw UnimplementedError();
}

@override
Future<void> destroyThing(ExposableThing thing) {
// TODO: implement destroyThing
throw UnimplementedError();
}
}
18 changes: 15 additions & 3 deletions lib/src/binding_http/http_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@
//
// SPDX-License-Identifier: BSD-3-Clause

import "dart:io";

/// Allows for configuring the behavior of HTTP clients and servers.
class HttpConfig {
/// Creates a new [HttpConfig] object.
HttpConfig({this.port, this.secure});
HttpConfig({
int? port,
this.secure = false,
InternetAddress? bindAddress,
}) : port = port ?? (secure ? 443 : 80),
bindAddress = bindAddress ?? InternetAddress.anyIPv4;

/// Custom port number that should be used by a server.
///
/// Defaults to 80 for HTTP and 443 for HTTPS.
int? port;
final int port;

/// Indicates if the client or server should use HTTPS.
bool? secure;
bool secure;

/// The IP address the HTTP server should bind to.
///
/// Defaults to [InternetAddress.anyIPv4].
final InternetAddress bindAddress;
}
41 changes: 41 additions & 0 deletions lib/src/binding_http/http_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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

import "../../core.dart";

/// Extension for determining the HTTP method that corresponds with an
/// [OperationType].
extension HttpMethodExtension on OperationType {
/// Returns the default HTTP method as defined in the [HTTP binding template]
/// specification.
///
/// If the [OperationType] value has no default method defined, an
/// [ArgumentError] will be thrown.
///
/// [HTTP binding template]: https://w3c.github.io/wot-binding-templates/bindings/protocols/http/#http-default-vocabulary-terms
String get defaultHttpMethod {
switch (this) {
case OperationType.readproperty:
return "GET";
case OperationType.writeproperty:
return "PUT";
case OperationType.invokeaction:
return "POST";
case OperationType.readallproperties:
return "GET";
case OperationType.writeallproperties:
return "PUT";
case OperationType.readmultipleproperties:
return "GET";
case OperationType.writemultipleproperties:
return "PUT";
default:
throw ArgumentError(
"OperationType $this has no default HTTP method defined.",
);
}
}
}
Loading
Loading