Skip to content

Commit 7d03e6c

Browse files
committed
feat!: add supportsOperation methods across codebase
1 parent 8e012d7 commit 7d03e6c

File tree

8 files changed

+95
-14
lines changed

8 files changed

+95
-14
lines changed

lib/src/binding_coap/coap_client_factory.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import "../../core.dart";
88

99
import "coap_client.dart";
1010
import "coap_config.dart";
11+
import "coap_definitions.dart";
1112

1213
/// A [ProtocolClientFactory] that produces CoAP clients.
1314
final class CoapClientFactory implements ProtocolClientFactory {
@@ -45,4 +46,21 @@ final class CoapClientFactory implements ProtocolClientFactory {
4546
bool init() {
4647
return true;
4748
}
49+
50+
@override
51+
bool supportsOperation(OperationType operationType, String? subprotocol) {
52+
const observeOperations = [
53+
OperationType.observeproperty,
54+
OperationType.unobserveproperty,
55+
OperationType.subscribeevent,
56+
OperationType.unsubscribeevent,
57+
];
58+
59+
if (observeOperations.contains(operationType)) {
60+
return CoapSubprotocol.tryParse(subprotocol ?? "") ==
61+
CoapSubprotocol.observe;
62+
}
63+
64+
return subprotocol == null;
65+
}
4866
}

lib/src/binding_http/http_client_factory.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,24 @@ final class HttpClientFactory implements ProtocolClientFactory {
4242
bool init() {
4343
return true;
4444
}
45+
46+
@override
47+
bool supportsOperation(OperationType operationType, String? subprotocol) {
48+
const unsupportedOperations = [
49+
OperationType.observeproperty,
50+
OperationType.unobserveproperty,
51+
OperationType.subscribeevent,
52+
OperationType.unsubscribeevent,
53+
];
54+
55+
if (unsupportedOperations.contains(operationType)) {
56+
return false;
57+
}
58+
59+
if (subprotocol != null) {
60+
return false;
61+
}
62+
63+
return true;
64+
}
4565
}

lib/src/binding_mqtt/mqtt_client_factory.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,10 @@ final class MqttClientFactory implements ProtocolClientFactory {
4242

4343
@override
4444
Set<String> get schemes => {mqttUriScheme, mqttSecureUriScheme};
45+
46+
@override
47+
bool supportsOperation(OperationType operationType, String? subprotocol) {
48+
// MQTT client does not support any subprotocols
49+
return subprotocol == null;
50+
}
4551
}

lib/src/core/implementation/consumed_thing.dart

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,19 @@ class ConsumedThing implements scripting_api.ConsumedThing {
9797
}
9898
} else {
9999
foundForm = augmentedForms.firstWhere(
100-
(form) =>
101-
hasClientFor(form.href.scheme) &&
102-
_supportsOperationType(form, interactionAffordance, operationType),
100+
(form) {
101+
final opValues = form.op;
102+
103+
if (!opValues.contains(operationType)) {
104+
return false;
105+
}
106+
107+
return servient.supportsOperation(
108+
form.resolvedHref.scheme,
109+
operationType,
110+
form.subprotocol,
111+
);
112+
},
103113
// TODO(JKRhb): Add custom Exception
104114
orElse: () => throw Exception("No matching form found!"),
105115
);
@@ -429,17 +439,6 @@ class ConsumedThing implements scripting_api.ConsumedThing {
429439
}
430440
}
431441

432-
static bool _supportsOperationType(
433-
Form form,
434-
InteractionAffordance interactionAffordance,
435-
OperationType operationType,
436-
) {
437-
final opValues =
438-
form.op ?? OperationType.defaultOpValues(interactionAffordance);
439-
440-
return opValues.contains(operationType);
441-
}
442-
443442
/// Cleans up the resources used by this [ConsumedThing].
444443
void destroy() {
445444
for (final observedProperty in _observedProperties.values) {

lib/src/core/implementation/protocol_interfaces/protocol_client_factory.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
//
55
// SPDX-License-Identifier: BSD-3-Clause
66

7+
import "package:meta/meta.dart";
8+
9+
import "../../definitions.dart";
710
import "protocol_client.dart";
811

912
/// Base class for a factory that produces [ProtocolClient]s.
@@ -24,4 +27,9 @@ abstract interface class ProtocolClientFactory {
2427
/// Creates a new [ProtocolClient] with that supports one or more of the given
2528
/// [schemes].
2629
ProtocolClient createClient();
30+
31+
/// Indicates whether this [ProtocolClientFactory] supports a given
32+
/// [operationType] and subprotocol.
33+
@experimental
34+
bool supportsOperation(OperationType operationType, String? subprotocol);
2735
}

lib/src/core/implementation/servient.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//
55
// SPDX-License-Identifier: BSD-3-Clause
66

7+
import "package:meta/meta.dart";
8+
79
import "../definitions.dart";
810
import "../scripting_api.dart" as scripting_api;
911
import "consumed_thing.dart";
@@ -222,6 +224,26 @@ class Servient {
222224
return clientFactory.createClient();
223225
}
224226

227+
/// Indicates whether there is a registered [ProtocolClientFactory] supporting
228+
/// the given [operationType] and [subprotocol].
229+
///
230+
/// Also returns `false` if there is no [ProtocolClientFactory] registered for
231+
/// the given [scheme].
232+
@experimental
233+
bool supportsOperation(
234+
String scheme,
235+
OperationType operationType,
236+
String? subprotocol,
237+
) {
238+
final protocolClient = _clientFactories[scheme];
239+
240+
if (protocolClient == null) {
241+
return false;
242+
}
243+
244+
return protocolClient.supportsOperation(operationType, subprotocol);
245+
}
246+
225247
/// Requests a [ThingDescription] from a [url].
226248
Future<ThingDescription> requestThingDescription(Uri url) async {
227249
final client = clientFor(url.scheme);

test/core/discovery_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ class _MockedProtocolClientFactory implements ProtocolClientFactory {
258258

259259
@override
260260
Set<String> get schemes => {testUriScheme};
261+
262+
@override
263+
bool supportsOperation(OperationType operationType, String? subprotocol) =>
264+
true;
261265
}
262266

263267
extension _DiscoveryContentCreationExtension on String {

test/core/servient_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class MockedProtocolClientFactory implements ProtocolClientFactory {
2727

2828
@override
2929
Set<String> get schemes => {testUriScheme};
30+
31+
@override
32+
bool supportsOperation(OperationType operationType, String? subprotocol) =>
33+
true;
3034
}
3135

3236
void main() {

0 commit comments

Comments
 (0)