@@ -44,6 +44,10 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
4444 final Map <String , scripting_api.EventSubscriptionHandler >
4545 _eventUnsubscribeHandlers = {};
4646
47+ final Map <String , ContentListener > _propertyChangeListeners = {};
48+
49+ final Map <String , ContentListener > _eventListeners = {};
50+
4751 Property _obtainProperty (String name) {
4852 final property = thingDescription.properties? [name];
4953
@@ -57,6 +61,19 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
5761 return property;
5862 }
5963
64+ Event _obtainEvent (String name) {
65+ final event = thingDescription.events? [name];
66+
67+ if (event == null ) {
68+ throw ArgumentError (
69+ "Event $name does not exist in ExposedThing "
70+ "with title ${thingDescription .title }." ,
71+ );
72+ }
73+
74+ return event;
75+ }
76+
6077 void _checkReadableProperty (String name) {
6178 final property = _obtainProperty (name);
6279
@@ -85,9 +102,32 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
85102 }
86103
87104 @override
88- Future <void > emitPropertyChange (String name) {
89- // TODO(JKRhb): implement emitPropertyChange
90- throw UnimplementedError ();
105+ Future <void > emitPropertyChange (
106+ String name, [
107+ String contentType = "application/json" ,
108+ ]) async {
109+ final property = _obtainProperty (name);
110+
111+ final readHandler = _propertyReadHandlers[name];
112+
113+ // TODO: Does this need to be a ProtocolListenerRegistry?
114+ final propertyChangeHandler = _propertyChangeListeners[name];
115+
116+ // TODO: Do we need to throw an error here?
117+ if (readHandler == null || propertyChangeHandler == null ) {
118+ return ;
119+ }
120+
121+ final interactionInput = await readHandler ();
122+
123+ final content = Content .fromInteractionInput (
124+ interactionInput,
125+ contentType,
126+ _servient.contentSerdes,
127+ property,
128+ );
129+
130+ propertyChangeHandler (content);
91131 }
92132
93133 @override
@@ -96,9 +136,27 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
96136 }
97137
98138 @override
99- Future <void > emitEvent (String name, Object ? data) {
100- // TODO(JKRhb): implement emitEvent
101- throw UnimplementedError ();
139+ Future <void > emitEvent (
140+ String name,
141+ scripting_api.InteractionInput data, [
142+ String contentType = "application/json" ,
143+ ]) async {
144+ final event = _obtainEvent (name);
145+
146+ final eventListener = _eventListeners[name];
147+
148+ if (eventListener == null ) {
149+ return ;
150+ }
151+
152+ final content = Content .fromInteractionInput (
153+ data,
154+ contentType,
155+ _servient.contentSerdes,
156+ event.data,
157+ );
158+
159+ eventListener (content);
102160 }
103161
104162 @override
@@ -343,7 +401,8 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
343401
344402 @override
345403 Future <void > handleObserveProperty (
346- String propertyName, {
404+ String propertyName,
405+ ContentListener contentListener, {
347406 int ? formIndex,
348407 Map <String , Object >? uriVariables,
349408 Object ? data,
@@ -356,6 +415,8 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
356415 );
357416 }
358417
418+ _propertyChangeListeners[propertyName] = contentListener;
419+
359420 await observeHandler (
360421 data: data,
361422 uriVariables: uriVariables,
@@ -387,7 +448,8 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
387448
388449 @override
389450 Future <void > handleSubscribeEvent (
390- String eventName, {
451+ String eventName,
452+ ContentListener contentListener, {
391453 int ? formIndex,
392454 Map <String , Object >? uriVariables,
393455 Object ? data,
0 commit comments