Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI implementation #176

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions bin/dart_wot.dart
Original file line number Diff line number Diff line change
@@ -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<void> main(List<String> 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);
}
4 changes: 4 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,3 +31,6 @@ dependencies:
typed_data: ^1.3.2
uri: ^1.0.0
uuid: ^4.2.1

executables:
dart_wot:
Loading