Skip to content

Commit 0a59efe

Browse files
committed
Implement ExposedThing functionality
1 parent 9535a91 commit 0a59efe

16 files changed

+1010
-585
lines changed
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2024 Contributors to the Eclipse Foundation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
// ignore_for_file: avoid_print
8+
9+
import "package:dart_wot/binding_http.dart";
10+
import "package:dart_wot/core.dart";
11+
12+
String property = "hi :)";
13+
14+
void main() async {
15+
final servient = Servient.create(
16+
clientFactories: [HttpClientFactory()],
17+
servers: [HttpServer(HttpConfig(port: 3000))],
18+
);
19+
20+
final wot = await servient.start();
21+
22+
final exposedThing = await wot.produce({
23+
"@context": "https://www.w3.org/2022/wot/td/v1.1",
24+
"title": "My Lamp Thing",
25+
"id": "test",
26+
"properties": {
27+
"status": {
28+
"type": "string",
29+
"forms": [
30+
{
31+
"href": "/status",
32+
}
33+
],
34+
},
35+
},
36+
"actions": {
37+
"toggle": {
38+
"input": {
39+
"type": "boolean",
40+
},
41+
"output": {
42+
"type": "null",
43+
},
44+
"forms": [
45+
{
46+
"href": "/toggle",
47+
}
48+
],
49+
},
50+
},
51+
});
52+
53+
exposedThing
54+
..setPropertyReadHandler("status", ({
55+
data,
56+
formIndex,
57+
uriVariables,
58+
}) async {
59+
return InteractionInput.fromString(property);
60+
})
61+
..setPropertyWriteHandler("status", (
62+
interactionOutput, {
63+
data,
64+
formIndex,
65+
uriVariables,
66+
}) async {
67+
final value = await interactionOutput.value();
68+
69+
if (value is String) {
70+
property = value;
71+
return;
72+
}
73+
74+
throw const FormatException();
75+
})
76+
..setActionHandler("toggle", (
77+
actionInput, {
78+
data,
79+
formIndex,
80+
uriVariables,
81+
}) async {
82+
print(await actionInput.value());
83+
84+
return InteractionInput.fromNull();
85+
});
86+
87+
final thingDescription = await wot
88+
.requestThingDescription(Uri.parse("http://localhost:3000/test"));
89+
print(thingDescription.toJson());
90+
final consumedThing = await wot.consume(thingDescription);
91+
92+
var value = await (await consumedThing.readProperty("status")).value();
93+
print(value);
94+
95+
await consumedThing.writeProperty(
96+
"status",
97+
DataSchemaValueInput(DataSchemaValue.fromString("bye")),
98+
);
99+
100+
value = await (await consumedThing.readProperty("status")).value();
101+
print(value);
102+
103+
final actionOutput = await consumedThing.invokeAction(
104+
"toggle",
105+
input: InteractionInput.fromBoolean(true),
106+
);
107+
108+
print(await actionOutput.value());
109+
110+
await servient.shutdown();
111+
}

lib/src/binding_coap/coap_server.dart

+8-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ final class CoapServer implements ProtocolServer {
2626
final int? preferredBlockSize;
2727

2828
@override
29-
Future<void> expose(ExposedThing thing) {
29+
Future<void> expose(ExposableThing thing) {
3030
// TODO(JKRhb): implement expose
3131
throw UnimplementedError();
3232
}
3333

3434
@override
35-
Future<void> start([ServerSecurityCallback? serverSecurityCallback]) {
35+
Future<void> start(Servient servient) {
3636
// TODO(JKRhb): implement start
3737
throw UnimplementedError();
3838
}
@@ -42,4 +42,10 @@ final class CoapServer implements ProtocolServer {
4242
// TODO(JKRhb): implement stop
4343
throw UnimplementedError();
4444
}
45+
46+
@override
47+
Future<void> destroyThing(ExposableThing thing) {
48+
// TODO: implement destroyThing
49+
throw UnimplementedError();
50+
}
4551
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2024 Contributors to the Eclipse Foundation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
import "../../core.dart";
8+
9+
/// Extension for determining the HTTP method that corresponds with an
10+
/// [OperationType].
11+
extension HttpMethodExtension on OperationType {
12+
/// Returns the default HTTP method as defined in the [HTTP binding template]
13+
/// specification.
14+
///
15+
/// If the [OperationType] value has no default method defined, an
16+
/// [ArgumentError] will be thrown.
17+
///
18+
/// [HTTP binding template]: https://w3c.github.io/wot-binding-templates/bindings/protocols/http/#http-default-vocabulary-terms
19+
String get defaultHttpMethod {
20+
switch (this) {
21+
case OperationType.readproperty:
22+
return "GET";
23+
case OperationType.writeproperty:
24+
return "PUT";
25+
case OperationType.invokeaction:
26+
return "POST";
27+
case OperationType.readallproperties:
28+
return "GET";
29+
case OperationType.writeallproperties:
30+
return "PUT";
31+
case OperationType.readmultipleproperties:
32+
return "GET";
33+
case OperationType.writemultipleproperties:
34+
return "PUT";
35+
default:
36+
throw ArgumentError(
37+
"OperationType $this has no default HTTP method defined.",
38+
);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)