diff --git a/lib/src/core/implementation/exposed_thing.dart b/lib/src/core/implementation/exposed_thing.dart index 94ab6051..5d15cbe6 100644 --- a/lib/src/core/implementation/exposed_thing.dart +++ b/lib/src/core/implementation/exposed_thing.dart @@ -30,22 +30,60 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing { final Map _propertyWriteHandlers = {}; + final Map + _propertyObserveHandlers = {}; + + final Map + _propertyUnobserveHandlers = {}; + final Map _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 emitPropertyChange(String name) { // TODO(JKRhb): implement emitPropertyChange throw UnimplementedError(); } - @override - void setPropertyWriteHandler( - String name, - scripting_api.PropertyWriteHandler handler, - ) { - _propertyWriteHandlers[name] = handler; - } - @override Future destroy() async { _servient.destroyThing(this); @@ -62,23 +100,31 @@ 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 @@ -86,24 +132,27 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing { 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