Skip to content

Commit

Permalink
Merge pull request #89 from eclipse-thingweb/json_serializable
Browse files Browse the repository at this point in the history
feat!: improve TD data model and serialization behavior
  • Loading branch information
JKRhb authored Jan 4, 2024
2 parents 61e4a32 + 273d57e commit 60524b4
Show file tree
Hide file tree
Showing 54 changed files with 1,790 additions and 1,287 deletions.
25 changes: 9 additions & 16 deletions example/coaps_readproperty.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,24 @@ Future<void> main(List<String> args) async {

final wot = await servient.start();

const thingDescriptionJson = '''
{
"@context": "http://www.w3.org/ns/td",
const thingDescriptionJson = {
"@context": "https://www.w3.org/2022/wot/td/v1.1",
"title": "Test Thing",
"base": "coaps://californium.eclipseprojects.io",
"security": ["psk_sc"],
"securityDefinitions": {
"psk_sc": {
"scheme": "psk",
"identity": "Client_identity"
}
"psk_sc": {"scheme": "psk", "identity": "Client_identity"},
},
"properties": {
"status": {
"forms": [
{
"href": "/test"
}
]
}
}
}
''';
{"href": "/test"},
],
},
},
};

final thingDescription = ThingDescription(thingDescriptionJson);
final thingDescription = thingDescriptionJson.toThingDescription();
final consumedThing = await wot.consume(thingDescription);
final status = await consumedThing.readProperty("status");
final value = await status.value();
Expand Down
82 changes: 29 additions & 53 deletions example/complex_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,105 +8,81 @@

import "package:dart_wot/dart_wot.dart";

const thingDescriptionJson = '''
{
const thingDescriptionJson = {
"@context": [
"http://www.w3.org/ns/td",
{
"@language": "de",
"coap": "http://www.example.org/coap-binding#"
}
"https://www.w3.org/2022/wot/td/v1.1",
{"@language": "de", "coap": "http://www.example.org/coap-binding#"},
],
"title": "Test Thing",
"id": "urn:test",
"base": "coap://coap.me",
"securityDefinitions": {
"nosec_sc": {
"scheme": "nosec",
"descriptions": {
"de": "Keine Sicherheit",
"en": "No Security"
}
"descriptions": {"de": "Keine Sicherheit", "en": "No Security"},
},
"basic_sc": {
"scheme": "basic",
"description": "Test"
}
"basic_sc": {"scheme": "basic", "description": "Test"},
},
"security": "nosec_sc",
"properties": {
"status": {
"observable": true,
"forms": [
{
"href": "/.well-known/core"
},
{"href": "/.well-known/core"},
{
"href": "coap://californium.eclipseprojects.io/obs",
"op": ["observeproperty", "unobserveproperty"]
"op": ["observeproperty", "unobserveproperty"],
}
]
],
},
"differentStatus": {
"forms": [
{
"href": "coap://coap.me",
"coap:method": "GET"
}
]
{"href": "coap://coap.me", "coap:method": "GET"},
],
},
"anotherStatus": {
"uriVariables": {
"test": {
"type": "string"
}
},
"uriVariables": {
"test": {"type": "string"},
},
"forms": [
{
"href": "coap://coap.me/query{?test}"
}
]
{"href": "coap://coap.me/query{?test}"},
],
},
"test": {
"forms": [
{
"href": "http://example.org",
"security": ["basic_sc"]
"security": ["basic_sc"],
}
]
}
],
},
},
"actions": {
"toggle": {
"forms": [
{
"href": "coap://coap.me/large-create"
}
]
}
{"href": "coap://coap.me/large-create"},
],
},
},
"events": {
"overheating": {
"forms": [
{
"href": "coap://coap.me"
}
]
}
}
}
''';
{"href": "coap://coap.me"},
],
},
},
};

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

Future<BasicCredentials?> basicCredentialsCallback(
Uri uri,
Form? form, [
AugmentedForm? form, [
BasicCredentials? invalidCredentials,
]) async {
final id = form?.thingDescription.identifier;
final id = form?.tdIdentifier;

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

final thingDescription = ThingDescription(thingDescriptionJson);
final thingDescription = thingDescriptionJson.toThingDescription();
final consumedThing = await wot.consume(thingDescription);
final status = await consumedThing.readProperty("status");
final value1 = await status.value();
Expand Down
25 changes: 15 additions & 10 deletions example/core_link_format_discovery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,35 @@

import "package:dart_wot/dart_wot.dart";

const propertyName = "status";
const actionName = "toggle";

Future<void> main(List<String> args) async {
final servient = Servient(clientFactories: [CoapClientFactory()]);

final wot = await servient.start();

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

await for (final thingDescription
in wot.discover(discoveryUri, method: DiscoveryMethod.coreLinkFormat)) {
print(thingDescription.title);

if (thingDescription.title != "Smart-Coffee-Machine") {
continue;
}

final consumedThing = await wot.consume(thingDescription);

try {
final statusBefore = await consumedThing.readProperty(propertyName);
final statusBefore =
await consumedThing.readProperty("allAvailableResources");
print(await statusBefore.value());

await consumedThing.invokeAction(actionName);
final result = await consumedThing.invokeAction("makeDrink");

print(await result.value());

final statusAfter = await consumedThing.readProperty(propertyName);
final statusAfter =
await consumedThing.readProperty("allAvailableResources");
print(await statusAfter.value());
} on Exception catch (e) {
print(e);
Expand Down
63 changes: 26 additions & 37 deletions example/http_basic_authentication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,29 @@ import "package:dart_wot/dart_wot.dart";

const username = "username";
const password = "password";
const thingDescriptionJson = '''
{
"@context": ["http://www.w3.org/ns/td"],
"title": "Test Thing",
"id": "urn:test",
"base": "https://httpbin.org",
"securityDefinitions": {
"auto_sc": {
"scheme": "auto"
},
"basic_sc": {
"scheme": "basic"
}
},
"security": "auto_sc",
"properties": {
"status": {
"forms": [
{
"href": "/basic-auth/$username/$password"
}
]
},
"status2": {
"forms": [
{
"href": "/basic-auth/$username/$password",
"security": "basic_sc"
}
]
}
}
}
''';
const thingDescriptionJson = {
"@context": "https://www.w3.org/2022/wot/td/v1.1",
"title": "Test Thing",
"id": "urn:test",
"base": "https://httpbin.org",
"securityDefinitions": {
"auto_sc": {"scheme": "auto"},
"basic_sc": {"scheme": "basic"},
},
"security": "auto_sc",
"properties": {
"status": {
"forms": [
{"href": "/basic-auth/$username/$password"},
],
},
"status2": {
"forms": [
{"href": "/basic-auth/$username/$password", "security": "basic_sc"},
],
},
},
};

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

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

Future<BasicCredentials?> basicCredentialsCallback(
Uri uri,
Form? form,
AugmentedForm? form,
BasicCredentials? invalidCredentials,
) async {
if (form == null) {
return basicCredentials;
}

final id = form.thingDescription.identifier;
final id = form.tdIdentifier;

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

final thingDescription = ThingDescription(thingDescriptionJson);
final thingDescription = thingDescriptionJson.toThingDescription();
final consumedThing = await wot.consume(thingDescription);
final status = await consumedThing.readProperty("status");

Expand Down
Loading

0 comments on commit 60524b4

Please sign in to comment.