Skip to content

Commit

Permalink
feat: implement dart_wot CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jun 2, 2024
1 parent e1040ff commit 9d10a2d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
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:

0 comments on commit 9d10a2d

Please sign in to comment.