Skip to content

Commit 2dcba20

Browse files
committed
fixup! Implement ExposedThing functionality
1 parent d375c7d commit 2dcba20

File tree

4 files changed

+206
-4
lines changed

4 files changed

+206
-4
lines changed

example/iceflow_test.dart

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2022 Contributors to the Eclipse Foundation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
// ignore_for_file: avoid_print
8+
9+
import "dart:convert";
10+
11+
import "package:dart_wot/binding_coap.dart";
12+
import "package:dart_wot/binding_http.dart";
13+
import "package:dart_wot/core.dart";
14+
15+
Future<void> main(List<String> args) async {
16+
final servient = Servient.create(
17+
clientFactories: [
18+
CoapClientFactory(),
19+
],
20+
);
21+
final wot = await servient.start();
22+
23+
final url =
24+
Uri.parse("coap://test-jan.testbed.maveric.internal/.well-known/wot");
25+
print("Requesting TD from $url ...");
26+
final thingDescription = await wot.requestThingDescription(url);
27+
28+
print(thingDescription.toJson());
29+
30+
final consumedThing = await wot.consume(thingDescription);
31+
32+
final uriVariables = {
33+
"topic": "text2lines/dataMain",
34+
};
35+
36+
// await consumedThing.invokeAction(
37+
// "subscribeToTopic",
38+
// uriVariables: uriVariables,
39+
// );
40+
41+
// final blargh = await consumedThing.observeProperty(
42+
// "pullInterface",
43+
// (interactionOutput) {
44+
// interactionOutput.data?.transform(utf8.decoder).forEach(print);
45+
// },
46+
// uriVariables: uriVariables,
47+
// );
48+
49+
final yeah = [
50+
"repairs. It is safer to ask the student to leave the classroom than it is to take",
51+
"the phone away completely.Cell phone restrictions in classrooms should also include specific disciplinary",
52+
"actions for breaking the rules. If a student is caught using the phone in",
53+
"class, he or she should be excused for the rest of the day. Professors should",
54+
"refrain from physically taking possession of a student’s phone because of",
55+
];
56+
57+
for (final yo in yeah) {
58+
await consumedThing.writeProperty(
59+
"pushInterface",
60+
InteractionInput.fromString(yo),
61+
uriVariables: {
62+
"topic": "/text2lines/dataMain",
63+
},
64+
);
65+
}
66+
67+
// print(
68+
// "Successfully retrieved and consumed TD with title "
69+
// '"${thingDescription.title}"!',
70+
// );
71+
72+
// print(consumedThing.thingDescription.events);
73+
// final subscription = await consumedThing.subscribeEvent("change", print);
74+
75+
// print("Incrementing counter ...");
76+
// await consumedThing.invokeAction("increment");
77+
78+
// final status = await consumedThing.readProperty("count");
79+
// final value = await status.value();
80+
// print("New counter value: $value");
81+
82+
// await subscription.stop();
83+
}

lib/src/binding_http/http_server.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ final class HttpServer implements ProtocolServer {
174174
request.mimeType ?? "application/json",
175175
request.read(),
176176
);
177-
final blah =
177+
final actionOutput =
178178
await thing.handleInvokeAction(affordance.key, content);
179179

180180
return Response(
181-
body: blah?.body,
181+
body: actionOutput?.body,
182182
204,
183183
);
184184
});
@@ -192,7 +192,6 @@ final class HttpServer implements ProtocolServer {
192192
),
193193
);
194194

195-
// TODO: Handle observe
196195
case Event():
197196
// TODO: Implement
198197
continue;

