|
8 | 8 |
|
9 | 9 | import 'package:dart_wot/dart_wot.dart'; |
10 | 10 |
|
11 | | -final Map<String, BasicCredentials> basicCredentials = { |
12 | | - 'urn:test': BasicCredentials('rw', 'readwrite'), |
13 | | -}; |
14 | | - |
15 | | -Future<BasicCredentials?> basicCredentialsCallback( |
16 | | - Uri uri, |
17 | | - Form? form, [ |
18 | | - BasicCredentials? invalidCredentials, |
19 | | -]) async { |
20 | | - final id = form?.thingDescription.identifier; |
21 | | - |
22 | | - return basicCredentials[id]; |
23 | | -} |
24 | | - |
25 | 11 | Future<void> main(List<String> args) async { |
26 | | - final coapClientFactory = CoapClientFactory(); |
27 | | - final httpClientFactory = |
28 | | - HttpClientFactory(basicCredentialsCallback: basicCredentialsCallback); |
29 | | - final mqttClientFactory = MqttClientFactory(); |
30 | | - |
31 | 12 | final servient = Servient( |
32 | 13 | clientFactories: [ |
33 | | - coapClientFactory, |
34 | | - httpClientFactory, |
35 | | - mqttClientFactory, |
| 14 | + CoapClientFactory(), |
36 | 15 | ], |
37 | 16 | ); |
38 | | - |
39 | 17 | final wot = await servient.start(); |
40 | 18 |
|
41 | | - const thingDescriptionJson = ''' |
42 | | - { |
43 | | - "@context": "http://www.w3.org/ns/td", |
44 | | - "title": "Test Thing", |
45 | | - "id": "urn:test", |
46 | | - "base": "coap://coap.me", |
47 | | - "security": ["auto_sc"], |
48 | | - "securityDefinitions": { |
49 | | - "auto_sc": { |
50 | | - "scheme": "auto" |
51 | | - } |
52 | | - }, |
53 | | - "properties": { |
54 | | - "status": { |
55 | | - "forms": [ |
56 | | - { |
57 | | - "href": "/hello" |
58 | | - } |
59 | | - ] |
60 | | - }, |
61 | | - "status2": { |
62 | | - "observable": true, |
63 | | - "forms": [ |
64 | | - { |
65 | | - "href": "mqtt://test.mosquitto.org:1884", |
66 | | - "mqv:filter": "test", |
67 | | - "op": ["readproperty", "observeproperty"], |
68 | | - "contentType": "text/plain" |
69 | | - } |
70 | | - ] |
71 | | - } |
72 | | - }, |
73 | | - "actions": { |
74 | | - "toggle": { |
75 | | - "forms": [ |
76 | | - { |
77 | | - "href": "mqtt://test.mosquitto.org:1884", |
78 | | - "mqv:topic": "test", |
79 | | - "mqv:retain": true |
80 | | - } |
81 | | - ] |
82 | | - } |
83 | | - } |
84 | | - } |
85 | | - '''; |
| 19 | + final url = Uri.parse('coap://plugfest.thingweb.io/counter'); |
| 20 | + print('Requesting TD from $url ...'); |
| 21 | + final thingDescription = await wot.requestThingDescription(url); |
86 | 22 |
|
87 | | - final thingDescription = ThingDescription(thingDescriptionJson); |
88 | 23 | final consumedThing = await wot.consume(thingDescription); |
89 | | - final status = await consumedThing.readProperty('status'); |
90 | | - final value = await status.value(); |
91 | | - print(value); |
92 | | - final subscription = await consumedThing.observeProperty( |
93 | | - 'status2', |
94 | | - (data) async { |
95 | | - final value = await data.value(); |
96 | | - print(value); |
97 | | - }, |
| 24 | + print( |
| 25 | + 'Successfully retrieved and consumed TD with title ' |
| 26 | + '"${thingDescription.title}"!', |
98 | 27 | ); |
99 | 28 |
|
100 | | - await consumedThing.invokeAction('toggle', 'Hello World!'); |
101 | | - await consumedThing.invokeAction('toggle', 'Hello World!'); |
102 | | - await consumedThing.invokeAction('toggle', 'Hello World!'); |
103 | | - await consumedThing.invokeAction('toggle', 'Hello World!'); |
104 | | - await subscription.stop(); |
| 29 | + print('Incrementing counter ...'); |
| 30 | + await consumedThing.invokeAction('increment'); |
105 | 31 |
|
106 | | - final thingUri = Uri.parse( |
107 | | - 'https://raw.githubusercontent.com/w3c/wot-testing' |
108 | | - '/b07fa6124bca7796e6ca752a3640fac264d3bcbc/events/2021.03.Online/TDs' |
109 | | - '/Oracle/oracle-Festo_Shared.td.jsonld', |
110 | | - ); |
111 | | - |
112 | | - final thingDiscovery = wot.discover(thingUri); |
113 | | - |
114 | | - await for (final thingDescription in thingDiscovery) { |
115 | | - final consumedDiscoveredThing = await wot.consume(thingDescription); |
116 | | - print( |
117 | | - 'The title of the fetched TD is ' |
118 | | - '${consumedDiscoveredThing.thingDescription.title}.', |
119 | | - ); |
120 | | - } |
121 | | - |
122 | | - await consumedThing.invokeAction('toggle', 'Bye World!'); |
123 | | - await consumedThing.readAndPrintProperty('status2'); |
124 | | - print('Done!'); |
125 | | -} |
126 | | - |
127 | | -extension ReadAndPrintExtension on ConsumedThing { |
128 | | - Future<void> readAndPrintProperty(String propertyName) async { |
129 | | - final output = await readProperty(propertyName); |
130 | | - final value = await output.value(); |
131 | | - print(value); |
132 | | - } |
| 32 | + final status = await consumedThing.readProperty('count'); |
| 33 | + final value = await status.value(); |
| 34 | + print('New counter value: $value'); |
133 | 35 | } |
0 commit comments