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 Jun 22, 2024
1 parent 7304fc0 commit b38fd84
Showing 1 changed file with 70 additions and 21 deletions.
91 changes: 70 additions & 21 deletions lib/src/core/implementation/exposed_thing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,60 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
final Map<String, scripting_api.PropertyWriteHandler> _propertyWriteHandlers =
{};

final Map<String, scripting_api.PropertyReadHandler>
_propertyObserveHandlers = {};

final Map<String, scripting_api.PropertyReadHandler>
_propertyUnobserveHandlers = {};

final Map<String, scripting_api.ActionHandler> _actionHandlers = {};

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

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

return property;
}

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

if (property.writeOnly) {
final title = property.title ?? "without title";
throw ArgumentError("Property $title is not readable.");
}
}

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

if (property.readOnly) {
final title = property.title ?? "without title";
throw ArgumentError("Property $title is not writable.");
}
}

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

if (!property.observable) {
final title = property.title ?? "without title";
throw ArgumentError("Property $title is not observable.");
}
}

@override
Future<void> emitPropertyChange(String name) {
// TODO(JKRhb): implement emitPropertyChange
throw UnimplementedError();
}

@override
void setPropertyWriteHandler(
String name,
scripting_api.PropertyWriteHandler handler,
) {
_propertyWriteHandlers[name] = handler;
}

@override
Future<void> destroy() async {
_servient.destroyThing(this);
Expand All @@ -62,48 +100,59 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {

@override
void setActionHandler(String name, scripting_api.ActionHandler handler) {
if (thingDescription.actions?[name] == null) {
throw ArgumentError("ExposedThing does not an Action with the key $name");
}

_actionHandlers[name] = handler;
}

@override
void setEventHandler(
void setPropertyReadHandler(
String name,
scripting_api.EventListenerHandler handler,
scripting_api.PropertyReadHandler handler,
) {
// TODO(JKRhb): implement setEventHandler
_checkReadableProperty(name);

_propertyReadHandlers[name] = handler;
}

@override
void setEventSubscribeHandler(
void setPropertyWriteHandler(
String name,
scripting_api.EventSubscriptionHandler handler,
scripting_api.PropertyWriteHandler handler,
) {
// TODO(JKRhb): implement setEventSubscribeHandler
_checkWritableProperty(name);

_propertyWriteHandlers[name] = handler;
}

@override
void setPropertyObserveHandler(
String name,
scripting_api.PropertyReadHandler handler,
) {
// TODO(JKRhb): implement setPropertyObserveHandler
_checkObservableProperty(name);

_propertyObserveHandlers[name] = handler;
}

@override
void setPropertyReadHandler(
void setPropertyUnobserveHandler(
String name,
scripting_api.PropertyReadHandler handler,
) {
// TODO: Ensure that the property is actually readable.
_propertyReadHandlers[name] = handler;
_checkObservableProperty(name);

_propertyUnobserveHandlers[name] = handler;
}

@override
void setPropertyUnobserveHandler(
void setEventSubscribeHandler(
String name,
scripting_api.PropertyReadHandler handler,
scripting_api.EventSubscriptionHandler handler,
) {
// TODO(JKRhb): implement setPropertyUnobserveHandler
// TODO(JKRhb): implement setEventSubscribeHandler
}

@override
Expand Down

0 comments on commit b38fd84

Please sign in to comment.