Skip to content

Commit 0863862

Browse files
committed
feat: implement dart_wot CLI
1 parent 74edf94 commit 0863862

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

bin/dart_wot.dart

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2024 Contributors to the Eclipse Foundation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
import "dart:convert";
8+
import "dart:io";
9+
10+
import "package:args/args.dart";
11+
import "package:dart_wot/binding_coap.dart";
12+
import "package:dart_wot/binding_http.dart";
13+
import "package:dart_wot/binding_mqtt.dart";
14+
import "package:dart_wot/core.dart";
15+
16+
const success = 0;
17+
18+
Future<void> main(List<String> args) async {
19+
exitCode = success;
20+
21+
final servient = Servient.create(
22+
clientFactories: [
23+
CoapClientFactory(),
24+
HttpClientFactory(),
25+
MqttClientFactory(),
26+
],
27+
);
28+
29+
final wot = await servient.start();
30+
31+
final argParser = ArgParser()
32+
..addCommand("read-property")
33+
..addCommand("request-td");
34+
35+
final argResults = argParser.parse(args);
36+
37+
final command = argResults.command;
38+
39+
switch (command?.name) {
40+
case "read-property":
41+
final uri = Uri.parse(command?.arguments.first ?? "");
42+
final thingDescription = await wot.requestThingDescription(uri);
43+
44+
final consumedThing = await wot.consume(thingDescription);
45+
final propertyKey = command?.arguments.elementAtOrNull(1) ?? "";
46+
47+
final interactionOutput = await consumedThing.readProperty(propertyKey);
48+
final value = await interactionOutput.value();
49+
50+
stdout.write(value);
51+
case "request-td":
52+
final uri = Uri.parse(command?.arguments.first ?? "");
53+
final thingDescription = await wot.requestThingDescription(uri);
54+
writeThingDescription(thingDescription);
55+
}
56+
}
57+
58+
void writeThingDescription(ThingDescription thingDescription) {
59+
// TODO: Also support other serialization formats (especially CBOR)
60+
final thingDescriptionJson = jsonEncode(thingDescription.toJson());
61+
stdout.write(thingDescriptionJson);
62+
}

pubspec.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dev_dependencies:
1515
test: ^1.24.3
1616

1717
dependencies:
18+
args: ^2.5.0
1819
cbor: ^6.1.0
1920
coap: ^9.1.0
2021
collection: ^1.17.2
@@ -31,3 +32,6 @@ dependencies:
3132
typed_data: ^1.3.2
3233
uri: ^1.0.0
3334
uuid: ^4.2.1
35+
36+
executables:
37+
dart_wot:

0 commit comments

Comments
 (0)