Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: simplify InteractionOptions #85

Merged
merged 2 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/complex_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Future<void> main() async {

final status3 = await consumedThing.readProperty(
'anotherStatus',
const InteractionOptions(uriVariables: {'test': 'hi'}),
uriVariables: {'test': 'hi'},
);
final value3 = await status3.value();
print(value3);
Expand Down
10 changes: 5 additions & 5 deletions example/mqtt_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ Future<void> main(List<String> args) async {
},
);

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 consumedThing.invokeAction('toggle', input: 'Hello World!');
await consumedThing.invokeAction('toggle', input: 'Hello World!');
await consumedThing.invokeAction('toggle', input: 'Hello World!');
await consumedThing.invokeAction('toggle', input: 'Hello World!');
await subscription.stop();

await consumedThing.invokeAction('toggle', 'Bye World!');
await consumedThing.invokeAction('toggle', input: 'Bye World!');
await consumedThing.readAndPrintProperty('status');
print('Done!');
}
Expand Down
1 change: 0 additions & 1 deletion lib/scripting_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export 'src/scripting_api/discovery/discovery_method.dart';
export 'src/scripting_api/discovery/thing_discovery.dart';
export 'src/scripting_api/discovery/thing_filter.dart';
export 'src/scripting_api/exposed_thing.dart';
export 'src/scripting_api/interaction_options.dart';
export 'src/scripting_api/interaction_output.dart';
export 'src/scripting_api/subscription.dart';
export 'src/scripting_api/types.dart';
Expand Down
7 changes: 5 additions & 2 deletions lib/src/binding_coap/coap_subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import 'package:coap/coap.dart';

import '../scripting_api/interaction_options.dart';
import '../scripting_api/subscription.dart';

