Skip to content

Commit 60524b4

Browse files
authored
Merge pull request #89 from eclipse-thingweb/json_serializable
feat!: improve TD data model and serialization behavior
2 parents 61e4a32 + 273d57e commit 60524b4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1790
-1287
lines changed

example/coaps_readproperty.dart

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,24 @@ Future<void> main(List<String> args) async {
4444

4545
final wot = await servient.start();
4646

47-
const thingDescriptionJson = '''
48-
{
49-
"@context": "http://www.w3.org/ns/td",
47+
const thingDescriptionJson = {
48+
"@context": "https://www.w3.org/2022/wot/td/v1.1",
5049
"title": "Test Thing",
5150
"base": "coaps://californium.eclipseprojects.io",
5251
"security": ["psk_sc"],
5352
"securityDefinitions": {
54-
"psk_sc": {
55-
"scheme": "psk",
56-
"identity": "Client_identity"
57-
}
53+
"psk_sc": {"scheme": "psk", "identity": "Client_identity"},
5854
},
5955
"properties": {
6056
"status": {
6157
"forms": [
62-
{
63-
"href": "/test"
64-
}
65-
]
66-
}
67-
}
68-
}
69-
''';
58+
{"href": "/test"},
59+
],
60+
},
61+
},
62+
};
7063

71-
final thingDescription = ThingDescription(thingDescriptionJson);
64+
final thingDescription = thingDescriptionJson.toThingDescription();
7265
final consumedThing = await wot.consume(thingDescription);
7366
final status = await consumedThing.readProperty("status");
7467
final value = await status.value();

example/complex_example.dart

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,105 +8,81 @@
88

99
import "package:dart_wot/dart_wot.dart";
1010

11-
const thingDescriptionJson = '''
12-
{
11+
const thingDescriptionJson = {
1312
"@context": [
14-
"http://www.w3.org/ns/td",
15-
{
16-
"@language": "de",
17-
"coap": "http://www.example.org/coap-binding#"
18-
}
13+
"https://www.w3.org/2022/wot/td/v1.1",
14+
{"@language": "de", "coap": "http://www.example.org/coap-binding#"},
1915
],
2016
"title": "Test Thing",
2117
"id": "urn:test",
2218
"base": "coap://coap.me",
2319
"securityDefinitions": {
2420
"nosec_sc": {
2521
"scheme": "nosec",
26-
"descriptions": {
27-
"de": "Keine Sicherheit",
28-
"en": "No Security"
29-
}
22+
"descriptions": {"de": "Keine Sicherheit", "en": "No Security"},
3023
},
31-
"basic_sc": {
32-
"scheme": "basic",
33-
"description": "Test"
34-
}
24+
"basic_sc": {"scheme": "basic", "description": "Test"},
3525
},
3626
"security": "nosec_sc",
3727
"properties": {
3828
"status": {
3929
"observable": true,
4030
"forms": [
41-
{
42-
"href": "/.well-known/core"
43-
},
31+
{"href": "/.well-known/core"},
4432
{
4533
"href": "coap://californium.eclipseprojects.io/obs",
46-
"op": ["observeproperty", "unobserveproperty"]
34+
"op": ["observeproperty", "unobserveproperty"],
4735
}
48-
]
36+
],
4937
},
5038
"differentStatus": {
5139
"forms": [
52-
{
53-
"href": "coap://coap.me",
54-
"coap:method": "GET"
55-
}
56-
]
40+
{"href": "coap://coap.me", "coap:method": "GET"},
41+
],
5742
},
5843
"anotherStatus": {
59-
"uriVariables": {
60-
"test": {
61-
"type": "string"
62-
}
63-
},
44+
"uriVariables": {
45+
"test": {"type": "string"},
46+
},
6447
"forms": [
65-
{
66-
"href": "coap://coap.me/query{?test}"
67-
}
68-
]
48+
{"href": "coap://coap.me/query{?test}"},
49+
],
6950
},
7051
"test": {
7152
"forms": [
7253
{
7354
"href": "http://example.org",
74-
"security": ["basic_sc"]
55+
"security": ["basic_sc"],
7556
}
76-
]
77-
}
57+
],
58+
},
7859
},
7960
"actions": {
8061
"toggle": {
8162
"forms": [
82-
{
83-
"href": "coap://coap.me/large-create"
84-
}
85-
]
86-
}
63+
{"href": "coap://coap.me/large-create"},
64+
],
65+
},
8766
},
8867
"events": {
8968
"overheating": {
9069
"forms": [
91-
{
92-
"href": "coap://coap.me"
93-
}
94-
]
95-
}
96-
}
97-
}
98-
''';
70+
{"href": "coap://coap.me"},
71+
],
72+
},
73+
},
74+
};
9975

10076
final Map<String, BasicCredentials> basicCredentials = {
10177
"urn:test": BasicCredentials("username", "password"),
10278
};
10379

