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 20, 2024
1 parent d375c7d commit 2dcba20
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 4 deletions.
83 changes: 83 additions & 0 deletions example/iceflow_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2022 Contributors to the Eclipse Foundation. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// SPDX-License-Identifier: BSD-3-Clause

// ignore_for_file: avoid_print

import "dart:convert";

import "package:dart_wot/binding_coap.dart";
import "package:dart_wot/binding_http.dart";
import "package:dart_wot/core.dart";

Future<void> main(List<String> args) async {
final servient = Servient.create(
clientFactories: [
CoapClientFactory(),
],
);
final wot = await servient.start();

final url =
Uri.parse("coap://test-jan.testbed.maveric.internal/.well-known/wot");
print("Requesting TD from $url ...");
final thingDescription = await wot.requestThingDescription(url);

print(thingDescription.toJson());

final consumedThing = await wot.consume(thingDescription);

final uriVariables = {
"topic": "text2lines/dataMain",
};

// await consumedThing.invokeAction(
// "subscribeToTopic",
// uriVariables: uriVariables,
// );

// final blargh = await consumedThing.observeProperty(
// "pullInterface",
// (interactionOutput) {
// interactionOutput.data?.transform(utf8.decoder).forEach(print);
// },
// uriVariables: uriVariables,
// );

final yeah = [
"repairs. It is safer to ask the student to leave the classroom than it is to take",
"the phone away completely.Cell phone restrictions in classrooms should also include specific disciplinary",
"actions for breaking the rules. If a student is caught using the phone in",
"class, he or she should be excused for the rest of the day. Professors should",
"refrain from physically taking possession of a student’s phone because of",
];

for (final yo in yeah) {
await consumedThing.writeProperty(
"pushInterface",
InteractionInput.fromString(yo),
uriVariables: {
"topic": "/text2lines/dataMain",
},
);
}

// print(
// "Successfully retrieved and consumed TD with title "
// '"${thingDescription.title}"!',
// );

// print(consumedThing.thingDescription.events);
// final subscription = await consumedThing.subscribeEvent("change", print);

// print("Incrementing counter ...");
// await consumedThing.invokeAction("increment");

// final status = await consumedThing.readProperty("count");
// final value = await status.value();
// print("New counter value: $value");

// await subscription.stop();
}
5 changes: 2 additions & 3 deletions lib/src/binding_http/http_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ final class HttpServer implements ProtocolServer {
request.mimeType ?? "application/json",
request.read(),
);
final blah =
final actionOutput =
await thing.handleInvokeAction(affordance.key, content);

return Response(
body: blah?.body,
body: actionOutput?.body,
204,
);
});
Expand All @@ -192,7 +192,6 @@ final class HttpServer implements ProtocolServer {
),
);

// TODO: Handle observe
case Event():
// TODO: Implement
continue;
Expand Down
69 changes: 69 additions & 0 deletions lib/src/core/implementation/exposed_thing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,73 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
null,
);
}

@override
Stream<Content> handleObserveProperty(
String eventName, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
}) {
// TODO: implement handleObserveProperty
throw UnimplementedError();
}

@override
Future<void> handleReadAllProperties(
List<String> propertyNames,
PropertyContentMap inputs, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
}) {
// TODO: implement handleReadAllProperties
throw UnimplementedError();
}

@override
Future<PropertyContentMap> handleReadMultipleProperties(
List<String> propertyNames,
Content input, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
}) {
// TODO: implement handleReadMultipleProperties
throw UnimplementedError();
}

@override
Stream<Content> handleSubscribeEvent(
String eventName, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
}) {
// TODO: implement handleSubscribeEvent
throw UnimplementedError();
}

@override
Stream<Content> handleUnsubscribeEvent(
String eventName, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
}) {
// TODO: implement handleUnsubscribeEvent
throw UnimplementedError();
}

@override
Future<void> handleWriteMultipleProperties(
List<String> propertyNames,
Content input, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
}) {
// TODO: implement handleWriteMultipleProperties
throw UnimplementedError();
}
}
53 changes: 52 additions & 1 deletion lib/src/core/protocol_interfaces/exposable_thing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import "../definitions.dart";
import "../implementation.dart";

typedef PropertyContentMap = Map<String, Content>;

/// Interface that allows ProtocolServers to interact with ExposedThings.
// TODO: This needs a better name
abstract interface class ExposableThing {
Expand All @@ -21,6 +23,22 @@ abstract interface class ExposableThing {
Object? data,
});

Future<PropertyContentMap> handleReadMultipleProperties(
List<String> propertyNames,
Content input, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
});

Future<void> handleReadAllProperties(
List<String> propertyNames,
PropertyContentMap inputs, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
});

/// Handles a `writeproperty` operation triggered by a TD consumer.
Future<void> handleWriteProperty(
String propertyName,
Expand All @@ -30,12 +48,45 @@ abstract interface class ExposableThing {
Object? data,
});

/// Handles a `invokeaction` operation triggered by a TD consumer.
/// Handles a `writemultipleproperties` operation triggered by a TD consumer.
Future<void> handleWriteMultipleProperties(
List<String> propertyNames,
Content input, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
});

/// Handles an `observeproperty` operation triggered by a TD consumer.
Stream<Content> handleObserveProperty(
String eventName, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
});

/// Handles an `invokeaction` operation triggered by a TD consumer.
Future<Content?> handleInvokeAction(
String propertyName,
Content input, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
});

/// Handles a `subscribeevent` operation triggered by a TD consumer.
Stream<Content> handleSubscribeEvent(
String eventName, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
});

/// Handles an `unsubscribeevent` operation triggered by a TD consumer.
Stream<Content> handleUnsubscribeEvent(
String eventName, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
});
}

0 comments on commit 2dcba20

Please sign in to comment.