From 2dcba20349dda85ba6b316a5eb1663d2e8da5ab6 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Thu, 20 Jun 2024 19:17:25 +0200 Subject: [PATCH] fixup! Implement ExposedThing functionality --- example/iceflow_test.dart | 83 +++++++++++++++++++ lib/src/binding_http/http_server.dart | 5 +- .../core/implementation/exposed_thing.dart | 69 +++++++++++++++ .../protocol_interfaces/exposable_thing.dart | 53 +++++++++++- 4 files changed, 206 insertions(+), 4 deletions(-) create mode 100644 example/iceflow_test.dart diff --git a/example/iceflow_test.dart b/example/iceflow_test.dart new file mode 100644 index 00000000..d8ce3312 --- /dev/null +++ b/example/iceflow_test.dart @@ -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 main(List 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(); +} diff --git a/lib/src/binding_http/http_server.dart b/lib/src/binding_http/http_server.dart index 5b5bcfff..0b701b0a 100644 --- a/lib/src/binding_http/http_server.dart +++ b/lib/src/binding_http/http_server.dart @@ -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, ); }); @@ -192,7 +192,6 @@ final class HttpServer implements ProtocolServer { ), ); - // TODO: Handle observe case Event(): // TODO: Implement continue; diff --git a/lib/src/core/implementation/exposed_thing.dart b/lib/src/core/implementation/exposed_thing.dart index 69281273..a1e8b1a4 100644 --- a/lib/src/core/implementation/exposed_thing.dart +++ b/lib/src/core/implementation/exposed_thing.dart @@ -223,4 +223,73 @@ class ExposedThing implements scripting_api.ExposedThing, ExposableThing { null, ); } + + @override + Stream handleObserveProperty( + String eventName, { + int? formIndex, + Map? uriVariables, + Object? data, + }) { + // TODO: implement handleObserveProperty + throw UnimplementedError(); + } + + @override + Future handleReadAllProperties( + List propertyNames, + PropertyContentMap inputs, { + int? formIndex, + Map? uriVariables, + Object? data, + }) { + // TODO: implement handleReadAllProperties + throw UnimplementedError(); + } + + @override + Future handleReadMultipleProperties( + List propertyNames, + Content input, { + int? formIndex, + Map? uriVariables, + Object? data, + }) { + // TODO: implement handleReadMultipleProperties + throw UnimplementedError(); + } + + @override + Stream handleSubscribeEvent( + String eventName, { + int? formIndex, + Map? uriVariables, + Object? data, + }) { + // TODO: implement handleSubscribeEvent + throw UnimplementedError(); + } + + @override + Stream handleUnsubscribeEvent( + String eventName, { + int? formIndex, + Map? uriVariables, + Object? data, + }) { + // TODO: implement handleUnsubscribeEvent + throw UnimplementedError(); + } + + @override + Future handleWriteMultipleProperties( + List propertyNames, + Content input, { + int? formIndex, + Map? uriVariables, + Object? data, + }) { + // TODO: implement handleWriteMultipleProperties + throw UnimplementedError(); + } } diff --git a/lib/src/core/protocol_interfaces/exposable_thing.dart b/lib/src/core/protocol_interfaces/exposable_thing.dart index d8945054..8a6f3712 100644 --- a/lib/src/core/protocol_interfaces/exposable_thing.dart +++ b/lib/src/core/protocol_interfaces/exposable_thing.dart @@ -7,6 +7,8 @@ import "../definitions.dart"; import "../implementation.dart"; +typedef PropertyContentMap = Map; + /// Interface that allows ProtocolServers to interact with ExposedThings. // TODO: This needs a better name abstract interface class ExposableThing { @@ -21,6 +23,22 @@ abstract interface class ExposableThing { Object? data, }); + Future handleReadMultipleProperties( + List propertyNames, + Content input, { + int? formIndex, + Map? uriVariables, + Object? data, + }); + + Future handleReadAllProperties( + List propertyNames, + PropertyContentMap inputs, { + int? formIndex, + Map? uriVariables, + Object? data, + }); + /// Handles a `writeproperty` operation triggered by a TD consumer. Future handleWriteProperty( String propertyName, @@ -30,7 +48,24 @@ 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 handleWriteMultipleProperties( + List propertyNames, + Content input, { + int? formIndex, + Map? uriVariables, + Object? data, + }); + + /// Handles an `observeproperty` operation triggered by a TD consumer. + Stream handleObserveProperty( + String eventName, { + int? formIndex, + Map? uriVariables, + Object? data, + }); + + /// Handles an `invokeaction` operation triggered by a TD consumer. Future handleInvokeAction( String propertyName, Content input, { @@ -38,4 +73,20 @@ abstract interface class ExposableThing { Map? uriVariables, Object? data, }); + + /// Handles a `subscribeevent` operation triggered by a TD consumer. + Stream handleSubscribeEvent( + String eventName, { + int? formIndex, + Map? uriVariables, + Object? data, + }); + + /// Handles an `unsubscribeevent` operation triggered by a TD consumer. + Stream handleUnsubscribeEvent( + String eventName, { + int? formIndex, + Map? uriVariables, + Object? data, + }); }