Skip to content

Commit

Permalink
Implement ExposedThing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jul 17, 2024
1 parent 9535a91 commit 0a59efe
Show file tree
Hide file tree
Showing 16 changed files with 1,010 additions and 585 deletions.
111 changes: 111 additions & 0 deletions example/exposed_thing/http_server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 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 property = "hi :)";

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(property);
})
..setPropertyWriteHandler("status", (
interactionOutput, {
data,
formIndex,
uriVariables,
}) async {
final value = await interactionOutput.value();

if (value is String) {
property = 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();
}
}
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

0 comments on commit 0a59efe

Please sign in to comment.