Skip to content

Commit

Permalink
fixup! Implement ExposedThing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jul 17, 2024
1 parent ac13ae0 commit 3b5d350
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 16 deletions.
20 changes: 16 additions & 4 deletions lib/src/binding_http/http_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ final class HttpServer implements ProtocolServer {
switch (affordanceValue) {
// TODO: Refactor
// TODO: Handle values from protocol bindings
case Property(:final readOnly, :final writeOnly):
case Property(:final readOnly, :final writeOnly, :final observable):
if (!writeOnly) {
const operationType = OperationType.readproperty;
final methodName = operationType.defaultHttpMethod;
Expand Down Expand Up @@ -161,7 +161,15 @@ final class HttpServer implements ProtocolServer {
),
);
}
// TODO: Handle observe

if (observable) {
const _ = [
OperationType.observeproperty,
OperationType.unobserveproperty,
];

// TODO: Implement some kind of event mechanism (e.g., longpolling)
}
case Action():
const operationType = OperationType.invokeaction;
final methodName = operationType.defaultHttpMethod;
Expand Down Expand Up @@ -193,8 +201,12 @@ final class HttpServer implements ProtocolServer {
);

case Event():
// TODO: Implement
continue;
const _ = [
OperationType.subscribeevent,
OperationType.unsubscribeevent,
];

// TODO: Implement some kind of event mechanism (e.g., longpolling)
}
}

Expand Down
78 changes: 70 additions & 8 deletions lib/src/core/implementation/exposed_thing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
final Map<String, scripting_api.EventSubscriptionHandler>
_eventUnsubscribeHandlers = {};

final Map<String, ContentListener> _propertyChangeListeners = {};

final Map<String, ContentListener> _eventListeners = {};

Property _obtainProperty(String name) {
final property = thingDescription.properties?[name];

Expand All @@ -57,6 +61,19 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
return property;
}

Event _obtainEvent(String name) {
final event = thingDescription.events?[name];

if (event == null) {
throw ArgumentError(
"Event $name does not exist in ExposedThing "
"with title ${thingDescription.title}.",
);
}

return event;
}

void _checkReadableProperty(String name) {
final property = _obtainProperty(name);

Expand Down Expand Up @@ -85,9 +102,32 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
}

@override
Future<void> emitPropertyChange(String name) {
// TODO(JKRhb): implement emitPropertyChange
throw UnimplementedError();
Future<void> emitPropertyChange(
String name, [
String contentType = "application/json",
]) async {
final property = _obtainProperty(name);

final readHandler = _propertyReadHandlers[name];

// TODO: Does this need to be a ProtocolListenerRegistry?
final propertyChangeHandler = _propertyChangeListeners[name];

// TODO: Do we need to throw an error here?
if (readHandler == null || propertyChangeHandler == null) {
return;
}

final interactionInput = await readHandler();

final content = Content.fromInteractionInput(
interactionInput,
contentType,
_servient.contentSerdes,
property,
);

propertyChangeHandler(content);
}

@override
Expand All @@ -96,9 +136,27 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
}

@override
Future<void> emitEvent(String name, Object? data) {
// TODO(JKRhb): implement emitEvent
throw UnimplementedError();
Future<void> emitEvent(
String name,
scripting_api.InteractionInput data, [
String contentType = "application/json",
]) async {
final event = _obtainEvent(name);

final eventListener = _eventListeners[name];

if (eventListener == null) {
return;
}

final content = Content.fromInteractionInput(
data,
contentType,
_servient.contentSerdes,
event.data,
);

eventListener(content);
}

@override
Expand Down Expand Up @@ -343,7 +401,8 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {

@override
Future<void> handleObserveProperty(
String propertyName, {
String propertyName,
ContentListener contentListener, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
Expand All @@ -356,6 +415,8 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
);
}

_propertyChangeListeners[propertyName] = contentListener;

await observeHandler(
data: data,
uriVariables: uriVariables,
Expand Down Expand Up @@ -387,7 +448,8 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {

@override
Future<void> handleSubscribeEvent(
String eventName, {
String eventName,
ContentListener contentListener, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
Expand Down
9 changes: 7 additions & 2 deletions lib/src/core/protocol_interfaces/exposable_thing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import "../implementation.dart";
///
typedef PropertyContentMap = Map<String, Content>;

///
typedef ContentListener = void Function(Content content);

/// Interface that allows ProtocolServers to interact with ExposedThings.
// TODO: This needs a better name
abstract interface class ExposableThing {
Expand Down Expand Up @@ -58,7 +61,8 @@ abstract interface class ExposableThing {

/// Handles an `observeproperty` operation triggered by a TD consumer.
Future<void> handleObserveProperty(
String eventName, {
String eventName,
ContentListener contentListener, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
Expand All @@ -83,7 +87,8 @@ abstract interface class ExposableThing {

/// Handles a `subscribeevent` operation triggered by a TD consumer.
Future<void> handleSubscribeEvent(
String eventName, {
String eventName,
ContentListener contentListener, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
Expand Down
15 changes: 13 additions & 2 deletions lib/src/core/scripting_api/exposed_thing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// SPDX-License-Identifier: BSD-3-Clause

import "package:meta/meta.dart";

import "../definitions.dart";
import "interaction_input.dart";
import "interaction_output.dart";
Expand Down Expand Up @@ -88,7 +90,11 @@ abstract interface class ExposedThing {

/// Informs all subscribers about the change of the property with the given
/// [name].
Future<void> emitPropertyChange(String name);
@experimental
Future<void> emitPropertyChange(
String name, [
String contentType = "application/json",
]);

/// Assigns a [handler] function to an action with a given [name].
///
Expand All @@ -115,5 +121,10 @@ abstract interface class ExposedThing {
/// occurred.
///
/// You can provide (optional) input [data] that is emitted with the event.
Future<void> emitEvent(String name, InteractionInput data);
@experimental
Future<void> emitEvent(
String name,
InteractionInput data, [
String contentType = "application/json",
]);
}

0 comments on commit 3b5d350

Please sign in to comment.