-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ExposedThing functionality
- Loading branch information
Showing
16 changed files
with
1,010 additions
and
585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.", | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.