diff --git a/bin/dart_wot.dart b/bin/dart_wot.dart new file mode 100644 index 00000000..0d5b786b --- /dev/null +++ b/bin/dart_wot.dart @@ -0,0 +1,62 @@ +// 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 "dart:convert"; +import "dart:io"; + +import "package:args/args.dart"; +import "package:dart_wot/binding_coap.dart"; +import "package:dart_wot/binding_http.dart"; +import "package:dart_wot/binding_mqtt.dart"; +import "package:dart_wot/core.dart"; + +const success = 0; + +Future main(List args) async { + exitCode = success; + + final servient = Servient.create( + clientFactories: [ + CoapClientFactory(), + HttpClientFactory(), + MqttClientFactory(), + ], + ); + + final wot = await servient.start(); + + final argParser = ArgParser() + ..addCommand("read-property") + ..addCommand("request-td"); + + final argResults = argParser.parse(args); + + final command = argResults.command; + + switch (command?.name) { + case "read-property": + final uri = Uri.parse(command?.arguments.first ?? ""); + final thingDescription = await wot.requestThingDescription(uri); + + final consumedThing = await wot.consume(thingDescription); + final propertyKey = command?.arguments.elementAtOrNull(1) ?? ""; + + final interactionOutput = await consumedThing.readProperty(propertyKey); + final value = await interactionOutput.value(); + + stdout.write(value); + case "request-td": + final uri = Uri.parse(command?.arguments.first ?? ""); + final thingDescription = await wot.requestThingDescription(uri); + writeThingDescription(thingDescription); + } +} + +void writeThingDescription(ThingDescription thingDescription) { + // TODO: Also support other serialization formats (especially CBOR) + final thingDescriptionJson = jsonEncode(thingDescription.toJson()); + stdout.write(thingDescriptionJson); +} diff --git a/pubspec.yaml b/pubspec.yaml index b738ada5..7b86bb36 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,6 +15,7 @@ dev_dependencies: test: ^1.24.3 dependencies: + args: ^2.5.0 cbor: ^6.1.0 coap: ^9.0.0 collection: ^1.17.2 @@ -30,3 +31,6 @@ dependencies: typed_data: ^1.3.2 uri: ^1.0.0 uuid: ^4.2.1 + +executables: + dart_wot: