|
4 | 4 | // |
5 | 5 | // SPDX-License-Identifier: BSD-3-Clause |
6 | 6 |
|
| 7 | +import 'dart:async'; |
| 8 | + |
7 | 9 | import '../../scripting_api.dart' as scripting_api; |
8 | 10 | import '../definitions/thing_description.dart'; |
9 | 11 | import '../scripting_api/discovery/discovery_method.dart'; |
10 | 12 | import 'consumed_thing.dart'; |
11 | 13 | import 'exposed_thing.dart'; |
12 | 14 | import 'servient.dart'; |
13 | | -import 'thing_discovery.dart' show ThingDiscovery; |
| 15 | +import 'thing_discovery.dart' |
| 16 | + show DiscoveryException, ThingDiscovery, ThingDiscoveryProcess; |
14 | 17 |
|
15 | 18 | /// This [Exception] is thrown if an error during the consumption of a |
16 | 19 | /// [ThingDescription] occurs. |
@@ -95,4 +98,93 @@ class WoT implements scripting_api.WoT { |
95 | 98 | Future<ThingDescription> requestThingDescription(Uri url) { |
96 | 99 | return _servient.requestThingDescription(url); |
97 | 100 | } |
| 101 | + |
| 102 | + @override |
| 103 | + Future<scripting_api.ThingDiscoveryProcess> exploreDirectory( |
| 104 | + Uri url, [ |
| 105 | + scripting_api.ThingFilter? filter, |
| 106 | + ]) async { |
| 107 | + final thingDescription = await requestThingDescription(url); |
| 108 | + |
| 109 | + if (!thingDescription.isValidDirectoryThingDescription) { |
| 110 | + throw DiscoveryException( |
| 111 | + 'Encountered an invalid Directory Thing Description', |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + final consumedDirectoryThing = await consume(thingDescription); |
| 116 | + |
| 117 | + final interactionOutput = |
| 118 | + await consumedDirectoryThing.readProperty('things'); |
| 119 | + final rawThingDescriptions = await interactionOutput.value(); |
| 120 | + |
| 121 | + if (rawThingDescriptions is! List<dynamic>) { |
| 122 | + throw DiscoveryException( |
| 123 | + 'Expected an array of Thing Descriptions but received an ' |
| 124 | + 'invalid output instead.', |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + final thingDescriptionStream = Stream.fromIterable( |
| 129 | + rawThingDescriptions.whereType<Map<String, dynamic>>(), |
| 130 | + ).toThingDescriptionStream(); |
| 131 | + |
| 132 | + return ThingDiscoveryProcess(thingDescriptionStream, filter); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +extension _DirectoryValidationExtension on ThingDescription { |
| 137 | + bool get isValidDirectoryThingDescription { |
| 138 | + final atTypes = atType; |
| 139 | + |
| 140 | + if (atTypes == null) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + const discoveryContextUri = 'https://www.w3.org/2022/wot/discovery'; |
| 145 | + const type = 'ThingDirectory'; |
| 146 | + const fullIri = '$discoveryContextUri#$type'; |
| 147 | + |
| 148 | + if (atTypes.contains(fullIri)) { |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + return context.contains((value: discoveryContextUri, key: null)) && |
| 153 | + atTypes.contains(type); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +extension _DirectoryTdDeserializationExtension on Stream<Map<String, dynamic>> { |
| 158 | + Stream<ThingDescription> toThingDescriptionStream() { |
| 159 | + const streamTransformer = StreamTransformer(_transformerMethod); |
| 160 | + |
| 161 | + return transform(streamTransformer); |
| 162 | + } |
| 163 | + |
| 164 | + static StreamSubscription<ThingDescription> _transformerMethod( |
| 165 | + Stream<Map<String, dynamic>> rawThingDescriptionStream, |
| 166 | + bool cancelOnError, |
| 167 | + ) { |
| 168 | + final streamController = StreamController<ThingDescription>(); |
| 169 | + |
| 170 | + final streamSubscription = rawThingDescriptionStream.listen( |
| 171 | + (rawThingDescription) { |
| 172 | + try { |
| 173 | + streamController.add(ThingDescription.fromJson(rawThingDescription)); |
| 174 | + } on Exception catch (exception) { |
| 175 | + streamController.addError(exception); |
| 176 | + } |
| 177 | + }, |
| 178 | + onDone: streamController.close, |
| 179 | + onError: streamController.addError, |
| 180 | + cancelOnError: cancelOnError, |
| 181 | + ); |
| 182 | + |
| 183 | + streamController |
| 184 | + ..onPause = streamSubscription.pause |
| 185 | + ..onResume = streamSubscription.resume |
| 186 | + ..onCancel = streamSubscription.cancel; |
| 187 | + |
| 188 | + return streamController.stream.listen(null); |
| 189 | + } |
98 | 190 | } |
0 commit comments