10480
Future<BasicCredentials?> basicCredentialsCallback(
10581
Uri uri,
106-
Form? form, [
82+
AugmentedForm? form, [
10783
BasicCredentials? invalidCredentials,
10884
]) async {
109-
final id = form?.thingDescription.identifier;
85+
final id = form?.tdIdentifier;
11086

11187
return basicCredentials[id];
11288
}
@@ -127,7 +103,7 @@ Future<void> main() async {
127103
);
128104
final wot = await servient.start();
129105

130-
final thingDescription = ThingDescription(thingDescriptionJson);
106+
final thingDescription = thingDescriptionJson.toThingDescription();
131107
final consumedThing = await wot.consume(thingDescription);
132108
final status = await consumedThing.readProperty("status");
133109
final value1 = await status.value();

example/core_link_format_discovery.dart

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,35 @@
88

99
import "package:dart_wot/dart_wot.dart";
1010

11-
const propertyName = "status";
12-
const actionName = "toggle";
13-
1411
Future<void> main(List<String> args) async {
1512
final servient = Servient(clientFactories: [CoapClientFactory()]);
1613

1714
final wot = await servient.start();
1815

19-
// TODO(JKRhb): Replace with an endpoint providing CoRE Format Links pointing
20-
// to TDs. At the moment, this URI is just for illustrative
21-
// purpose and will not return actual Thing Description links.
22-
final discoveryUri = Uri.parse("coap://coap.me/.well-known/core");
16+
final discoveryUri =
17+
Uri.parse("coap://plugfest.thingweb.io/.well-known/core");
2318

2419
await for (final thingDescription
2520
in wot.discover(discoveryUri, method: DiscoveryMethod.coreLinkFormat)) {
21+
print(thingDescription.title);
22+
23+
if (thingDescription.title != "Smart-Coffee-Machine") {
24+
continue;
25+
}
26+
2627
final consumedThing = await wot.consume(thingDescription);
2728

2829
try {
29-
final statusBefore = await consumedThing.readProperty(propertyName);
30+
final statusBefore =
31+
await consumedThing.readProperty("allAvailableResources");
3032
print(await statusBefore.value());
3133

32-
await consumedThing.invokeAction(actionName);
34+
final result = await consumedThing.invokeAction("makeDrink");
35+
36+
print(await result.value());
3337

34-
final statusAfter = await consumedThing.readProperty(propertyName);
38+
final statusAfter =
39+
await consumedThing.readProperty("allAvailableResources");
3540
print(await statusAfter.value());
3641
} on Exception catch (e) {
3742
print(e);

example/http_basic_authentication.dart

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,29 @@ import "package:dart_wot/dart_wot.dart";
1010

1111
const username = "username";
1212
const password = "password";
13-
const thingDescriptionJson = '''
14-
{
15-
"@context": ["http://www.w3.org/ns/td"],
16-
"title": "Test Thing",
17-
"id": "urn:test",
18-
"base": "https://httpbin.org",
19-
"securityDefinitions": {
20-
"auto_sc": {
21-
"scheme": "auto"
22-
},
23-
"basic_sc": {
24-
"scheme": "basic"
25-
}
26-
},
27-
"security": "auto_sc",
28-
"properties": {
29-
"status": {
30-
"forms": [
31-
{
32-
"href": "/basic-auth/$username/$password"
33-
}
34-
]
35-
},
36-
"status2": {
37-
"forms": [
38-
{
39-
"href": "/basic-auth/$username/$password",
40-
"security": "basic_sc"
41-
}
42-
]
43-
}
44-
}
45-
}
46-
''';
13+
const thingDescriptionJson = {
14+
"@context": "https://www.w3.org/2022/wot/td/v1.1",
15+
"title": "Test Thing",
16+
"id": "urn:test",
17+
"base": "https://httpbin.org",
18+
"securityDefinitions": {
19+
"auto_sc": {"scheme": "auto"},
20+
"basic_sc": {"scheme": "basic"},
21+
},
22+
"security": "auto_sc",
23+
"properties": {
24+
"status": {
25+
"forms": [
26+
{"href": "/basic-auth/$username/$password"},
27+
],
28+
},
29+
"status2": {
30+
"forms": [
31+
{"href": "/basic-auth/$username/$password", "security": "basic_sc"},
32+
],
33+
},
34+
},
35+
};
4736

4837
final basicCredentials = BasicCredentials("username", "password");
4938

@@ -53,14 +42,14 @@ final Map<String, BasicCredentials> basicCredentialsMap = {
5342

5443
Future<BasicCredentials?> basicCredentialsCallback(
5544
Uri uri,
56-
Form? form,
45+
AugmentedForm? form,
5746
BasicCredentials? invalidCredentials,
5847
) async {
5948
if (form == null) {
6049
return basicCredentials;
6150
}
6251

63-
final id = form.thingDescription.identifier;
52+
final id = form.tdIdentifier;
6453

6554
return basicCredentialsMap[id];
6655
}
@@ -78,7 +67,7 @@ Future<void> main(List<String> args) async {
7867
);
7968
final wot = await servient.start();
8069

81-
final thingDescription = ThingDescription(thingDescriptionJson);
70+
final thingDescription = thingDescriptionJson.toThingDescription();
8271
final consumedThing = await wot.consume(thingDescription);
8372
final status = await consumedThing.readProperty("status");
8473

0 commit comments

Comments
 (0)