lib/src/core/implementation/exposed_thing.dart

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,73 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
223223
null,
224224
);
225225
}
226+
227+
@override
228+
Stream<Content> handleObserveProperty(
229+
String eventName, {
230+
int? formIndex,
231+
Map<String, Object>? uriVariables,
232+
Object? data,
233+
}) {
234+
// TODO: implement handleObserveProperty
235+
throw UnimplementedError();
236+
}
237+
238+
@override
239+
Future<void> handleReadAllProperties(
240+
List<String> propertyNames,
241+
PropertyContentMap inputs, {
242+
int? formIndex,
243+
Map<String, Object>? uriVariables,
244+
Object? data,
245+
}) {
246+
// TODO: implement handleReadAllProperties
247+
throw UnimplementedError();
248+
}
249+
250+
@override
251+
Future<PropertyContentMap> handleReadMultipleProperties(
252+
List<String> propertyNames,
253+
Content input, {
254+
int? formIndex,
255+
Map<String, Object>? uriVariables,
256+
Object? data,
257+
}) {
258+
// TODO: implement handleReadMultipleProperties
259+
throw UnimplementedError();
260+
}
261+
262+
@override
263+
Stream<Content> handleSubscribeEvent(
264+
String eventName, {
265+
int? formIndex,
266+
Map<String, Object>? uriVariables,
267+
Object? data,
268+
}) {
269+
// TODO: implement handleSubscribeEvent
270+
throw UnimplementedError();
271+
}
272+
273+
@override
274+
Stream<Content> handleUnsubscribeEvent(
275+
String eventName, {
276+
int? formIndex,
277+
Map<String, Object>? uriVariables,
278+
Object? data,
279+
}) {
280+
// TODO: implement handleUnsubscribeEvent
281+
throw UnimplementedError();
282+
}
283+
284+
@override
285+
Future<void> handleWriteMultipleProperties(
286+
List<String> propertyNames,
287+
Content input, {
288+
int? formIndex,
289+
Map<String, Object>? uriVariables,
290+
Object? data,
291+
}) {
292+
// TODO: implement handleWriteMultipleProperties
293+
throw UnimplementedError();
294+
}
226295
}

lib/src/core/protocol_interfaces/exposable_thing.dart

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import "../definitions.dart";
88
import "../implementation.dart";
99

10+
typedef PropertyContentMap = Map<String, Content>;
11+
1012
/// Interface that allows ProtocolServers to interact with ExposedThings.
1113
// TODO: This needs a better name
1214
abstract interface class ExposableThing {
@@ -21,6 +23,22 @@ abstract interface class ExposableThing {
2123
Object? data,
2224
});
2325

26+
Future<PropertyContentMap> handleReadMultipleProperties(
27+
List<String> propertyNames,
28+
Content input, {
29+
int? formIndex,
30+
Map<String, Object>? uriVariables,
31+
Object? data,
32+
});
33+
34+
Future<void> handleReadAllProperties(
35+
List<String> propertyNames,
36+
PropertyContentMap inputs, {
37+
int? formIndex,
38+
Map<String, Object>? uriVariables,
39+
Object? data,
40+
});
41+
2442
/// Handles a `writeproperty` operation triggered by a TD consumer.
2543
Future<void> handleWriteProperty(
2644
String propertyName,
@@ -30,12 +48,45 @@ abstract interface class ExposableThing {
3048
Object? data,
3149
});
3250

33-
/// Handles a `invokeaction` operation triggered by a TD consumer.
51+
/// Handles a `writemultipleproperties` operation triggered by a TD consumer.
52+
Future<void> handleWriteMultipleProperties(
53+
List<String> propertyNames,
54+
Content input, {
55+
int? formIndex,
56+
Map<String, Object>? uriVariables,
57+
Object? data,
58+
});
59+
60+
/// Handles an `observeproperty` operation triggered by a TD consumer.
61+
Stream<Content> handleObserveProperty(
62+
String eventName, {
63+
int? formIndex,
64+
Map<String, Object>? uriVariables,
65+
Object? data,
66+
});
67+
68+
/// Handles an `invokeaction` operation triggered by a TD consumer.
3469
Future<Content?> handleInvokeAction(
3570
String propertyName,
3671
Content input, {
3772
int? formIndex,
3873
Map<String, Object>? uriVariables,
3974
Object? data,
4075
});
76+
77+
/// Handles a `subscribeevent` operation triggered by a TD consumer.
78+
Stream<Content> handleSubscribeEvent(
79+
String eventName, {
80+
int? formIndex,
81+
Map<String, Object>? uriVariables,
82+
Object? data,
83+
});
84+
85+
/// Handles an `unsubscribeevent` operation triggered by a TD consumer.
86+
Stream<Content> handleUnsubscribeEvent(
87+
String eventName, {
88+
int? formIndex,
89+
Map<String, Object>? uriVariables,
90+
Object? data,
91+
});
4192
}

0 commit comments

Comments
 (0)