@@ -44,6 +44,10 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
44
44
final Map <String , scripting_api.EventSubscriptionHandler >
45
45
_eventUnsubscribeHandlers = {};
46
46
47
+ final Map <String , ContentListener > _propertyChangeListeners = {};
48
+
49
+ final Map <String , ContentListener > _eventListeners = {};
50
+
47
51
Property _obtainProperty (String name) {
48
52
final property = thingDescription.properties? [name];
49
53
@@ -57,6 +61,19 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
57
61
return property;
58
62
}
59
63
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
+
60
77
void _checkReadableProperty (String name) {
61
78
final property = _obtainProperty (name);
62
79
@@ -85,9 +102,32 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
85
102
}
86
103
87
104
@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);
91
131
}
92
132
93
133
@override
@@ -96,9 +136,27 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
96
136
}
97
137
98
138
@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);
102
160
}
103
161
104
162
@override
@@ -343,7 +401,8 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
343
401
344
402
@override
345
403
Future <void > handleObserveProperty (
346
- String propertyName, {
404
+ String propertyName,
405
+ ContentListener contentListener, {
347
406
int ? formIndex,
348
407
Map <String , Object >? uriVariables,
349
408
Object ? data,
@@ -356,6 +415,8 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
356
415
);
357
416
}
358
417
418
+ _propertyChangeListeners[propertyName] = contentListener;
419
+
359
420
await observeHandler (
360
421
data: data,
361
422
uriVariables: uriVariables,
@@ -387,7 +448,8 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
387
448
388
449
@override
389
450
Future <void > handleSubscribeEvent (
390
- String eventName, {
451
+ String eventName,
452
+ ContentListener contentListener, {
391
453
int ? formIndex,
392
454
Map <String , Object >? uriVariables,
393
455
Object ? data,
0 commit comments