/// [Subscription] to a CoAP resource, based on the observe option ([RFC 7641]).
Expand Down Expand Up @@ -34,7 +33,11 @@ class CoapSubscription implements Subscription {
final void Function() _complete;

@override
Future<void> stop([InteractionOptions? options]) async {
Future<void> stop({
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
}) async {
if (!_active) {
return;
}
Expand Down
7 changes: 5 additions & 2 deletions lib/src/binding_mqtt/mqtt_subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:mqtt_client/mqtt_server_client.dart';

import '../core/content.dart';
import '../definitions/form.dart';
import '../scripting_api/interaction_options.dart';
import '../scripting_api/subscription.dart' as scripting_api;

/// [scripting_api.Subscription] for the MQTT protocol.
Expand Down Expand Up @@ -61,7 +60,11 @@ class MqttSubscription implements scripting_api.Subscription {
bool get active => _active;

@override
Future<void> stop([InteractionOptions? options]) async {
Future<void> stop({
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
}) async {
_client.disconnect();
_active = false;
_complete();
Expand Down
142 changes: 91 additions & 51 deletions lib/src/core/consumed_thing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ class ConsumedThing implements scripting_api.ConsumedThing {
List<Form> forms,
OperationType operationType,
_AffordanceType affordanceType,
InteractionOptions? options,
InteractionAffordance interactionAffordance,
) {
InteractionAffordance interactionAffordance, {
required int? formIndex,
required Map<String, Object>? uriVariables,
}) {
if (forms.isEmpty) {
throw StateError(
'ConsumedThing "$title" has no links for this interaction',
Expand All @@ -88,8 +89,6 @@ class ConsumedThing implements scripting_api.ConsumedThing {
final ProtocolClient client;
final Form foundForm;

final formIndex = options?.formIndex;

if (formIndex != null) {
if (formIndex >= 0 && formIndex < forms.length) {
foundForm = forms[formIndex];
Expand All @@ -114,17 +113,18 @@ class ConsumedThing implements scripting_api.ConsumedThing {
client = servient.clientFor(scheme);
}

final form =
foundForm.resolveUriVariables(options?.uriVariables) ?? foundForm;
final form = foundForm.resolveUriVariables(uriVariables) ?? foundForm;

return (client: client, form: form);
}

@override
Future<InteractionOutput> readProperty(
String propertyName, [
InteractionOptions? options,
]) async {
String propertyName, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
}) async {
final property = thingDescription.properties[propertyName];

if (property == null) {
Expand All @@ -138,8 +138,9 @@ class ConsumedThing implements scripting_api.ConsumedThing {
property.forms,
OperationType.readproperty,
_AffordanceType.property,
options,
property,
formIndex: formIndex,
uriVariables: uriVariables,
);

final form = clientAndForm.form;
Expand All @@ -152,9 +153,11 @@ class ConsumedThing implements scripting_api.ConsumedThing {
@override
Future<void> writeProperty(
String propertyName,
Object? interactionInput, [
InteractionOptions? options,
]) async {
InteractionInput input, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
}) async {
// TODO(JKRhb): Refactor
final property = thingDescription.properties[propertyName];

Expand All @@ -169,23 +172,26 @@ class ConsumedThing implements scripting_api.ConsumedThing {
property.forms,
OperationType.writeproperty,
_AffordanceType.property,
options,
property,
formIndex: formIndex,
uriVariables: uriVariables,
);

final form = clientAndForm.form;
final client = clientAndForm.client;
final content = servient.contentSerdes
.valueToContent(interactionInput, property, form.contentType);
.valueToContent(input, property, form.contentType);
await client.writeResource(form, content);
}

@override
Future<InteractionOutput> invokeAction(
String actionName, [
Object? interactionInput,
InteractionOptions? options,
]) async {
String actionName, {
InteractionInput input,
Object? data,
int? formIndex,
Map<String, Object>? uriVariables,
}) async {
// TODO(JKRhb): Refactor
final action = thingDescription.actions[actionName];

Expand All @@ -200,26 +206,27 @@ class ConsumedThing implements scripting_api.ConsumedThing {
action.forms,
OperationType.invokeaction,
_AffordanceType.action,
options,
action,
uriVariables: uriVariables,
formIndex: formIndex,
);

final form = clientAndForm.form;
final client = clientAndForm.client;
final input = servient.contentSerdes
.valueToContent(interactionInput, action.input, form.contentType);
final content = servient.contentSerdes
.valueToContent(input, action.input, form.contentType);

final content = await client.invokeResource(form, input);
final output = await client.invokeResource(form, content);

final response = form.response;
if (response != null) {
if (content.type != response.contentType) {
if (output.type != response.contentType) {
throw UnexpectedReponseException('Unexpected type in response');
}
}

return InteractionOutput(
content,
output,
servient.contentSerdes,
form,
action.output,
Expand All @@ -229,10 +236,12 @@ class ConsumedThing implements scripting_api.ConsumedThing {
@override
Future<Subscription> observeProperty(
String propertyName,
scripting_api.InteractionListener listener, [
scripting_api.InteractionListener listener, {
scripting_api.ErrorListener? onError,
InteractionOptions? options,
]) async {
Object? data,
int? formIndex,
Map<String, Object>? uriVariables,
}) async {
final property = thingDescription.properties[propertyName];

if (property == null) {
Expand All @@ -251,24 +260,26 @@ class ConsumedThing implements scripting_api.ConsumedThing {

return _createSubscription(
property,
options,
listener,
onError,
propertyName,
property,
SubscriptionType.property,
formIndex: formIndex,
uriVariables: uriVariables,
);
}

Future<Subscription> _createSubscription(
InteractionAffordance affordance,
scripting_api.InteractionOptions? options,
scripting_api.InteractionListener listener,
scripting_api.ErrorListener? onError,
String affordanceName,
DataSchema? dataSchema,
SubscriptionType subscriptionType,
) async {
SubscriptionType subscriptionType, {
required int? formIndex,
required Map<String, Object>? uriVariables,
}) async {
final OperationType operationType;
final _AffordanceType affordanceType;
final Map<String, Subscription> subscriptions;
Expand All @@ -287,8 +298,9 @@ class ConsumedThing implements scripting_api.ConsumedThing {
affordance.forms,
operationType,
affordanceType,
options,
affordance,
uriVariables: uriVariables,
formIndex: formIndex,
);

final form = clientAndForm.form;
Expand Down Expand Up @@ -318,13 +330,20 @@ class ConsumedThing implements scripting_api.ConsumedThing {
}

Future<PropertyReadMap> _readProperties(
List<String> propertyNames,
InteractionOptions? options,
) async {
List<String> propertyNames, {
Object? data,
int? formIndex,
Map<String, Object>? uriVariables,
}) async {
final Map<String, Future<InteractionOutput>> outputs = {};

for (final propertyName in propertyNames) {
outputs[propertyName] = readProperty(propertyName, options);
outputs[propertyName] = readProperty(
propertyName,
data: data,
formIndex: formIndex,
uriVariables: uriVariables,
);
}

final outputList = await Future.wait(outputs.values);
Expand All @@ -333,28 +352,46 @@ class ConsumedThing implements scripting_api.ConsumedThing {
}

@override
Future<PropertyReadMap> readAllProperties([InteractionOptions? options]) {
Future<PropertyReadMap> readAllProperties({
Object? data,
int? formIndex,
Map<String, Object>? uriVariables,
}) {
final propertyNames =
thingDescription.properties.keys.toList(growable: false);

return _readProperties(propertyNames, options);
return _readProperties(
propertyNames,
data: data,
formIndex: formIndex,
uriVariables: uriVariables,
);
}

@override
Future<PropertyReadMap> readMultipleProperties(
List<String> propertyNames, [
InteractionOptions? options,
]) {
return _readProperties(propertyNames, options);
List<String> propertyNames, {
Object? data,
int? formIndex,
Map<String, Object>? uriVariables,
}) {
return _readProperties(
propertyNames,
data: data,
formIndex: formIndex,
uriVariables: uriVariables,
);
}

@override
Future<Subscription> subscribeEvent(
String eventName,
scripting_api.InteractionListener listener, [
scripting_api.InteractionListener listener, {
scripting_api.ErrorListener? onError,
InteractionOptions? options,
]) {
Object? data,
int? formIndex,
Map<String, Object>? uriVariables,
}) {
// TODO(JKRhb): Handle subscription and cancellation data.
final event = thingDescription.events[eventName];

Expand All @@ -374,20 +411,23 @@ class ConsumedThing implements scripting_api.ConsumedThing {

return _createSubscription(
event,
options,
listener,
onError,
eventName,
event.data,
SubscriptionType.event,
formIndex: formIndex,
uriVariables: uriVariables,
);
}

@override
Future<void> writeMultipleProperties(
PropertyWriteMap valueMap, [
InteractionOptions? options,
]) async {
PropertyWriteMap valueMap, {
Object? data,
int? formIndex,
Map<String, Object>? uriVariables,
}) async {
await Future.wait(
valueMap.keys.map((key) => writeProperty(key, valueMap[key])),
);
Expand Down
Loading