|
| 1 | +// Copyright 2023 Contributors to the Eclipse Foundation. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: BSD-3-Clause |
| 6 | + |
| 7 | +// ignore_for_file: avoid_print |
| 8 | + |
| 9 | +import 'package:dart_wot/dart_wot.dart'; |
| 10 | + |
| 11 | +const thingDescriptionJson = ''' |
| 12 | + { |
| 13 | + "@context": "https://www.w3.org/2022/wot/td/v1.1", |
| 14 | + "title": "Test Thing", |
| 15 | + "id": "urn:test", |
| 16 | + "base": "coap://coap.me", |
| 17 | + "security": ["auto_sc"], |
| 18 | + "securityDefinitions": { |
| 19 | + "auto_sc": { |
| 20 | + "scheme": "auto" |
| 21 | + } |
| 22 | + }, |
| 23 | + "properties": { |
| 24 | + "status": { |
| 25 | + "observable": true, |
| 26 | + "forms": [ |
| 27 | + { |
| 28 | + "href": "mqtt://test.mosquitto.org:1884", |
| 29 | + "mqv:filter": "test", |
| 30 | + "op": ["readproperty", "observeproperty"], |
| 31 | + "contentType": "text/plain" |
| 32 | + } |
| 33 | + ] |
| 34 | + } |
| 35 | + }, |
| 36 | + "actions": { |
| 37 | + "toggle": { |
| 38 | + "input": { |
| 39 | + "type": "string" |
| 40 | + }, |
| 41 | + "forms": [ |
| 42 | + { |
| 43 | + "href": "mqtt://test.mosquitto.org:1884", |
| 44 | + "mqv:topic": "test", |
| 45 | + "mqv:retain": true |
| 46 | + } |
| 47 | + ] |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + '''; |
| 52 | + |
| 53 | +final Map<String, BasicCredentials> basicCredentials = { |
| 54 | + 'urn:test': BasicCredentials('rw', 'readwrite'), |
| 55 | +}; |
| 56 | + |
| 57 | +Future<BasicCredentials?> basicCredentialsCallback( |
| 58 | + Uri uri, |
| 59 | + Form? form, [ |
| 60 | + BasicCredentials? invalidCredentials, |
| 61 | +]) async { |
| 62 | + final id = form?.thingDescription.identifier; |
| 63 | + |
| 64 | + return basicCredentials[id]; |
| 65 | +} |
| 66 | + |
| 67 | +Future<void> main(List<String> args) async { |
| 68 | + final servient = Servient( |
| 69 | + clientFactories: [ |
| 70 | + MqttClientFactory(basicCredentialsCallback: basicCredentialsCallback), |
| 71 | + ], |
| 72 | + ); |
| 73 | + |
| 74 | + final wot = await servient.start(); |
| 75 | + |
| 76 | + final thingDescription = ThingDescription(thingDescriptionJson); |
| 77 | + final consumedThing = await wot.consume(thingDescription); |
| 78 | + await consumedThing.readAndPrintProperty('status'); |
| 79 | + |
| 80 | + final subscription = await consumedThing.observeProperty( |
| 81 | + 'status', |
| 82 | + (data) async { |
| 83 | + final value = await data.value(); |
| 84 | + print(value); |
| 85 | + }, |
| 86 | + ); |
| 87 | + |
| 88 | + await consumedThing.invokeAction('toggle', 'Hello World!'); |
| 89 | + await consumedThing.invokeAction('toggle', 'Hello World!'); |
| 90 | + await consumedThing.invokeAction('toggle', 'Hello World!'); |
| 91 | + await consumedThing.invokeAction('toggle', 'Hello World!'); |
| 92 | + await subscription.stop(); |
| 93 | + |
| 94 | + await consumedThing.invokeAction('toggle', 'Bye World!'); |
| 95 | + await consumedThing.readAndPrintProperty('status'); |
| 96 | + print('Done!'); |
| 97 | +} |
| 98 | + |
| 99 | +extension ReadAndPrintExtension on ConsumedThing { |
| 100 | + Future<void> readAndPrintProperty(String propertyName) async { |
| 101 | + final output = await readProperty(propertyName); |
| 102 | + final value = await output.value(); |
| 103 | + print(value); |
| 104 | + } |
| 105 | +} |
0 commit comments