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 15, 2024
1 parent dad3192 commit 0f9d1a3
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 251 deletions.
2 changes: 1 addition & 1 deletion example/exposed_thing/http_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import "package:dart_wot/binding_http.dart";
import "package:dart_wot/core.dart";

var property = "hi :)";
String property = "hi :)";

void main() async {
final servient =
Expand Down
2 changes: 1 addition & 1 deletion lib/src/binding_coap/coap_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class CoapServer implements ProtocolServer {
final int? preferredBlockSize;

@override
Future<void> expose(ExposedThing thing) {
Future<void> expose(ExposableThing thing) {
// TODO(JKRhb): implement expose
throw UnimplementedError();
}
Expand Down
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 @@ -14,7 +14,6 @@ import "package:shelf_router/shelf_router.dart";

import "../../core.dart" hide ExposedThing;

import "../core/implementation/exposed_thing.dart";
import "http_config.dart";

const _thingsPath = "things";
Expand All @@ -38,7 +37,7 @@ final class HttpServer implements ProtocolServer {

io.HttpServer? _server;

final _things = <String, ExposedThing>{};
final _things = <String, ExposableThing>{};

late final Servient _servient;

Expand All @@ -51,7 +50,7 @@ final class HttpServer implements ProtocolServer {
}

@override
Future<void> expose(ExposedThing thing) async {
Future<void> expose(ExposableThing thing) async {
final thingDescription = thing.thingDescription;
final key = thingDescription.id;

Expand Down
11 changes: 4 additions & 7 deletions lib/src/core/implementation/exposed_thing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
//
// SPDX-License-Identifier: BSD-3-Clause

import "package:meta/meta.dart";

import "../definitions.dart";
import "../protocol_interfaces/exposable_thing.dart";
import "../scripting_api.dart" as scripting_api;

import "content.dart";
import "interaction_output.dart";
import "servient.dart";

/// Implementation of the [scripting_api.ExposedThing] interface.
class ExposedThing implements scripting_api.ExposedThing {
class ExposedThing implements scripting_api.ExposedThing, ExposableThing {
/// Creates a new [ExposedThing] from a [_servient] and an [exposedThingInit].
ExposedThing(this._servient, scripting_api.ExposedThingInit exposedThingInit)
: thingDescription = ThingDescription.fromJson(exposedThingInit);
Expand Down Expand Up @@ -117,8 +116,7 @@ class ExposedThing implements scripting_api.ExposedThing {
// TODO(JKRhb): implement setEventUnsubscribeHandler
}

/// Handles a `readproperty` operation triggered by a TD consumer.
@internal
@override
Future<Content> handleReadProperty(
String propertyName, {
int? formIndex,
Expand Down Expand Up @@ -147,8 +145,7 @@ class ExposedThing implements scripting_api.ExposedThing {
);
}

/// Handles a `writeproperty` operation triggered by a TD consumer.
@internal
@override
Future<void> handleWriteProperty(
String propertyName,
Content input, {
Expand Down
1 change: 1 addition & 0 deletions lib/src/core/protocol_interfaces.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// SPDX-License-Identifier: BSD-3-Clause

export "protocol_interfaces/exposable_thing.dart";
export "protocol_interfaces/protocol_client.dart";
export "protocol_interfaces/protocol_client_factory.dart";
export "protocol_interfaces/protocol_discoverer.dart";
Expand Down
32 changes: 32 additions & 0 deletions lib/src/core/protocol_interfaces/exposable_thing.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 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

import "../definitions.dart";
import "../implementation.dart";

/// Interface that allows ProtocolServers to interact with ExposedThings.
// TODO: This needs a better name
abstract interface class ExposableThing {
/// The [ThingDescription] that represents this [ExposableThing].
ThingDescription get thingDescription;

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

/// Handles a `writeproperty` operation triggered by a TD consumer.
Future<void> handleWriteProperty(
String propertyName,
Content input, {
int? formIndex,
Map<String, Object>? uriVariables,
Object? data,
});
}
4 changes: 2 additions & 2 deletions lib/src/core/protocol_interfaces/protocol_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import "../implementation/exposed_thing.dart";
import "../implementation/servient.dart";
// import "../scripting_api/exposed_thing.dart";
import "exposable_thing.dart";

/// Base class for a Protocol Server.
abstract interface class ProtocolServer {
Expand All @@ -24,5 +24,5 @@ abstract interface class ProtocolServer {
Future<void> stop();

/// Exposes a [thing].
Future<void> expose(ExposedThing thing);
Future<void> expose(ExposableThing thing);
}
2 changes: 0 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ environment:
sdk: '>=3.0.0 <4.0.0'

dev_dependencies:
build_runner: ^2.3.0
coverage: ^1.0.4
lint: ^2.0.2
lints: ^4.0.0
mockito: ^5.0.17
test: ^1.24.3

dependencies:
Expand Down
7 changes: 0 additions & 7 deletions test/binding_coap/binding_coap_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
import "package:dart_wot/binding_coap.dart";
import "package:dart_wot/core.dart";

import "package:mockito/annotations.dart";
import "package:test/test.dart";
import "binding_coap_test.mocks.dart";

@GenerateMocks([ExposedThing])
void main() {
group("CoAP Binding Tests", () {
setUp(() {
Expand All @@ -37,10 +34,6 @@ void main() {
() async => defaultServer.stop(),
throwsA(const TypeMatcher<UnimplementedError>()),
);
expect(
() async => defaultServer.expose(MockExposedThing()),
throwsA(const TypeMatcher<UnimplementedError>()),
);

final customServer =
CoapServer(const CoapConfig(port: 9001, blocksize: 64));
Expand Down
Loading

0 comments on commit 0f9d1a3

Please sign in to comment.