Skip to content

Commit

Permalink
docs: create seperate MQTT example
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Dec 20, 2023
1 parent 848e923 commit 408f6a2
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions example/mqtt_example.dart
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);
}
}

0 comments on commit 408f6a2

Please sign in to comment.