-
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.
- Loading branch information
Showing
1 changed file
with
105 additions
and
0 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,105 @@ | ||
// Copyright 2023 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/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" | ||
} | ||
}, | ||
"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 | ||
} | ||
] | ||
} | ||
} | ||
} | ||
'''; | ||
|
||
final Map<String, BasicCredentials> basicCredentials = { | ||
'urn:test': BasicCredentials('rw', 'readwrite'), | ||
}; | ||
|
||
Future<BasicCredentials?> basicCredentialsCallback( | ||
Uri uri, | ||
Form? form, [ | ||
BasicCredentials? invalidCredentials, | ||
]) async { | ||
final id = form?.thingDescription.identifier; | ||
|
||
return basicCredentials[id]; | ||
} | ||
|
||
Future<void> main(List<String> args) async { | ||
final servient = Servient( | ||
clientFactories: [ | ||
MqttClientFactory(basicCredentialsCallback: basicCredentialsCallback), | ||
], | ||
); | ||
|
||
final wot = await servient.start(); | ||
|
||
final thingDescription = ThingDescription(thingDescriptionJson); | ||
final consumedThing = await wot.consume(thingDescription); | ||
await consumedThing.readAndPrintProperty('status'); | ||
|
||
final subscription = await consumedThing.observeProperty( | ||
'status', | ||
(data) async { | ||
final value = await data.value(); | ||
print(value); | ||
}, | ||
); | ||
|
||
await consumedThing.invokeAction('toggle', 'Hello World!'); | ||
await consumedThing.invokeAction('toggle', 'Hello World!'); | ||
await consumedThing.invokeAction('toggle', 'Hello World!'); | ||
await consumedThing.invokeAction('toggle', 'Hello World!'); | ||
await subscription.stop(); | ||
|
||
await consumedThing.invokeAction('toggle', 'Bye World!'); | ||
await consumedThing.readAndPrintProperty('status'); | ||
print('Done!'); | ||
} | ||
|
||
extension ReadAndPrintExtension on ConsumedThing { | ||
Future<void> readAndPrintProperty(String propertyName) async { | ||
final output = await readProperty(propertyName); | ||
final value = await output.value(); | ||
print(value); | ||
} | ||
} |