Skip to content

Commit

Permalink
feat!: improve TD serialization behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 4, 2024
1 parent 61e4a32 commit 88bbe14
Show file tree
Hide file tree
Showing 53 changed files with 1,714 additions and 1,233 deletions.
23 changes: 8 additions & 15 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 = '''
{
const thingDescriptionJson = {
"@context": "http://www.w3.org/ns/td",
"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 = ThingDescription.fromJson(thingDescriptionJson);
final consumedThing = await wot.consume(thingDescription);
final status = await consumedThing.readProperty("status");
final value = await status.value();
Expand Down
80 changes: 28 additions & 52 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#"
}
{"@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 = ThingDescription.fromJson(thingDescriptionJson);
final consumedThing = await wot.consume(thingDescription);
final status = await consumedThing.readProperty("status");
final value1 = await status.value();
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": ["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"},
],
},
},
};

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 = ThingDescription.fromJson(thingDescriptionJson);
final consumedThing = await wot.consume(thingDescription);
final status = await consumedThing.readProperty("status");

Expand Down
78 changes: 36 additions & 42 deletions example/mqtt_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,52 @@

import "package:dart_wot/dart_wot.dart";

const thingDescriptionJson = '''
{
"@context": "https://www.w3.org/2022/wot/td/v1.1",
"title": "Test Thing",
"id": "urn:test",
"base": "coap://coap.me",
"security": ["auto_sc"],
"securityDefinitions": {
"auto_sc": {
"scheme": "auto"
}
const thingDescriptionJson = {
"@context": "https://www.w3.org/2022/wot/td/v1.1",
"title": "Test Thing",
"id": "urn:test",
"base": "coap://coap.me",
"security": ["auto_sc"],
"securityDefinitions": {
"auto_sc": {"scheme": "auto"},
},
"properties": {
"status": {
"observable": true,
"forms": [
{
"href": "mqtt://test.mosquitto.org:1884",
"mqv:filter": "test",
"op": ["readproperty", "observeproperty"],
"contentType": "text/plain",
}
],
},
"properties": {
"status": {
"observable": true,
"forms": [
{
"href": "mqtt://test.mosquitto.org:1884",
"mqv:filter": "test",
"op": ["readproperty", "observeproperty"],
"contentType": "text/plain"
}
]
}
},
"actions": {
"toggle": {
"input": {"type": "string"},
"forms": [
{
"href": "mqtt://test.mosquitto.org:1884",
"mqv:topic": "test",
"mqv:retain": true,
}
],
},
"actions": {
"toggle": {
"input": {
"type": "string"
},
"forms": [
{
"href": "mqtt://test.mosquitto.org:1884",
"mqv:topic": "test",
"mqv:retain": true
}
]
}
}
}
''';
},
};

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

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 @@ -73,7 +67,7 @@ Future<void> main(List<String> args) async {

final wot = await servient.start();

final thingDescription = ThingDescription(thingDescriptionJson);
final thingDescription = ThingDescription.fromJson(thingDescriptionJson);
final consumedThing = await wot.consume(thingDescription);
await consumedThing.readAndPrintProperty("status");

Expand Down
1 change: 1 addition & 0 deletions lib/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ library core;

export "package:dcaf/dcaf.dart";

export "src/core/augmented_form.dart";
export "src/core/codecs/content_codec.dart";
export "src/core/content_serdes.dart";
export "src/core/credentials/ace_credentials.dart";
Expand Down
Loading

0 comments on commit 88bbe14

Please sign in to comment.